/**
	This file contains javascript functions for Login
 */
document.onkeypress = grabKeyPress;

function grabKeyPress(e)
{
	if (!e) var e = window.event;

	if (e.keyCode == 13)
		submitLogin();
}

function validate_mobile() {
	var formName = document.getElementById('loginform');
	var username = formName.username.value;
	var password = formName.password.value;

	if ( username.trim().length == 0 ) {
		alert("User Name cannot be left blank");
		formName.username.focus();
		return;
	}

	if ( password.trim().length == 0 ) {
		alert("Password cannot be left blank");
		formName.password.focus();
		return;
	}
	
	formName.submit();
	return;
}

/**
    Validation for username and password
*/
function submitLogin()
{
	showStatus(true, getLocalValue('MSG_LOGIN_STATUS'));
							 
	var formobj = document.getElementById('loginform');
	var username = formobj.username.value;
	var password = formobj.password.value;

	if ( vAlert(getLocalValue('LBL_LOGIN_USER'), username, 'r', 0, 0) == false ) {
		showStatus(false);
		return;
	}
	if ( vAlert(getLocalValue('LBL_LOGIN_PWD'), password, 'r', 0, 0) == false ) {
		showStatus(false);
		return;
	}

	var xmlDoc = validateLogin(username, password, true);
	
	if ( xmlDoc != null ) {
		if ( getEntityValue(xmlDoc, getLocalValue('WEB_LOGIN_ACTIVE')) != 'Y' ) {
			alert(getLocalValue('MSG_LOGIN_INACTIVE'));
			showStatus(false);
			return;
		}
		
		formobj.LOGIN_ID.value = getEntityValue(xmlDoc, getLocalValue('WEB_LOGIN_PERSON'));
		formobj.LOGIN_TYPE.value = getEntityValue(xmlDoc, getLocalValue('WEB_LOGIN_LOGIN_TYPE'));
		formobj.LOGIN_WEB_LOGIN_ID.value = getEntityID(xmlDoc);
		
		formobj.submit();
		showStatus(false);
		return;
	}
	
	alert(getLocalValue('MSG_LOGIN_INVALID'));
	showStatus(false);
}

function validateLogin(username, password, fvalidatepwd) {
	var prefix = '&' + getGlobalValue('ADAPT_PREFIX');
	var searchname = '&' + getGlobalValue('SEARCHFORM_NAME');
	var asxml = '&' + getGlobalValue('SEARCHFORM_XMLRESULTS') + '=yes';
	
	// check the first email address
	var params = searchname + '=US_WebLoginSearch' + asxml;
	params += prefix + 'LoginName=' + username;

	var xml = runSearch( params );
	var xmlDoc = getXMLDocument(xml);

	if ( getSearchCount(xmlDoc) != 1 ) {
		return null;	
	}
	
	var sid = getSearchResult(xmlDoc, 1);	
	
	xml = getEntityAsXML(sid);
	xmlDoc = getXMLDocument(xml);
	
	if ( fvalidatepwd == true ) {
		var pwd = getEntityValue(xmlDoc, getLocalValue('WEB_LOGIN_PASSWORD'));
		return ( pwd == password )?xmlDoc:null;
	}
	
	return xmlDoc;
}

function sendPassword() {
	var formobj = document.getElementById('loginform');
	var username = formobj.username.value;
	
	if ( vAlert(getLocalValue('LBL_LOGIN_USER'), username, 'r', 0, 0) == false ) {
		showStatus(false);
		return;
	}
	
	var xmlDoc = validateLogin(username, '', false);
	
	if ( xmlDoc == null ) {
		alert(getLocalValue('MSG_LOGIN_INVALID'));
	}
	else {
		// get the password and email address
		var pwd = getEntityValue(xmlDoc, getLocalValue('WEB_LOGIN_PASSWORD'));
		var pid = getEntityValue(xmlDoc, getLocalValue('WEB_LOGIN_PERSON'));
		var xml = getEntityAsXML(pid);
		xmlDoc = getXMLDocument(xml);
		var email = getEntityValue(xmlDoc, getLocalValue('CONTACT_EMAIL'));
		
		var subj = getLocalValue('EMAIL_SUBJ_PWD');
		var body = getLocalValue('EMAIL_BODY_PWD_NN');
		body = body.replace(getGlobalValue('NUM_SUB'), username);
		body = body.replace(getGlobalValue('NUM_SUB2'), pwd);
		var status = sendEmail(email, subj, body);
		if ( status.trim() == 'true' )
			alert(getLocalValue('MSG_SECURITY_PWD_SENT'));
		else
			alert(status);
	}
	
	showStatus(false);
	return;
}
