<!--
//   Programmer: Chris Jaehnen
//       Script: JavaScript Form Validation Library v1.3
// Date Created: 5/12/2003
//     Modified: 8/4/2003 - Code optimization
//   Disclaimer: This code may be used as long as this header remains in the code.
// Developed At: ISOC, http://www.isoc.net/
//         Note: Only edit code between the /* BEGIN CUSTOM ERROR MSGS */ and /* END CUSTOM ERROR MSGS */ code block.

function validateFormFields(strFormName) {
  // declarations
  var frmForm = strFormName;
  var strErrorFields = "";
  var strErrorMsg = "";
  var strRequiredFieldsListValues = "";
  var strRequiredFieldsListNames = frmForm.Required.value;
  var blnValidFields = 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] + ',';
      blnValidFields = false;
    }
    // check for invalid state data
    else if (strRequiredFieldsListNames[i] == "State" && isSelected(frmForm, "State") == false) {
      strErrorFields += "State" + ',';
      blnValidFields = false;
    }
  }

  // split error messages into an array at the comma
  strErrorFields = strErrorFields.split(",")

  // reset the required fields list
  strRequiredFieldsListNames = frmForm.Required.value;

  // if an error occurred, display an error message box
  if (blnValidFields == 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;
  }
  /* BEGIN CUSTOM ERROR MSGS */

  // validate email address
  else if (strRequiredFieldsListNames.indexOf("Email") != -1 && isEmail(frmForm, "Email") == false) {
    strErrorMsg = "The following error occurred in the Email Address 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 state
  else if (strRequiredFieldsListNames.indexOf("State") != -1 && isSelected(frmForm, "State") == false) {
    strErrorMsg = "The following error occurred in the State field:\n\n";
    strErrorMsg += "You must select a state."; 
    window.alert(strErrorMsg); 
    frmForm.State.focus();
    return false;
  }
  // validate the zip code
  else if (strRequiredFieldsListNames.indexOf("Zip") != -1 && isZip(frmForm, "Zip") == 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("Telephone") != -1 && isPhoneNumber(frmForm, "Telephone") == false) {
    strErrorMsg = "The following error occurred in the Telephone Number field:\n\n";
    strErrorMsg += "Your telephone number is not valid.\nShould be in the format (999) 999-9999 or (999)999-9999"; 
    window.alert(strErrorMsg); 
    frmForm.Telephone.focus();
    return false;
  }
  // validate the fax number
  else if (strRequiredFieldsListNames.indexOf("Fax") != -1 && isFaxNumber(frmForm, "Fax") == 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, "CurDate") == 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, "SSN") == 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, "Time") == 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, "IPAddress") == 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;
  }
  /* END CUSTOM ERROR MSGS */

  // otherwise, all values are valid, return true
  else
    return true;
}
function isEmail(strFormName, strFieldName) {
  var strEmail = eval('strFormName.' + strFieldName + '.value');
  var strFilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/i;
  return strFilter.test(strEmail);
}
function isSelected(strFormName, strFieldName) {
  var strSelected = eval('strFormName.' + strFieldName + '.options[strFormName.' + strFieldName + '.selectedIndex].value');

  if (strSelected == "")
    return false;
  else
    return true;
}
function isZip(strFormName, strFieldName) {
  var intZip = eval('strFormName.' + strFieldName + '.value');
  var strFilter = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
  return strFilter.test(intZip);
}
function isPhoneNumber(strFormName, strFieldName) {
  var strPhoneNumber = eval('strFormName.' + strFieldName + '.value');
  var strFilter = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
  return strFilter.test(strPhoneNumber);
}
function isFaxNumber(strFormName, strFieldName) {
  var strFaxNumber = eval('strFormName.' + strFieldName + '.value');
  var strFilter = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
  return strFilter.test(strFaxNumber);
}
function isDate(strFormName, strFieldName) {
  var strDate = eval('strFormName.' + strFieldName + '.value');
  var strFilter = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
  return strFilter.test(strDate);
}
function isSSN(strFormName, strFieldName) {
  var strSSN = eval('strFormName.' + strFieldName + '.value');
  var strFilter = /^\d{3}\-?\d{2}\-?\d{4}$/;
  return strFilter.test(strSSN);
}
function isTime(strFormName, strFieldName) {
  var strTime = eval('strFormName.' + strFieldName + '.value');
  var strFilter = /^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$/;
  return strFilter.test(strTime);
}
function isIPAddress(strFormName, strFieldName) {
  var strIPAddress = eval('strFormName.' + strFieldName + '.value');
  var strFilter = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
  return strFilter.test(strIPAddress);
}
//-->