function Validator() 
{
  this.resetRules();
  this.resetState();
}

Validator.prototype =  
{
  resetState: function() {
    this.is_valid = true;
    this.error_list = [];
  },

  resetRules: function() {
    this.rules = [];
  },

  addRule: function(rule) {
    this.rules[this.rules.length] = rule;
  },

  validate: function() {
    this.resetState();
    for(i = 0; i < this.rules.length; i++)
      if(this.rules[i].check(this.error_list) == false)
        this.is_valid = false;
    return this.is_valid;
  },

  isValid: function() { return this.is_valid; },

  getErrors: function() { return this.error_list; },

  getErrorsString: function()
  {
    var error_message = '';
    for(i = 0; i < this.error_list.length; i++)
    {
      var n = i + 1;
      if(this.error_list[i])
        error_message += n + '. ' + this.error_list[i] + "<br />\n";
    }
    return error_message;
  },

  messageErrors: function()
  {
    GameModalWindow.message(i18n['following_fields_contain_errors'] + ':<br/>' + this.getErrorsString(), '');
  }
};

function RequiredFieldRule(field_id, message) {
  this.field_id = field_id;
  this.message = message;
};

RequiredFieldRule.prototype = 
{
  check: function(error_list)
  {
    var field = jQuery('#' + this.field_id);
    if(!field.val())
    {
      if(this.message)
        error_list[error_list.length] = this.message;
      else
        error_list[error_list.length] = i18n['field_required'].replace('%field', field.attr('title'));
      return false;
    }
    return true;
  }
};

function MaxLengthFieldRule(field_id, max_length) {
  this.field_id = field_id;
  this.max_length = max_length;
}
MaxLengthFieldRule.prototype = 
{
  check: function(error_list)
  {
    var field = jQuery('#' + this.field_id);
    if(field.val().length > this.max_length)
    {
      error_list[error_list.length] = i18n['max_length'].replace('%field', field.attr('title')).replace('%length', this.max_length);
      return false;
    }
    return true;
  }
};

