// ********* jQuery *********

jQuery.noConflict();

jQuery(document).ready(function(){

	jQuery(function() {
		var dates = jQuery( "#book_arrival, #book_departure" ).datepicker({
			dateFormat: "dd.mm.yy",
			showOn: "button",
			buttonImage: "/typo3conf/ext/flm_minibuchung/res/calendar.png",
			buttonImageOnly: true,
			defaultDate: "+1d",
			changeMonth: true,
			numberOfMonths: 1,
			onSelect: function( selectedDate ) {
				var option = this.id == "book_arrival" ? "minDate" : "maxDate",
					instance = jQuery( this ).data( "datepicker" ),
					date = jQuery.datepicker.parseDate(
						instance.settings.dateFormat ||
						jQuery.datepicker._defaults.dateFormat,
						selectedDate, instance.settings ),
					ts = Date.parse(date);
            	date = this.id == "book_arrival" ? new Date(ts+24*60*60*1000) : new Date(ts-24*60*60*1000);
            	dates.not( this ).datepicker( "option", option, date );
			}
		});
	jQuery.datepicker.setDefaults( jQuery.datepicker.regional[ "de" ] );							       
	});

});


// ********* Prototype *********

document.observe("dom:loaded", function() {
	
	// Buchungsbutton
	$('bookingSubmit').observe('click', function () {
	
		var today = new Date();
	
		// Konfigurierte Buchungs-URL checken
		var bookURL = $('bookingForm').readAttribute('action');

		// Jugendherberge checken
		var selectedJH = $F('jhSelectBox');
		if (selectedJH == '') {
			alert ('Bitte wählen Sie eine Jugendherberge aus!');
			return false;
		}
		
		// Anreisetag checken
		var bookArrival = $F('book_arrival');
		if (bookArrival == '' || bookArrival == 'tt.mm.jjjj') {
			alert ('Bitte wählen Sie einen Anreisetag!');
			return false;
		}
		var bookArrivalStringSplit = bookArrival.split(".");
		var bookArrivalDay = bookArrivalStringSplit[0];
		var bookArrivalMonth = (bookArrivalStringSplit[1] - 1);
		var bookArrivalYear = bookArrivalStringSplit[2];
		var bookArrivalDate = new Date();
		bookArrivalDate.setFullYear(bookArrivalYear, bookArrivalMonth, bookArrivalDay);
		if (bookArrivalDate < today)
		{
			alert("Bitte wählen Sie einen korrekten Anreisetag!");
			return false;
		}

		// Abreisetag checken
		var bookDeparture = $F('book_departure');
		if (bookDeparture == '' || bookDeparture == 'tt.mm.jjjj') {
			alert ('Bitte wählen Sie einen Abreisetag!');
			return false;
		}
		var bookDepartureStringSplit = bookDeparture.split(".");
		var bookDepartureDay = bookDepartureStringSplit[0];
		var bookDepartureMonth = (bookDepartureStringSplit[1] - 1);
		var bookDepartureYear = bookDepartureStringSplit[2];
		var bookDepartureDate = new Date();
		bookDepartureDate.setFullYear(bookDepartureYear, bookDepartureMonth, bookDepartureDay);
		if (bookDepartureDate <= today )
		{
			alert("Bitte wählen Sie einen korrekten Abreisetag!");
			return false;
		}

		// Reisetyp wählen
		var bookType = $$('input:checked[type="radio"][name="type"]').pluck('value');
		if (bookType == '') {
			alert ('Bitte wählen Sie einen Reisetyp aus!');
			return false;
		}
		
		var completeBookUrl = bookURL + "&house1=" + selectedJH + "&type=" + bookType + "&da_a=" + bookArrivalDay + "&mo_a=" + bookArrivalMonth + "&ye_a=" + bookArrivalYear+ "&da_d=" + bookDepartureDay + "&mo_d=" + bookDepartureMonth + "&ye_d=" + bookDepartureYear + "&st_stay=Y";
		
		window.open(completeBookUrl); 
		
	});
	
});
