
var takenDays = { 1328569200:'', 1328655600:'', 1328742000:'', 1328828400:'', 1328914800:'', 1329001200:'', 1329087600:'', 1329174000:'', 1329260400:'', 1329346800:'', 1329433200:'', 1329519600:'', 1329606000:'', 1329692400:'', 1329778800:'', 1329865200:'', 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:'', 1332716400:'', 1332802800:'', 1332889200:'', 1332975600:'bezet', 1333062000:'', 1333148400:'', 1333234800:'', 1333321200:'', 1333407600:'', 1333494000:'', 1333580400:'', 1333666800:'', 1333753200:'', 1333839600:'', 1333926000:'', 1334012400:'', 1334098800:'', 1334185200:'', 1334271600:'', 1334358000:'', 1334444400:'', 1334530800:'', 1334617200:'', 1334703600:'', 1334790000:'', 1334876400:'', 1334962800:'', 1335049200:'', 1335135600:'', 1335222000:'', 1335308400:'', 1335394800:'', 1335481200:'', 1335567600:'', 1335654000:'', 1335740400:'', 1335826800:'', 1335913200:'', 1335999600:'', 1336086000:'', 1336172400:'', 1336258800:'', 1336345200:'', 1336431600:'', 1336518000:'', 1336604400:'', 1336690800:'', 1336777200:'', 1336863600:'', 1336950000:'', 1337036400:'', 1337122800:'', 1337209200:'', 1337295600:'', 1337382000:'', 1337468400:'', 1337554800:'', 1337641200:'', 1337727600:'', 1337814000:'', 1337900400:'', 1337986800:'', 1338073200:'', 1338159600:'', 1338246000:'', 1338332400:'', 1338418800:'', 1338505200:'', 1338591600:'', 1338678000:'', 1338764400:'', 1338850800:'', 1338937200:'', 1339023600:'', 1339110000:'', 1339196400:'', 1339282800:'', 1339369200:'', 1339455600:'bezet'};

$(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();
                           }
                        }
                     }
                  }
               },
					messages: {
						aantal_personen: {
							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();
	});
	
	$("#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(), 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(),
							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>Aanvraag succesvol</h2>Uw aanvraag is succesvol verwerkt. Ter bevestiging ontvangt u een email met de door u ingevulde gegevens.");
						$("div.agenda_info>div.body").html("");
						
						setTimeout("location.reload(true);",5000);
					}
				}
			});
		}
		
	});

});

