/*
 * Login form
 */

function ihrLogin(sendForm){
  regBadIDInput = /[^\w@_\.-]/;
  regBadIDInput2 = /[\.]{2,}/;
  regBadPDInput = /[^\w@_-]/;
  //removed.... regBadSQLInput = /[-]{2,}/;
  var nm = document.toIHR.name.value;
  var pd = document.toIHR.pswd.value;

  while ((nm.substring(0,1) == ' ') || (nm.substring(0,1) == '\n') || (nm.substring(0,1) == '\r')){
    nm = nm.substring(1,nm.length);
  }

  while ((nm.substring(nm.length-1,nm.length) == ' ') || (nm.substring(nm.length-1,nm.length) == '\n') || (nm.substring(nm.length-1,nm.length) == '\r')){
    nm = nm.substring(0,nm.length-1);
  }


  while ((pd.substring(0,1) == ' ') || (pd.substring(0,1) == '\n') || (pd.substring(0,1) == '\r')){
    pd = pd.substring(1,pd.length);
  }

  while ((pd.substring(pd.length-1,pd.length) == ' ') || (pd.substring(pd.length-1,pd.length) == '\n') || (pd.substring(pd.length-1,pd.length) == '\r')){
    pd = pd.substring(0,pd.length-1);
  }


  if (nm.length == 0){
    document.toIHR.name.focus();
    alert("A User ID is required. Please enter one and try again.");
    return false;
  }

  else if ((regBadIDInput.test(nm))||(regBadIDInput2.test(nm))){
    document.toIHR.name.focus();
    alert("Please provide a valid User ID.");
    return false;
  }


  else if (pd.length == 0){
    document.toIHR.pswd.focus();
    alert("A password is required. Please enter one and try again.");
    return false;

  }
  else if (regBadPDInput.test(pd)){
    document.toIHR.pswd.focus();
    alert("Please provide a valid password.");
    return false;
  }
  
  document.toIHR.fUrl.value="http://www.medfusion.net/ihealthrecord/index.html?error=true";

/*  if (window.location.toString().indexOf("googlehealth") >= 0) {
  document.toIHR.home.value="googlehealth";
  document.toIHR.fUrl.value="http://www.ihealthrecord.org/googlehealth/index.php?error=true";
  }
*/
  document.toIHR.loginName.value=nm;
  document.toIHR.password.value=pd;
  document.toIHR.siteID.value=document.toIHR.sID.value;
  document.toIHR.failureUrl.value=document.toIHR.fUrl.value;
  document.toIHR.odlp.value=true;
  document.toIHR.noWarning.value=true;
  document.toIHR.osp.value=true;
  if (sendForm == true){
    setTimeout("document.toIHR.submit();", 10);
  }

  return true;
}
