var Validator = Class.create();
Validator.prototype = {
  
  initialize: function(id) {
    this.id = id;
    this.valid = true;
    this.errors = new Hash();
  },
  
  clear: function() {
    this.valid = true;
    this.errors = new Hash();
  },
  
  error_count: function() {
    return this.errors.toArray().length;
  },
  
  remove_error: function(key) {
    this.errors.unset(key);
    if (this.error_count() == 0) { 
      this.valid = true;
    }
  },

  validate_presence_of: function(field, msg) {
    if ($F(field)=="") {
      this.valid = false;
      this.errors.set(field, msg);
    }
  },
  
  validate_format_of: function(field, pattern, msg) {
    if ($F(field)!="") {
      if (!pattern.test($F(field))) {
        this.valid = false;
        this.errors.set(field, msg);
      }
    }
  },
  
  validate_presence_and_format_of: function(field, pattern, msg) {
    this.validate_presence_of(field, msg);
    this.validate_format_of(field, pattern, msg)
  },
  
  validate_checkbox_or_radio_selected: function(field, msg) {
    valid = false;
    $A(document.getElementsByName(field)).each(function(input){if(input.checked)valid=true;});
    if (!valid) {
      this.valid = false;
      this.errors.set(field, msg);
    }
  },
  
  validate_confirmation_of: function(field1, field2, msg) {
    if ($F(field1)!=$F(field2)) {
      this.valid = false;
      this.errors.set(field1+'_'+field2, msg);
    }
  },
    
  return_all_errors: function() {
    var msg = "";
    this.errors.each(function(error) {
       msg += " * " + error.value + "\n";
    });
    return msg;
  }
}
