var ReservationForm = {
	init : function(){

		/* initial setup */		
		var obj = this;
		var curr_step = 0;
		var trailer_checked = jQuery("#trailer").attr("checked");
		var car_checked = (jQuery("#car_type").val() == "") ? false : true;
		var back_checked = jQuery("#fSave").attr("checked");
		var open_checked = jQuery("#fOpen").attr("checked");
		
		
		jQuery("#f_step0").show();
		jQuery("#f_step1").hide();
		jQuery("#f_step2").hide();
		jQuery("#f_step3").hide();
		jQuery("#f_step4").hide();
		jQuery("#thx").hide();
		
		if (back_checked) {
			jQuery("#fBack").show();
		}
		else {
			jQuery("#fBack").hide();
			jQuery("#fBack select").hide();
		}
		
		if (open_checked) jQuery(".backDate").hide();
		else jQuery(".backDate").show();
		
		if (car_checked)
			jQuery("#travel_with_car").show();
		else
			jQuery("#travel_with_car").hide();
		
		if (trailer_checked)
			jQuery("#trailer_attached").show();
		else
			jQuery("#trailer_attached").hide();
		
		/* step-change actions */
		jQuery(".step0").click(function(){
			jQuery("#reservation_step").html("Krok 1 z 4");jQuery("#f_step0").hide();jQuery("#f_step1").show();jQuery("#f_step2").hide();jQuery("#f_step3").hide();jQuery("#f_step4").hide();
			curr_step = 1;
		});
		
		jQuery(".step1").click(function(){
			if (obj.validateStep(curr_step)) {
				jQuery("#reservation_step").html("Krok  1 z 4");jQuery("#f_step1").show();jQuery("#f_step2").hide();jQuery("#f_step3").hide();jQuery("#f_step4").hide();
				curr_step = 1;
			}
		});
		jQuery(".step2").click(function(){
			if (obj.validateStep(curr_step)) {
				jQuery("#reservation_step").html("Krok 2 z 4");jQuery("#f_step1").hide();jQuery("#f_step2").show();jQuery("#f_step3").hide();jQuery("#f_step4").hide();
				curr_step = 2;
			}
		});
		jQuery(".step3").click(function(){
			if (obj.validateStep(curr_step)) {
				jQuery("#reservation_step").html("Krok 3 z 4");jQuery("#f_step1").hide();jQuery("#f_step2").hide();jQuery("#f_step3").show();jQuery("#f_step4").hide();
				curr_step = 3;
			}
		});
		jQuery(".step4").click(function(){
			if (obj.validateStep(curr_step)) {
				jQuery("#reservation_step").html("Krok 4 z 4 - podsumowanie");jQuery("#required_info").hide();jQuery("#f_step1").hide();jQuery("#f_step2").hide();jQuery("#f_step3").hide();
				obj.fillSummary();
				jQuery("#f_step4").show();
				curr_step = 4;
			}
		});
		/*jQuery(".send").click(function() {
			// IE <=6
			if (window.ActiveXObject) {
				jQuery("#reservation_step").html("Dziękujemy");
				jQuery("#f_step4").hide();
				jQuery("#thx").show();
			}
			jQuery("form[name='reservation']").submit();
		});*/

		/* step actions */
		jQuery("a.increase").click(function(){
			inp = jQuery(this).prev().prev();
			if (inp.val() < 10)
				inp.val(parseInt(inp.val())+1);
		});
		jQuery("a.decrease").click(function(){
			inp = jQuery(this).prev();
			if (inp.val() > 0)
				inp.val(parseInt(inp.val())-1);
		});
		jQuery("td.room a").click(function(){
			jQuery(this).parent().next().next().children("input").val(0);
		});
		jQuery("td.people a").click(function(){
			jQuery(this).parent().prev().prev().children("input").val(0);
		});
		jQuery("#car_type").change(function(){
			if (jQuery(this).val() != "") {
					jQuery("#travel_with_car").show();
					jQuery("#car_number").focus();
				}
			else 
				jQuery("#travel_with_car").hide();
		});
		jQuery("#trailer").click(function(){
			if (!trailer_checked)
				jQuery("#trailer_attached").show();
			else 
				jQuery("#trailer_attached").hide();
			trailer_checked = !trailer_checked;
			jQuery("#trailer_number").focus();
		});
		
	},
	
	fillSummary : function() {

		jQuery("#sum_cruise_line span").next().html((jQuery("#cruise_pl-se").attr("checked")) ? "Świnoujście - Ystad" : "Ystad - Świnoujście");
		var open = "";
		var backDate = "";
		
		startDate = jQuery("#cruise_start_month").val().substr(0, 4) + "-" + jQuery("#cruise_start_month").val().substr(4, 2) + "-" + ((jQuery("#cruise_start_day").val() < 10) ? "0" + jQuery("#cruise_start_day").val() : jQuery("#cruise_start_day").val()) + " (m/f " + jQuery("#cruise_start_ferry").val().toUpperCase() + ")";
		jQuery("#sum_cruise_start_date span").next().html(startDate);
		
		if (jQuery("#fSave").attr("checked")) {
			if (jQuery("#fOpen").attr("checked")) {
				open = "Tak";
				backDate = "---";
			}
			else {
				open = "Nie";
				backDate = jQuery("#cruise_back_month").val().substr(0, 4) + "-" + jQuery("#cruise_back_month").val().substr(4, 2) + "-" + ((jQuery("#cruise_back_day").val() < 10) ? "0" + jQuery("#cruise_back_day").val() : jQuery("#cruise_back_day").val()) + " (m/f " + jQuery("#cruise_back_ferry").val().toUpperCase() + ")";
			}
		}
		else {
			open = "Nie";
			backDate = "---";
		}
		
		jQuery("#sum_cruise_back_date span").next().html(backDate);
		jQuery("#sum_open span").next().html(open);
		
		if (jQuery("#car_type").val() != "") {
			car_type = jQuery("#car_type").val();
			car_number = jQuery("#car_number").val();
		}
		else {
			car_type = "Nie";
			car_number = "---";
		}
		jQuery("#sum_car_type span").next().html(car_type);
		jQuery("#sum_car_number span").next().html(car_number);
		
		jQuery("#sum_trailer_number span").next().html((jQuery("#trailer_number").val() == "") ? "---" : jQuery("#trailer_number").val());
		
		jQuery("#sum_discount_retired span").next().html(jQuery("#discount_retired").val());
		jQuery("#sum_discount_students span").next().html(jQuery("#discount_students").val());
		jQuery("#sum_discount_children span").next().html(jQuery("#discount_children").val());
		jQuery("#sum_discount_youth span").next().html(jQuery("#discount_youth").val());
		
		jQuery("#sum_comment").html(jQuery("#comment").val());
		
		jQuery("#sum_contact_name span").next().html(jQuery("#contact_name").val());
		jQuery("#sum_contact_phone span").next().html(jQuery("#contact_phone").val());
		jQuery("#sum_contact_fax span").next().html(jQuery("#contact_fax").val());
		jQuery("#sum_contact_mail span").next().html(jQuery("#contact_mail").val());
		
		offArr = new Array("cab_off_2p_win", "cab_off_2p_nowin", "cab_off_4p_win", "cab_off_4p_nowin", "cab_off_lux", "cab_off_4p_nobath");
		for (var i = 0; i < offArr.length; i++) {
			cabins = jQuery("input[name='" + offArr[i] + "']");
			persons = jQuery("input[name='" + offArr[i] + "_p']");

			jQuery("#sum_" + offArr[i]).show();
			if (cabins.val() > persons.val())
				jQuery("#sum_" + offArr[i] + " span").next().html(cabins.val() + " (całość)");
			else if (cabins.val() < persons.val())
				jQuery("#sum_" + offArr[i] + " span").next().html(persons.val() + " (miejsc)");
			else
				jQuery("#sum_" + offArr[i]).hide();
		} 
		
		arrArr = new Array("cab_arr_2p_win", "cab_arr_2p_nowin", "cab_arr_4p_win", "cab_arr_4p_nowin", "cab_arr_lux", "cab_arr_4p_nobath");
		for (var i = 0; i < arrArr.length; i++) {
			cabins = jQuery("input[name='" + arrArr[i] + "']");
			persons = jQuery("input[name='" + arrArr[i] + "_p']");

			jQuery("#sum_" + arrArr[i]).show();
			if (cabins.val() > persons.val())
				jQuery("#sum_" + arrArr[i] + " span").next().html(cabins.val() + " (całość)");
			else if (cabins.val() < persons.val())
				jQuery("#sum_" + arrArr[i] + " span").next().html(persons.val() + " (miejsc)");
			else
				jQuery("#sum_" + arrArr[i]).hide();
		} 
		
		for (var i = 1; i <= 5; i++) {
			name = jQuery("input[name='passanger" + i + "_name']").val();
			sex = jQuery("select[name='passanger" + i + "_sex']").val();
			birth = jQuery("input[name='passanger" + i + "_birth']").val();
			jQuery("#sum_passanger" + i).show();
			if (name != "")
				jQuery("#sum_passanger" + i).html(name + " " + birth + " " + sex);
			else
				jQuery("#sum_passanger" + i).hide();
		} 
	},
	
	validateStep : function (step) {
		validated = true;
		section = null;
		
		if (step == 2)
			section = jQuery("#f_step2");
		else if (step == 3)
			section = jQuery("#f_step3");
			
		if (section != null)
			section.find(".required").each(function (i) {
		        if (this.value == "") {
		          jQuery("#" + this.id).addClass("red");
		          validated = false;
		        }
		        else
		        	jQuery("#" + this.id).removeClass("red");
		    });
		
		if (!validated) {
			alert("Wypełnij wymagane pola");
			return false;
		}
		else {
			if (step == 3 && !checkEmail(jQuery("#contact_mail").val())) {
		    	jQuery("#contact_mail").addClass("red");
		    	alert("Podaj poprawny adres e-mail");
		    	return false;
		    }
		    else
		    	jQuery("#contact_mail").removeClass("red");
		}
		
		return true;
	}
}

