var http;
var forgotEmailFormName = "forgot_password";

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function resendPassword() {
  try {
    if (http == null) http = GetXmlHttpObject();
    var emailAddress = window.document.forms[forgotEmailFormName].email.value;
    if (!EmailIsValid(emailAddress)) {
      alert("Please enter a valid email address.");
      window.document.forms[forgotEmailFormName].email.focus();
      return false;
    }
    http.onreadystatechange = showForgotEmailText;    
    document.getElementById('info').innerHTML="Please wait... verifying email address...";
    http.open("POST", "forgot.jsp", true);
    http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    http.send("email=" + emailAddress);
  } catch (e) {    
    alert("resendPassword error: " + e.description);
  }
  return false; // When showForgotEmail is called, the form will update it's text
}

function showForgotEmailText() {
  if ((http.responseXML != null) && 
      (http.responseXML.getElementsByTagName("responseCode")[0] != null)){
    var responseCode = http.responseXML.getElementsByTagName("responseCode")[0].firstChild.nodeValue;
    switch (responseCode) {
      case "0":
        document.getElementById('info').innerHTML="The email address you provided is not a registered email address. If you need to register, please " +
                                                  "click <a href='registration.jsp'>here</a>. If you entered the correct email address, please contact HPDI support by " +
                                                  "clicking <a href='contactUs.jsp'>here</a>.";
        break;
      case "1":
        document.getElementById('info').innerHTML="Your password was emailed to " + http.responseXML.getElementsByTagName("emailAddress")[0].firstChild.nodeValue + 
                                                  ". If you do not receive this email, please contact HPDI " +
                                                  "support by clicking <a href='contactUs.jsp'>here</a>.";
        break;
      case "2":
        document.getElementById('info').innerHTML="An error was encountered. Please contact HPDI support by clicking <a href='contactUs.jsp'>here</a>.<br><br>" +
                                                  "Error message: " + http.responseXML.getElementsByTagName("exception")[0].firstChild.nodeValue;
        break;
      case "3":
        document.getElementById('info').innerHTML="An invalid email address was entered: " + http.responseXML.getElementsByTagName("emailAddress")[0].firstChild.nodeValue;
        break;
    }
    http = null;
  }
}

function submitEnter(myfield, e) {
  var keycode;
  if (window.event) keycode = window.event.keyCode;
  else if (e) keycode = e.which;
  else return true;

  if (keycode == 13) {
      if (validEmail(window.document.forms[forgotEmailFormName].email.value)) return resendPassword();
      else return false;
  } else return true;
}

