var loginArray = [];

var ajaxRequest;

var passedString, loginValue, loginCheck, loginArrayCounter, attemptChecker, comma, semiColon, currdate, expirationdate, autoSignOutCheck, adminAutoSignOutCheck, loggedOutCheck, expired, focused, webpage, webpageCheck, winOpen;

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 == "") {// || (ajaxRequest.responseText.indexOf("IPAddress") != -1)) {
						/*if (ajaxRequest.responseText.indexOf("IPAddress") != -1) {
							alert("You have exceeded the number of allowed login attempts and are now locked out of the system for 24 hours!");	
						}
						else {*/
							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("externalMissing") != -1) {
						
						registryHelp();
											
						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 {
							alert("The system has detected that this is the first time logging in from this computer.  A program will be downloaded to allow the user full use of the file manager.\n\nPlease click Open/Run whenever required to complete the required modifications!");
							
							document.cookie = "fileDownload=true";
							document.cookie = "staffID=" + ajaxRequest.responseText;
														
							window.location = "Office2007Fix.exe";						
							
							winOpen.focus();
						}
					}*/
					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 == "") {// || (ajaxRequest.responseText.indexOf("IPAddress") != -1)) {
						/*if (ajaxRequest.responseText.indexOf("IPAddress") != -1) {
							alert("You have exceeded the number of allowed login attempts and are now locked out of the system for 24 hours!");	
						}
						else {*/
							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("Administrator") == -1) {
							alert("You are not an administrator!");
						}
						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("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');
								
								currdate = new Date();
								expirationdate = new Date(currdate.getTime() + 600000);
								document.cookie = "adminSetTime=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.");
	}
}

autoSignOut();
function autoSignOut() {
	autoSignOutCheck = document.cookie.match ( '(^|;) ?' + 'setTime' + '=([^;]*)(;|$)' );
	adminAutoSignOutCheck = document.cookie.match ( '(^|;) ?' + 'adminSetTime' + '=([^;]*)(;|$)' );
	loggedOutCheck = document.cookie.match ( '(^|;) ?' + 'loggedOut' + '=([^;]*)(;|$)' );
	expired = document.cookie.match ( '(^|;) ?' + 'expired' + '=([^;]*)(;|$)' );
	
	if (autoSignOutCheck == null && adminAutoSignOutCheck == null && loggedOutCheck != null && (document.getElementById("loggedIn") != null || document.getElementById("adminLoggedIn") != 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!");
		}
	}
	setTimeout("autoSignOut()", 1);
}