var Carbonfrost = window.Carbonfrost || {};

Carbonfrost.StandardForms = {
  getLabelForEdit: function(elementId) {
    return $('label[for=' + elementId + ']:not(label[generated])');
  },
  applyStandardValidation: function(options, element) {
    $(element).validate(
      jQuery.extend(true, 
                   {}, 
                   Carbonfrost.StandardForms.standardValidationSettings, 
                   Carbonfrost.StandardForms.baseValidationSettings, 
                   options));
  },
  applyModalValidation: function(options, element) {
    $(element).validate(
      jQuery.extend(true, 
                   {}, 
                   Carbonfrost.StandardForms.standardValidationSettings, 
                   Carbonfrost.StandardForms.baseValidationSettings, 
                   options));
  },  
  baseValidationSettings: {
      onfocusout: false,
      onkeyup: false,
      onclick: false,
      onsubmit: true,
      highlight: function(element, errorClass) {
        Carbonfrost.StandardForms.getLabelForEdit(element.id).addClass("highlighted-field");
      },
      unhighlight: function(element, errorClass) {
        Carbonfrost.StandardForms.getLabelForEdit(element.id).removeClass("highlighted-field");
        Carbonfrost.StandardForms.getLabelForEdit(element.id).attr("title", "");
      },
      wrapper: "li",
      errorElement: "a"
  },
  placeError: function(target, label, element) {
      $(target).append(label);
      var anchor = label.children("a");
      anchor.attr("href", "javascript:;");
      anchor.click(function() {
        $("#" + $(this).attr("htmlfor")).focus();
      });
      this.getLabelForEdit(element.attr("id")).attr("title", anchor.text());
  },
  standardValidationSettings: {
      invalidHandler: function(form, validator) {
        $("#errors").slideDown();
      },
      errorPlacement: function(label, element) {
        Carbonfrost.StandardForms.placeError("#errors", label, element);
      }
  },
  modalValidationSettings: {
      invalidHandler: function(form, validator) {
        $("#modalErrors").slideDown();
      },
      errorPlacement: function(label, element) {
        Carbonfrost.StandardForms.placeError("#modalErrors", label, element);
      }
  }
};

$(document).ready(function() {

  $(".reset-form-link").click(function() {
    $(this).closest("form")[0].reset();
  });

});