var ReservationCalendar = {
	init : function(selectedDates){

		/* initial setup */	
		var obj = this;
		obj.getMonthsList(jQuery("#cruise_start_month"), selectedDates["cruise_start_month"]);
		obj.getMonthsList(jQuery("#cruise_back_month"), selectedDates["cruise_back_month"]);
		obj.getDaysList(jQuery("#cruise_start_day"), selectedDates["cruise_start_month"], selectedDates["cruise_start_day"]);
		obj.getDaysList(jQuery("#cruise_back_day"), selectedDates["cruise_back_month"], selectedDates["cruise_back_day"]);
		obj.setFerryHours(jQuery("input[name='cruise_line']:checked").val());
		obj.toogleCabinsTable(jQuery("#fSave").attr("checked"));

		jQuery("#fSave").click(function() {
			obj.toogleCabinsTable(jQuery(this).attr("checked"));
		});

		jQuery("input[name='cruise_line']").click(function() {
			obj.setFerryHours(jQuery(this).val());
		});
		
		jQuery("#cruise_start_ferry").change(function() {
			obj.toogleCabinAvailability("start", jQuery(this).val());
		});
		
		jQuery("#cruise_back_ferry").change(function() {
			obj.toogleCabinAvailability("back", jQuery(this).val());
		});

		jQuery("#cruise_start_day").change(function(){
			obj.setCruiseEndDate(obj);
		});
		
		jQuery("#cruise_start_month").change(function() {
			obj.getDaysList(jQuery("#cruise_start_day"), jQuery("#cruise_start_month").val(), jQuery("#cruise_start_day").val());
			obj.setCruiseEndDate(obj);
		});
		
		jQuery("#cruise_back_day").change(function(){
			obj.validateCruiseDates(obj);
		});
		
		jQuery("#cruise_back_month").change(function(){
			obj.getDaysList(jQuery("#cruise_back_day"), jQuery("#cruise_back_month").val(), jQuery("#cruise_back_day").val());
			obj.validateCruiseDates(obj);
		});
		
		return true;
	},
	
	toogleCabinsTable: function(fSave) {
		if (fSave)
			jQuery("#cabins_arr").show();
		else
			jQuery("#cabins_arr").hide();
	},
	
	setFerryHours: function(cruiseLine) {
		
		var pl_se_polonia = "<option value='polonia'>13:00 (m/f Polonia)</option>";
		var pl_se_skania = "<option value='skania'>23:00 (m/f Skania)</option>";
		var se_pl_polonia = "<option value='polonia'>22:00 (m/f Polonia)</option>";
		var se_pl_skania = "<option value='skania'>13:30 (m/f Skania)</option>";

		if (cruiseLine == "pl-se") {
			jQuery("#cruise_start_ferry").empty().append(pl_se_polonia).append(pl_se_skania);
			jQuery("#cruise_back_ferry").empty().append(se_pl_skania).append(se_pl_polonia);
			this.toogleCabinAvailability("start", "polonia");
			this.toogleCabinAvailability("back", "skania");
		}
		else {
			jQuery("#cruise_start_ferry").empty().append(se_pl_skania).append(se_pl_polonia);
			jQuery("#cruise_back_ferry").empty().append(pl_se_polonia).append(pl_se_skania);
			this.toogleCabinAvailability("start", "skania");
			this.toogleCabinAvailability("back", "polonia");
		}
	},
	
	toogleCabinAvailability: function(direction, ferry) {
		
		if (direction == "start")
			if (ferry == "skania") {
				jQuery("#c2in_off").hide();
				jQuery("#c4wc_off").hide();
			}
			else {
				jQuery("#c2in_off").show();
				jQuery("#c4wc_off").show();
			}
		
		else if (direction == "back")
			if (ferry == "skania") {
				jQuery("#c2in_arr").hide();
				jQuery("#c4wc_arr").hide();
			}
			else {
				jQuery("#c2in_arr").show();
				jQuery("#c4wc_arr").show();
			}
	},
	
	setCruiseEndDate: function(obj) {
		startMonth = parseInt(jQuery("#cruise_start_month").val());
		startDay = parseInt(jQuery("#cruise_start_day").val());
		backMonth = parseInt(jQuery("#cruise_back_month").val());
		backDay = parseInt(jQuery("#cruise_back_day").val());
		
		if (backMonth < startMonth)
			obj.getMonthsList(jQuery("#cruise_back_month"), startMonth, startMonth);
		if (backDay < startDay && 
			backMonth <= startMonth)
			obj.getDaysList(jQuery("#cruise_back_day"), jQuery("#cruise_back_month").val(), jQuery("#cruise_start_day").val());
	},
	
	validateCruiseDates: function(obj) {
		startMonth = parseInt(jQuery("#cruise_start_month").val());
		startDay = parseInt(jQuery("#cruise_start_day").val());
		backMonth = parseInt(jQuery("#cruise_back_month").val());
		backDay = parseInt(jQuery("#cruise_back_day").val());

		if (backMonth < startMonth) {
			alert("Data powrotu musi być późniejsza niż wypłynięcia.");
			obj.getMonthsList(jQuery("#cruise_back_month"), jQuery("#cruise_start_month").val(), jQuery("#cruise_start_month").val());
		}
		else if (backDay < startDay && backMonth == startMonth) {
			alert("Data powrotu musi być późniejsza niż wypłynięcia.");
			obj.getDaysList(jQuery("#cruise_back_day"), jQuery("#cruise_back_month").val(), jQuery("#cruise_start_day").val());
		} 
	},
	
	getMonthsList: function(targetObj, selected) {
		jQuery.getJSON("scripts/reservation.php?action=getMonthsList", function(data){
			if (data) {
				targetObj.empty();
				for (i=0; i<data.months.length; i++) {
					selStr = (selected == data.months[i].value) ? "selected=selected" : "";
					targetObj.append("<option " + selStr + " value='" + data.months[i].value + "'>" + data.months[i].name + "</option>");
				}
			}	
		});
	},
	
	getDaysList: function(targetObj, monthId, selected) {
		jQuery.getJSON("scripts/reservation.php?action=getDaysList&monthId=" + monthId, function(data){
			if (data) {
				targetObj.empty();
				for (i=0; i<data.days.length; i++) {
					selStr = (selected == data.days[i].value) ? "selected=selected" : "";
					targetObj.append("<option " + selStr + " value='" + data.days[i].value + "'>" + data.days[i].value + "</option>");
				}
			}	
		});
	}
}

