// File name: login_lib.js
// Description: A file of scripts used for the "remember me" login functionality.

 
	function Trim(TRIM_VALUE)
	{
		if(TRIM_VALUE.length < 1){
			return"";
		}
		TRIM_VALUE = RTrim(TRIM_VALUE);
		TRIM_VALUE = LTrim(TRIM_VALUE);
		if(TRIM_VALUE==""){
			return "";
		}
		else{
			return TRIM_VALUE;
		}
	} //End Function
	
	function RTrim(VALUE){
		var w_space = String.fromCharCode(32);
		var v_length = VALUE.length;
		var strTemp = "";
		if(v_length < 0){
			return"";
		}
		var iTemp = v_length -1;
		
		while(iTemp > -1){
			if(VALUE.charAt(iTemp) == w_space){
			}
			else{
				strTemp = VALUE.substring(0,iTemp +1);
				break;
			}
			iTemp = iTemp-1;
		} //End While
		return strTemp;
	} //End Function
	
	function LTrim(VALUE){
		var w_space = String.fromCharCode(32);
		if(v_length < 1){
		return"";
		}
		var v_length = VALUE.length;
		var strTemp = "";
	
		var iTemp = 0;
	
		while(iTemp < v_length){
			if(VALUE.charAt(iTemp) == w_space){
			}
			else{
				strTemp = VALUE.substring(iTemp,v_length);
				break;
			}
			iTemp = iTemp + 1;
		} //End While
		return strTemp;
	} //End Function

 
 // Set Cookie: Create the user login cookie
 function setCookie(name, emailValue, expire) {       
  	
  	document.cookie = name + "=" + escape(emailValue) 
  	+ "; expires=" + expire.toGMTString() + "; path=/";
 }
  
 // Erase the user login cookie
 function eraseCookie(name, emailValue) {
	// To delete the cookie: Set the expiry date to -1
	document.cookie = name + "=" + escape(emailValue)        
  	+ "; expires=" + -1 + "; path=/";
 }
 
 // Set the user login cookie
 function setUserCookie(){
 	// Get the login form values
 	var email = document.loginFormBean.emailAddress.value;
 	var rememberMe = document.loginFormBean.rememberMe.checked;
	email = Trim(email);

 	// Only set the cookie if email is populated and rememberMe is checked
 	if(email!=null && email!='' && rememberMe){

 		var today = new Date();          
 		var expires = new Date(); 
 		// Set the number of days before the cookie expires (temp set to 30 days for testing)
 		daysExpire = 30;         
	 	expires.setTime(today.getTime() + daysExpire*60*60*24*365);

 		// Check if this cookie exists 
 		if (document.cookie.length > 0) {

			offset = document.cookie.indexOf("iCokeLoginCookie="+escape(email));

 			// Call setCookie to create the cookie only if the cookie does not exist     
 			if (offset == -1) {
 				// Set the cookie
	 			setCookie("iCokeLoginCookie", email, expires);
	 		}
	 	}
	}
	
	// If remember me is not checked: Erase the existing cookie
	if(!rememberMe){
		eraseCookie("iCokeLoginCookie", email);
	}
 }
 

 // Set the user Full login cookie
 function setUserCookieFullLogin(){
 	// Get the login form values
 	var email = document.loginFormBean2.emailAddress.value;
 	var rememberMe = document.loginFormBean2.rememberMe.checked;
	email = Trim(email);

 	// Only set the cookie if email is populated and rememberMe is checked
 	if(email!=null && email!='' && rememberMe){

 		var today = new Date();          
 		var expires = new Date(); 
 		// Set the number of days before the cookie expires (temp set to 30 days for testing)
 		daysExpire = 30;         
	 	expires.setTime(today.getTime() + daysExpire*60*60*24*365);

 		// Check if this cookie exists 
 		if (document.cookie.length > 0) {

			offset = document.cookie.indexOf("iCokeLoginCookie="+escape(email));

 			// Call setCookie to create the cookie only if the cookie does not exist     
 			if (offset == -1) {
 				// Set the cookie
	 			setCookie("iCokeLoginCookie", email, expires);
	 		}
	 	}
	}
	
	// If remember me is not checked: Erase the existing cookie
	if(!rememberMe){
		eraseCookie("iCokeLoginCookie", email);
	}
 }
 
 // Get current the login cookie and set the form values
 function getCookie(Name) {
 
 	var search = Name + "=";

 	// check if there are any cookies    
 	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search);

 		// if the cookie exists                    
 		if (offset != -1) {   
 		                            
 	 		offset += search.length;    
 	 		                         
 	 		// set index of beginning of value                              
 	 		end = document.cookie.indexOf(";", offset); 
                   
 	 		// set index of end of cookie value                             
 	  		if (end == -1) {                                        
 	  			end = document.cookie.length;
			} 

 			//Get the saved form values
 			emailValue = document.cookie.substring(offset, end);

 	  		//Set the login form values 
 	  		document.getElementById('emailAddress').value = emailValue;
 	  		document.getElementById('rememberMe').checked = true; 
 	  			 
 	  		return unescape(document.cookie.substring(offset, end));         
 	  }
 	}
 }