/**
* Description	: This function is to set focus when login page loads
*/
function init() {
	document.getElementById("usermail").focus();
}

/**
* Description	: This function sends request so that different fields for the 
*				  registration page initializes.
*/
function openUserRegistration() {
	var url = "../registration.epi"
	createRequest();
	if(ajaxRequest == null)
		return;
		
	ajaxRequest.onreadystatechange=checkServerResponse;
	ajaxRequest.open("POST", url, true);
	ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajaxRequest.send("method=showNewUserPage");
	return false;		
}

function checkServerResponse() {
	if (ajaxRequest.readyState==4 && ajaxRequest.status==200) {
    	var response = ajaxRequest.responseText;
    	if(response == 1) {
    			document.location="login.jsp";
		} else if(response == 2) {
			document.location="../view/registration.jsp";
		}
	}
}
function updateErrorPanel(message) {
	var panel = document.getElementById("errorPanel");
	if(message == "") {
		panel.innerHTML = "";
	} else {
		panel.innerHTML = "<font color=\"red\" size='2'>" + message + "</font><br/>";
	}
	
	init();	
}


function validateFields() {
	clearForgotPasswordHolder();
	var email = strtrim(document.getElementById("usermail").value);
	
	if(email == "") {
		document.getElementById("errorPanel").innerHTML = "<font color='red' size='2'>Please enter your Email address.</font>";
		document.loginform.usermail.focus();
		return false;
	} 
	
	var isMailValid = emailValidation(document.loginform.usermail);
	
	if(isMailValid == false)  {
		document.getElementById("errorPanel").innerHTML = "<font color='red' size='2'>Please enter a valid Email address.</font>";
		document.loginform.usermail.focus();
		return false;
	}
	
	var pwd = strtrim(document.getElementById("upassword").value);
	if(pwd == "") {
		document.getElementById("errorPanel").innerHTML = "<font color='red' size='2'>Please enter password.</font>";
		document.loginform.upassword.focus();
		return false;
	} 
	
	document.loginform.submit();
}

function clearFields() {
	clearForgotPasswordHolder();
	if(document.getElementById("usermail") != null) {
		document.getElementById("usermail").value = "";
		document.getElementById("usermail").focus();
	}
	
	if(document.getElementById("upassword") != null) {
		document.getElementById("upassword").value = "";
	}
}

function forgotPassword() {
	clearForgotPasswordHolder();
	var mail = strtrim(document.getElementById("usermail").value);
	
	if(mail == "") {
		document.getElementById("invalidMailError").innerHTML = "<font color='red' size='2'>Please enter an Email address.</font>";
		return false;
	} 
	
	var isMailValid = emailValidation(document.getElementById("usermail"));
	if(!isMailValid) {
		document.getElementById("invalidMailError").innerHTML = "<font color='red' size='2'>This is not valid Email address. Please enter a valid Email address.</font>";
		return false;
	} else if(mail.length > 320) {
		document.getElementById("invalidMailError").innerHTML = "<font color='red' size='2'>Maximum 320 characters are allowed for Email address.</font>";
		return false;
	}
	var url = "../SendForgotMail"
	
	createRequest();
	if(ajaxRequest == null)
		return;
		
	var params = "method=forgotpasswordmail&mailtosent=" + mail;
	
	ajaxRequest.onreadystatechange = function() { 
											if (ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
												document.getElementById("errorPanel").innerHTML = "";
												document.getElementById("invalidMailError").innerHTML = "";
												var response = ajaxRequest.responseText;
												showProgressBar(0);
												document.getElementById("errorPanel").innerHTML = "<font color=\"red\" size=\"2\">" + response + "</font>"
											} else {
												//document.getElementById("errorPanel").innerHTML = '<table id="content" cellpadding="10"><tr><td><h2>Sending ' + "...".substr(0,ajaxRequest.readyState) + '</h2></td></tr></table>';
												showProgressBar(1);
											}
										};
	ajaxRequest.open("POST", url, true);
	ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajaxRequest.send(params);
	return false;	
	
}


function onCancel() {
	window.location='../view/login.jsp';
}


function showProgressBar(sw) {
	if (sw == 1) {
		// Show popup
		if(document.getElementById('blackoutProgress') != null)
			document.getElementById('blackoutProgress').style.visibility = 'visible';
		if(document.getElementById('divpopupProgress') != null)
			document.getElementById('divpopupProgress').style.visibility = 'visible';
		if(document.getElementById('blackoutProgress') != null)
			document.getElementById('blackoutProgress').style.display = 'block';
		if(document.getElementById('divpopupProgress') != null)
			document.getElementById('divpopupProgress').style.display = 'block';
	} else {
	
		// Hide popup
		if(document.getElementById('blackoutProgress') != null)
			document.getElementById('blackoutProgress').style.visibility = 'hidden';
		if(document.getElementById('divpopupProgress') != null)
			document.getElementById('divpopupProgress').style.visibility = 'hidden';
		if(document.getElementById('blackoutProgress') != null)
			document.getElementById('blackoutProgress').style.display = 'none';
		if(document.getElementById('divpopupProgress') != null)
			document.getElementById('divpopupProgress').style.display = 'none';
	}

}

function clearForgotPasswordHolder() {
	if(document.getElementById("errorPanel") != null)
		document.getElementById("errorPanel").innerHTML = "";
	if(document.getElementById("invalidMailError") != null)
		document.getElementById("invalidMailError").innerHTML = "";
	
	if(document.getElementById("pwdErrorHolder") != null)
		document.getElementById("pwdErrorHolder").innerHTML = "";
	if(document.getElementById("mailErrorHolder") != null)
		document.getElementById("mailErrorHolder").innerHTML = "";
	
	if(document.getElementById("pwdErrorHolder1") != null)
		document.getElementById("pwdErrorHolder1").innerHTML = "";
	if(document.getElementById("mailErrorHolder1") != null)
		document.getElementById("mailErrorHolder1").innerHTML = "";
		
}
