// holds an instance of XMLHttpRequest
var xmlHttpLogIn = createXmlHttpRequestObject();

// when set to true, display detailed error messages
var showErrors = true;

/* variables that establish how often to access the server */
var updateInterval = 1000;

/* URL for updating Log In page */
var logInURL = "logInProcess.php";

/* checks the input values given by the user */
function checkLogIn()
{
  var testUsername = document.getElementById("username").value;
  var testPassword = document.getElementById("password").value;
if (testUsername.length >= 5 && testPassword.length >= 5) {
	logIn();
		} else {
			alert("User name and password must be at least five characters!");
			}  
}

/* makes asynchronous request to log in user */
function logIn()
{
  // save the message to a local variable and clear the text box
  var oUsername = document.getElementById("username").value;
  var oPassword = document.getElementById("password").value;
  // only continue if xmlHttpLogIn isn't void
  if(xmlHttpLogIn)
  {
    try
    {
      // don't start another server operation if such an operation is already in progress 
      if (xmlHttpLogIn.readyState == 4 || 
          xmlHttpLogIn.readyState == 0) 
      {

    paramsLogIn =  "mode=LogInUser" +
              "&username=" + encodeURIComponent(oUsername) + 
             "&password=" + encodeURIComponent(oPassword);

        // call the server page to execute the server-side operation
        xmlHttpLogIn.open("POST", logInURL, true);
        xmlHttpLogIn.setRequestHeader("Content-Type", 
                                   "application/x-www-form-urlencoded");
        xmlHttpLogIn.onreadystatechange = handleLogIn;
        xmlHttpLogIn.send(paramsLogIn);
      }
      else
      {
        // we will check again for new messages 
        setTimeout("logIn();", updateInterval);
      }
    }
    catch(e)
    {
      displayError(e.toString());
    }
  }
}

// function that handles the HTTP response
function handleLogIn() 
{
  // when readyState is 4, we read the server response
  if (xmlHttpLogIn.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttpLogIn.status == 200) 
    {
      try
      {
        // read the response from the server
        readLogIn();
      }
      catch(e)
      {
        // display error message
        displayError(e.toString());
      }
    }
    else
    {
      // display error message
      displayError(xmlHttpLogIn.statusText);
    }
  }
}

// read server's response 
function readLogIn()
{
  // retrieve the server's response 
  var response = xmlHttpLogIn.responseText;
  // server error?
  if (response.indexOf("ERRNO") >= 0 
      || response.indexOf("error:") >= 0
      || response.length == 0)
    throw(response.length == 0 ? "Server error." : response);

	//prints response from server
  	document.getElementById("logPanel_Container").innerHTML = response;
  	//returns focus to input box if failure
	if (document.getElementById("username")) {document.getElementById("username").focus();}
}