/*
'Author					: Stefan Kruger
'Date Created			: 21-Feb-2005
'Last Changed by		: Stefan Kruger
'Date Changed			: 08-Sep-2005
'Version				: 1.0.08092005
'Comments:
'Date:			Desc:															
'--------------------------------------------------------------------------------------------
'16-Mar-2005	Added non-refresh login/logout functionality
'24-Mar-2005	Switched to Shield NLAF
'30-Mar-2005	Changed minor naming convention in login/tab containers
'05-Apr-2005	Update genericSubmit() for stopProcessing functionality
'09-Jul-2005	Added content loading functions and a member id loader on login
'08-Sep-2005	Added functions used for supplier id loading.

'ToDoList:
'Date:			Desc:															Status:
'--------------------------------------------------------------------------------------------

*/

//////////////////////////////////////////////////////////////////////////////////////////////
//PREDEFINED USER FUNCTIONS USED IN THE POST-LOADING OF LOGIN
//////////////////////////////////////////////////////////////////////////////////////////////

/*
This function is called on successful finish of an actual login to check for login success or failure
*/
var loginChecker = function (oXML,outputDiv) {
	var retar = new Array();
	retar = (oXML.responseText).split("^||^");
	//alert(retar);
	if (retar.length) {
		if (retar.length == 5) {
			//Valid login
			if (parseInt(retar[0]) == 1){
				logInUser(retar[4]);
				//This would usually load the default page instead of hiding content.
				//contentHider();
			}
			//Invalid login
			else {
				showLogin(retar[3]);
				getMessageBox(retar[1],retar[2],'topcontent');
			}
		}
		else {
			alert ("Invalid length of login verification data returned.");
		}
	}
	else {
		alert ("Invalid login verification data returned.");
	}
	regularCursor();
	oXML = null;
};

/*
This function is used when displaying a loaded login page
*/
var completedLoginLoad = function (oXML,outputDiv) {
	var display = pageLoadChecker(oXML,outputDiv)
	display = oXML.responseText
	if (display) {
		eval(outputDiv).innerHTML = display;
	}
	regularCursor();
	display = null;
	oXML = null;
	//Focus fixer.
	if (document.frmLogin.username.value == "") {
		document.frmLogin.username.focus();
	}
	else {
		document.frmLogin.password.focus();
	}
};

//////////////////////////////////////////////////////////////////////////////////////////////
//AUXILIARY LOGIN AND LOGOUT FUNCTIONS
//////////////////////////////////////////////////////////////////////////////////////////////

/*
Function to show either the tabContainer or LoginContainer
*/
function toggleTabLogin(showLogin){
	if (showLogin) {
		tabcontainer.style.display = 'none';
		logincontainer.style.display = '';
	}
	else {
		tabcontainer.style.display = '';
		logincontainer.style.display = 'none';
	}
}

/*
A function to load (initialize) the left menu content display panel.
*/
function loadInitialMenu(){
	genericSubmit("content/staticcontentlinks.asp",'',null,'menucontent',completedSingleLoad,regularloadingstring,false);
}

/*
A function to load (initialize) the welcome screen content display panel.
*/
function loadWelcomeScreen(){
	contentHider();
	genericSubmit("content/staticwelcomepage.asp",'',null,'topcontent',completedSingleLoad,regularloadingstring,false);
}

/*
A function to load (initialize) the full static content startup screen.
*/
function doFullStaticContentLoad(){
	loadInitialMenu();
	loadWelcomeScreen();
}

/*
A function to load (initialize) or unload the member select screen at login.
*/
function loadMemberID(unloadFlag){
	if (unloadFlag) {
		try {
			document.getElementById("membersearchcontent").style.display = 'none';
		}
		catch (ex) {}
	}
	else {
		var checkOnCookieWrite = function(oXML){
			var retvalue = oXML.responseText;
			if (retvalue != ""){
				//Fine. Member select is to remain hidden.
			}
			else {
				//Show memeber selection
				try {
					document.getElementById("membersearchcontent").style.display = '';
					document.getElementById("currentmembername").innerText = "None";
					document.getElementById("membersearch").value = "";
				}
				catch (ex) {}
			}
			regularCursor();
			oXML = null;
		}
		hiddenSubmit("bin/loadmemberid.asp","action=loadmemberid",checkOnCookieWrite,0,null,true);
	}
}

/*
A function to load (initialize) or unload the supplier select screen at login.
*/
function loadSupplierID(unloadFlag){
	if (unloadFlag) {
		try {
			document.getElementById("suppliersearchcontent").style.display = 'none';
		}
		catch (ex) {}
	}
	else {
		var checkOnCookieWrite = function(oXML){
			var retvalue = oXML.responseText;
			if (retvalue != ""){
				//Fine.Supplier select is to remain hidden.
			}
			else {
				//Show Supplier selection
				try {
					document.getElementById("suppliersearchcontent").style.display = '';
					document.getElementById("currentsuppliername").innerText = "None";
					document.getElementById("suppliersearch").value = "";
				}
				catch (ex) {}
			}
			regularCursor();
			oXML = null;
		}
		hiddenSubmit("bin/loadsupplierid.asp","action=loadsupplierid",checkOnCookieWrite,0,null,true);
	}
}

