/*
FORM VALIDATION SCRIPT
version 1.2 (beta)
written by John McGaraghan for inter@ctivate; 7.31.02
Updated by John McGaraghan; 8.13.02

the document's <form> declaration tag should have an onSubmit parameter as follows:
    onSubmit="return formVal(this, this.emailfield.value);
where "emailfield" is the name of the form input field for an email address
if the second argument is not passed, no validation will be done on email.

Select boxes should have a default "Select One" options at index 0 (with value="").

support for checkbox and textarea field types is not present at this time.

support for radio buttons added 8.13.02
  --checks to see if one of a group of like named radio buttons is selected

support for simple/verbose error reporting added 8.13.02

*/

// set to true for verbose error reporting;
verbose=true;

// populate an array like this with arrays of FieldName,errorLabel pairs in the html file that calls this script
// in this way, you may use this single file to validate forms on different pages
/*
var reqFields=new Array(
  Array ('EmailAddress','Email Address'),
  Array ('Salutation','Salutation'),
  Array ('FirstName','First Name'),
  Array ('LastName','Last Name')
);
*/
function formVal(f, eml)  {
var err=new Array();
  // loop through required fields
  for(i=0;i<reqFields.length;i++)  {
    // check for a selected option if field type is select
    if(eval("f."+reqFields[i][0]+".type") && eval("f."+reqFields[i][0]+".type.indexOf('elect')>0")) {
      if(eval("f."+reqFields[i][0]+".options[f."+reqFields[i][0]+".options.selectedIndex].value==''"))   {
        err.push(reqFields[i][1]);
      }
    // check for a selected option if field type is radio
    } else if(eval("f."+reqFields[i][0]+"[0]") && eval("f."+reqFields[i][0]+"[0].type.indexOf('adio')>0")) {
      var radioElements=eval("f."+reqFields[i][0]+".length");
      var radioChecked=false;
      for(n=0;n<radioElements;n++)   {
        if(eval("f."+reqFields[i][0]+"["+n+"].checked==true")) {
          radioChecked=true;
          break;
        }
      }
      if(!radioChecked) {
        err.push(reqFields[i][1]);
      }
      radioChecked=false;
    } else {
    // check for value in input fields
      if(eval("f."+reqFields[i][0]+".value==''")) {
        err.push(reqFields[i][1]);
      }
    }
  }
  if(err.length>0)  {
    var errText="";
    if(verbose) {
      errText="  Please provide information for the following fields:\n  -";
      errText+=err.join(",\n  -");
    }
    alert("You must fill out all required fields."+errText);
    return false;
  }
  if(eml) {
    return emailVal(eml);
  } else {
    return true;
  }
}

re = /\S*@.\S*\..\S*/

function emailVal(emailadr) {
  OK = re.test(emailadr);
  if (OK != true) {
    alert("Please make sure your email address is in the format yourname@company.com.");
    return false;
  } else {
    return true;
  }
}

