
function check_anzahl_personen() {

	$(".personen").show();

	$(".personen select").each(function() {
 		$(this).addClass("required");
 	});

	var anzahl = parseInt($("#anzahl").val())+1;

 	for(var i=anzahl; i<=5; i++) {
	 	$("#person" + i).hide();

	 	$("#person" + i + " select").each(function() {
	 		$(this).removeClass("required");
	 	});

	}

}

// Bei aktiver Checkbox, werden die Auswahlfelder eingeblendet
function check_depot() {

	if ($('#depot_reservierung').is(':checked')) {

		$('#depot_tage').removeAttr("disabled");
		$('#depot_personen').removeAttr("disabled");

	} else  {

		$('#depot_tage').attr("disabled", "disabled");
		$('#depot_personen').attr("disabled", "disabled");

	}

}

$(document).ready(function() {

	//fix PNG-BUG IE<=6
	$("img").ifixpng();
	$("div#header").ifixpng();
	$("div#navileft").ifixpng();
	$("div.content_rubrik_header").ifixpng();
	$("div.content_rubrik_footer").ifixpng();
	$("div.footer").ifixpng();

	$(".header_nav").hover(

		//mouseover
		function () {

			if ($(this).attr("rel")) {

				var bu_up = $(this).attr("rel");

				$(this).attr("rel", $(this).children("img").attr("src"));

				$(this).children("img").attr("src", bu_up);

				$("#" + $(this).attr("id") + "_content").show();

			}

		},

		//mouseout
		function () {

			if ($(this).attr("rel")) {

				var bu_do = $(this).attr("rel");

				$(this).attr("rel", $(this).children("img").attr("src"));

				$(this).children("img").attr("src", bu_do);

				$("#" + $(this).attr("id") + "_content").hide();

			}

		}

	);

////////////////////////////////////////////////////////////////////////

	$("#navileft img").hover(

		//mouseover
		function () {

			$(this).attr("src", $(this).parent("a").attr("rel_up"));

			$(this).ifixpng();

		},

		//mouseout
		function () {

			$(this).attr("src", $(this).parent("a").attr("rel_do"));

			$(this).ifixpng();

		}

	);

////////////////////////////////////////////////////////////////////////////



	// Um die Seiten-Navigation komplett Klickbar zu machen
	$(".header_nav").click(function() {
	window.location.href = $(this).children("div.content_rubrik").children("div.content_rubrik_footer").children("a").attr("href");
	});


    //drucken
    $("#drucken").click(function () {
    	window.print();
    });

    //bookmark
    $("#bookmark").click(function () {

		var url = $(this).attr("rel_url");
		var title = $(this).attr("rel_title");

		if (window.sidebar) { // Mozilla Firefox
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite(url, title);
		} else if(window.opera) { // Opera 7+
			var elem = document.createElement('a');
		    elem.setAttribute('href',url);
		    elem.setAttribute('title',title);
		    elem.setAttribute('rel','sidebar');
		    elem.click();
		} else {
			alert('Ihr Browser unterstützt diese Aktion leider nicht. Bitte fügen Sie diese Seite manuell zu Ihren Favoriten hinzu.');
		}

    });
	//Datum
	$("#date").datepicker({
		dateFormat: 'dd.mm.yy',
		showOn: 'both',
		buttonImage: '../style/calendar.gif',
		buttonImageOnly: true,
		minDate: 0,
		numberOfMonths: 1
	});


	//Beim Start Anzahl der Personen überprüfen
	check_anzahl_personen();

	//Anzahl der Personen bei onchange überprüfen
	$("#anzahl").change(function () {
		check_anzahl_personen();
	});


	// Captcha
	$("#captcha").click(function(){
		var random = Math.floor(Math.random()*25600);

		$("#captcha img").attr("src", "/captcha/image.php?new_captcha=true&" + random);

	});



	//Funktionsaufruf (Zeile 23) ausserhalb Query um die zu prüfen ob die Checkbox selectiert ist.
	check_depot();
	$("#depot_reservierung").change(function() {

		check_depot();



	});

	//Calculate Reservierungsformular -> zum errechne der Preise
	//werden mit $POST an die Ajax.php übergeben und weiter verarbeitet
	function calculate_preis(akt_element) {
		var person_id = akt_element.closest("tr").attr("id").replace("person", "");

		$.post("/inc/ajax.php", { dauer: $("[name=reservierung" + person_id + "]").val(), artikel: $("[name=artikel" + person_id + "]").val(), schuh: $("[name=schuh" + person_id + "]:checked").val() },
			function(data){

				$("[name=preis" + person_id + "]").val(data);

				$.post("/inc/ajax.php", { aktion: "gesamtpreis", preis1: $("[name=preis1]").val(), preis2: $("[name=preis2]").val(), preis3: $("[name=preis3]").val(), preis4: $("[name=preis4]").val(), preis5: $("[name=preis5]").val() },

					function(preis){

						var arr_preis = preis.split(";");

						$("[name=gesamt_exkl_rabatt]").val(arr_preis[0]);
						$("[name=rabatt]").val(arr_preis[1]);
						$("[name=gesamtpreis]").val(arr_preis[2]);
					}
				);

			}
		);
	}

	//beim wechseln der Radio Buttons wird neu berechnet ( Bei RadioButtons funktioniert das Event onchange nicht!!!!)
	$(".calculate").click(function() {

		calculate_preis($(this));

	});
	//beim wechseln der SelectBoxen wird der Preis neu berechnet
	$(".calculate").change(function() {

		calculate_preis($(this));

	});

	// Layer einblenden wenn Javascript inaktiv
	$("#javascript").hide();

	//Prüfung ob alle Pflichtfelder ausgefüllt sind.
	$(".validate").validate();
});
