/*

FORM VALIDATION SCRIPT
written by John McGAraghan for inter@ctivate

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.

Required Select boxes should have a default "--Select One--" field at index 0 and the 
value of that option should be null (i.e. value="").

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

*/

// populate this array with arrays of FieldName,errorLabel pairs
var reqFields=new Array(
  Array ('EmailAddress','Email Address'),
  Array ('Salutation','Salutation'),
  Array ('FirstName','First Name'),
  Array ('LastName','Last Name'),
  Array ('Address1','Address'),
  Array ('City','City'),
  Array ('State','State'),
  Array ('PostalCode','Zip')
);

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.indexOf('elect')>0")) {
      if(eval("f."+reqFields[i][0]+".options[f."+reqFields[i][0]+".options.selectedIndex].value==''"))   {
        err.push(reqFields[i][1]);
      }
    } 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=err.join(",\n -");
    alert("You must fill out all required fields.  Please provide information for the following fields:\n -"+errText);
    return false;
  }
  if(eml) {
    return emailVal(eml);
  }
}

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;
  }
}
