var loginArray = [];

var ajaxRequest;

var passedString, loginValue, loginCheck, loginArrayCounter, attemptChecker, comma, semiColon, currdate, expirationdate, autoSignOutCheck, adminAutoSignOutCheck, loggedOutCheck, expired, focused, webpage, webpageCheck, winOpen, parentRefresh, id;

var browser = navigator.appName;

var focused = false;
var adminFocused = false;

function focusedLogin() {
	focused = true;
}

function blurredLogin() {
	focused = false;
}

function adminFocusedLogin() {
	adminFocused = true;
}

function adminBlurredLogin() {
	adminFocused = false;
}

document.onkeyup = keyCheck;
function keyCheck(ev) {
	if (browser == "Microsoft Internet Explorer") {
		if (event.keyCode==13) {
			if (focused == true) {
				login(webpageCheck);
			}
			if (adminFocused == true) {
				adminLogin(webpageCheck);
			}
		}
	}
	else {
		if (ev.keyCode==13) {
			if (focused == true) {
				login(webpageCheck);
			}
			if (adminFocused == true) {
				adminLogin(webpageCheck);
			}
		}
	}
}

function login(webpageCheck) {
	//if (browser == "Microsoft Internet Explorer") {
		passedString = "";
		loginCounter = 0;
		loginCheck = true;
		while (loginCounter < loginArray.length) {
			loginValue = document.getElementById(loginArray[loginCounter]).value;
			if (passedString == "") {
				passedString = passedString + "?" + loginArray[loginCounter] + "=" + document.getElementById(loginArray[loginCounter]).value;
			}
			else {
				passedString = passedString + "&" + loginArray[loginCounter] + "=" + document.getElementById(loginArray[loginCounter]).value;
			}
			if (loginValue == "") {				
				loginCheck = false;			
			}
			loginCounter += 1;
		}	
		
		if (loginCheck == false) {
			alert("You have not filled in all of the required fields!");
		}
		else {	
			ajaxRequest = new XMLHttpRequest();
	
			ajaxRequest.onreadystatechange = function() {
				if(ajaxRequest.readyState == 4) {
					if (ajaxRequest.responseText == "") {
						attemptChecker = document.cookie.match ( '(^|;) ?' + 'adminAttempts' + '=([^;]*)(;|$)' );
						if (attemptChecker == null) {	
							document.cookie = "adminAttempts=3";
						}
						attemptChecker = document.cookie.match ( '(^|;) ?' + 'adminAttempts' + '=([^;]*)(;|$)' );
						attemptChecker = String(attemptChecker);
						comma = attemptChecker.lastIndexOf(",");
						semiColon = attemptChecker.lastIndexOf(";", comma);
						attemptChecker = attemptChecker.substring(semiColon, attemptChecker.length);
						attemptChecker = attemptChecker.replace(/,/g, "");
						attemptChecker = attemptChecker.replace(/;/g, "");
						
						if (attemptChecker != 0) {
							attemptChecker = attemptChecker - 1;
						}
						document.cookie = "adminAttempts=" + attemptChecker;
						if (attemptChecker == 0) {						
							alert("You have exceeded the number of allowed login attempts and are now locked out of the system for 24 hours!");						
							//window.location = "lockOut.asp";
							currdate = new Date();
							expirationdate = new Date(currdate.getTime() + 86400000);
							document.cookie = "adminAttempts=0;expires=" + expirationdate.toGMTString();
						}					
						else {
							if (attemptChecker != 1) {
								alert("The username/password entered do not match our records! You have " + attemptChecker + " attempts left.");	
							}
							else {
								alert("The username/password entered do not match our records! You have " + attemptChecker + " attempt left.");
							}						
						}
					}
					else if (ajaxRequest.responseText.indexOf("password") != -1) {
						id = ajaxRequest.responseText.replace("password", "");
						
						document.getElementById("passwordChanger").style.top = ((screen.height/2)-316)+"px";
						document.getElementById("passwordChanger").style.left = ((screen.width/2)-150)+"px";
						document.getElementById("passwordChanger").style.visibility = "visible";
						
						document.getElementById("password").value = "";
						
						document.getElementById("passwordChange").value = "";
						document.getElementById("passwordChangeConfirm").value = "";
						
						document.getElementById("passwordChange").select();
					}
					else {
						document.cookie = "adminAttempts=3";
					
						document.cookie = "staffID=" + ajaxRequest.responseText;
						
						winOpen = window.open(webpage, "_blank", "scrollbars=yes,status=yes");
						
						if(!winOpen || winOpen.closed || typeof winOpen.closed=='undefined') {
							document.cookie = "setTime='' ;expires=Thu, 01-Jan-1970 00:00:01 GMT";
							alert("Pop-ups are blocked on this browser!\n\nClick the Pop-up link at the top of the page and select the 'Always Allow Pop-ups from This Site option'.");
						}
						else {									
							document.getElementById("loggedIn").innerHTML = "<br><br><span style='padding-left:105px; font-size:1.3em; font-weight:bold;'>You are now logged in</span><br><br>";
							document.getElementById("flashLoggedIn").setAttribute('className', 'showFlash');
							
							forceLogout();
							
							currdate = new Date();
							expirationdate = new Date(currdate.getTime() + 600000);
							document.cookie = "setTime=true;expires=" + expirationdate.toGMTString();
							document.cookie = "divStatus='' ;expires=Thu, 01-Jan-1970 00:00:01 GMT";	
						}
					}
				}
			}
			//window.open(webpageCheck+passedString);
			ajaxRequest.open("GET", webpageCheck+passedString);
			ajaxRequest.send(null); 
		}
	//}
	//else {
	//	alert("The system has detected that you are not using Internet Explorer.\n\nPlease log in from Internet Explorer as some of the features in the Staff Zone are dependent on it.");
	//}
}

