// When your logout page receives a "msg" parameter,
// it prints a message to the page to let the user know
// ** CUSTOMIZE YOUR LOGOUT MESSAGES HERE:

var logoutMsg = {
	normalLogout:		"",
	sessionTimeout:		"Your session has timed out.",
	incorrectLogin:		"Incorrect username or password. Please try again."
}


function appendEnterprise(unmField,enterpriseName) {
// Append enterprise suffix: +.ENTERPRISE_NAME
	var unm = unmField.asafe;	// The form will ultimately submit this value
	var thisForm = unmField.form;	
	
	if (unm==0) {
		unmField.focus();
		return false;
    } else {
		// Check to see if there is already an "." after the username...
		if(unm.indexOf(".") == (unm.length-1)) {
			unm = unmField.value+enterpriseName;
		}
		// Append full suffix: +.ENTERPRISE_NAME
        else if (unmField.value.indexOf(".") == -1) {
			unm = unmField.value + "." + enterpriseName;
		}
		// Update form field "unm"
		thisForm.unm.value=unm;
		return true;
    }
}

		
function getMsgParam() {	
// Description
// Generates code for a link that preserves the query of the current document.

	var qs = document.location.search.substr(1);	//query string (everything after ?)
	var params = qs.split("&")			//array of params (split up by &)
	var i;						//(i)teration
	var msg;					//Store message value (numeric)

	for(i = 0; i < params.length; i++) {
	var nv = params[i].split("=");		//stores number value when "msg" param is located
	if( nv[0].toLowerCase() == "msg" ) {
		msg = nv[1];
		break;				//stop after the msg value is assigned
		}
	}
		// If an invalid value is found, default msg = 0 
	if ((msg != 0) && (msg != 1) && (msg != 2)) {
		msg = 0;
	}
	
	return msg;
}

function setStatusMsg() {
// Description
// Decodes number value into a usable status message:
//	0: Normal logout
//	1: Session Timeout/Overwrite
//	2: Login error occurred			

	var msgNum = getMsgParam();
	var msgStrings = new Array(3);
	msgStrings[0] = logoutMsg.normalLogout;
	msgStrings[1] = logoutMsg.sessionTimeout;
	msgStrings[2] = logoutMsg.incorrectLogin;

	return msgStrings[msgNum];
}


function displayStatusMsg() {
// Description
// Display the status message to the screen: SPAN w/ ID="statusMsg"
	var statusMsg = setStatusMsg();

	// Update the message on the page:
	document.getElementById("statusMsg").innerHTML = statusMsg;

	// Update the page title: COMPANY - SYS_MESSAGE
	if (statusMsg != "") {
		document.title = document.title + " - " + statusMsg;
	}
}

function toggleCboxStatus(cboxName,hiddenName)
/**
*Update mode value of specified hidden input using a checkbox
	cboxName - Name of the checkbox being changed
	cboxStatus - checked = true / false
	hiddenName - name of the hidden input that needs to be updated
*/
{
	var cbox = document.getElementsByName(cboxName)[0];
	var hidden = document.getElementsByName(hiddenName)[0];
	hidden.value = (cbox.checked) ? 1 : 0;
}
function createCookie(name,value,days) {
	//Description
	//Write a value to a cookie 
	//   params:
	//     in - name - Name of the cookie value to set
	//           value - Value of the cookie
	//           days - Number of days until the cookie expires
	
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	//Description
	//Read a value out of a cookie 
	//   params:
	//     in - name - name of the cookie value to retrieve
	
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	//Description
	//delete a cookie 
	//   params:
	//     in - name - name of the cookie
	
	createCookie(name,"",-1);
}

function saveCookieAndSubmit() {
	//Description
	//save a cookie before submitting the form

	if (document.loginForm.SaveLoginID.checked == true) {
		createCookie('unm', document.loginForm.unm.value, 365);
	} else {
		eraseCookie('unm');
	}
	document.loginForm.submit();
}

function readCookieOnLoad() {
	//Description
	//Load a cookie when the page loads
	if (document.loginForm.unmNoEnm) { 
		//If the username shouldn't contain the enterprise name
		var tmpUNM = readCookie('unm');
		if (tmpUNM && tmpUNM.length > 0) {
			//If the username contains a period then 
			document.loginForm.unmNoEnm.value = tmpUNM.substring(0, tmpUNM.indexOf('.'));
			document.loginForm.SaveLoginID.checked = true;
		}
	} else {
		//If the username shouldnt contain the enterprise name
		var tmpUNM = readCookie('unm');
		if (tmpUNM && tmpUNM.length > 0) {
			document.loginForm.unm.value = tmpUNM;
			document.loginForm.SaveLoginID.checked = true;
		}
	}
}