
var takenDays = { 1329951600:'', 1330038000:'', 1330124400:'', 1330210800:'', 1330297200:'', 1330383600:'', 1330470000:'', 1330556400:'', 1330642800:'', 1330729200:'', 1330815600:'', 1330902000:'', 1330988400:'', 1331074800:'', 1331161200:'', 1331247600:'', 1331334000:'', 1331420400:'', 1331506800:'', 1331593200:'', 1331679600:'', 1331766000:'', 1331852400:'', 1331938800:'', 1332025200:'', 1332111600:'', 1332198000:'', 1332284400:'', 1332370800:'', 1332457200:'', 1332543600:'', 1332630000:'', 1332712800:'', 1332799200:'', 1332885600:'', 1332972000:'boeking', 1333058400:'', 1333144800:'', 1333231200:'', 1333317600:'', 1333404000:'', 1333490400:'', 1333576800:'', 1333663200:'', 1333749600:'', 1333836000:'', 1333922400:'boeking', 1334008800:'', 1334095200:'', 1334181600:'', 1334268000:'', 1334354400:'', 1334440800:'', 1334527200:'', 1334613600:'', 1334700000:'', 1334786400:'', 1334872800:'', 1334959200:'boeking', 1335045600:'', 1335132000:'', 1335218400:'', 1335304800:'', 1335391200:'', 1335477600:'', 1335564000:'', 1335650400:'', 1335736800:'', 1335823200:'', 1335909600:'', 1335996000:'', 1336082400:'', 1336168800:'', 1336255200:'', 1336341600:'', 1336428000:'', 1336514400:'', 1336600800:'boeking', 1336687200:'', 1336773600:'boeking', 1336860000:'', 1336946400:'', 1337032800:'', 1337119200:'', 1337205600:'boeking', 1337292000:'', 1337378400:'', 1337464800:'', 1337551200:'', 1337637600:'', 1337724000:'', 1337810400:'', 1337896800:'boeking', 1337983200:'', 1338069600:'', 1338156000:'', 1338242400:'', 1338328800:'', 1338415200:'', 1338501600:'boeking', 1338588000:'', 1338674400:'', 1338760800:'', 1338847200:'', 1338933600:'', 1339020000:'', 1339106400:'boeking', 1339192800:'', 1339279200:'', 1339365600:'', 1339452000:'', 1339538400:'', 1339624800:'', 1339711200:'boeking', 1339797600:'boeking', 1339884000:'', 1339970400:'', 1340056800:'', 1340143200:'', 1340229600:'', 1340316000:'', 1340402400:'', 1340488800:'', 1340575200:'', 1340661600:'', 1340748000:'', 1340834400:'', 1340920800:'', 1341007200:'', 1341093600:'boeking', 1341180000:'', 1341266400:'', 1341352800:'', 1341439200:'', 1341525600:'', 1341612000:'boeking', 1341698400:'', 1341784800:'', 1341871200:'', 1341957600:'', 1342044000:'', 1342130400:'', 1342216800:'', 1342303200:'', 1342389600:'', 1342476000:'', 1342562400:'', 1342648800:'', 1342735200:'', 1342821600:'', 1342908000:'', 1342994400:'', 1343080800:'', 1343167200:'', 1343253600:'', 1343340000:'', 1343426400:'', 1343512800:'', 1343599200:'', 1343685600:'', 1343772000:'', 1343858400:'', 1343944800:'', 1344031200:'', 1344117600:'', 1344204000:'', 1344290400:'', 1344376800:'', 1344463200:'', 1344549600:'', 1344636000:'boeking', 1344722400:'', 1344808800:'', 1344895200:'', 1344981600:'', 1345068000:'', 1345154400:'', 1345240800:'', 1345327200:'', 1345413600:'', 1345500000:'', 1345586400:'', 1345672800:'', 1345759200:'bezet', 1345845600:'bezet', 1345932000:'boeking', 1346018400:'', 1346104800:'', 1346191200:'', 1346277600:'', 1346364000:'', 1346450400:'', 1346536800:'', 1346623200:'', 1346709600:'', 1346796000:'', 1346882400:'', 1346968800:'', 1347055200:'', 1347141600:'', 1347228000:'', 1347314400:'', 1347400800:'', 1347487200:'', 1347573600:'', 1347660000:'boeking'};

