Authentication = function(){

       var logoutSuccess = function(){
	    return function(o){
               document.location.reload();
            }
       };             

       var logoutFailure = function(){
	    return function(o){
       	        WIDGET.messagebox("connErrMsg", "Connection error", "", "", 3);
            }
       };             
       

       var doAuthenticationSuccess = function(authDialog, successReturnURL, failureReturnURL){
	    return function(o){
	        var doingAuthMsg = document.getElementById('doingAuthMsg').parentNode;
	        doingAuthMsg.parentNode.removeChild(doingAuthMsg);
		if(o.responseText!='' && o.responseText*1>-1){
          	   WIDGET.messagebox("authFailedMsg", "Authentication succeeded", "", "", 3);
                   if(successReturnURL!=null){
		       document.location.href = successReturnURL;
		   }
                   else{
		        document.location.reload();
                   }
		}
		else{
          	   WIDGET.messagebox("authFailedMsg", "Authentication failed", "", "", 3);
                   if(failureReturnURL!=null){
                       document.location.href = failureReturnURL;
                   }
		}
            }
     }

	var doAuthenticationFailure = function(failureReturnURL){
	    return function(o){
	        doingAuthMsg.parentNode.removeChild(doingAuthMsg);
       	        WIDGET.messagebox("connErrMsg", "Connection error", "", "", 3);
                if(failureReturnURL!=null){
                    document.location.href = failureReturnURL;
                }
            }
	}

       var doAuthentication = function(authenticationForm, authDialog, ajaxPath, successReturnURL, failureReturnURL){
	    return function(e){
    		    if(authDialog!=null){
                        authDialog.parentNode.removeChild(authDialog);
                    }
         	    WIDGET.messagebox("doingAuthMsg", "Verifying <img src='ajaxspinner.gif'></img>", "", "", 0);
  	            YAHOO.util.Connect.setForm(authenticationForm);  
                    YAHOO.util.Event.stopEvent(e);
 	            var callback =
		    {
		    	success: doAuthenticationSuccess(authDialog, successReturnURL, failureReturnURL),
		    	failure: doAuthenticationFailure(failureReturnURL)
		    };  
                    var cObj = YAHOO.util.Connect.asyncRequest('POST', ajaxPath, callback); 
	    }
     }
    
       var cancelAuthentication = function(authDialog, cancelReturnURL){
	    return function(e){
                if(authDialog!=null){
                    authDialog.parentNode.removeChild(authDialog);
                }
                if(cancelReturnURL!=null){
                    document.location.href = cancelReturnURL;
                }
	    }

	}

       var forgotPasswordSuccess = function(){
	    return function(o){
                if(o.responseText==1){
                    alert("A new password has been generated and emailed to you");
                }
                else{
                    alert("No account found");
		}
            }
	}

       var forgotPasswordFailure = function(){
	    return function(o){
       	        WIDGET.messagebox("connErrMsg", "Connection error", "", "", 3);            
            }
	}
	    
       var forgotPassword = function(usernameInput, forgotPasswordURL){
           return function(e){
           if(usernameInput.value == ''){
               alert('Please enter a username or email address into the username field');
           }
           else{
 	        var callback =
	        {
   		        success: forgotPasswordSuccess(),
		    	failure: forgotPasswordFailure()
	        };  
                var cObj = YAHOO.util.Connect.asyncRequest('POST', forgotPasswordURL, callback, 'em='+usernameInput.value); 
           }
           }
       }
	         
       return{

	   authenticate : function(ajaxPath, forgotPasswordURL, regLink, authenticationFormContainer, successReturnURL, failureReturnURL, cancelReturnURL){
	       return function(e){
	           var submitButton = $input({type:'button', value:'Submit', _class:'button', classname:'button'});
                   if(authenticationFormContainer==null){
   	               var cancelButton = $input({type:'button', value:'Cancel', _class:'button', classname:'button', id:'cancelButton'});
                   }
                   else{
                       var cancelButton = $span();;
                    }
	       
                    var usernameInput = $input({id: 'username', name: 'username', _class:'input', classname: 'input', type:'text'});
	            var credsContainer = $ul({}, 
				       $li({}, $label({}, "Username:"), $div({}, usernameInput)),
				       $li({}, $label({}, "Password:"), $div($input({id: 'password', name: 'password', _class:'input', classname: 'input', type: 'password'}))),
			     $li({_class:'listText', className: 'listText'}, submitButton, cancelButton)

				       );
                     if(regLink!=null){
		         credsContainer.appendChild($li({_class:'listText', className: 'listText'},  'Not a member? Register ', $a({href: regLink},'here!')));
  	                 credsContainer.appendChild($li({},  $a({href: regLink})));
                     }

                     var forgotPasswordLink = $a({href:'#'}, 'Forgot password?');
                     YAHOO.util.Event.on(forgotPasswordLink, 'click', forgotPassword(usernameInput, forgotPasswordURL));
		   credsContainer.appendChild($li({_class:'listText', className: 'listText'}, forgotPasswordLink));              

	            var credsForm = $form({}, credsContainer);
                    var authDialog = null;
              if(authenticationFormContainer==null){
   	          WIDGET.messagebox("authDialog", credsForm, "", "Authenticate", 0);
  	          authDialog = document.getElementById('authDialog').parentNode;
              }
              else{
                  document.getElementById(authenticationFormContainer).appendChild(credsForm);                 
              }
	      YAHOO.util.Event.on(submitButton, 'click', doAuthentication(credsForm, authDialog, ajaxPath, successReturnURL, failureReturnURL));
              if(cancelButton!=null){
    	          YAHOO.util.Event.on(cancelButton, 'click', cancelAuthentication(authDialog, cancelReturnURL));
               }

	       }
	   },

           logout: function(ajaxPath){
               return function(e){
                  var callback =
     	          {
                     success: logoutSuccess(),
       	             failure: logoutFailure()
       	          };  
                  var cObj = YAHOO.util.Connect.asyncRequest('GET', ajaxPath, callback);            
               }
            }

                   
	};

}();

// Register 
YAHOO.register("Authentication", Authentication, {
    version: "1",
    build: "1"
});


