
/*

Name: validate_form.js
Author: pperez
Description:

validate_form.js is a library of HTML form validation functions. To use, edit the <form> tag to include the form name "webForm" and an onSubmit event trigger like this:

<form name="webForm" method="POST" action="myform.php" onSubmit="return validate_form();">

Then create an array (don't forget to put this between javascript tags) of form fields to be validated and the functions to validate them like this:

fieldList['name'] = 'jsval_text("name", "Please enter your name.")';
fieldList['zip']  = 'jsval_select("zip", "Please pick a zip code")';

Note, that each array item needs a unique name. Naming the array item after the form field to be validated works well, but be sure to add 2, 3, 4, etc. if multiple validations are to be performed:

fieldList['email']  = 'jsval_text("email", "An email address is required.")';
fieldList['email2'] = 'jsval_email("email")';
fieldList['email3'] = 'jsval_text_repeat("email", "repeat_email", "The repeated email address does not match.")'; 

All validations will be performed in the order that they are entered into the array. The form will not be submitted until all validations are passed. Be sure to test at least the last form field to ensure that validation is working since any syntax or other error in the javascript will interrupt the validation and the form will be submitted without validation.

*/
 
var retcode;
var found;
var fieldList = new Array();

function validate_form() {

  retcode   = true;
  for (var v_field in fieldList) {

    if (retcode) {
//alert(fieldList[v_field]);
      eval(fieldList[v_field]);
    }
  }
  return retcode;
}

function jsval_alphanumeric(fieldname, alertmsg) {
 var valid = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
 var ok = 'yes';
 var temp;
 fname = 'document.webForm.' + fieldname;
 fn = eval(fname);
 flength = 'document.webForm.' + fieldname + '.value.length';
 fl=eval(flength);
 for (var i=0; i<fl; i++) {
 temp = "" + fn.value.substring(i, i+1);
 if (valid.indexOf(temp) == '-1') ok = 'no';
 }
 if (ok == 'no') {
 alert(alertmsg);
 fn.focus();
 retcode = false;
 }
 if(fn.value == ""){
 alert(alertmsg);
 fn.focus();
 retcode = false;
 }
}

function jsval_state_province() {

  var country;
  var state;
  
  fl = document.webForm.country.length;

  for(var i=0; i < fl; i++){
    if(document.webForm.country[i].selected){
      country = document.webForm.country[i].value;
    }
  }
  
  if (country == "USA") {
  
    //verify state was selected
    fl = document.webForm.usa_state.length;
    for(var i=0; i < fl; i++){
      if(document.webForm.usa_state[i].selected){
        state = document.webForm.usa_state[i].value;
      }
    } 

    if (state == "") {
      document.webForm.usa_state.focus();
      alert("Please select your state from the state or province list.");
      retcode = false;
    }
  }
  else {
    if (country == "CAN") {
  
      //verify state was selected
      fl = document.webForm.can_state.length;
      for(var i=0; i < fl; i++){
        if(document.webForm.can_state[i].selected){
          state = document.webForm.can_state[i].value;
        }
      } 

      if (state == "") {
        document.webForm.can_state.focus();
        alert("Please select your province from the state or province list.");
        retcode = false;
      }
    }
    else {
      if (country == "CHN") {
  
        //verify state was selected
        fl = document.webForm.chn_state.length;
        for(var i=0; i < fl; i++){
          if(document.webForm.chn_state[i].selected){
            state = document.webForm.chn_state[i].value;
          }
        } 
        if (state == "") {
          document.webForm.chn_state.focus();
          alert("Please select your province or region from the state or province list.");
          retcode = false;
        }
      }
      else {
        if (country == "TWN") {

          //verify state was selected
          fl = document.webForm.twn_state.length;
          for(var i=0; i < fl; i++){
            if(document.webForm.twn_state[i].selected){
              state = document.webForm.twn_state[i].value;
            }
          }
          if (state == "") {
            document.webForm.twn_state.focus();
            alert("Please select your region from the state or province list. For Taiwan this would be a county or municipality.");
            retcode = false;
          }
        }
        else {

          // all other countries require a province
          if (document.webForm.state_input.value == "") {
            document.webForm.state_input.focus;
            alert("Please provide your state or province. If none, please enter \"None\".");
            retcode = false;
          }
        }
      }
    }
  }
  return retcode;
}