/*
Loader / Unloader for top right (Logoff,etc) menu list
loadOrUnload = true --> load
loadOrUnload = false --> unload (for Logoff)
*/
function loadTopRightMenus(loadOrUnload){ 
	if (toprightlinks) {
		if (loadOrUnload) {
			toprightlinks.innerHTML = "<a href='javascript:void(0)' onclick='logOffUser()' class=submit1>logoff</a>";
		}
		else {
			//This blank link is used to keep the spacing correct;
			toprightlinks.innerHTML = "<a href='javascript:void(0)' class=top_right>&nbsp;</a>";
		}
	}
}

/*
A function to display/clear the User Label
*/
function displayUserLabel(label){
	if (userlabel) {
		userlabel.innerHTML = label;
	}
}

/*
A function to display/clear the Title banner
*/
function displayTitleBanner(banner){
	if (titlebanner) {
		titlebanner.innerHTML = banner;
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////
//ACTUAL LOGIN FUNCTIONS
//////////////////////////////////////////////////////////////////////////////////////////////

/*
The function displaying a login page
*/
function showLogin(qsdata) {
	try {
		toggleTabLogin(true);
		var page = "login.asp";
		if (!(qsdata)) { qsdata = ""; }
		genericSubmit(page,qsdata,null,'logincontainer',completedLoginLoad,tabloadingstring,false);
	}
	catch (ex){}
}

/*
Batch function to process login display setup when user has been authorised
*/
function logInUser(username){
	toggleTabLogin(false);
	//Load Tab menu
	getTabMenu(true);
	//Load Static Content
	doFullStaticContentLoad();
	//Load Top right (Links) menu
	loadTopRightMenus(true);
	//Display username label
	//displayUserLabel(username);
	//Display title banner
	//displayTitleBanner("Home");
	//Load grid content
	//buildGridDataStructures();
	//Load member id
	loadMemberID();
	loadSupplierID();
}

/*
Batch function to process a complete logoff
If sessionExpiry is true a sessionexpired message is displayed instead of the default logoff
*/
function logOffUser(sessionExpiry){
	//Clear cookies
	hiddenSubmit("bin/logoff.asp",'',null,3,null,true);
	//Clear tab menu
	getTabMenu(false);
	//Hide scroll arrows
	toggleArrows(false)
	//Clear page content
	contentHider();
	loadInitialMenu();
	//Display logged off message
	if (sessionExpiry) {
		getMessageBox("Session Expired","Because of more strict security measures in place, you are unable to view any more pages.|^^|This should only happen every 24 hours or if your IP address changes.|^^|Please log in again.",'topcontent');
	}
	else {
		getMessageBox("Logged off","You have logged off successfully.",'topcontent');
	}
	//Show login
	showLogin(null);
	//Clear Top right (Links) menu
	loadTopRightMenus(false);
	//Tab left reset to prevent missing menu when logging out and then in again.
	if (tabcontent) {
		tabcontent.style.left = 0;
	}
	//Clear username label
	//displayUserLabel("");
	//Clear title banner
	//displayTitleBanner("")
	//Unload Member selection
	loadMemberID(true);
	loadSupplierID(true);
}
/*
A function that logs off the current user and displays the session expiry message
*/
function expireSession(){
	logOffUser(true);
}

/*
This function is an entry point to either authorising a user login or mailing of password from login screen
loginOrMail = true --> login
loginOrMail = false --> mail password (for Logoff)
*/
function doLogin(loginOrMail,site){
	if (document.frmLogin.username.value != ""){
		if (loginOrMail){
			if (document.frmLogin.password.value != ""){
				hiddenSubmit(document.frmLogin.action,fetchInputFormData('frmLogin'),loginChecker,0,null,true);
			}
			else {
				alert("Please enter your password.");
			}
		}
		else {
			mailPassword(document.frmLogin.username.value,site)
		}
	}
	else {
		alert("Please enter an e-mail address.");
	}
	return false;
}

/*
This function calls for the mailing of a users password.
*/
function mailPassword(mailad,site) {
	var page = "bin/mailer.asp"
	var data = "action=forgotpassword&txtemail=" + mailad + "&txtsite=" + site
	hiddenSubmit(page,data,alertHiddenResponse,0,null,true);
}

//////////////////////////////////////////////////////////////////////////////////////////////
//EVENT LISTENERS
//////////////////////////////////////////////////////////////////////////////////////////////

/*
Login password box enter press listener
*/
function checkLoginForEnter(fld,evt){
	if (isEnterKey(evt)){
		doLogin(true,"");
	}
}
