var eTypeField=null, pinNumField=null;

function parseLogin() {
  if(isNull(eTypeField) || isNull(pinNumField)) return "Error on page";
  var entType="";
  var entNum="";
  var entDup="";
  var unparsedID="";
  var pinNum="";
  var i;

  // Don't like to deal with null values, so make sure any nulls are converted to empty string . . .
  unparsedID=setEmpty(trim(eTypeField.value));
  pinNum=setEmpty(trim(pinNumField.value));
  var idLen=unparsedID.length;

  /* First - do basic checking for empty fields or fields that are obviously too short */
  if(isEmpty(unparsedID)) {
    focusTo=eTypeField;
    return "Enter An Entity Type And Number";
  }
  if(idLen==1) {
    // User hit button without entering a complete entity value
    focusTo=eTypeField;
    return "Invalid Entity Value";
  }
  // Check for entity type (single alpha char)
  entType=unparsedID.charAt(0);
  if(isNull(entType.match(/[a-zA-Z]/))) {
    focusTo=eTypeField;
    return "Missing Entity Type";
  } else {
    // Check for an alpha char at the end (entDup)
    if(idLen>=2) {
      entDup=unparsedID.charAt(idLen-1);
      if(isNull(entDup.match(/[a-zA-Z]/))) {
        entDup="";
        entNum=trim(unparsedID.substring(1));
      } else entNum=trim(unparsedID.substring(1, idLen - 1));
    }
    if(isEmpty(entNum)) {
      focusTo=eTypeField;
      return "Invalid Entity Number";
    }
    // Remove any non-numeric chars (such as space or dash) within the entity number . . .
    var temp="";
    for(i=0; i<entNum.length; i++) {
      if(notNull(entNum.charAt(i).match(/[0-9]/))) temp+=entNum.charAt(i);
    }
    entNum=temp;
    // Verify that the entity number is indeed numeric . . .
    for(i=0; i<entNum.length; i++) {
      if(isNaN(entNum)) {
        // Numerical part of the string is invalid
        focusTo=eTypeField;
        return "Not A Valid Entity Number";
      } // end if
    } // end for
  }
  // Validate the pin number . . .
  if(isEmpty(pinNum)) {
    // User hit button without entering a pin number
    focusTo=pinNumField;
    return "Enter Your Pin Number";
  }
  if(pinNum.length<12) {
    // Pin number must be 12 chars in length . . .
    focusTo=pinNumField;
    return "The Pin Number Must Be 12 Digits Long";
  }
  for(i=0; i<pinNum.length; i++) {
    // Pin number must be numeric . . .
    if(isNull(pinNum.charAt(i).match(/[0-9]/))) {
      focusTo=pinNumField;
      return "The Pin Must Be A Numeric Value";
    }
  }
  // If we get here, it is ok to proceed
  var frmLogin=$("frmLogin");
  frmLogin.entityType.value=entType;
  frmLogin.entityNum.value=entNum;
  frmLogin.entityDup.value=entDup;
  frmLogin.pinNumber.value=pinNum;
  return "";
}

function openHelpWindow(path) {
  var helpWin=window.open(path, 'helpWin', 'scrollbars,resizable,width=750,height=600');
  helpWin.focus();
}

function buttonClicked(btn) {
  var btnName=btn.name;
  if(btnName=="b_login") {
    var errMsg=parseLogin();
    if(isEmpty(errMsg)) {
      var frmLogin=$("frmLogin");
      frmLogin.action="verifyLogin.jsp";
      frmLogin.submit();
    } else {
      buildAlert(errMsg);
      return;
    }
  } else if(btnName=="b_exit") location.href="/";
}