function forceLogout() {
	if(!winOpen || winOpen.closed || typeof winOpen.closed=='undefined' && autoSignOutCheck == null) {
		document.cookie = "ascending='' ;expires=Thu, 01-Jan-1970 00:00:01 GMT";
		document.cookie = "descending='' ;expires=Thu, 01-Jan-1970 00:00:01 GMT";
		
		document.cookie = "setTime='' ;expires=Thu, 01-Jan-1970 00:00:01 GMT";	
		document.cookie = "docOpened='' ;expires=Thu, 01-Jan-1970 00:00:01 GMT";
		document.cookie = "loggedOut=true";
	}
	else {
		setTimeout("forceLogout()", 1);
	}
}

function adminLogin(webpageCheck) {
	//if (browser == "Microsoft Internet Explorer") {
		passedString = "";
		loginCounter = 0;
		loginCheck = true;
		while (loginCounter < loginArray.length) {
			loginValue = document.getElementById(loginArray[loginCounter]).value;
			if (passedString == "") {
				passedString = passedString + "?" + loginArray[loginCounter] + "=" + document.getElementById(loginArray[loginCounter]).value;
			}
			else {
				passedString = passedString + "&" + loginArray[loginCounter] + "=" + document.getElementById(loginArray[loginCounter]).value;
			}
			if (loginValue == "") {				
				loginCheck = false;			
			}
			loginCounter += 1;
		}	
		
		if (loginCheck == false) {
			alert("You have not filled in all of the required fields!");
		}
		else {	
			ajaxRequest = new XMLHttpRequest();
	
			ajaxRequest.onreadystatechange = function() {
				if(ajaxRequest.readyState == 4) {
					if (ajaxRequest.responseText == "") {
						attemptChecker = document.cookie.match ( '(^|;) ?' + 'adminAttempts' + '=([^;]*)(;|$)' );
						if (attemptChecker == null) {	
							document.cookie = "adminAttempts=3";
						}
						attemptChecker = document.cookie.match ( '(^|;) ?' + 'adminAttempts' + '=([^;]*)(;|$)' );
						attemptChecker = String(attemptChecker);
						comma = attemptChecker.lastIndexOf(",");
						semiColon = attemptChecker.lastIndexOf(";", comma);
						attemptChecker = attemptChecker.substring(semiColon, attemptChecker.length);
						attemptChecker = attemptChecker.replace(/,/g, "");
						attemptChecker = attemptChecker.replace(/;/g, "");
						
						if (attemptChecker != 0) {
							attemptChecker = attemptChecker - 1;
						}
						document.cookie = "adminAttempts=" + attemptChecker;
						if (attemptChecker == 0) {						
							alert("You have exceeded the number of allowed login attempts and are now locked out of the system for 24 hours!");						
							
							currdate = new Date();
							expirationdate = new Date(currdate.getTime() + 86400000);
							document.cookie = "adminAttempts=0;expires=" + expirationdate.toGMTString();
						}					
						else {
							if (attemptChecker != 1) {
								alert("The username/password entered do not match our records! You have " + attemptChecker + " attempts left.");	
							}
							else {
								alert("The username/password entered do not match our records! You have " + attemptChecker + " attempt left.");
							}						
						}
					}
					else {
						if (ajaxRequest.responseText.indexOf("Administrator") == -1) {
							alert("You are not an administrator!");
						}
						else {
							document.cookie = "adminAttempts=3";
							
							document.cookie = "staffID=" + ajaxRequest.responseText;
							
							currdate = new Date();
							expirationdate = new Date(currdate.getTime() + 600000);
							document.cookie = "adminSetTime=true;expires=" + expirationdate.toGMTString();
						
							winOpen = window.open(webpage, "_blank", "scrollbars=yes,status=yes");
							
							if(!winOpen || winOpen.closed || typeof winOpen.closed=='undefined') {
								document.cookie = "adminSetTime='' ;expires=Thu, 01-Jan-1970 00:00:01 GMT";
								alert("Pop-ups are blocked on this browser!\n\nClick the Pop-up link at the top of the page and select the 'Always Allow Pop-ups from This Site option'.");
							}
							else {									
								document.getElementById("adminLoggedIn").innerHTML = "<br><br><span style='padding-left:105px; font-size:1.3em; font-weight:bold;'>You are now logged in</span><br><br>";
								document.getElementById("flashLoggedIn").setAttribute('className', 'showFlash');
								
								
								document.cookie = "divStatus='' ;expires=Thu, 01-Jan-1970 00:00:01 GMT";	
							}
						}						
					}
				}
			}
			//window.open(webpageCheck+passedString);
			ajaxRequest.open("GET", webpageCheck+passedString);
			ajaxRequest.send(null); 
		}
	//}
	//else {
	//	alert("The system has detected that you are not using Internet Explorer.\n\nPlease log in from Internet Explorer as some of the features in the Staff Zone are dependent on it.");
	//}
}