function jsval_city() {

  var country;
  var city;
  
  fl = document.webForm.country.length;

  for(var i=0; i < fl; i++){
    if(document.webForm.country[i].selected){
      country = document.webForm.country[i].value;
    }
  }
  
  if (country == "USA") {
    if (document.webForm.usa_city.value == "") {
      alert("Please enter a city");
      document.webForm.usa_city.focus;
      retcode = false;
    }
  }
  else {
    if (country == "CAN") {
      if (document.webForm.can_city.value == "") {
        alert("Please enter a city");
        document.webForm.can_city.focus;
        retcode = false;
      }
    }
    else {
      if (country == "CHN") {
        if (document.webForm.chn_city.value == "") {
          alert("Please enter a city");
          retcode = false;
        }
      }
      else {
        if (country == "TWN") {
          if (document.webForm.twn_city.value == "") {
            alert("Please enter a city");
            retcode = false;
          }
        }
        else {
          if (document.webForm.city.value == "") {
            alert("Please enter a city");
            document.webForm.city.focus;
            retcode = false;
          }
        }
      }
    }
  }
  return retcode;
}

function jsval_postal() {

  var country;

  fl = document.webForm.country.length;

  for(var i=0; i < fl; i++){
    if(document.webForm.country[i].selected){
      country = document.webForm.country[i].value;
    }
  }

  if (country == "USA") {
    if (document.webForm.postal.value == "") {
      document.webForm.postal.focus;
      alert("Please enter your postal zipcode in the postal code box.");
      retcode = false;
    }
  }
  else {
    if ((country == "CAN") || (country == "CHN")) {
      if (document.webForm.postal.value == "") {
        document.webForm.postal.focus;
        alert("Please enter your postal code in the postal code box. If you don\'t have a postal code, enter \"none\".");
        retcode = false;
      }
    }
  }
  return retcode;
}

function jsval_numeric_range (fieldname, min, max, alertmsg) {

  fnvalue = "document.webForm." + fieldname + ".value";
  fn      =eval(fnvalue);

  var reg = /^[0-9]+?/.test(fn) ;

  if ((fn < min) || (fn > max) || (reg == false)) {

    fnfocus="document.webForm." + fieldname + ".focus()";
    //eval(fnfocus);
    alert(alertmsg);
    retcode = false;
  }

  return retcode;

}

function jsval_text (fieldname, alertmsg) {
  fnvalue = "document.webForm." + fieldname + ".value";
  fn      =eval(fnvalue);
  if(fn == ""){
    fnfocus="document.webForm." + fieldname + ".focus()";
    //eval(fnfocus);
    alert(alertmsg);
    retcode = false;
  }
  return retcode;
}

function jsval_text_repeat (fieldname1, fieldname2, alertmsg) {

  fnvalue = "document.webForm." + fieldname1 + ".value";
  var f1  =eval(fnvalue);
  fnvalue = "document.webForm." + fieldname2 + ".value";
  var f2  =eval(fnvalue);

  if(!(f1 == f2)){
    fnfocus="document.webForm." + fieldname1 + ".focus()";
    eval(fnfocus);
    alert(alertmsg);
    retcode = false;
  }
  return retcode;
}

function jsval_text_minmax (fieldname, min, max, alertmsg) {

  fnvalue = "document.webForm." + fieldname + ".value";
  fn      =eval(fnvalue);

  // set re using syntax: var re = /\w{6,20}/;
  var temp = "re = /\\\w{" + min + "," + max + "}/";
  eval(temp);

  if (!re.test(fn)) {
    alert(alertmsg);
    retcode = false;
  }

  return retcode;
 
}

function jsval_checkbox(fieldname, alertmsg) {
  fnvalue = "document.webForm." + fieldname + ".checked";
  fn      = eval(fnvalue);

  if (fn == false){
    fnfocus="document.webForm." + fieldname + ".focus()";
    eval(fnfocus);
    alert(alertmsg);
    retcode = false;
  }
  return retcode;
}

// jsval_checkbox_multi will validate that at least one of multiple checkboxes of the same name is checked.
function jsval_checkbox_multi(fieldname, alertmsg) {
  found   = false;
  chosen  = '';
  fname   = "document.webForm." + fieldname;
  fn      = eval(fname);
  flength = "document.webForm." + fieldname + ".length";
  fl=eval(flength);

  for(var i=0; i < fl; i++){
    if(fn[i].checked){
      found = true;
    }
  }
  if(found == false){
    alert(alertmsg);
    retcode = false;
  }
  return retcode;
}

