  //Here is a utility function that returns true if a string contains only
  //whitespace characters:
  function isblank(s)
  {
    for (var I = 0; I < s.length; I++)
    {
      var c = s.charAt(I);
      if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
  }
  
  function checkDate(the_date)
  {
      var date_array = the_date.split("/");
      
      if ((date_array.length == 3) &&
          (date_array[0].length > 0) &&
          (date_array[1].length > 0) &&
          (date_array[2].length > 0) &&
          (date_array[0] > 0) && (date_array[0] < 13) && (date_array[0].length == 2) &&
          (date_array[1] > 0) && (date_array[1] < 32) && (date_array[1].length == 2) &&
          (date_array[2] >= 0) && (date_array[2].length == 2))
      {
          return true;
      }
	  else
	  {
          return false;
      }
  }  
  
  //Here is the function that performs the form verification. It will be invoked
  //from the onSubmit() event handler.  The handler should return whatever value
  //this function returns.
  function verify(f)
  {
    var msg;
    var empty_fields = "";
    var errors = "";
    var empty_count = 0;
    var number_count = 0;
    
    // First, do any custom checking that the user wants.  The user's function
    // customValidation() will return true if all is well, and false if there
    // is an error.
    
    if (customValidation(f))
    {
  	  var date_count = 0;
      //Loop through the elements of the form, looking for all
      //text, textarea, and select elements that have a "required" property
      //defined.  Then, check for fields that are empty and make a list of them.
      //Also, if any of these elements have a "min" or a "max" property defined,
      //then verify that they are numbers and that they are in the right range.
      //Put together error messages for fields that are wrong.
      for (var I = 0; I < f.length; I++)
      {
        var e = f.elements[I];
        if (((e.type == "text") || (e.type == "textarea")) && e.required)
        {
          //first check if the field is empty
          if ((e.value == null) || (e.value == "") || isblank(e.value))
          {
            empty_fields += "\n          " + e.name;
            empty_count++;
            continue;
          }
          
          //Now check for fields that are supposed to be numeric.
          if (e.numeric || (e.min != null) || (e.max != null))
          {
            var v = parseFloat(e.value);
            if (isNaN(v) ||
               ((e.min != null) && (v < e.min)) ||
               ((e.max != null) && (v > e.max)))
            {
              errors += " - The field " + e.name + " must be a number";
              if (e.min != null)
                errors += " that is greater than or equal to " + e.min;
              if ((e.max != null) && (e.min != null))
                errors += " and less than or equal to " + e.max;
              else if (e.max != null)
                errors += " that is less than " + e.max;
              errors += ".\n";
              number_count++;
            }
          }
  		
          //Now check for fields that are supposed to be date fields.
          if (e.datefield)
          {
            if (!checkDate(e.value))
  		  {
  		    errors += " - The field " + e.name + " must be a date in the format mm/dd/yy\n";
  			date_count++;
  		  }
          }
        }
        
        if (((e.type == "select-one") || (e.type == "select-multiple")) && (e.required))
        {
          var itemSelected = false;
          
          for (var itemNum = 0; itemNum < e.length; itemNum++)
          {
            if (e[itemNum].selected)
            {
              itemSelected = true;
              break;
            }
          }
          
          if (!itemSelected)
          {
            empty_fields += "\n          " + e.name;
            empty_count++;
          }
        }
        
        if (((e.type == "radio") || (e.type == "checkbox")) && (e.required))
        {
          var itemChecked = false;
          
          for (var itemNum = 0; itemNum < f[e.name].length; itemNum++)
          {
            if (f[e.name][itemNum].checked)
            {
              itemChecked = true;
              break;
            }
          }
          
          if (!itemChecked)
          {
            empty_fields += "\n          " + e.name;
            empty_count++;
          }
        }
      }  
      
      //Now, if there were any errors, display the messages, and
      //return false to prevent the form from being submitted.
      //Otherwise, return true.
      if (!empty_fields && !errors) return true;
      
      msg = "_____________________________________________________\n\n";
      if (empty_count + number_count + date_count == 1)
      {
        msg += "The form was not submitted because of the following error.\n";
        msg += "Please correct this error and re-submit.\n";
      }
      else
      {
        msg += "The form was not submitted because of the following errors.\n";
        msg += "Please correct these errors and re-submit.\n";
      }
      msg += "_____________________________________________________\n\n";
      
      if (empty_fields)
      {
        if (empty_count == 1)
        {
          msg += " - The following required field is empty or needs to be selected: " + empty_fields + "\n";
        }
        else
        {
          msg += " - The following required fields are empty or need to be selected: " + empty_fields + "\n";
        }
        if (errors) msg += "\n";
      }
      
      msg += errors;
      alert(msg);
      return false;
    }
    else
    {
      return false;
    }
  }

