﻿function handleLoginResponse() {
	if (http.readyState == 4) {
		var strResponse = http.responseText;
		if (strResponse == "ok") {
      if (document.all && window.external) {
        try { // Sorting a problem with one user's IE7 installation
          window.external.AutoCompleteSaveForm(frmLogin);
        }
        catch (err) {
        }
      }
		  document.getElementById("frmLogin").submit();
		}
		else if (strResponse.substr(0, 11) == "newaccount=") {
		  window.location = "dea_login.asp?ac=" + strResponse.substr(11);
		}
		else
		{
		  if (strResponse == "nosuchemail")
		  {
		    alert("Sorry, we couldn't find that email address.\r\n\r\nNeed to set up a new login? Click OK, then click 'Create new login'.");
		  }
		  else if (strResponse == "wrongpassword")
		  {
		    alert("Incorrect password. Please try again.");
		  }
		  else
		  {
		    alert("Login failed. Please try again.");
		  }
      document.getElementById("btnLogin").disabled = false;
      document.getElementById("btnLogin").value = "Login";
		}
	}
}

function jsOnClickLogin()
{
  var objUserName = document.getElementById("txtEmail");
  objUserName.value = alltrim(objUserName.value);
  
  var objPassword = document.getElementById("txtPassword");
  objPassword.value = alltrim(objPassword.value);
  
  if (objUserName.value.length < 8)
    alert("User name must be at least 8 characters (it's usually your email address).");
  else {
    // Check if the username/password combination matches the one in our db.
    document.getElementById("btnLogin").disabled = true;
    document.getElementById("btnLogin").value = "Checking...";
    
    var dtNow = new Date();
	  var nTicks = dtNow.getTime();
  	
	  http.open("POST", strPublisherSite + "/logincheck.asp?t=" + nTicks, true);
	  http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	  http.onreadystatechange = handleLoginResponse;
  	
	  var strSend = "x=y&u=" + encodeURIComponent(objUserName.value) + "&h=" + Hash32(objUserName.value, objPassword.value, nTicks.toString());
	  http.send(strSend); 
  }
}

function jsOnKey(e) 
{
  if ((e.which ? e.which : e.keyCode) == "13")
    jsOnClickLogin();
}
