/*
'Author					: Stefan Kruger
'Date Created			: 28-Jul-2005
'Last Changed by		: Stefan Kruger
'Date Changed			: 29-Jul-2005
'Version				: 1.0.29072005
'Comments:
'Date:			Desc:
'--------------------------------------------------------------------------------------------
'28-Jul-2005	Code update run completed
'31-Jul-2005	Minor bugfix in completedLoginLoad

'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 CMS = false;
var loginChecker = function (oXML,outputDiv) {
	var retar = new Array();
	retar = (oXML.responseText).split("^||^");
	//bottomcontent.innerHTML = oXML.responseText;
	if (retar.length == 6) {
		//Valid login
		if (parseInt(retar[0]) == 1){
			if (retar[5] == 'CMS'){ CMS = true; };
			logInUser(retar[4]);
		}
		//Invalid login
		else {
			//showLogin(retar[3]);
			getMessageBox(retar[1],retar[2],'topcontent');
		}
	}
	else {
		//alert(oXML.responseText);
		alert ("Invalid length of 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,"topcontent");
	if (display) {
		outputDiv.innerHTML = display;
	}
	regularCursor();
	display = null;
	oXML = null;
	//Focus fixer - used if page has been loaded
	if (document.frmLogin){
		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) {
		document.getElementById("tabcontainer").style.display = 'none';
		document.getElementById("toprightlinks").style.display = 'none';
		document.getElementById("logincontainer").style.display = '';
	}
	else {
		document.getElementById("tabcontainer").style.display = '';
		document.getElementById("toprightlinks").style.display = '';
		document.getElementById("logincontainer").style.display = 'none';
	}
}

/*
Loader / Unloader for top right (Logoff,etc) menu list
loadOrUnload = true --> load
loadOrUnload = false --> unload (for Logoff)
*/
function loadTopRightMenus(loadOrUnload){ 
	if (document.getElementById("toprightlinks")) {
		if (loadOrUnload) {
			document.getElementById("toprightlinks").innerHTML = "<table cellpadding=0 cellspacing=0 border=0 height=100%><tr><td><a href='javascript:void(0)' onclick='logOffUser();ChangeMenuState(this,1);' class='submitlog_link'>logoff</a></td><td valign=middle><img src='images/log_arrow2.gif'></td></tr></table>";
		}
		else {
			//This blank link is used to keep the spacing correct;
			document.getElementById("toprightlinks").innerHTML = "<a href='javascript:void(0)' class='submitlog_link'>&nbsp;</a>";
		}
	}
}

/*
A function to display/clear the User Label
*/
function displayUserLabel(label){
	if (document.getElementById("userlabel")) {
		document.getElementById("userlabel").innerHTML = label;
	}
}

/*
A function to display/clear the Title banner
*/
function displayTitleBanner(banner){
	if (document.getElementById("titlebanner")) {
		document.getElementById("titlebanner").innerHTML = banner;
	}
}

/*
The function displaying home page content
*/
function showHome(){
	contentHider();
	genericSubmit("content/homepage.asp","",null,"topcontent",completedSingleLoad,regularloadingstring,false);
}

function showLastUpdated(){
	contentHider();
	genericSubmit("content/lastupdated.asp","",null,"topcontent",completedSingleLoad,regularloadingstring,false);
}

