<!--
//   Programmer: Chris Jaehnen
//       Script: JavaScript Form Validation Library v1.1
// Date Created: 5/12/2003
//     Modified: 7/30/2003 - Added credit card number validation
//   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] == "Program" && isSelected(frmForm, "Program") == false) {
     strErrorFields += "Program" + ',';
     boolValidFields = false;
    }
    else if (strRequiredFieldsListNames[i] == "Location" && isSelected(frmForm, "Location") == false) {
     strErrorFields += "Location" + ',';
     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 (strRequiredFieldsListNames.indexOf("Email") != -1 && 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("HomePhone") != -1 && isPhoneNumber(frmForm, "HomePhone") == 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.HomePhone.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 isSelected(strFormName, strFieldName) {
  // declarations
  var frmForm = strFormName;
  var strState = eval('frmForm.' + strFieldName + '.options[frmForm.' + strFieldName + '.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, strFieldName) {
  // declarations
  var frmForm = strFormName;
  var strPhoneNumber = eval('frmForm.' + strFieldName + '.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 isDate(strFormName) {
  // declarations
  var frmForm = strFormName;
  var strDate = frmForm.ExpDate.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 isCreditCard(strFormName) {
  // declarations
  var frmForm = strFormName;
  var strCreditCardType = frmForm.Credit_Card.options[frmForm.Credit_Card.selectedIndex].value;
  var strCreditCardNumber = frmForm.CreditCardNumber.value;

  if (strCreditCardType == "Visa") {
    // Visa: length 16, prefix 4, dashes optional.
    var strFilter = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
  } else if (strCreditCardType == "MasterCard") {
    // Mastercard: length 16, prefix 51-55, dashes optional.
    var strFilter = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
  } else if (strCreditCardType == "Disc") {
    // Discover: length 16, prefix 6011, dashes optional.
    var strFilter = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
  } else if (strCreditCardType == "AmEx") {
    // American Express: length 15, prefix 34 or 37.
    var strFilter = /^3[4,7]\d{13}$/;
  } else if (strCreditCardType == "Diners") {
    // Diners: length 14, prefix 30, 36, or 38.
    var strFilter = /^3[0,6,8]\d{12}$/;
  }

  // test RegEx for credit card number
  if (! strFilter.test(strCreditCardNumber))
    return false;
  else
    return true;
}

//-->