<!--
//   Programmer: Chris Jaehnen
//       Script: JavaScript Form Validation Library v1.0
// Date Created: 5/12/2003
//   Disclaimer: This code may be used as long as this header remains in the code.
// Developed At: ISOC, http://www.isoc.net/

function validateFormFields(strFormName) {
  // declarations
  var frmForm = strFormName;
  var strErrorFields = "";
  var strErrorMsg = "";
  var strRequiredFieldsListValues = "";
  var strRequiredFieldsListNames = frmForm.Required.value;
  var boolValidFields = true;

  // split required field list names at the comma into an array
  strRequiredFieldsListNames = strRequiredFieldsListNames.split(",")

  // iterate through required field names array to build a value string from the form
  for (var x = 0; x < strRequiredFieldsListNames.length; x++) {
    strRequiredFieldsListValues += eval('frmForm.' + strRequiredFieldsListNames[x] + '.value') + ',';
  }

  // split values into an array at the comma
  strRequiredFieldsListValues = strRequiredFieldsListValues.split(",")

  // iterate through array to check to see if data is valid
  for (var i = 0; i < strRequiredFieldsListValues.length - 1; i++) {
    if (strRequiredFieldsListValues[i] == "") {
     strErrorFields += strRequiredFieldsListNames[i] + ',';
     boolValidFields = false; }
    // check for invalid state data
    else if (strRequiredFieldsListNames[i] == "State" && isState(frmForm) == false) {
     strErrorFields += "State" + ',';
     boolValidFields = false;
    }
  }

  // split error messages into an array at the comma
  strErrorFields = strErrorFields.split(",")

  strRequiredFieldsListNames = frmForm.Required.value;

  // if an error occurred, display an error message box
  if (boolValidFields == false) {
    strErrorMsg = "An error occurred in the following required fields:\n\n";
    for (var y = 0; y < strErrorFields.length - 1; y++ ) {
      strErrorMsg += "* " + strErrorFields[y] + " is blank\n"; 
    }
    window.alert(strErrorMsg);
    eval('frmForm.' + strErrorFields[0] + '.focus()');
    return false;
  }
  // validate email address
  else if (isEmail(frmForm) == false) {
    strErrorMsg = "The following error occurred in the email field:\n\n";
    strErrorMsg += "Your email address is not valid.\nShould be in the format Ex. username@domainname.com"; 
    window.alert(strErrorMsg); 
    frmForm.Email.focus();
    return false;
  }
  // validate the zip code
  else if (strRequiredFieldsListNames.indexOf("Zip") != -1 && isZip(frmForm) == false) {
    strErrorMsg = "The following error occurred in the zip code field:\n\n";
    strErrorMsg += "Your zip code is not valid.\nShould be in the format 12345 or 12345-6789"; 
    window.alert(strErrorMsg); 
    frmForm.Zip.focus();
    return false;
  }
  // validate the phone number
  else if (strRequiredFieldsListNames.indexOf("Phone") != -1 && isPhoneNumber(frmForm) == false) {
    strErrorMsg = "The following error occurred in the phone number field:\n\n";
    strErrorMsg += "Your phone number is not valid.\nShould be in the format (999) 999-9999 or (999)999-9999"; 
    window.alert(strErrorMsg); 
    frmForm.Phone.focus();
    return false;
  }
  // validate the fax number
  else if (strRequiredFieldsListNames.indexOf("Fax") != -1 && isFaxNumber(frmForm) == false) {
    strErrorMsg = "The following error occurred in the fax number field:\n\n";
    strErrorMsg += "Your fax number is not valid.\nShould be in the format (999) 999-9999 or (999)999-9999"; 
    window.alert(strErrorMsg); 
    frmForm.Fax.focus();
    return false;
  }
  // validate the date
  else if (strRequiredFieldsListNames.indexOf("CurDate") != -1 && isDate(frmForm) == false) {
    strErrorMsg = "The following error occurred in the date field:\n\n";
    strErrorMsg += "Your date is not valid.\nShould be in the format mm/dd/yyyy"; 
    window.alert(strErrorMsg); 
    frmForm.CurDate.focus();
    return false;
  }
  // validate the SSN
  else if (strRequiredFieldsListNames.indexOf("SSN") != -1 && isSSN(frmForm) == false) {
    strErrorMsg = "The following error occurred in the social security number field:\n\n";
    strErrorMsg += "Your social security number is not valid.\nShould be in the format 999-99-9999 or 999999999"; 
    window.alert(strErrorMsg); 
    frmForm.SSN.focus();
    return false;
  }
  // validate the time
  else if (strRequiredFieldsListNames.indexOf("Time") != -1 && isTime(frmForm) == false) {
    strErrorMsg = "The following error occurred in the time field:\n\n";
    strErrorMsg += "Your time is not valid.\nShould be in the format HH:MM or HH:MM:SS or HH:MM:SS.mmm"; 
    window.alert(strErrorMsg); 
    frmForm.Time.focus();
    return false;
  }
  // validate the IP address
  else if (strRequiredFieldsListNames.indexOf("IPAddress") != -1 && isIPAddress(frmForm) == false) {
    strErrorMsg = "The following error occurred in the IP address field:\n\n";
    strErrorMsg += "Your time is not valid.\nShould be in the format 999.999.999.999"; 
    window.alert(strErrorMsg); 
    frmForm.IPAddress.focus();
    return false;
  }
  // otherwise, all values are valid, return true
  else
    return true;
}
function isEmail(strFormName) {
  // declarations
  var frmForm =  strFormName;
  var strEmail = frmForm.Email.value;
  var strFilter = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;

  // test the RegEx for a valid email address
  if (! strFilter.test(strEmail))
    return false;
  else
    return true;
}
function isState(strFormName) {
  // declarations
  var frmForm = strFormName;
  var strState = frmForm.State.options[frmForm.State.selectedIndex].value;

  if (strState == "")
    return false;
  else
    return true;
}
function isZip(strFormName) {
  // declarations
  var frmForm = strFormName;
  var intZip = frmForm.Zip.value;
  var strFilter = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

  // test RegEx for valid zip code
  if (! strFilter.test(intZip))
    return false;
  else
    return true;
  }
