function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
	  try {
	  req = new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (err2) {
	    try {
	    req = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (err3) {
	      req = false;
	    } 
	  } 
}
return req;
}

var http = getXMLHTTPRequest();

function closeWindow(windowID) { 
	document.getElementById(windowID).style.display='none';
}

function retrieveLogin(logCookie) {
	var username = document.login.username.value;
	if(logCookie!='n/a') {
		http.open("GET", "/includes/updates/getPwd.php?u="+username+"&p="+logCookie, true);
		http.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		http.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
		http.setRequestHeader("Pragma", "no-cache"); 
		http.onreadystatechange = function () {
			if (http.readyState == 4) {
				if(http.status == 200) { 
				    var str = http.responseText;
					str = str.split('*');
					// document.getElementById("loginFeedback").innerHTML=str[0];
					document.login.password.value = str[1];
				}
			} 
		}
		http.send(null);
	}
}

function loginAttempt(page) {

  var username = document.getElementsByName("username");
  var password = document.getElementsByName("password");
  
  var loginResultsHTML = document.getElementById("loginResults").innerHTML;
  loginResultsHTML = loginResultsHTML.split('<!-- error -->');
  loginResultsHTML = loginResultsHTML[1];
  
  document.getElementById("loginButtonWrapper").innerHTML = '<img src="/images/indicator.gif">';
  username=username[0].value;
  password=password[0].value;
  http.open("GET", "/includes/updates/logIn.php?username="+username+"&password="+password+"&page="+page, true);
  http.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
  http.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
  http.setRequestHeader("Pragma", "no-cache"); 
  http.onreadystatechange = function () {
	 if (http.readyState == 4) {
	   if(http.status == 200) { 
		  if(http.responseText=='failed') {
			document.getElementById("loginResults").innerHTML = 'This isn&rsquo;t a valid username and/or password &ndash; please try again.<!-- error -->'+loginResultsHTML;
			document.getElementById("loginButtonWrapper").innerHTML = '<a href="javascript:;" onClick="loginAttempt();"><img name="loginSubmit" src="/images/buttons_silver/submit.gif" border="0" id="loginSubmit" alt="Submit" /></a>';
		  } else {
			document.getElementById("loginResults").innerHTML = http.responseText;
		  }
	   }
	 } 
  }
  http.send(null);
}

function keyPressed(TB,evt,url) {
  var thisKey = "-";
  if(evt) {
	 thisKey = evt.which;
  } else {
	thisKey=window.event.keyCode;
  }
  if(thisKey==13) {
     loginAttempt(url);
  }
}

function adminLogin(self,logCookie) {
  document.getElementById("login").innerHTML = '<div id="loginWindow" style="position: relative; padding: 8px 8px 8px 20px; margin-top: -120px; margin-left: -40px; width: 300px; background: #f2efdc; border: 1px solid #5a5a5a;"><div style="float: right; font-size: 10px;"><a href="javascript:;" onClick="closeWindow(\'loginWindow\');">Close</a></div><form name="login" action="javascript:keyPressed(this.id,event,\''+self+'\');"><h4 style="margin: 13px 0 5px 0;">Administrative Login</h4><div id="loginFeedback"></div><div id="loginResults" style="margin: 2px 0 12px 0;"><!-- error --><p style="margin: 8px 0 3px 0; padding: 0;">Username</p><input style="width: 227px; margin: 0;" type="text" name="username" value=""><p style="margin: 8px 0 3px 0; padding: 0;">Password</p><input type="password" style="margin: 0; width: 227px;" onFocus="retrieveLogin(\''+logCookie+'\');" onkeydown="keyPressed(this.id,event,\''+self+'\');" name="password" value=""><br /><div id="loginButtonWrapper" style="margin: 15px 0 2px 0;"><a href="javascript:;" onClick="loginAttempt(\''+self+'\');"><img name="loginSubmit" src="/images/buttons_silver/submit.gif" border="0" id="loginSubmit" alt="Submit" /></a></div></div></form></div>';
}
