/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
    xmlHttp = false;
  }
}
@end @*/

if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  xmlHttp = new XMLHttpRequest();
}

//grab the employer login form data and send it to the php script to log in the user.
function employerlog() {
  var username=document.employerlogform.username.value;
  var password=document.employerlogform.password.value;
      var params = "username="+username+"&password="+password;
      // Build the URL to connect to
      // Open a connection to the server
      xmlHttp.open("POST", 'include/loginemployer.php', true);
      xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttp.setRequestHeader("Content-length", params.length);
      xmlHttp.setRequestHeader("Connection", "close");
      // Setup a function for the server to run when it's done
      xmlHttp.onreadystatechange = employerlog2;

      // Send the request
      xmlHttp.send(params);
}

//grab the employee login form data and send it to the php script to log in the user.
function employeelog() {
  var username=document.employeelogform.username.value;
  var password=document.employeelogform.password.value;
      var params = "username="+username+"&password="+password;
      // Build the URL to connect to
      // Open a connection to the server
      xmlHttp.open("POST", 'include/loginemployee.php', true);
      xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttp.setRequestHeader("Content-length", params.length);
      xmlHttp.setRequestHeader("Connection", "close");
      // Setup a function for the server to run when it's done
      xmlHttp.onreadystatechange = employeelog2;

      // Send the request
      xmlHttp.send(params);
}

//after calling the phpscript to log the user in update the index page to show the new login box and or messages.
function employeelog2()
{

  if (xmlHttp.readyState == 4) {
    var response = xmlHttp.responseText;
    //if login successful, reload main page.
    if(response=="success")
    {
      window.location='employees.php';
    }
      //if login failed, display error message.
    else
    {
      document.getElementById("messagebox").innerHTML ="<span style=\"color:red; font-size:1.2em; font-weight:bold; \">Incorrect User name and Password, try again...</span>";
      document.employeelogform.password.value="";
      document.employeelogform.username.value="";
    }
  }
}
function employerlog2()
{

  if (xmlHttp.readyState == 4) {
    var response = xmlHttp.responseText;
    //if login successful, reload main page.
    if(response=="success")
    {
       window.location='employers.php';
    }
      //if login failed, display error message.
    else
    {
      document.getElementById("messagebox").innerHTML ="<span style=\"color:red; font-size:1.2em; font-weight:bold;\">Incorrect User name and Password, try again...</span>";
      document.employerlogform.password.value="";
      document.employerlogform.username.value="";
    }
  }
}

function keysubmit(isemployee,myfield,e) {
  var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;

if (keycode == 13)
{
    if(isemployee==1)
    {
      employeelog();
    }
    else
    {
      employerlog();
    }
  }
}
//-->