ns('Carib.Order.Validation');

/**
 * Errors container
 * 
 * @version $Id: errors_container.js 544 2009-09-09 15:33:39Z  $
 */
Carib.Order.Validation.ErrorsContainer = function() {
  this.errors = {};
  this.is_valid = true;
}

Carib.Order.Validation.ErrorsContainer.prototype = {
  reset: function() {
    this.reset = {};
    this.is_valid = true;
  },
  
  add: function(field, msg) {
    if(!this.errors[field])
      this.errors[field] = [];
      
    this.errors[field].push(msg);
    this.is_valid = false;
  },
  
  isValid: function() {
    return this.is_valid;
  },
  
  hasError: function(field) {
    return this.errors[field] != undefined;
  },
  
  get: function(field) {
    return this.errors[field];
  },
  
  getFirst: function(field) {
    if(!this.hasError(field))
      return undefined;
      
    return this.errors[field][0];
  },
  
  each: function(func, scope) {
    if(scope)
      func = func.createDelegate(scope);
      
    $.each(this.errors, func);
  },
  
  eachByMsg: function(func, scope) {
    if(scope)
      func = func.createDelegate(scope);
      
    this.each(function(name, messages) {
      for(var i = 0, l = messages.length; i < l; i ++) {
        func(name, messages[i]);
      }
    });
  },
  
  getAllMessages: function() {
    var messages = [];
    this.eachByMsg(function(name, message) {
      messages.push(message);
    });
    return messages;
  }
}

