$(function() {
	
	var name = $("#name"),
		email = $("#email"),
		tel = $("#tel"),
		depart = $("#depart"),
		arrivee = $("#arrivee"),
		datetaxi = $("#datetaxi"),
		heure = $("#heure"),
		nb_personne = $("#nb_personne"),
		comment = $("#comment")
		allFields = $([]).add(name).add(email).add(tel).add(depart).add(arrivee).add(datetaxi).add(heure).add(nb_personne).add(comment),
		tips = $("#validateTips");

	function updateTips(t) {
		tips.text(t).effect("highlight",{},1500);
	}

	function checkLength(o,n,min,max) {

		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			updateTips("Le champs " + n + " est obligatoire.");
			return false;
		} else {
			return true;
		}

	}

	function checkRegexp(o,regexp,n) {

		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}

	}
	
	 $("#result_error").dialog({
		bgiframe: true,
		modal: true,
		autoOpen : false,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});
	 $("#result").dialog({
			bgiframe: true,
			modal: true,
			autoOpen : false,
			buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
			}
		});
	
   $("#dialog").dialog({
        bgiframe: true,
        autoOpen: false,
        modal: true,
        buttons: {
           'Réserver un TAXI': function() {
              var bValid = true;
              allFields.removeClass('ui-state-error');

              bValid = bValid && checkLength(name,"Nom",3,100);
              bValid = bValid && checkLength(email,"Email",6,100);
              bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Entrer un e-mail valide ex. info@hep.ch");
              bValid = bValid && checkLength(tel,"Téléphone",6,100);
              bValid = bValid && checkLength(depart,"Départ",1,100);
              bValid = bValid && checkLength(arrivee,"Arrivée",1,100);
              bValid = bValid && checkLength(datetaxi,"Date",1,100);
              bValid = bValid && checkLength(heure,"Heure",1,100);
              bValid = bValid && checkLength(nb_personne,"Nombre de personne",1,100);

              
              
              if (bValid) {
                 
                 // prepare Options Object 
                 var options = { 
                     target:     '#result_text', 
                     url:        '/public/inc/taxibook.php',
                     type:       'POST',
                     success:    function() { 
                       $('#dialog').dialog('close');
                       $('#result').dialog('open');
                     }, 
                     error: function(){
                       $('#result_text_error').html('Une erreur est survenue, appelez nous au 026 677 44 44');
                       $('#result_error').dialog('open');
                     }
                 }; 

                 // pass options to ajaxForm 
                 $('#book_online_form').ajaxSubmit(options);
                 

              }
           },
           Annuler: function() {
              $(this).dialog('close');
           }
        },
        close: function() {
           allFields.val('').removeClass('ui-state-error');
        }
     });
     
     
     
     $('#book_online').click(function() {
        $('#dialog').dialog('open');
        $('#name').focus();
//         
        
        return false;
     });

     $('#book_telephone').click(function(){
       $("body").append(
       $('<div>').attr('id', 'largeTypeAction').html($('<span>').text($(this).text())).fadeIn("fast").one('click',
       function() {
         $(this).remove();
       }));
       return false;
     })
     
     $('#datetaxi').datepicker($.datepicker.regional['fr']);




});
