//Create xmlHttp Object via external file
ajaxobject();

// make asynchronous HTTP request using the XMLHttpRequest object
function Login(){
	// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		// retrieve the name typed by the user on the form
		//name = encodeURIComponent(document.getElementById("action").value);
		var UserName = document.getElementById('LogUserName').value;
		var Pass = document.getElementById('LogPass').value;
		var sender = 'LogUserName=' + UserName + '&LogPass=' + Pass + '&LogOrReg=1';
		// execute the form.php page from the server
		xmlHttp.open("POST", "logreg.php", true);
		// Defines type of data sent via POST method
		xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		// define the method to handle server responses
		xmlHttp.onreadystatechange = handleServerResponse;
		// make the server request
		xmlHttp.send(sender);
		document.getElementById('LoginButton').value="Loading...";
	}
	else{
	// if the connection is busy, try again after one second
	setTimeout('Login()', 1000);
	}
	
	// executed automatically when a message is received from the server
	function handleServerResponse(){
		// move forward only if the transaction has completed
		if (xmlHttp.readyState == 4){
			// status of 200 indicates the transaction completed successfully
			if (xmlHttp.status == 200){
				//Get XML response from server
				xmlResponse = xmlHttp.responseXML;
				// Get the document element (the root element) of the XML structure
				xmlDocumentElement = xmlResponse.documentElement;
				// Get the data fromt he desired element
				Success = xmlDocumentElement.getElementsByTagName("loginsuccess")[0].childNodes[0].nodeValue;
				UNerror = xmlDocumentElement.getElementsByTagName("loginusernameerror")[0].childNodes[0].nodeValue;
				PWerror = xmlDocumentElement.getElementsByTagName("loginpassworderror")[0].childNodes[0].nodeValue;
				EMerror = xmlDocumentElement.getElementsByTagName("loginemailvaliderror")[0].childNodes[0].nodeValue;
				
				// If login is successful
				if(Success == 1){
					var MemberLinks='<div style="position:absolute;top:0px;width:120px;text-align:center;text-decoration:none;"><a style="cursor:pointer;" onclick="Members()">Members Section</a></div><div style="position:absolute;top:20px;width:120px;text-align:center;"><a style="cursor:pointer;" onclick="Logout()">Logout</a></div>';
					// Update the client display using the data received from the server
					document.getElementById('LogReg').innerHTML=MemberLinks;
					document.getElementById('UserStorage').innerHTML=UserName;
					document.getElementById('LogReg').style.height="45px";
					// Open members section and display in main content area
					Members();
					// Close login screen
					LogReg(0);
				}
				else {
					//Create error report
					document.getElementById('LoginButton').value="Login";
					if(UNerror==1){
						document.getElementById('UNCheck').innerHTML="Invalid";
						}
					else {
						document.getElementById('UNCheck').innerHTML="";
						}
					if(PWerror==1){
						document.getElementById('PWCheck').innerHTML="Invalid";
						}
					else {
						document.getElementById('PWCheck').innerHTML="";
						}
				}
			}
			// a HTTP status different than 200 signals an error
			else{
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
			}
		}
	}
}