// jsval_checkbox_array_multi will validate that at least one of multiple checkboxes of the same name is checked.
// Use this "array" version when the checkbox name is name="something[]", as is often the case in PHP processed forms.
// Be sure to add the [] brackets after the form field name in the fieldList array. For example:
// 'jsval_checkbox_array_multi("languages[]", "Please indicate what languages you speak.")';


function jsval_checkbox_array_multi(fieldname, alertmsg) {
  retcode = false;
  fields = document.webForm.elements.length;
  for(i=0;i<fields;i++) {
    if (document.webForm.elements[i].name == fieldname) {
       if (document.webForm.elements[i].checked == true) {
         retcode = true;
         return retcode;
       }
    }
    
  }
  alert(alertmsg);
}

// jsval_checkbox_multi_other will require that at if a particuliar one of multiple checkboxes of the same name is checked, then another field is required.

function jsval_checkbox_multi_other(fieldname1, fieldvalue1, fieldname2, alertmsg) {
  fname   = "document.webForm." + fieldname1;
  fn      = eval(fname);
  flength = "document.webForm." + fieldname1 + ".length";
  fl=eval(flength);
  for(var i=0; i < fl; i++){
    if(fn[i].value == fieldvalue1){
      if(fn[i].checked){
        fnvalue = "document.webForm." + fieldname2 + ".value";
        fn      =eval(fnvalue);
        if(fn == ""){
          fnfocus="document.webForm." + fieldname2 + ".focus()";
          eval(fnfocus);
          alert(alertmsg);
          retcode = false;
        }
      }
    }
  }
  return retcode;
}

// jsval_checkbox_array_multi_other will require that at if a particuliar one of multiple checkboxes of the same
//  name is checked, then another field is required.
// Use this "array" version when the checkbox name is name="something[]", as is often the case in PHP processed forms.
// Be sure to add the [] brackets after the form field name in the fieldList array. For example:
// fieldList['rating_other'] = 'jsval_checkbox_array_multi_other("rating[]", "ot", "rating_other", "Fill in other please.")';

function jsval_checkbox_array_multi_other(fieldname1, fieldvalue1, fieldname2, alertmsg) {
  fields = document.webForm.elements.length;
  for(i=0;i<fields;i++) {
    if (document.webForm.elements[i].name == fieldname1) {
      if (document.webForm.elements[i].value == fieldvalue1) {
        if (document.webForm.elements[i].checked) {
          fnvalue = "document.webForm." + fieldname2 + ".value";
          fn =eval(fnvalue);
          if(fn == ""){
            fnfocus="document.webForm." + fieldname2 + ".focus()";
            eval(fnfocus);
            alert(alertmsg);
            retcode = false;
          }
        }
      break;
      }
    }
  }
  return retcode;
}

// jsval_checkbox_array_multi_array_multi will require that at if a particuliar one of multiple checkboxes of the same
//  name is checked, then one of another set of multiple checkboxes of the same name is checked.

function jsval_checkbox_array_multi_array_multi(fieldname1, fieldvalue1, fieldname2, alertmsg) {

  found  = "n";
  fields = document.webForm.elements.length;
  for(i=0;i<fields;i++) {
    if ((document.webForm.elements[i].name == fieldname1) && 
        (document.webForm.elements[i].value == fieldvalue1) &&
        (document.webForm.elements[i].checked)) {

       for(j=0;j<fields;j++) {
          if ((document.webForm.elements[j].name == fieldname2) && 
              (document.webForm.elements[j].checked)) {
             found = "y";
          }
       }

    }
  }
  if (found == "n") {
     alert(alertmsg);
     retcode = false;
  }
  return retcode;
}

// jsval_checkbox_multi_radio will require that at if a particuliar one of multiple checkboxes of the same name is checked, then a radio button must also be selected.

function jsval_checkbox_multi_radio(fieldname1, fieldvalue1, fieldname2, alertmsg) {

  fname   = "document.webForm." + fieldname1;
  fn      = eval(fname);
  flength = "document.webForm." + fieldname1 + ".length";
  fl=eval(flength);
  for(var i=0; i < fl; i++){
    if(fn[i].value == fieldvalue1){
      if(fn[i].checked){
        chosen  = '';
        fname   = "document.webForm." + fieldname2;
        fn      = eval(fname);
        flength = "document.webForm." + fieldname2 + ".length";
        fl=eval(flength);
        for(var i=0; i < fl; i++){
          if(fn[i].checked){
            fvalue = "fn[i].value";
            chosen = eval(fvalue);
          }
        }
        if(chosen == ""){
          alert(alertmsg);
          retcode = false;
        }
      }
    }
  }
  return retcode;
}


