(function($) {
  
  $(function() {
    var name = $( "#name" ),
      telephone = $( "#telephone" ),
      allFields = $( [] ).add( name ).add( telephone ),
      tips = $( ".validateTips" );

    function updateTips( t ) {
      tips.text( t ).addClass( "ui-state-highlight" );
      setTimeout(function() {
        tips.removeClass( "ui-state-highlight", 1500 );
      }, 500 );
    }
    function checkLength( o, n, min, max ) {
      if ( o.val().length > max || o.val().length < min ) {
        o.addClass( "ui-state-error" );
        updateTips( n + " is mandatory field. Please fill that field" );
        return false;
      } else {
        return true;
      }
    }
    $("#book-form").dialog({
      autoOpen: false,
      height: 260,
      width: 350,
      modal: true,
      buttons: {
        Submit: function() {
          var bValid = true;
          bValid = bValid && checkLength( name, "Name", 3, 16 );
          bValid = bValid && checkLength( telephone, "Telephone", 6, 80 );
          allFields.removeClass( "ui-state-error" );
          if ( bValid ) {
            $("form").submit();
          }
        }
      }
    });

    $(".book-demo").click( function(){
      $("#book-form").dialog( "open" );
    });
  });
})(jQuery);
