$(function () {
  $("#value, #accValue").addServerValidation({
    path: "validation.svc/PremiumValueInRange?bike=",
    getValue: function () {
      return $("#value").val() + "&acc=" + $("#accValue").val();
    },
    handler: handleValueValidation
  });

  $("#startDate").addValidation(function () {
    var now = new Date();
    now = new Date(now.getFullYear(), now.getMonth(), now.getDate());
    var nextMonth = new Date();
    nextMonth.setMonth(nextMonth.getMonth() + 1);
    if ($("#isJLTDealer").val() != "True") {
      if (!(this.valid = !(JLTA.Date.toJsDate(this.value) < now || JLTA.Date.toJsDate(this.value) > nextMonth))) {
        this.error = "Start date must be today or up to a month into the future.";
      }
    }
  });
});

function handleValueValidation(data) {
  var form = $("#" + data.Caller)[0].form;
  if (data.Message != "") {//display error
    form["value"].valid =
    form["accValue"].valid = false;
    form["accValue"].error = data.Message;
    $("#value, #accValue").displayError();
  } else {//clear errors where appropriate
    this.validated = false;
    if (form["value"].error == "") {//error for value validation is only shown on accValue so test value before clearing
      $("#value").clearError();
    }
    $("#accValue").clearError();
  }
}

