﻿jQuery.validator.addMethod(
  "selectNone",
  function(value, element) {
      if (element.value == "") {
          return false;
      }
      else return true;
  },
  "Please select an option."
);

$.validator.setDefaults({
    submitHandler: function() {
        BookLesson();
    }
});

$(document).ready(function() {

$("#aspnetForm").validate({
    rules: {
            txtName: "required",
            txtEmail: {
                required: true,
                email: true
            },
            txtContact: "required",
            selectLessonType: {selectNone: true}
        }
	});
});

function BookLesson() {
    $.blockUI({ message: "<img src='images/ajax-loader.gif' />", css: { width: '300px', padding: '15px'} });
    var KiteboardingLesson = new Object();
    KiteboardingLesson.Name = $("#txtName").val();
    KiteboardingLesson.Email = $("#txtEmail").val();
    KiteboardingLesson.Contact = $("#txtContact").val();
    KiteboardingLesson.LessonType = $("#selectLessonType").val();

    //add record
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "School.aspx/BookLesson",
        data: "{'Lesson':" + JSON.stringify(KiteboardingLesson) + "}",
        dataType: "json",
        success: function(msg) {
            $.unblockUI();
            $.blockUI({ message: msg.d, css: { width: '300px', padding: '15px' } });
        },
        complete: function() {
            $('.blockOverlay').attr('title', 'Click outside of window to close').click($.unblockUI);
        }
    });
}