function isPhoneNumber(strFormName) {
  // declarations
  var frmForm = strFormName;
  var strPhoneNumber = frmForm.Phone.value;
  var strFilter = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;

  // test RegEx for valid phone number
  if (! strFilter.test(strPhoneNumber))
    return false;
  else
    return true;
}
function isFaxNumber(strFormName) {
  // declarations
  var frmForm = strFormName;
  var strFaxNumber = frmForm.Fax.value;
  var strFilter = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;

  // test RegEx for valid phone number
  if (! strFilter.test(strFaxNumber))
    return false;
  else
    return true;
}
function isDate(strFormName) {
  // declarations
  var frmForm = strFormName;
  var strDate = frmForm.CurDate.value;
  var strFilter = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;

  // test RegEx for valid date
  if (! strFilter.test(strDate))
    return false;
  else
    return true;
}
function isSSN(strFormName) {
  // declarations
  var frmForm = strFormName;
  var strSSN = frmForm.SSN.value;
  var strFilter = /^\d{3}\-?\d{2}\-?\d{4}$/;

  // test RegEx for valid social security number
  if (! strFilter.test(strSSN))
    return false;
  else
    return true;
}
function isTime(strFormName) {
  // declarations
  var frmForm = strFormName;
  var strTime = frmForm.Time.value;
  var strFilter = /^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$/;

  // test RegEx for valid time
  if (! strFilter.test(strTime))
    return false;
  else
    return true;
}
function isIPAddress(strFormName) {
  // declarations
  var frmForm = strFormName;
  var strIPAddress = frmForm.IPAddress.value;
  var strFilter = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;

  // test RegEx for valid time
  if (! strFilter.test(strIPAddress))
    return false;
  else
    return true;
}
//-->
