var xmlhttp;

function clearLogin() {
  document.getElementById("username").value = '';
  document.getElementById("password").value = '';
}

function doLogin() {
  var username = document.getElementById("username");
  if (username.value == '') {
    alert('Please Enter Username');
    username.focus();
    username.className = 'error';
    return false;
  }
  var password = document.getElementById("password");
  if (password.value == '') {
    alert('Please Enter Password');
    password.focus();
    password.className = 'error';
    return false;
  }
  var pass = hex_md5(password.value);
  document.getElementById("div-fields").style.display = 'none';
  document.getElementById("div-status").style.display = 'block';
  if (window.XMLHttpRequest) {
    xmlhttp=new XMLHttpRequest()
    xmlhttp.onreadystatechange=state_Change
    xmlhttp.open("GET","doLogin.php?username="+username.value+"&password=" +pass,true)
    xmlhttp.send(null)
  } else if (window.ActiveXObject) {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    if (xmlhttp) {
      xmlhttp.onreadystatechange=state_Change
      xmlhttp.open("GET","doLogin.php?username="+username.value+"&password=" +pass,true)
      xmlhttp.send()
    }
  }
}

function state_Change() {
  if (xmlhttp.readyState==4) {
    if (xmlhttp.status==200) {
      if (xmlhttp.responseText == 'Login Okay') {
        document.getElementById("div-status").innerHTML = 'Login Successful';
        location.href = returnURL;
      } else {
        var password = document.getElementById("password");
        var username = document.getElementById("username");
        document.getElementById("div-status").style.display = 'none';
        document.getElementById("div-fields").style.display = 'block';
        password.value = '';
        username.value = '';
        alert("Invalid username or password!");
//alert (xmlhttp.responseText);
        password.className = 'error';
        username.focus();
        username.className = 'error';
      }
    } else {
      alert("Data Connection Error! "+xmlhttp.status);
    }
  }
}