$().ready(function() {
  $('.datepicker').datepicker();
});

function form_error(form, message) {
  $(form).find('#error_message').show().html(message)
    }

// Jurisdiction Requests
$().ready(function() {
  $("form#jurisdiction_requests").validate({
    rules: {
      'content[name]': {required: true},
      'content[physical_address_of_property]': {required: true},
      'content[property_subdivision_or_cos]': {required: true},
      'content[property_legal_description]': {required: true},
      'content[contact_phone_number]': {required: true},
      'content[contact_email]': {required: true, email: true}
    },
    success: function(label) {
      label.text("Success!").addClass("success");
    },
    invalidHandler: function(form) {
      form_error(form.currentTarget, "Please fill in the required fields.")
    },
    submitHandler: function(form) {
      if($(form).valid()) {
        $.ajax({
          url: $(form).attr('action'),
          data: $(form).serializeArray(),
          type: "post",
          success: function(data) {
            if (data.errors == null) {
              $('#jurisdiction_requests').fadeOut(function(){
                $('#enquiry_thanks').fadeIn();
              })
            } else {
              form_error(form, "Sorry, something went wrong. We will look into this as soon as possible.")
            }
          },
          error: function(data) {
            form_error(form, "Sorry, something went wrong. We will look into this as soon as possible.")
          }
        })
      }
      return false;
    }
  });
});

// Covenant Compliance
$().ready(function() {
  $("form#covenant_compliance").validate({
    rules: {
      'content[name]': {required: true},
      'content[address]': {required: true},
      'content[phone]': {required: true},
      'content[email]': {required: true, email: true },
      'content[property_address]': {required: true},
      'content[property_owner]': {required: true},
      'content[property_contact]': {required: true},
      'content[description]': {required: true},
      'content[violation_date]': {required: true},
      'content[file_1]': {required: false},
      'content[file_2]': {required: false},
      'content[file_3]': {required: false}
    },
    success: function(label) {
      $(form).submit();
      label.text("Success!").addClass("success");
    },
    invalidHandler: function(form) {
      form_error(form.currentTarget, "Please fill in the required fields.")
        },
    submitHandler: function(form) {
      if($(form).valid()) {
        return true;
      }
      return false;
    }
  });
});

// Contact Requests
$().ready(function() {
  $("form#contact_requests").validate({
    rules: {
      'content[name]': {required: true},
      'content[phone]': {required: true},
      'content[email]': {required: true, email: true },
      'content[message]': {required: true}
    },
    success: function(label) {
      label.text("Success!").addClass("success");
    },
    invalidHandler: function(form) {
      form_error(form.currentTarget, "Please fill in the required fields.")
    },
    submitHandler: function(form) {
      if($(form).valid()) {
        $.ajax({
          url: $(form).attr('action'),
          data: $(form).serializeArray(),
          type: "post",
          success: function(data) {
            if (data.errors == null) {
              $('#contact_requests').fadeOut(function(){
                $('#enquiry_thanks').fadeIn();
              })
            } else {
              form_error(form, "Sorry, something went wrong. We will look into this as soon as possible.")
            }
          },
          error: function(data) {
            form_error(form, "Sorry, something went wrong. We will look into this as soon as possible.")
          }
        })
      }
    }
  });
});

