/* ---------------------------- */
/* XMLHTTPRequest Enable        */
/* ---------------------------- */
function GetXmlHttpObject() {

 var xmlHttp=null;

try {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 } catch (e) {
 // Internet Explorer
 try {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
}
 
	return xmlHttp;

}

var http = GetXmlHttpObject();


/* -------------------------- */
/* Login Function             */
/* Returns: 1 / 0             */
/* -------------------------- */

var nocache = 0;

function login() {
 
	// Show a loading graphic to the end user
	document.getElementById('login_response').innerHTML = "<img src=\"images/loading.gif\" alt=\"Loading...\" style=\"margin: 0 0 0 55px;\" />"
	
	// Required: verify that all fileds are not empty. Use encodeURI() to solve some issues about character encoding.
	var strUsername = encodeURI(document.getElementById('strUsername').value);
	var strPassword = encodeURI(document.getElementById('strPassword').value);
	
	// Set a random number for use in the URL
	nocache = Math.random();
	
  // Pass the login variables like URL variable
  http.open('get', 'loginChecker.php?strUsername='+strUsername+'&strPassword='+strPassword+'&nocache='+nocache);

	http.onreadystatechange = loginReply;
	
	http.send(null);

}

/* ----------------------------- */
/* Login Reply Function          */
/* Returns: Error Msg / Redirect */
/* ----------------------------- */

function loginReply() {
	
	if(http.readyState == 4) {
	
	 var response = http.responseText;
		
	 if(response == 1) {
		
		 // Redirect user to login page. NB: All pages check for valid authentication
		 window.location = "/clients/";
		
		} else {
			
			// Clear the loading graphic
 		document.getElementById('login_response').innerHTML = "";
			
		 // Login failed. Display error message
	 	document.getElementById('login_response').innerHTML = 'Invalid login credentials. Please try again.';
		document.getElementById('strPassword').value = '';
		
		}
	}
}