/* Author: reynardmh
   Date: 1-Dec-2000
   Desc: return true if all elements that is required (optional is not 
set to true) is not blank
  
   Notes:
   * If the element is in email format, the email property of the 
element must be set to true so
     it will check for valid email format eg: this.email_address.email = 
true;
   * In order that the error message displays the human readable format for
     the fields name, give name for each elements in the desc property.
     eg: this.fname.desc = "First Name";
*/
function validate(f)
  {
  var msg;
  var empty_fields = "";
  var error = "";
 
  for (var i=0; i < f.length; i++)
    {
    var e = f.elements[i];
    if (e.email)
      {
      if (e.optional)
        {
        if (e.value != null && e.value != "" && !isblank(e.value))
          {
          if (!valid_email(e.value))
            error += "Email address is not valid";
          }
        }
      else
        {
        if (!valid_email(e.value))
          error += "Email address is not valid";
        }
      }
    else if ((e.type == "text" || e.type == "textarea" || e.type == 
"password") && !e.optional)
      {
      if (e.value == null || e.value == "" || isblank(e.value))
        {
        if (e.desc != null)
          empty_fields += "\n     " + e.desc;
        else
          empty_fields += "\n     " + e.name;
        }
      }
    else if (e.type == "select-one" && !e.optional)
      {
      if (e.options[e.selectedIndex].value == null || 
e.options[e.selectedIndex].value == ""
          || isblank(e.options[e.selectedIndex].value))
        {
        error += "Select appropriate value for ";
        if (e.desc != null)
          error += e.desc + "\n";
        else
          error += e.name + "\n";
        }
      }
    }
  if (!empty_fields && !error)
    return true;
 
  msg = "Oops, Please correct these error(s) and resubmit. \n";
  if (empty_fields)
    msg += "The following required field(s) are empty:" + empty_fields + 
"\n\n";
  msg += error; 
  alert (msg);
  return false;
  }
 
 
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;
  }