// jsval_checkbox_array_multi_radio will require that at if a particuliar one of multiple checkboxes of the same
//  name is checked, then a particuliar radio input field is required.
// Use this "array" version when the checkbox name is name="something[]", as is often the case in PHP processed forms.
// Be sure to add the [] brackets after the form field name in the fieldList array. For example:
// fieldList['occ'] = 'jsval_checkbox_array_multi_radio("occ[]", "e", "empl", "Please indicate full or part time.")';

function jsval_checkbox_array_multi_radio(fieldname1, fieldvalue1, fieldname2, alertmsg) {
  fields = document.webForm.elements.length;
  for(i=0;i<fields;i++) {
    if (document.webForm.elements[i].name == fieldname1) {
      if (document.webForm.elements[i].value == fieldvalue1) {
        if (document.webForm.elements[i].checked) {
          chosen  = '';
          fname   = "document.webForm." + fieldname2;
          fn      = eval(fname);
          flength = "document.webForm." + fieldname2 + ".length";
          fl=eval(flength);
          for(var i=0; i < fl; i++){
            if(fn[i].checked){
              fvalue = "fn[i].value";
              chosen = eval(fvalue);
            }
          }
          if(chosen == ""){
            alert(alertmsg);
            retcode = false;
          }
        }
      break;
      }
    }
  }
  return retcode;
}

// jsval_multicheck requires an array "myfields" containing the names of all checkbox items to be considered. At least one of the items must be checked. Use this when the checkboxes don't share the same name. IMPORTANT: Do not enclose arranyName in quotes when passing it as a parameter to jsval_multicheck! It's an array, not a string. 

function jsval_multicheck (arrayName, alertmsg) {
  found = false;
  fl =arrayName.length;
  for(var i=0; i < fl; i++){
    fnvalue = "document.webForm." + arrayName[i] + ".checked";
    fn = eval(fnvalue);
    if (fn == true){
      found = true;
    }
  }
  if (found == false) {
    fnfocus="document.webForm." + arrayName[0] + ".focus()";
    eval(fnfocus);
    alert(alertmsg);
    retcode = false;
  }
  return retcode;
}

// jsval_multi_input_numeric requires an array "myfields" containing the names of all input boxes to be considered. At least one of the items must contain a number. Use this when the input boxes don't share the same name. IMPORTANT: Do not enclose arranyName in quotes when passing it as a paramete! It's an array, not a string. greater_than and less_than are for indicating the range that the number must fall into.

function jsval_multi_input_numeric (arrayName, alertmsg, greater_than, less_than) {
  found = false;
  fl =arrayName.length;
  for(var i=0; i < fl; i++){
    fnvalue = "document.webForm." + arrayName[i] + ".value";
    fn = eval(fnvalue);
    if ((fn > greater_than) && (fn < less_than)){
      found = true;
    }
  }
  if (found == false) {
    fnfocus="document.webForm." + arrayName[0] + ".focus()";
    eval(fnfocus);
    alert(alertmsg);
    retcode = false;
  }
  return retcode;
}

// jsval_other_checkbox requires that if fieldname1 is checked, then fieldname2 must contain text.

function jsval_other_checkbox (fieldname1, fieldname2, alertmsg) {
  fnvalue = "document.webForm." + fieldname1 + ".checked";
  fn      = eval(fnvalue);
  if (fn == true) {
    fnvalue = "document.webForm." + fieldname2 + ".value";
    fn      =eval(fnvalue);
    if(fn == ""){
      fnfocus="document.webForm." + fieldname2 + ".focus()";
      eval(fnfocus);
      alert(alertmsg);
      retcode = false;
    }
  }
  return retcode;
}

// jsval_text_select requires that if text fieldname1 contains data, then select fieldname2 must have an item be selected.

