$(function () {
  $("#postcode").predictOzState($("#state"));
  $("#currentInsurer").change(function () {
    $("#otherInsurerSet").toggle(this.value == "OTHER");
    $("#otherInsurer")[0].validated = false; //reset validation
  }).change();

  $("#otherInsurer").addValidation(function () {
    if (!(this.valid = !(this.form["currentInsurer"].value == "OTHER" && this.value == ""))) {
      this.error = "Value not entered";
    }
  });

  $("#publicLiability").change(function () {
    if (!$(this).prop("checked")) {
      $("#associationLiability").prop("checked", false);
      $("#associationLiability").validate();
    }
  });
  $("#associationLiability").change(function () {
    if ($(this).prop("checked")) {
      $("#publicLiability").prop("checked", true);
    }
  });
  $("#publicLiability,#associationLiability,#paVolunteers,#festival").addValidation(function () {
    if ($("#publicLiability")[0].valid = $("#publicLiability").prop("checked") || $("#associationLiability").prop("checked") || $("#paVolunteers").prop("checked") || $("#festival").prop("checked")) {
      $("#noProductsError").remove();
    } else {
      if ($("#noProductsError").length == 0) {
        $("#publicLiability").parent().before("<p id=\"noProductsError\" class=\"invalid\"><em class=\"errata\">At least one product must be selected.</em></p>");
      }
    }
    this.validated = false;
  });
});
