// JavaScript Document
var http = getXMLHTTPRequest();
var LogInMess = 0;
var myRand = parseInt(Math.random()*999999999999999);

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;
}

function LogInChecker() {
	if (document.formLogin.username.value == "" && document.formLogin.password.value == "") {
		alert("Please enter your\nusername and password.");
		document.formLogin.username.focus();
		return false;}
	else {try {NameUser = '&param1=' + document.formLogin.username.value ; // field username
				NamePass = '&param2='+ document.formLogin.password.value;  // field password
				myUrl = "main/script/LogInRequest.php?" + NameUser + NamePass;
				//myRand = parseInt(Math.random()*999999999999999);
				modUrl = myUrl + '&rand=' + myRand;
				http.open("GET",modUrl,true);
				http.onreadystatechange = LogInRequestChecker;
				http.send(null);}
			catch(err1) {alert('Error login\n'+err1);
						LogInMess = 0;}}
	if (LogInMess == 0) {
		return false;}
}

function LogInRequestChecker() {
	if (http.readyState == 4) {
		if (http.status == 200) {
			try {
				NodeValue = http.responseText;
				switch (parseInt(NodeValue)) {
					case 0:
						alert("Invalid username or password.\nPlease try again.");
						document.formLogin.username.focus();
						LogInMess = 0;
						break;
					case 1:
						//window.location.href='network_listing.php?reload=Yes&page=0&option=0'+'&rand='+myRand;
						window.location.href="main/vo/main.php?reload=Yes";
						//document.sLogIn.submit();
						break;
					case 2:
						window.location.href='main/admin/main.php?&log='+document.formLogin.username.value+'&rand='+myRand;
						//document.sLogIn.submit();
						break;
					case 3:
						//alert("User currently blocked...");
						//document.sLogIn.username.focus();
						//LogInMess = 0;
						break;
				}
				
			} catch(err1) {alert('Error login encountered\n' + err1);return false;}
		} 
	} else {return false;}
}
