validation = {
      validFields: Array(),
      types: Array(),
      rules: {
                'year': /^((19|20)[0-9]{2})?$/,
                'integer': /^[0-9]*$/,
                'number': /^[0-9\., ]*$/,
                'email': /^([^@]+@([^@\.]{2,}\.)+[^@\.]{2,})?$/i
        },

      addField: function(field, type) {
                validation.types[field] = type;
                validation.validFields[field] = true;
        },

      formValidation : function() {
              var x;
              for (x in validation.validFields) {
                      if ((validation.validFields[x]==false)||(document.getElementById(x).value=="")) {
                          return false;
                          }
                      }
              return true;
              },

      updateButton: function () {
            var button;
            if ( document.getElementById ('submit') ) {
                button = document.getElementById ('submit');
            } else {
                    return false;
            }

            if (!validation.formValidation()) {
                 button.disabled = true;
                 } else {
                 button.disabled = false;
                 }

      },

      validateKey: function(event) {
              switch (event.keyCode) {
                                                  case 8:
                                                  case 9:
                                                  case 16:
                                                  case 35:
                                                  case 36:
                                                  case 37:
                                                  case 38:
                                                  case 39:
                                                  case 40:
                                                  case 46:
                                                  case 116:
                                                           return true;
                                                           break;

                                                  default:
                                                           return false;
                                                           break;
                                          }

              },

      checkValidation: function(event) {
        field=this;
        type=validation.types[this.id];
        switch (type) {
                case "email":
                              with (field) {
                                  check=new RegExp(validation.rules["email"]);
                                  if (check.test(field.value+" ")!=true) {
                                          style.backgroundColor="#FFCCCC";
                                          validation.validFields[field.id] = false;
                                          } else {
                                             style.backgroundColor="#FFFFFF";
                                             validation.validFields[field.id] = true;
                                             }
                                  validation.updateButton();
                                  }
                              break;
                case "name":
                              with (field) {
                                  if ((value.length>-1)&&(value.length<3)) {
                                          style.backgroundColor="#FFCCCC";
                                          validation.validFields[field.id] = false;
                                          } else {
                                             style.backgroundColor="#FFFFFF";
                                             validation.validFields[field.id] = true;
                                             }
                                  validation.updateButton();
                                  if (value.length>15) {
                                          return validation.validateKey(event);
                                          }
                                  }
                              break;
                case "passwd":
                              with (field) {
                                  if (((value.length>-1)&&(value.length<3)) || (value.length>18)) {
                                          style.backgroundColor="#FFCCCC";
                                          validation.validFields[field.id] = false;
                                          }  else {
                                             style.backgroundColor="#FFFFFF";
                                             validation.validFields[field.id] = true;
                                             }
                                  validation.updateButton();
                                  }
                              break;
                case "text":
                              with (field) {
                                  if (value.length>499) {
                                          return validation.validateKey(event);
                                          }
                                  validation.updateButton();
                                  }
                              break;
                }

    }
}