function jsval_text_select (fieldname1, fieldname2, alertmsg) {
  fnvalue = "document.webForm." + fieldname1 + ".value";
  fn      = eval(fnvalue);
  if (fn != "") {
    chosen  = '';
    fname   = "document.webForm." + fieldname2;
    fn      = eval(fname);
    flength = "document.webForm." + fieldname2 + ".length";
    fl=eval(flength);
    for(var i=0; i < fl; i++){
      if(fn[i].selected){
        fvalue = "fn[i].value";
        chosen = eval(fvalue);
      }
    }
    if(chosen == ""){
      fnfocus="document.webForm." + fieldname2 + ".focus()";
      eval(fnfocus);
      alert(alertmsg);
      retcode = false;
    }
  }
  return retcode;
}

// jsval_select_other requires that if a particular select option is selected that a text field must contain data.

function jsval_select_other (fieldname1, fieldvalue, fieldname2, alertmsg) {
  picked  = 'n';
  fname   = "document.webForm." + fieldname1;
  fn      = eval(fname);
  flength = "document.webForm." + fieldname1 + ".length";
  fl=eval(flength);

  for(var i=0; i < fl; i++){
    if(fn[i].selected){
      if (fn[i].value == fieldvalue) {
        picked = 'y';
      }
    }
  }
  if (picked == 'y') {
    fnvalue = "document.webForm." + fieldname2 + ".value";
    fn      = eval(fnvalue);
    if (fn == "") {
      fnfocus="document.webForm." + fieldname2 + ".focus()";
      eval(fnfocus);
      alert(alertmsg);
      retcode = false;
    }
  }
  return retcode;
}

// jsval_other_radio requires that if a radio button fieldname1 is a particuliar value, then fieldname2 must contain text.

function jsval_other_radio (fieldname1, fieldvalue1, fieldname2, alertmsg) {

  chosen  = '';
  fname   = "document.webForm." + fieldname1;
  fn      = eval(fname);
  flength = "document.webForm." + fieldname1 + ".length";
  fl=eval(flength);
  for(var i=0; i < fl; i++){
    if(fn[i].checked){
      fvalue = "fn[i].value";
      chosen = eval(fvalue);
    }
  }
  if (chosen == fieldvalue1) {
      fnvalue = "document.webForm." + fieldname2 + ".value";
      fn      =eval(fnvalue);
      if(fn == ""){
        fnfocus="document.webForm." + fieldname2+ ".focus()";
        eval(fnfocus);
        alert(alertmsg);
        retcode = false;
      }
  }
  return retcode;
}

function jsval_select (fieldname, alertmsg) {

  chosen  = '';
  fname   = "document.webForm." + fieldname;
  fn      = eval(fname);
  flength = "document.webForm." + fieldname + ".length";
  fl=eval(flength);
  for(var i=0; i < fl; i++){
    if(fn[i].selected){
      fvalue = "fn[i].value";
      chosen = eval(fvalue);
    }
  }
  if(chosen == ""){
    alert(alertmsg);
    fnfocus = "fn.focus()";
    eval(fnfocus);
    retcode = false;
  }
  return retcode;
}

function jsval_radio(fieldname, alertmsg){
  chosen  = '';
  fname   = "document.webForm." + fieldname;
  fn      = eval(fname);
  flength = "document.webForm." + fieldname + ".length";
  fl=eval(flength);
  for(var i=0; i < fl; i++){
    if(fn[i].checked){
      fvalue = "fn[i].value";
      chosen = eval(fvalue);
    }
  }
  if(chosen == ""){
    alert(alertmsg);
    retcode = false;
  }
  return retcode;
}

function jsval_radio_array(fieldname, alertmsg) {
  retcode = false;
  fields = document.webForm.elements.length;
  for(i=0;i<fields;i++) {
    if (document.webForm.elements[i].name == fieldname) {
       if (document.webForm.elements[i].checked == true) {
         retcode = true;
         return retcode;
       }
    }
    
  }
  alert(alertmsg);
}

function jsval_login(fieldname) {
 var valid = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
 var ok = 'yes';
 var temp;
 fname = 'document.webForm.' + fieldname;
 fn = eval(fname);
 flength = 'document.webForm.' + fieldname + '.value.length';
 fl=eval(flength);
 for (var i=0; i<fl; i++) {
 temp = "" + fn.value.substring(i, i+1);
 if (valid.indexOf(temp) == '-1') ok = 'no';
 }
 if (ok == 'no') {
 alert('The login name must be regular characters or numbers with no spaces.');
 fn.focus();
 retcode = false;
 }
 if(fn.value == ""){
 alert("Please enter a login");
 fn.focus();
 retcode = false;
 }
}

