Wego = typeof Wego == 'undefined' || !Wego ? {} : Wego;

Wego.Errors = function() {
  this.errors = {};
};

Wego.Errors.prototype.add = function(id, msg) {
  this.errors[id] = this.errors[id] || [];
  this.errors[id].push(msg);
};

Wego.Errors.prototype.each = function(callback) {
  for (var id in this.errors) {
    if (callback.call(this.errors[id], id, this.errors[id]) === false) break;
  }
};

Wego.Errors.prototype.clear = function() {
  this.errors = {};
};

Wego.Errors.prototype.on = function(id) {
  errors = this.errors[id];
  if (!errors || errors.length == 0) return null;
  return errors.length == 1 ? errors[0] : errors;
};

Wego.Errors.prototype.count = function() {
  var count = 0;
  for (var id in this.errors) {
    if (this.errors.hasOwnProperty(id)) {
      count += this.errors[id].length;
    }
  }
  return count;
};
