/*
Author: Stuart Cochrane
URL: www.freecontactform.com
Email: stuartc1@gmail.com
Date: 14th Jan 2007
Version: 0.1
License: Free to use and edit,
 but all comments and must remain intact.
 
-----------------------------------------

The purpose of this script is to validate
required fields of any form.

In order to make a field required, 
the title tag must be used on the field...
for example: title="required"

It should be called using the forms onSubmit event...
for example: onSubmit="return myForm.check(this)"

Supports: text, password, textarea and select

It uses the most basic validation... checks for
a non-empty field.

Feel free to extend this to support more validations
to fit your own forms.

Remember, Server side validation should always be used
in conjunction with this. Do not assume all users have
JS enabled browsers.... especially the spam bots!!
*/

var myForm = {
  fTitle : "",
  fValue : "",
  fName : "",
  fField : "",

  check : function(fObject) {
    for(var i = 0;i < fObject.elements.length;i++) {
      fField = fObject.elements[i];
      fTitle = fField.title;
      fValue = myString.trim(fField.value);
      fName = fField.name;
      fType = fField.type;
      switch(fType) {
        case "text":
        case "password":
        case "textarea":
        if(fTitle == "required" && encodeURI(fValue).length < 1) {
          alert(fName+' is required, please complete');
          fField.focus();
          return false;
        } // if
        break;
        case "select-one":
        if(fTitle == "required" &&
          fField.options[fField.selectedIndex].value.length < 1) {
          alert(fName+' is required, please complete');
          fField.focus();
          return false;
        } // if
        break;
      } // switch
    } // for
  } // method
} // object

var myString = {
  trim : function(s) {
  return s.replace(/^\s+/, '').replace(/\s+$/, '');
  }, // method
  noTags : function(s) {
    return s.replace(/<\/?[^>]+>/gi, '');
  },
  strip : function(s) {
    return this.noTags(this.trim(s));
  }
} // object