$(function() {   
   jQuery.extend(jQuery.validator.messages, {
		required: "Verplicht veld",
		maxlength: jQuery.format("U kunt niet meer dan {0} karakters invoeren."),
		minlength: jQuery.format("U dient minimaal {0} karakters in te voeren."),
		rangelength: jQuery.format("U dient minimaal {0} en maximaal {1} karakters in te voeren."),
		email: "Een geldig emailadres is verplicht.",
		url: "Een geldig URL is verplicht.",
		date: "Een geldige datum is verplicht.",
		number: "Een geldig getal is verplicht.",
		digits: "Gebruik alleen cijfers.",
		equalTo: "Niet gelijk aan vorige invoer.",
		range: jQuery.format("U dient een waarde tussen {0} en {1} in te voeren."),
		max: jQuery.format("U dient een waarde kleiner dan of gelijk aan {0} in te voeren."),
		min: jQuery.format("U dient een waarde groter dan of gelijk aan {0} in te voeren."),
		creditcard: "Een geldig creditcardnummer is verplicht."
	});
   
   function isAvailableDate(date){
      var class_name = takenDays[(parseInt($.datepicker.formatDate('@', date)) / 1000)];
      if(class_name == "bezet"){
         return [true, class_name];  
      }else{
         return [true, class_name];  
      }           
   }
	
	$("div.agenda").datepicker({
		onSelect: function(dateText, inst){
         jQuery.ajax({
				type: "GET",
				url: "/handlers/agenda.php",
				data: ({get_boeking_list: dateText}),
				success: function(msg){
					$("div.agenda_info > div.list").html(msg);
               $("div.agenda_info>div.body").html('');
				}
			});
		},
      dateFormat: 'dd-mm-yy',
      constrainInput: true,
		beforeShowDay: isAvailableDate,
      changeMonth: true,
      changeYear: true,
		minDate: new Date(),
		maxDate: '+12m'
	});
	
   $(".repetition_select").not("." + $("#repetition").val()).hide();
 
   
   $("#repetition").change(function(){
      $(".repetition_select").hide();
      $(".repetition_select." + $("#repetition").val()).show();
   });
         
   $("ul.agenda>li>a").live('click', function(e){
      e.preventDefault();
      jQuery.ajax({
         type: "GET",
         url: "/handlers/agenda.php",
         data: ({get_boeking: $(this).attr("name"),from: $(this).attr("rel"),to: $(this).attr("rev"), period_id: $(this).attr("id")}),
         success: function(msg){
            $("div.agenda_info>div.body").html(msg);
            $('form').validate({
               rules: {
                  aantal_personen: {
                     remote: {
                        url: "handlers/agenda.php",
                        type: "GET",
                        data: {
                           aantal_personen: function() {
                              return $("input#aantal_personen").val();
                           },
                           period_check_persons: function() {
                              return $("input#period").val();
                           }
                        }
                     }
                  },
				  aantal_kinderen: {
                     remote: {
                        url: "handlers/agenda.php",
                        type: "GET",
                        data: {
                           aantal_kinderen: function() {
                              return $("input#aantal_kinderen").val();
                           },
						   aantal_personen: function() {
                              return $("input#aantal_personen").val();
                           },
                           period_check_persons: function() {
                              return $("input#period").val();
                           }
                        }
                     }
                  }
               },
					messages: {
						aantal_personen: {
							remote: "Aantal niet toegestaan"
						},
						aantal_kinderen: {
							remote: "Aantal niet toegestaan"
						}
					},
               highlight: function(element, errorClass) {
                  $(element).addClass('invalid');
               },
               unhighlight: function(element, errorClass) {
                  $(element).removeClass('invalid');
               },
               errorPlacement: function(error, element) {
                  error.appendTo(element.parent(':last'));
               }            
            });
         }
      });
      
   });
   
   $("#multiplication").live('change', function(){
      var split = $("#from").val().split("-");
      var date = new Date(split[2],split[1]-1,parseInt(split[0]) + parseInt($(this).val()));
     
      $("#to").val($.datepicker.formatDate('dd-mm-yy', date));
   });
	
	$("#aantal_personen").live('change', function(){
		get_price();
	});
	
	$("#aantal_kinderen").live('change', function(){
		get_price();
	});
	
	$("#multiplication").live('change', function(){
		get_price();
	});
	
	function get_price(){
		jQuery.ajax({
			type: "GET",
			url: "/handlers/agenda.php",
			data: ({get_price_for: $("input#period").val(), aantal_personen: $("#aantal_personen").val(), aantal_kinderen: $("#aantal_kinderen").val(),multiplication: $("#multiplication").val()}),
			success: function(msg){
				if(msg == "false"){
					$("span#prijs").parent().hide();
				}else{
					$("span#prijs").parent().show();
					$("span#prijs").html("€" + msg);
				}
			}
		});
	}

	$("input#submit_agenda").live('click', function(e){
		if($("form#agenda_form").valid()){
			e.preventDefault();
			jQuery.ajax({
				type: "POST",
				url: "/handlers/agenda.php",
				data: ({	period: $("input#period").val(),
							from: $("input#from").val(),
							from_time: $("input#from_time").val(),
							to: $("input#to").val(),
							to_time: $("input#to_time").val(),
							multiplication: $("select#multiplication").val(),
							aantal_personen: $("input#aantal_personen").val(),
							aantal_kinderen: $("input#aantal_kinderen").val(),
							aanhef: $("select#aanhef").val(),
							achternaam: $("input#achternaam").val(),
							adres: $("input#adres").val(),
							postcode: $("input#postcode").val(),
							woonplaats: $("input#woonplaats").val(),
							land: $("input#land").val(),
							telefoon: $("input#telefoon").val(),
							email: $("input#email").val(),
							info: $("textarea#info").val()
						}),
				success: function(msg){
					if(msg == "false"){
						$("h3#msg").html("Er is iets fout gegaan bij de boeking. Probeer het nogmaals of neem contact op met de beheerder.").show();
					}else{
						$("form#agenda_form").hide();																		
						$("div.agenda_info>div.list").html("<h2>Boeking succesvol</h2>Uw boeking is succesvol verwerkt. Ter bevestiging ontvangt u een email met uw boekingsgegevens.");
						$("div.agenda_info>div.body").html("");
						
						setTimeout("window.location = window.location + '?voltooid=true'",5000);
					}
				}
			});
		}
		
	});

});