function viewAccreditation(page) {
	window.open(page, "_blank", "scrollbars=yes,menubar=yes,width="+screen.width+"px,height="+(screen.height-66)+"px,top=0px,left=0px");
}

autoSignOut();
function autoSignOut() {
	autoSignOutCheck = document.cookie.match ( '(^|;) ?' + 'setTime' + '=([^;]*)(;|$)' );
	adminAutoSignOutCheck = document.cookie.match ( '(^|;) ?' + 'adminSetTime' + '=([^;]*)(;|$)' );
	loggedOutCheck = document.cookie.match ( '(^|;) ?' + 'loggedOut' + '=([^;]*)(;|$)' );
	expired = document.cookie.match ( '(^|;) ?' + 'expired' + '=([^;]*)(;|$)' );
	parentRefresh = document.cookie.match ( '(^|;) ?' + 'parentRefresh' + '=([^;]*)(;|$)' );
	
	if (window.location.toString().indexOf("staff-login.htm") != -1) {		
		if (autoSignOutCheck == null && loggedOutCheck != null && document.getElementById("loggedIn") != null) {
			document.cookie = "loggedOut='' ;expires=Thu, 01-Jan-1970 00:00:01 GMT";
			
			if (document.getElementById("loggedIn") != null) {
				document.getElementById("loggedIn").innerHTML = "<p>Please enter your login details below:<table width='386'><tr><td align='left'>Username:</td><td><input class='textfield' type='text' id='username' onFocus='focusedLogin()' onBlur='blurredLogin()' /></td></tr><tr><td align='left'>Password:</td><td><input class='textfield' type='password' id='password' onFocus='focusedLogin()' onBlur='blurredLogin()' /></td></tr><tr><td></td><td align='right'><input type='button' id='Submit' value='Login' onMouseUp=login('"+webpageCheck+"')></td></tr></table></p>";
			}
			
			document.getElementById("flashLoggedIn").setAttribute('className', 'hideFlash');
			if (expired != null) {
				document.cookie = "expired='' ;expires=Thu, 01-Jan-1970 00:00:01 GMT";
				alert("Your session has expired and have been automatically logged out!");
			}
		}	
	}
	else if (window.location.toString().indexOf("admin-login.htm") != -1) {	
	//alert(adminAutoSignOutCheck + ", " + loggedOutCheck + ", " + document.getElementById("adminLoggedIn"));
		if (adminAutoSignOutCheck == null && loggedOutCheck != null && document.getElementById("adminLoggedIn") != null) {
			document.cookie = "loggedOut='' ;expires=Thu, 01-Jan-1970 00:00:01 GMT";
			
			if (document.getElementById("adminLoggedIn") != null) {
				document.getElementById("adminLoggedIn").innerHTML = "<p>Please enter your login details below:<table width='386'><tr><td align='left'>Username:</td><td><input class='textfield' type='text' id='username' onFocus='adminFocusedLogin()' onBlur='adminBlurredLogin()' /></td></tr><tr><td align='left'>Password:</td><td><input class='textfield' type='password' id='password' onFocus='adminFocusedLogin()' onBlur='adminBlurredLogin()' /></td></tr><tr><td></td><td align='right'><input type='button' id='Submit' value='Login' onMouseUp=adminLogin('"+webpageCheck+"')></td></tr></table></p>";
			}
			
			document.getElementById("flashLoggedIn").setAttribute('className', 'hideFlash');
			if (expired != null) {
				document.cookie = "expired='' ;expires=Thu, 01-Jan-1970 00:00:01 GMT";
				alert("Your session has expired and have been automatically logged out!");
			}
		}	
	}		
	
	if (parentRefresh != null) {
		document.cookie = "parentRefresh='' ;expires=Thu, 01-Jan-1970 00:00:01 GMT";
		location.reload(true);
	}
	
	setTimeout("autoSignOut()", 1);
}

function emissions() {
	ajaxRequest = new XMLHttpRequest();
	ajaxRequest.onreadystatechange = function() {
		if(ajaxRequest.readyState == 4) {			
			document.getElementById("emissions").innerHTML = "";
			document.getElementById("emissions").innerHTML = ajaxRequest.responseText;			
		}
	}
	//window.open("emissions.asp");
	ajaxRequest.open("GET", "emissions.asp");
	ajaxRequest.send(null);
}
