function validateInput(obj) {
	if(obj.id == "loginId" && ! isEmailValid(obj.value)) {
		document.getElementById("errorDiv").innerHTML = "Invalid Email!";
	} else if(obj.id == "password" && ! isPasswordValid(obj.value)) {
		document.getElementById("errorDiv").innerHTML = "Invalid Password!";
		$('password').value="";
	} else {
		document.getElementById("errorDiv").innerHTML = "";
	}
}
function validateLogin() {
	loginError = "Invalid Email-id or Password. Please try again!";
	deactiveError = "This email already exists for a deactivated account. To re-activate this account, please contact customer service.";
	not_allowed="Partners are not allowed to login";
	sysError = "Your request could not be processed. Please try later!";
	loginSuccess = "Login Successful. Please wait...";
	if(! isEmailValid( $('loginId').value) || 
			!isPasswordValid( $('password').value)) {
		$('errorDiv').innerHTML = loginError;
	} else {
		showWaiting();
		var ajaxRequest = new Ajax.Request($('login').action,
				{
					method: 'post',
					parameters: Form.serialize($('login'), false),
					onSuccess:function(transport){
						if(transport.responseText=="success") {
							setRememberMe();
							$('errorDiv').innerHTML = loginSuccess;
							$('errorDiv').style.color = "#79AE38";
							setTimeout("sendToHTTP('dashboard')", 2000);
						} else if(transport.responseText=="error"){
							$('errorDiv').innerHTML = loginError;
							$('password').value="";
						} else if(transport.responseText=="inactive"){
							thisForm = $('login');
							thisForm.action = "/resendActivationEmail";
							thisForm.submit();
						}else if(transport.responseText=="deactive"){
							$('errorDiv').innerHTML = deactiveError;
						}else if(transport.responseText=="not_allowed"){
							$('errorDiv').innerHTML = not_allowed;
						}else if(transport.responseText=="exception"){
						    $('password').value="";
							$('errorDiv').innerHTML = sysError;
						}
						hideWaiting();
					},
					onFailure:function(transport){
						$('errorDiv').innerHTML = sysError;
						hideWaiting();
					},
					onException:function(transport){
						$('errorDiv').innerHTML = sysError;
						hideWaiting();
					}
				});
	}
}
function validateFP() {
	invalidEmail = "Invalid Email. Please try again!";
	fpError = "This email address is not registered at National<b>BLS</b>.";
	sysError = "Your request could not be processed. Please try later!";
	fpSuccess = "Your password has been sent to your email account.";
	if(! isEmailValid2( $("fpEmail").value)) {
		$('fpErrorDiv').innerHTML = invalidEmail;
	} else {
		showWaiting();
		var ajaxRequest = new Ajax.Request($('forgotPass').action,
			{
				method: 'post',
				parameters: Form.serialize($('forgotPass'), false),
				onSuccess:function(transport){
					if(transport.responseText=="success") {
						$('fpErrorDiv').innerHTML = fpSuccess;
						setTimeout("closeFPWindow()", 2000);
					} else if(transport.responseText=="error"){
						$('fpErrorDiv').innerHTML = fpError;
					} else if(transport.responseText=="exception"){
						$('fpErrorDiv').innerHTML = sysError;
					}
					hideWaiting();
				},
				onFailure:function(transport){
					$('fpErrorDiv').innerHTML = sysError;
					hideWaiting();
				},
				onException:function(transport){
					$('fpErrorDiv').innerHTML = sysError;
					hideWaiting();
				}
			});
	}
}

function transferToHttps(param)
{
	//comment the below code (calling method ) for enabled HTTPS Settting
    openLoginWindow();
   //un comment the below code (calling method ) for enabled HTTPS Settting
    /*
	//var pageLocation = "https://"+window.location.host+"/home.action?login="+param;
    var pageLocationTemp = "http://"+window.location.host+"/home.action?http=true";
    window.location.href = pageLocationTemp;
	//window.location.href = pageLocation;
	*/
}

