var Login = new function() {

	this.init = function(){
		var self = Login;
		self.clearError = null;
		self.submitForm = false;
		$("email").focus();
		self.whichForm2Submit = 'login';
		self.checkCookie();
	};
	
	
// LOGIN FUNCTIONS
// MEMBER LOGIN
	this.sendLoginReq = function(){
		var self=Login;		
		var e = $("email");
		var pwd = $("password");
		var r = $("rememberMe");
		if (r.checked) {	self.setCookie('email',e.value,30);};
		if (e.value.length>0) {
			if (pwd.value.length>0) {
				new Ajax.Request('ajax_login.asp', {parameters: $('adminLoginForm').serialize() + '&sendLoginReq=1', onSuccess:self.sendLoginReqHandlerFunc, onFailure:Error.errFunc}); 
			}else{
				Error.throwErrorMsg('<h3>Opps...</h3><p>Please enter a password.</p>');
			};
		}else{
			Error.throwErrorMsg('<h3>Opps...</h3><p>Please enter the email address associated with your account.</p>');
		};
	};
	
		
	this.sendLoginReqHandlerFunc = function(t) {
		var self = Login;
		var arr = t.responseText.split(',');
				
			if (arr[0] == 'success') {
				document.location.href=arr[1];
			}else{
				Error.throwErrorMsg('<h3>Opps...</h3>' + arr[1]);
			};
	}
//reset pwd
	this.promptForEmail = function(){
		var self=Login;		
		self.whichForm2Submit = 'pwd';
		var str = new String;
		str+= "<h3>Send me my password</h3>"
		str += "<label>Email<span class='small'>The address associated with your account</span></label><input type='text' id='Email'  onchange='Login.setLoginButton(\"login\")'/>";
		str += '<p><button type="button"  id="btnCancel" class="btnSilverSml" onclick="Error.closeErrorMsg()">Cancel</button>';
		str += '<button type="button"  id="btnSendPwd" class="btnOrangeSml"  onclick="Login.sendPwd()">Send</button></p>';
		Error.throwErrorMsg(str, false);
		$('errorMsg').style.width='400px';
		$('Email').focus();
	};
	
	this.sendPwd = function(){
		var self=Login;	
		var e = $('Email');
		if (self.checkEmailIsValid(e.value)) {
			new Ajax.Request('ajax_login.asp', {parameters: 'sendPwd=' +e.value , onSuccess:self.sendPwdHandlerFunc, onFailure:Error.errFunc}); 
			Error.closeErrorMsg();
		}else{
				alert('Please enter a valid email address');
				e.value = '';
				e.focus();
		};
	};
	this.sendPwdHandlerFunc = function(t){
		var self=Login;	
		Error.throwErrorMsg(t.responseText);
	};
	
	this.checkEmailIsValid = function(str){
		var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;	
		if(str.match(emailRegEx)){
			return true;
		}else{	
			return false;
		};
	};
//end reset pwd

	
// END LOGIN FUNCTIONS

	this.setLoginButton = function(str){
		var self = Login; 
		self.whichForm2Submit = str;
	};
	
  this.keyup = function(e) {
    var self = Login;
    if (!e) {
      e = window.event;
    }
	if (self.checkEmailIsValid($("email").value) && $("password").value.length>0){
		self.submitForm = true;
	};
	
    if (e.keyCode == 13 && self.submitForm) {
			self.sendLoginReq();
    };
  };
  
// cookies
	this.setCookie = function(c_name,value,expiredays){
		var self = Login; 
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	};	
	
	this.getCookie = function(c_name){
		var self = Login; 
		if (document.cookie.length>0){
		  c_start=document.cookie.indexOf(c_name + "=");
		  if (c_start!=-1){ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
			} 
		  }
		return "";
	}
	
	
	this.checkCookie=function(){
		var self = Login; 
		var email=self.getCookie('email');
		if (email!=null && email!=""){
			$("email").value=email;
			$("password").focus();
		};
	};
//end cookies
};
window.onload = function(){
		Login.init();	
};

document.onkeyup = Login.keyup;
