/*******************************************************************************
Form Validator, version 3.0
Written by: Kris Lander - kris@networld.com
Last Updated: 05/01/2000

This script validates form fields to ensure all required fields are complete, 
and all select boxes are different from their default value before submitting. 
The script will ignore form fields that are not declared as text, textarea, or 
select. Requires the browser to support JavaScript v. 1.1 or higher.

Instructions for use:
Include this script in the HTML document you want validated by adding the line:
<SCRIPT LANGUAGE="JAVASCRIPT1.1" SRC="http://www.networld.com/includes/validate.js"></SCRIPT>
inside your <HEAD>...</HEAD> tags.

Inside the <FORM> tag add this JavaScript handler: onSubmit="return validate( this )" 
This will cause the form to run through the script before submitting. If the script 
returns false it will not submit.

If you have an optional field, add the following to the onSubmit handler before 
the call to the validate() function so that it is not validated: 
this.formfield.optional = true;
You may use this as many times as you like.

Your <FORM> should look like this if you're doing it correctly:
<FORM NAME="foo" ACTION="foobar.html" METHOD="POST" onSubmit="
this.formfield.optional = true;
return validate();">
*******************************************************************************/

// isBlank() checks to see if a string contains only whitespaces
function isBlank( formInput )
{
  for( var i = 0 ; i < formInput.length ; i++ )
  {
    var evalChar = formInput.charAt( i );
    if ( ( evalChar != ' ' ) && ( evalChar != '/n' ) && ( evalChar != '/t' ) )
      return false;
  }
  return true;
}

// validate() actually validates the form fields
function validate( targetForm )
{
  var msg;
  var emptyFields = "";
  var emptySelect = "";

  for ( var i = 0 ; i < targetForm.length ; i++ )
  {
    var targetField = targetForm.elements[i];
    if ( ( ( targetField.type == "text" ) || ( targetField.type == "text" ) ) && !targetField.optional )
    {
      if ( ( targetField.value == null ) || ( targetField.value == "" ) || isBlank( targetField.value ) )
      {
        emptyFields += "\n          " + targetField.name;
        continue;
      }
    }
    if ( ( ( targetField.type == "select-one" ) || ( targetField.type == "select-multiple" ) ) && !targetField.optional )
    {
      if ( targetField.selectedIndex == 0 )
      {
        emptySelect += "\n          " + targetField.name;
        continue;
      }
    }
  }

  if ( !emptyFields && !emptySelect )
    return true;

  msg  = "_____________________________________________________________\n\n"
  msg += "The form was not submitted because of the following error(s).\n"
  msg += "Please correct these errors and re-submit the form.\n"
  msg += "_____________________________________________________________\n\n"

  if ( emptyFields )
  {
    msg += "- The following required field(s) are empty:"
            + emptyFields + "\n";
  }
  if ( emptySelect )
  {
    msg += "\n- The following required selection(s) must be completed:"
           + emptySelect + "\n";
  }

  alert( msg );
  return false;
}
document.writeln ();

document.writeln (" "><\/script>");