/*
The function displaying home page content
*/
function showInitialMenu(){
	if (document.getElementById("menucontent")) {
		document.getElementById("menucontent").innerHTML = "";
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////
//USER LOGGING FUNCTIONS
//////////////////////////////////////////////////////////////////////////////////////////////

function logUserVisit(){
	//alert("Logging user visit.");
	var page = "include/sitelogfunctions.asp"
	var data = "action=writelog&typeid=1&logurl="
	hiddenSubmit(page,data,null,3,null,true);
	//Debug
	//hiddenSubmit(page,data,alertHiddenResponse,0,null,true);
}

function logBannerClick(url){
	//alert("Logging banner click.");
	var page = "include/sitelogfunctions.asp"
	var data = "action=writelog&typeid=2&logurl=" + encodeURIComponent(url)
	//alert(data);
	hiddenSubmit(page,data,null,3,null,false);
	//hiddenSubmit(page,data,alertHiddenResponse,0,null,false);
	window.open(url);
}

//////////////////////////////////////////////////////////////////////////////////////////////
//ACTUAL LOGIN FUNCTIONS
//////////////////////////////////////////////////////////////////////////////////////////////

/*
The function displaying a login page
*/
function showLogin(qsdata) {
	try {
		//toggleTabLogin(true); PLR 4-May-2009 Took out toggler, not required now
		var page = "login.asp";
		if (!(qsdata)) { qsdata = ""; }
		//genericSubmit(page,qsdata,null,'topcontent',completedLoginLoad,tabloadingstring,false); //PLR 4-May-2009: Changed logincontainer to topcontent for load of Login Screen
		contentHider();
		genericSubmit(page,qsdata,null,"topcontent",completedSingleLoad,regularloadingstring,false);
		
	}
	catch (ex){}
}

/*Function to check for first login and change pass if needed*/
function checkFirstLogin(){
	var validlogin = false,retval = null,modalreturn = false;
	var checkFirstLogin = function(oXML){
		retval = oXML.responseText;
		//alert(retval);
		if (retval == 1) {
			//Show popup 
			modalreturn = createModalWin("include/setpassword.asp",155,400);
			if (modalreturn) {
				try {
					hiddenSubmit("include/checkfirstlogin.asp","action=writenewpassword&newpass=" + encodeURIComponent(modalreturn),null,3,null,false);
					//hiddenSubmit("include/checkfirstlogin.asp","action=writenewpassword&newpass=" + encodeURIComponent(modalreturn),alertHiddenResponse,0,null,false);
					validlogin = true;
				}
				catch (ex) {}
			}
		}
		else {
			validlogin = true;
		}
		regularCursor();
		oXML = null;
	}
	hiddenSubmit("include/checkfirstlogin.asp","action=checkfirstlogin",checkFirstLogin,0,null,false);
	//alert(validlogin)
	return validlogin;
}

/*
Batch function to process login display setup when user has been authorised
*/
function logInUser(username,cmsused){

	var finishlogin = checkFirstLogin();
	if (finishlogin){
		//Log the user visit
		logUserVisit();
		//Toggle topdisplay
		//toggleTabLogin(false); PLR 4-May-2009 Took out toggler, not required now
		
		//Load tab menu
		//getTabMenu(true); PLR 5-May-2009 Took out
		//Load top right menu
		//loadTopRightMenus(true); PLR 5-May-2009 Took out
		
		//Display username label
		//displayUserLabel(username);
		//Display title banner
		displayTitleBanner("Log Off (" + username + ")"); // PLR 5-May-2009 Changed "Home Page" to say "Log Off"
		document.getElementById("login_ahref").href = "javascript:logOffUser();" // PLR 5-May-2009 Added this to toggle Login/Logoff on main page
		document.getElementById("search_ahref").onclick = function () { getPage('62DB4E51-CAB3-427A-B93F-E2D29A6E54D1',''); ChangeMenuState(this,1);}; // PLR 5-May-2009 Added this to toggle permission to see Master Search on main page

		if (cmsused == "CMS") {CMS = true;};
		if (CMS) {document.getElementById("cms_ahref").style.display = '';}	
		
		
		//Show Homepage
		//showHome();
		getPage('62DB4E51-CAB3-427A-B93F-E2D29A6E54D1','');
	}
}

/*
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("include/logoff.asp",'',null,3,null,true);
	//Clear tab menu
	//getTabMenu(false);PLR 5-May-2009 Took out
	
	//Hide scroll arrows
	//toggleArrows(false);PLR 5-May-2009 Took out
	
	//Clear page content
	contentHider();
	//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 also ensure that cookies have been enabled in your web browser.|^^|Please log in again.",'topcontent');
	}
	else {
		getMessageBox("Logged off","You have logged off successfully.",'topcontent');
	}
	//Tab left reset to prevent missing menu when logging out and then in again.
	if (document.getElementById("tabcontent")) {
		document.getElementById("tabcontent").style.left = 0;
	}
	//Show login
	//showLogin(null);PLR 5-May-2009 Took out
	
	displayTitleBanner("Login"); // PLR 5-May-2009 Added this to toggle Login/Logoff on main page
	document.getElementById("login_ahref").href = "javascript:showLogin();" // PLR 5-May-2009 Added this to toggle Login/Logoff on main page
	document.getElementById("search_ahref").onclick = function () {showLogin();ChangeMenuState(this,1);}; // PLR 5-May-2009 Added this to toggle permission to see Master Search on main page
	
	document.getElementById("cms_ahref").style.display = 'none';
	CMS = false;
	//Clear Top right (Links) menu
	//loadTopRightMenus(false); PLR 5-May-2009 Took out
	
	//Clear username label
	//displayUserLabel("");
	
	//Clear title banner
	//displayTitleBanner("Logged Off"); PLR 5-May-2009 Took out, using displayTitleBanner to toggle Login/Log Off button on main site
	
	//Show initial menu
	//showInitialMenu(); PLR 5-May-2009 Took out
}

/*
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){
			//alert(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 = "include/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(evt){
	if (isEnterKey(evt)){
		doLogin(true);
	}
}

// Change Temp User password

function UpdateUser(){
	if (document.frmUpdateUser.newpass.value == document.frmUpdateUser.confirm.value) {
		if (document.frmUpdateUser.newpass.value != ''){
			if (document.frmUpdateUser.confirm.value != ''){
					var page = "include/settemppassword.asp"
					var data = fetchInputFormData('frmUpdateUser') + "&txtMode=UpdateUser"
					hiddenSubmit(page,data,alertHiddenResponse,0,null,false);
					showHome();
					
			}else{
				alert("Please confirm the new password!");
			}
		}else{
			alert("You must enter a new password!");
		}
	}
	else {
		alert("Passwords do not match!");
	}
}

