
function display_errors(errors, error_additional) {
  var rendered = new Array();
  
  $H(errors).each(function(model){
    $H(model[1]).each(function(field){
      field[1].each(function(error){
        rendered.push(render_error(model[0].toLowerCase(), field[0].toLowerCase(), error.toLowerCase()));
      });
    });
  });
  
  var msg = "";
  // display their additional errors first
  if (error_additional) {
    error_additional.each(function(err){
      msg += err + '<br />';
    });
  }
  // display our errors
  rendered.each(function(err){
    msg += err + '<br />';
  });

  display_notification(msg);
}

function render_error(model, field, error) {
  // find the error message
  var txt = Try.these(
    function(){ if (!system_errors[model][field][error]) throw 'not_found'; return system_errors[model][field][error]; },
    function(){ return system_errors[model]['default'][error]; },
    function(){ return system_errors['default']['default'][error]; }
  );

  // and return
  var fieldName = field;
  fieldName = fieldName.replace('_id', "");
  fieldName = fieldName.replace('_', " ");
  return txt.replace('{name}', fieldName.capitalize());
};


var system_errors = {
  
  'default' : {
    'default' : {
      'not_unique'  : '{name} is already in use by someone else.',
      'required'    : '{name} is a required field. You must choose or enter a value.',
      'type'        : '{name} is not valid.',
      'after_end'   : 'The end date must be after the start date.',
      'invalid_html' : 'Your message contains invalid characters (or tags). Please remove before continuing.',
      'contact_me'   : 'You must accept to be contacted by ticking the "contact me" box before continuing.',
      'invalid_email': 'Your email address is invalid, please correct and try again.'
    }
  },
  
  'comment' : {
    'body' : {
      'has_link'    : 'Sorry, you can not post comments that contain html links.'
    }
  }
  
};