function jsval_password(fieldname) {
 var valid = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
 var ok = 'yes';
 var temp;
 fname = 'document.webForm.' + fieldname;
 fn = eval(fname);
 flength = 'document.webForm.' + fieldname + '.value.length';
 fl=eval(flength);
 for (var i=0; i<fl; i++) {
   temp = "" + fn.value.substring(i, i+1);
   if (valid.indexOf(temp) == '-1') ok = 'no';
 }
 if (ok == 'no') {
   alert('The password must be regular characters or numbers with no spaces.');
   fn.focus();
   retcode = false;
 }
 if(fn.value == ""){
   alert("Please enter a Password");
   fn.focus();
   retcode = false;
 }
 if(retcode) {
   if(fl < 6){
     alert("The password must be at least 6 characters long.");
     fn.focus();
     retcode = false;
   }
 }
}

function jsval_password_check(fieldname,fieldname1) {
 fname = 'document.webForm.' + fieldname;
 fn = eval(fname);
 fname1 = 'document.webForm.' + fieldname1;
 fn1 = eval(fname1);
 flength = 'document.webForm.' + fieldname + '.value.length';
 fl=eval(flength);
 flength1 = 'document.webForm.' + fieldname1 + '.value.length';
 fl1=eval(flength1);
 if(fn.value == fn1.value ){
 retcode = true;
 }
 else {
 alert("The repeated password does not match.");
 fn1.focus();
 retcode = false;
 }
}

// Use jsval_email to "ensure correct format" of email. Use
// jsval_text to "require" email. Note, alertmsg is optional.

function jsval_email(fieldname, alertmsg) {
   var fnvalue = "document.webForm." + fieldname + ".value"
   var fn      =eval(fnvalue)
   var fnfocus="document.webForm." + fieldname + ".focus()"
   var emailPat=/^(.+)@(.+)$/
   var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
   var validChars="\[^\\s" + specialChars + "\]"
   var firstChars=validChars
   var quotedUser="(\"[^\"]*\")"
   var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
   var atom="(" + firstChars + validChars + "*" + ")"
   var word="(" + atom + "|" + quotedUser + ")"
   var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
   var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
   var matchArray=fn.match(emailPat)

   if (fn!="") {

   if (matchArray==null) {
      if (alertmsg) { alert(alertmsg)}
      else { alert("Email address seems incorrect (check @ and .'s)")}
      eval(fnfocus)
      retcode = false
      return retcode
   }

   var user=matchArray[1]
   var domain=matchArray[2]
   if (user.match(userPat)==null) {
      if (alertmsg) { alert(alertmsg)}
      else { alert("The username in the email address appears to be invalid.") }
      eval(fnfocus)
      retcode = false
      return retcode
   }

   var IPArray=domain.match(ipDomainPat)
   if (IPArray!=null) {
      for (var i=1;i<=4;i++) {
	   if (IPArray[i]>255) {
            if (alertmsg) { alert(alertmsg)}
            else { alert("The email IP address is invalid.") }
            eval(fnfocus)
		return false
	   }
       }
      retcode = false
      return retcode
   }

   var domainArray=domain.match(domainPat)
   if (domainArray==null) {
      if (alertmsg) { alert(alertmsg)}
      else { alert("The domain name in the email address appears to be invalid.") }
      eval(fnfocus)
      retcode = false
      return retcode
   }

   var atomPat=new RegExp(atom,"g")
   var domArr=domain.match(atomPat)
   var len=domArr.length
   if ( domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 6 ) {
      if (alertmsg) { alert(alertmsg)}
      else { alert("The email address must end in a valid domain.") }
      eval(fnfocus)
      retcode = false
      return retcode
   }

   if (domArr[domArr.length-1].length==3 && len<2) {
      if (alertmsg) { alert(alertmsg)}
      else { alert("The email address is missing a hostname.") }
      eval(fnfocus)
      retcode = false
      return retcode
   }
}

   return retcode;
}
//END VALIDATE EMAIL

function jsval_maxchars(fieldname, maxc, alertmsg) {
   flength = "document.webForm." + fieldname + ".value.length";
   fl=eval(flength);

   if (fl > maxc) {
      alert(alertmsg);
      retcode = false;
   }
   else return retcode;
}