function transferToHttpsLogin(param)
{
	var pageLocation = "https://"+window.location.host+"/home?login="+param;
	window.location.href = pageLocation;
}

function openLoginWindow() {
	showHideSelectBox(false);
	emailId = readCookie("loginId");
	if(emailId) {
		$('loginId').value = emailId;
		$('password').value = readCookie("password");
		$('rememberMe').checked = true;
	}
	openModalWindow('loginModalWindow');
	$('loginId').focus();
}
function closeLoginWindow(param) {
	var pageLocation = window.location.href;
	$('loginId').value='';
	$('password').value='';
	$('rememberMe').checked = false;
	$('errorDiv').innerHTML='';
	closeModalWindow('loginModalWindow');
	showHideSelectBox(true);

	if(param == "close")
	{
		if(pageLocation.indexOf("https") >= 0)
		{
			pageLocation = pageLocation.replace(/https/,"http");
			if(pageLocation.indexOf("login=") >= 0)
				pageLocation = pageLocation.substring(0,pageLocation.indexOf("login=")-1);
			window.location.href = pageLocation;
		}
	}
	else if(param == "forgotPass")
	{
		openFPWindow();
	}
}

function openFPWindow(){
	showHideSelectBox(false);
	openModalWindow('FPModalWindow');
	$('fpEmail').focus();
}
function closeFPWindow() {
	var pageLocation = window.location.href;
	$('fpEmail').value='';
	$('fpErrorDiv').innerHTML='';
	closeModalWindow('FPModalWindow');
	showHideSelectBox(true);
	if(pageLocation.indexOf("https") >= 0 && !(pageLocation.indexOf("agentRegistration") >=0 || pageLocation.indexOf("sellerProperty") >=0 || pageLocation.indexOf("buyerRequirement") >=0))
	{
		pageLocation = pageLocation.replace(/https/,"http");
		if(pageLocation.indexOf("login=") >= 0)
			pageLocation = pageLocation.substring(0,pageLocation.indexOf("login=")-1);
		window.location.href = pageLocation;
	}
}
function setRememberMe() {
	if($('rememberMe').checked) {
		createCookie("loginId", $('loginId').value, 365);
		createCookie("password", $('password').value, 365);
	} else {
		eraseCookie("loginId");
		eraseCookie("password");
	}
}
function enableEnterKey(e, formNm){
  var key;
     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox
     
     if (document.all){
		  if (key == 13){
			  window.event.returnValue=false;
			  window.event.cancel = true;
			  document.getElementById('login').click();
		  }
   }
     if(key == 13){
		 var agt = navigator.userAgent.toLowerCase();
    	 if(agt.indexOf("safari") != -1)
    	 {
    		window.event.returnValue=false;
    		window.event.cancel = true;
    	 }
    	 validateForms(formNm); 
	 }
     return true;
}

function validateForms(formNm){
	if(formNm == "login"){
		validateLogin(); 
	}else if(formNm == "forgotPwd"){
		validateFP();
	}
}

function resendMail() {
	loginError = "Invalid Email-id or Password. Please try again!";
	resendSuccess = "An activation email has been sent to your address. Please click on the link in the email to activate your account.";

		showWaiting();
		var ajaxRequest = new Ajax.Request('resendEvMail.action',
				{
					method: 'post',
					parameters: Form.serialize($('resendEvMail'), false),
					onSuccess:function(transport){
						if(transport.responseText=="success") {
							$('popupMsgDiv').innerHTML = resendSuccess;
							openModalWindow('popupMsgModalWindow');
						} else if(transport.responseText=="error"){
							openModalWindow('errorModalWindow');
						} else{
							$('popupMsgDiv').innerHTML = loginError;
							openModalWindow('popupMsgModalWindow');
						}
						hideWaiting();
					},
					onFailure:function(transport){
						openModalWindow('errorModalWindow');
						hideWaiting();
					},
					onException:function(transport){
						openModalWindow('errorModalWindow');
						hideWaiting();
					}
				});
}