 //Limeade Sign In Gadget
//Author: Jahmai O'Sullivan $
//Date: 02/07/2008 16:58:59
//Revision: 2.0
//References to all important elements in the gadget
       var uname,pword,cookie;
        var http_request = false;
        var statusbox = document.getElementById('status');
        var emailtxtbox = document.getElementById('txtEmailAddress');
        var unametxtbox = document.getElementById('txtusername');
        var pwordtxtbox = document.getElementById('txtpassword');
        var rememberMeBox= document.getElementById('chkAutoLogin');
        var recoveryStatus = document.getElementById('recoveryStatus');
        var passRecovered = document.getElementById('passwordRecovered');
        var SignInBox = document.getElementById('SignInBox');
        var RecoveryBox = document.getElementById('RecoverSignIn');
        var sendingSpan = document.getElementById('spanSending');
        var youremail = document.getElementById('youremail');
        var signInDiv = document.getElementById('signingInDiv');
        var EmailRow = document.getElementById('EmailRow');
        var SendEmailBtnRow = document.getElementById('SendEmailBtnRow');
        var domainUrl = "https://mykaizenwellness.com/";
        var callbackParam;
        var loginUrl = domainUrl + "Gadget2/AuthenticateUser.aspx";
        var recoveryUrl = domainUrl + "Gadget2/RecoverPassword.aspx";
        var btnLogIn = document.getElementById('btnLogin');	
        var btnSendPassword = document.getElementById('btnSendPassword');
        var hiddenUser = document.getElementById('user1');
        var hiddenPass = document.getElementById('pass1'); 	
        var hiddenRemember = document.getElementById('remember1'); 
        var currentBoxFlag = 0; //currentBoxFlag tells which view is currently displayed : 0 for the sign in boxes and 1 for the recover password box
        var returnUrl = getQueryVariable('ReturnUrl');
        var redirectPage = domainUrl + "Gadget2/LogInUser.aspx?ReturnUrl="+encodeURI(returnUrl);
        
        //**************************** General Functions ******************************************
        init();
        function init() 
        { 
            btnLogIn.onmouseup = runLoginScript; 
	        btnLogIn.onmouseover = loadLoginScript; 
	        btnLogIn.onmouseout = removeLoginScript;
        	
	        btnSendPassword.onmouseup = runPasswordScript; 
	        btnSendPassword.onmouseover = loadPasswordScript; 
	        btnSendPassword.onmouseout = removePasswordScript;	
	        
	        unametxtbox.onfocus = clearSignInErrors;
	        pwordtxtbox.onfocus = clearSignInErrors;
	        emailtxtbox.onfocus = clearRecoveryBoxErrors;
	        
	        unametxtbox.focus();
        } 

        function clearSignInErrors()
        {
           statusbox.style.visibility = 'hidden';
        }
        
        function clearRecoveryBoxErrors()
        {
             recoveryStatus.style.visibility = 'hidden';
        }

        function HideElement(element)
        {
	        if(element != null)
	        {
		        element.style.visibility = 'hidden';
		        element.style.display = 'none';
	        }
        }


         
        function ShowElement(element)
        {
	        if(element != null)
	        {
		        element.style.visibility = 'visible';
		        element.style.display = '';
	        }
        }


        function ShowRecoveryBox()
        {
            clearSignInErrors();
	        HideElement(passRecovered);
	        ShowElement(EmailRow);
	        ShowElement(SendEmailBtnRow);
	        HideElement(SignInBox);
	        ShowElement(RecoveryBox);		
	        currentBoxFlag = 1;
        }

        function ShowSignInBox()
        {
            clearRecoveryBoxErrors();
	        HideElement(RecoveryBox);
	        statusbox.style.visibility = 'hidden';
	        ShowElement(SignInBox);
	        currentBoxFlag = 0;
        }

        function ShowPasswordRecoveredBox()
        {
        	
        	
	        if(emailtxtbox != null)
	        {
		        youremail.innerHTML = emailtxtbox.value;
	        }	
        	
	        if(passRecovered != null)
	        {
		        ShowElement(passRecovered);
		        HideElement(EmailRow);;
		        HideElement(SendEmailBtnRow)
		        HideElement(recoveryStatus);
	        }
        		
        }
        //**************************** End of General Functions ******************************************

        //**************************** User Authentication Functions ******************************************

        // hover: prepare the remote script 
        function loadLoginScript(v) 
        { 
             if (btnLogIn.className == 'loadScript') 
	        { 
	            //the url used to authenticate the user
	            
	            var uname = escape(unametxtbox.value);
	            var pword = escape(pwordtxtbox.value);
	            
	            
	            uname=uname.replace("+", "%2B");
                uname=uname.replace("/", "%2F");
                
                pword=pword.replace("+", "%2B");
                pword=pword.replace("/", "%2F"); 
  
		        callbackParam = '?user=' + uname + '&pass=' + pword + '&remember=' + rememberMeBox.checked;
		        hiddenUser = uname;
		        hiddenPass = pword;
		        hiddenRemember = rememberMeBox.checked;
		        var myLoginUrl = loginUrl + callbackParam;
		        // create a new node to host the remote script 
		        var remoteScript=document.createElement('script'); 
		        remoteScript.id = 'rs'; 
		        remoteScript.setAttribute('type','text/javascript'); 
		        remoteScript.setAttribute('src',myLoginUrl); 
		        var hd=document.getElementsByTagName('head')[0]; 
		        btnLogIn.className = 'runScript'; 
		        // Gotcha: set attribute and src BEFORE appending, or Safari won't work 
		        hd.appendChild(remoteScript); 
		     } 
        } 

        //Logs in the user if successful or generates error message	
        function runLoginScript(v) 
        { 
           if (btnLogIn.className == 'runScript') 
	        {
	  	        try 
		        {
		            if(unametxtbox != null && pwordtxtbox != null && rememberMeBox != null)
			        {
			            ShowElement(signInDiv);
				        statusbox.style.visibility = 'hidden';
				        // getStuff is a function in the remote script 
				        // which will call doStuffWith() here 
        				
				        // Try out our remote script
				        tryUserAuth(v);
        				
				        //getStuff(); 
        				
				        // and remove it 
				        removeScript('rs'); 
				        // and restyle the link for next time 
				        btnLogIn.className = 'loadScript'; 
			        }
		        } 
		        catch (e) 
		        {
        			
		        }
	        } 	       
        } 


        function tryUserAuth(v) 
        {
	        if (window.userAuthenticated) {
		        userAuthenticated();
	        } else {
		        setTimeout(function() { removeLoginScript(v);loadLoginScript(v);runLoginScript(v); }, 1000);
	        }
        }



        // outtahere: did not click, remove the remote script 

        function removeLoginScript(v) 
        { 
	       // el = getEl(v); 
	        if (btnLogIn.className == 'runScript') 
	        { 
        		
	        // remote script is loaded; 
		        //remove it 
		        removeScript('rs'); 
		        // and restyle the link for next time 
		        btnLogIn.className = 'loadScript'; 
	        } 
        } 

        // remove script node 
        function removeScript(id) 
        { 
	        var hd=document.getElementsByTagName('head')[0]; 
	        hd.removeChild(document.getElementById(id)); 
        } 

        // figure out which element the event is pointing to 
        function getEl(v) 
        { 
	        var tg = (v) ? v : event; 
	        if (tg.target) 
	        { 
		        var el = (tg.target.nodeType == 3) ? tg.target.parentNode : tg.target; 
	        } 
	        else 
	        { 
		        var el = tg.srcElement; 
	        } 
        	
	        return el; 
        } 

        // called by the remote script upon successful execution 
        function authenticateUser(authMsg) 
        { 
	        if(statusbox != null)
	        {
		        statusbox.style.visibility = 'visible';
		        statusbox.innerHTML = authMsg;
        	    
	           if(authMsg.indexOf('Sign in successful') != -1)
		        {
			        statusbox.className = 'successfulMessage';
			        //redirect to the welcome page
			        submitForm();
		        }
		        else
		        {
			        statusbox.className = 'errorMessage';
		        }
		        HideElement(signInDiv);
	        }
        }



        //****************************************End Of Login Functions *****************************************************************

        //************************ Beginning of Recover Password Functions ***************************************************************

        // hover: prepare the remote script 
        function loadPasswordScript(v) 
        { 
	        //el = getEl(v); 
	        if (btnSendPassword.className == 'loadScript') 
	        { 
		        //the url used to authenticate the user
		        var myPasswordUrl = recoveryUrl + '?email=' + escape(emailtxtbox.value);
		        // create a new node to host the remote script 
		        var remotePassScript=document.createElement('script'); 
		        remotePassScript.id = 'pw'; 
		        remotePassScript.setAttribute('type','text/javascript'); 
		        remotePassScript.setAttribute('src',myPasswordUrl);
		        var passHd=document.getElementsByTagName('head')[0]; 
		        // Gotcha: set attribute and src BEFORE appending, or Safari won't work 
		        passHd.appendChild(remotePassScript); 
		        btnSendPassword.className = 'runScript'; 
	        } 
        } 

        // klick: execute remote function, remove the remote script 
        function runPasswordScript(v) 
        { 
	       // el = getEl(v); 
	        if (btnSendPassword.className == 'runScript') 
	        { 
        	
		        try 
		        {
			        if(emailtxtbox != null)
			        {
				        ShowElement(sendingSpan);
				        HideElement(passRecovered);
				        HideElement(recoveryStatus);
        				
				        // Try out our remote script
				        trySendPassword(v);
        				
				        // and remove it 
				        removeScript('pw'); 
				        // and restyle the link for next time 
				        btnSendPassword.className = 'loadScript'; 		
			        }
		        } 
		        catch (e) 
		        {
        			
		        }
	        } 
        } 


        function trySendPassword(v) 
        {
	        if (window.sendPass) {
		        sendPass();
	        } else {
		        setTimeout(function() { removePasswordScript(v);loadPasswordScript(v);runPasswordScript(v); }, 1000);
	        }
        }



        // outtahere: did not click, remove the remote script 

        function removePasswordScript(v) 
        { 
	        //el = getEl(v); 
	        if (btnSendPassword.className == 'runScript') 
	        { 
        		
	        // remote script is loaded; 
		        //remove it 
		        removeScript('pw'); 
		        // and restyle the link for next time 
		        btnSendPassword.className = 'loadScript'; 
	        } 
        } 


        // called by the remote script upon successful execution 
        //Sends a password recovery email to a user
        function SendPasswordToEmail(sendPassStatus) 
        {
        
	        //Set the status
	        ShowElement(recoveryStatus);
	        recoveryStatus.innerHTML = sendPassStatus;
        	
	        if(recoveryStatus != null)
	        {
		        if(sendPassStatus.indexOf('Recovery Successful') != -1)
		        {
		            document.sending.method = "GET";
					document.sending.target = "_self";
			        ShowPasswordRecoveredBox();
		        }
		        HideElement(sendingSpan);
	        }
        }


         document.onkeydown = function(e)
         { 
            if (!e && event) e = event;
            
            if ( !e.target && e.srcElement ) 
            {
                   e.target = e.srcElement;
            }
            
            if (e.keyCode == 13)
            {
                if(currentBoxFlag == 0)
                {
					btnLogIn.focus();
                    loadLoginScript(btnLogIn);
                    runLoginScript(btnLogIn);                    
                }
                else if(currentBoxFlag == 1)
                {
					btnSendPassword.focus();
					loadPasswordScript(btnSendPassword);
                    runPasswordScript(btnSendPassword);               
                }
                
	        }
        }

       function getQueryVariable(variable) 
       {  
            var query = window.location.search.substring(1);  
            var vars = query.split("&");  
            for (var i=0;i<vars.length;i++) 
            {    
                //find the location of the first equal sign
                var firstEqual = vars[i].indexOf("=");
                var variableName = vars[i].substring(0, firstEqual);
                var variableValue = vars[i].substring(firstEqual + 1, vars[i].length);
                
                if (variableName == variable) 
                {      
                    return variableValue;    
                }                
             }   
             
             return "";
       }

        //********************************************************************************************************************************
        HideElement(RecoveryBox);
        HideElement(sendingSpan);
        HideElement(signInDiv);
        	
        	
        	
        	
        	