// jsval_number requires numbers with or without a decimal point and commas
function jsval_number(fieldname, alertmsg) {
 var valid = '.,0123456789';
 var ok = 'yes';
 var temp;
 fname = 'document.webForm.' + fieldname;
 fn = eval(fname);
 flength = 'document.webForm.' + fieldname + '.value.length';
 fl=eval(flength);
 for (var i=0; i<fl; i++) {
   temp = "" + fn.value.substring(i, i+1);
   if (valid.indexOf(temp) == '-1') ok = 'no';
 }
 if (ok == 'no') {
   alert(alertmsg);
   fn.focus();
   retcode = false;
 }
}

// jsval_date requires a date formated mm/dd/yy or mm/dd/yyyy depending upon
// parameters used. Use y for padding to pad days and months with leading 0.
// use 2 or 4 for yearschar. Checks leap year as well.
function jsval_date(fieldname, padding, yearchars, alertmsg) {
 month         = 0;
 day           = 0;
 year          = 0;
 fnvalue       = "document.webForm." + fieldname + ".value";
 myDate_string =  eval(fnvalue);
 myDate_array  = myDate_string.split( '/' );
 valid         = '0123456789';
 temp          = '';
 ok            = 'yes';
 if ((myDate_array[0]) && (myDate_array[1]) && (myDate_array[2])) {
    month = myDate_array[0];
    day   = myDate_array[1];
    year  = myDate_array[2];
 }
 else {
    if ((myDate_array[0]) || (myDate_array[1]) || (myDate_array[2])) {
       ok = 'no';
    }
    else {
       return;
    }
 }

 // Ensure Month is numeric
 fl=month.length;
 for (var i=0; i<fl; i++) {
 temp = "" + month.substring(i, i+1);
 if (valid.indexOf(temp) == '-1') ok = 'no';
 }

 // Ensure Day is numeric
 fl=day.length;
 for (var i=0; i<fl; i++) {
 temp = "" + day.substring(i, i+1);
 if (valid.indexOf(temp) == '-1') ok = 'no';
 }

 // Ensure Year is numeric
 fl=year.length;
 for (var i=0; i<fl; i++) {
 temp = "" + year.substring(i, i+1);
 if (valid.indexOf(temp) == '-1') ok = 'no';
 }

 if ((month < 1)    || 
     (month > 12)   ||
     (day < 1) ||
     (day > 31)) {
   ok = 'no';
 }
 if ( yearchars == '4' ) {
  if ((year < 1970) ||
      (year > 3000)) {
    ok = 'no';
  }
 }
 else {
  if ((year < 00) ||
      (year > 30)) {
    ok = 'no';
  }
 }

// Check maximum days allowed in month
 if ((month ==  2) && (day > 29)) { ok = 'no' }
 if ((month ==  4) && (day > 30)) { ok = 'no' }
 if ((month ==  6) && (day > 30)) { ok = 'no' }
 if ((month ==  9) && (day > 30)) { ok = 'no' }
 if ((month == 11) && (day > 30)) { ok = 'no' }

// Check for leap year
leap = 0;
if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
  leap = 1;
}
if ((month == 2) && (leap == 0) && (day > 28)) {
  ok = 'no';
}

 if (ok == 'no') {
 alert(alertmsg);
 fnfocus="document.webForm." + fieldname + ".focus()";
 eval(fnfocus);
 retcode = false;
 return
 }

if (padding == 'y') {
 if (month == '1') {month = '01'}
 if (month == '2') {month = '02'}
 if (month == '3') {month = '03'}
 if (month == '4') {month = '04'}
 if (month == '5') {month = '05'}
 if (month == '6') {month = '06'}
 if (month == '7') {month = '07'}
 if (month == '8') {month = '08'}
 if (month == '9') {month = '09'}

 if (day == '1') {day = '01'}
 if (day == '2') {day = '02'}
 if (day == '3') {day = '03'}
 if (day == '4') {day = '04'}
 if (day == '5') {day = '05'}
 if (day == '6') {day = '06'}
 if (day == '7') {day = '07'}
 if (day == '8') {day = '08'}
 if (day == '9') {day = '09'}
}

 if ((month > 0) && (day > 0) && (year > 0)) {
   fnvalue = "document.webForm." + fieldname + ".value = month + '/' + day + '/' + year"
   eval(fnvalue);
 }
}

