/*
$Source:$
$Revision:$
$Author:$
$Date:$

Created By: Tuan Tran
*/

//	Resets the login container to its default values
function resetLogin()
{
	document.frmLogin.reset();
	document.frmLogin.action = 'login.cfm';
	$('hiddenUserLoginMsg').hide();
	$('hiddenLoginHeaderMsg').update('Log in to the HRSA Web Reporting System');
	$('useridLabel').update('User id');
	$('passwordContainer').show();
	$('forgotPasswordOption').show();
	$('hiddenForgotPasswordMsg').hide();
	$('loadingContainer').hide();
	$('hiddenLoginLayer').hide();
}

//	Used to display login layer
function openLogin()
{
	var w = 236;
	var h = 350;
	var leftPos = (screen.width - w) / 4 + "px";
	var topPos = (screen.height - h) / 4 + "px";
	
	resetLogin();
	$('hiddenLoginContainer').setStyle({left:leftPos, top:topPos});
	setTimeout("new Effect.Grow('hiddenLoginContainer', {queue: {position:'front', scope: 'login', limit:2}})",100);
	setTimeout("new Effect.Appear('hiddenLoginLayer', {afterFinish: loginFocus, queue: {position:'end', scope: 'login', limit:2}})",150);
}

function loginFocus()
{
	document.frmLogin.userid.focus();
}

//	Used to hide login layer
function cancelLogin()
{
 resetLogin();
 new Effect.Shrink('hiddenLoginContainer');
}

//	Forgot password function
function forgotPassword()
{
	// Checks to see if lockedout container is hidden
	if ($('hiddenUserLockedout').style.display !== 'none')
		$('hiddenUserLockedout').hide();
	// Checks to see if incorrect login message is hidden
	if ($('hiddenUserLoginMsg').style.display !== 'none')
		$('hiddenUserLoginMsg').hide();
	//	Hides password div and forgot your password div
	new Effect.Fade('hiddenUserLoginMsg');
	new Effect.Fade('forgotPasswordOption');
	$('hiddenForgotPasswordMsg').update('Enter your email address below to reset your password');
	new Effect.Fade('passwordContainer');
	document.frmLogin.action = 'act_forgot_password.cfm';
	$('hiddenLoginHeaderMsg').update("Reset your password");
	new Effect.Appear('hiddenForgotPasswordMsg');
	$('useridLabel').update("Email Address:");
	document.frmLogin.signin.value = 'Submit';
	document.frmLogin.userid.focus();
}

// 	Ajax call.  Test to see if the user id and password combo is correct
function chkUser(qry)
{
		var user = qry;
		//	Escapes the # sign to work properly with the coldfusion function
		user = user.replace(/#/g, "##");
		user = user.replace(/"/g, '""');
		//	Activates the loader bar indicator
		$('loadingContainer').show();
		DWREngine._execute(_cfscriptLocation, null, 'chkUser', user, getUserResult);
}

//	Ajax call.  Test to make sure the email address the user entered to reset password is in the system
function chkEmail(qry)
{
	var email = qry;
	//	Activates the loader bar indicator
	$('loadingContainer').show();
	DWREngine._execute(_cfscriptLocation, null, 'chkEmail', email, getEmailResult);
}

//	Get the results of checking whether if the user exist in the system. if user exist then submit form else, display message
function getUserResult(userExist)
{
	//	if the user was not found in the system then display message and cancel submission
	if (userExist == 'true')
		document.frmLogin.submit();
	else if (userExist == 'false')
	{
		new Effect.Appear('hiddenUserLoginMsg');
		// Checks to see if lockedout container is hidden
		if ($('hiddenUserLockedout').style.display !== 'none')
			$('hiddenUserLockedout').hide();
		//	Deactivate loading bar indicator
		$('loadingContainer').hide();
	}
	else if (userExist == 'lockedout')
	{
		new Effect.Appear('hiddenUserLockedout');
		// Checks to see if incorrect login message is hidden
		if ($('hiddenUserLoginMsg').style.display !== 'none')
			$('hiddenUserLoginMsg').hide();
		//	Deactivate loading bar indicator
		$('loadingContainer').hide();
	}
	else
	{
		openLogin();
	}
}

//	Get the results of checking whether if the email exist in the system. if it exist then submit form else, display message
function getEmailResult(emailExist)
{
	//	if the email the user entered was not found in the system then display message and cancel submission
	if (emailExist == 'true')
		document.frmLogin.submit();
	else
	{
		$('hiddenForgotPasswordMsg').update('<span class=\'red\'><strong>The email address you entered was not found in our system</strong></span>');
		//	Deactivate loading bar indicator
		$('loadingContainer').hide();
	}
}

//	Test to see if email address is in the system
function submitLogin(e)
{
	form = e.form;
	//	if the forgot your password form is active then
	if ($('hiddenForgotPasswordMsg').style.display !== 'none')
	{
		chkEmail(form.userid.value);
	}
	else
	{
		chkUser(form.userid.value + "," + form.password.value);
		//	if forgot your password is not active then submit the login form
		//document.frmLogin.submit();
	}
}

//	Allow pressing enter key to submit login form
function addInputSubmitEvent(form, input) { 
    input.onkeydown = function(e) { 
        e = e || window.event; 
        if (e.keyCode == 13) { 
            submitLogin(input); 
            return false; 
        } 
    }; 
} 