
function update_nights()
{
	if($("#id_arrival_date").val()!="" && $("#id_departure_date").val()!="" )
	{
		var d1 = Date.parseDate($("#id_arrival_date").val(), "%Y-%m-%d");
		var d2 = Date.parseDate($("#id_departure_date").val(), "%Y-%m-%d");
//		alert($("#id_arrival_date").val());
//		alert($("#id_departure_date").val());
		days =  parseInt((d2 - d1)/Date.DAY);
		if (days<0)days=0;
		$("#jq_total_nights").html(days);
		$("#id_total_nights").val(days);
	}
	update_price();
}

function update_nights2()
{
	if($("#id_arrival_date2").val()!="" && $("#id_departure_date2").val()!="" )
	{
		var d1 = Date.parseDate($("#id_arrival_date2").val(), "%Y-%m-%d");
		var d2 = Date.parseDate($("#id_departure_date2").val(), "%Y-%m-%d");
		days =  parseInt((d2 - d1)/Date.DAY);
		if (days<0)days=0;
		$("#jq_total_nights2").html(days);
		$("#id_total_nights2").val(days);
	}
	update_price();
}

function update_price()
{
	html=""
	var hform=Array('arrival_date', 'departure_date', 'arrival_date2', 'departure_date2','guests','guests2',
	                  'extra_room', 'room_type', 'room_type2', 'visa_support_count','use_transfer','transfer',
	                  'extra_bed','extra_bed2','is_offers', 'offer');
		
	for(var i=0;i<hform.length;i++)
	{
		switch($("#id_"+hform[i]).attr('type'))
		{
		case "text":
		case "select-one":
		case "hidden":			
			html+="<input type='hidden' name='"+hform[i]+"' value='"+$("#id_"+hform[i]).val()+"'/>";
			break;
		case "checkbox":
			if($("#id_"+hform[i]+":checked").length>0)
			{
				html+="<input type='hidden' name='"+hform[i]+"' value='"+$("#id_"+hform[i]).val()+"'/>";
			}
			break;
		default:
			if($('input[name='+hform[i]+']:checked').val()!=undefined){
				html+="<input type='hidden' name='"+hform[i]+"' value='"+$('input[name='+hform[i]+']:checked').val()+"'/>"
			}
		}
	}
	$("#jq_booking_ajax_form").html(html);
	$("#jq_booking_ajax_form").submit();
}

SELECTED2 = '<option value="1" selected="selected">1</option><option value="2">2</option>';
SELECTED4 = '<option value="1" selected="selected">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option>';

function log(str)
{
//	$("#jq_booking_form").prepend(str+"<br/>");
}

function init9(name)
{
	$("#id2_"+name).datepicker('option', 'altField', "#id_"+name);

	$("#days_"+name+", #month_"+name).change(function (){
//		log("#id_"+name+"="+$("#month_"+name).val()+"-"+$("#days_"+name).val());
		$("#id_"+name).val($("#month_"+name).val()+"-"+$("#days_"+name).val());
		var d1 = Date.parseDate($("#id_"+name).val(), "%Y-%m-%d");
		$("#id2_"+name).val($.datepicker.formatDate('mm/dd/yy', d1));
		update_nights();
		update_nights2();
		update_price();
	});
}

function set_date(name, date)
{
	var days = $("#days_"+name);
	var month = $("#month_"+name);
	
	month.val(date.substring(6, 10)+"-"+date.substring(0, 2));
	days.val(date.substring(3, 5));
	
	$("#id_"+name).val(date.substring(6, 10)+"-"+date.substring(0, 2) + "-" + date.substring(3, 5));
}

$(document).ready(function()
{
	var today = new Date();
	var max = new Date();
	max.goto(365*2);
	if(!$("#id_arrival_date").val())
	{
		//init
		$("#id_arrival_date").val($.datepicker.formatDate('yy-mm-dd', today));
		var today2 = new Date();
		today2.goto(2);
		$("#id_departure_date").val($.datepicker.formatDate('yy-mm-dd', today2));
	}
	
	fill_calendar_ex("arrival_date");
	fill_calendar_ex("departure_date");
	fill_calendar_ex("arrival_date2");
	fill_calendar_ex("departure_date2");
	
	$("#jq_booking_ajax_form").ajaxForm(function(html){
		eval("ret="+html);
		$("#jq_total_cost").html(ret['total']);
		$("#jq_total_accomodation").html(ret['total1']);
		$("#jq_total_add_services").html(ret['total2']);

		$("#id_total_cost").val(ret['total']);
		$("#id_total_accomodation").val(ret['total1']);
		$("#id_total_add_services").val(ret['total2']);
	});	
//	

	
	init9("arrival_date");
	init9("departure_date");
	init9("arrival_date2");
	init9("departure_date2");
	
	$("#id2_arrival_date, #id2_arrival_date2, #id2_departure_date, #id2_departure_date2").datepicker({
	minDate: today, maxDate: max, 
	showOn: 'button', buttonImageOnly: true, buttonImage: '/media/img/ico-callendare.gif',
	duration:0, firstDay:1,
	showOptions:{direction: 'left' },
	altFormat:Calendar.DATE_FORMAT2,
	onSelect:function(date, ins){
		switch($(ins).attr('id'))
		{
		case "id2_arrival_date":
			set_date('arrival_date', date)
			update_nights();
			update_price();
			break;
		case "id2_departure_date":
			set_date('departure_date', date)
			update_nights();
			update_price();
			break;
		case "id2_arrival_date2":
			set_date("arrival_date2", date)
			update_nights2();
			update_price();
			break;
		case "id2_departure_date2":
			set_date("departure_date2", date)
			update_nights2();
			update_price();
			break;
		}		
	}
	});
	setTimeout(function(){			
		set_date('arrival_date2',$("#id2_arrival_date").val());
		set_date('departure_date2',$("#id2_departure_date").val());
		update_nights2();
		return {};  
	}, 10);  //HACK =) For IE6 =)))	


	$("#id_room_type").change(function(){
		switch (parseInt($("#id_room_type").val()))
		{
		//Standart
		case 1: 
			$("#id_guests").html(SELECTED2);
			$("#id_extra_bed").attr("disabled","disabled");
			$("#id_extra_bed option:contains(----)").attr("selected", "selected")
			break;
		//Deluxe
		case 2: 
			$("#id_guests").html(SELECTED2);
			$("#id_extra_bed").removeAttr("disabled");
			break;
		//Apartament
		case 3: 
			$("#id_guests").html(SELECTED4);
			$("#id_extra_bed").attr("disabled","disabled");
			$("#id_extra_bed option:contains(----)").attr("selected", "selected")

			break;
		}
		update_price();
	}).change();
	
	
	
	$("#id_room_type2").change(function(){
		switch (parseInt($("#id_room_type2").val()))
		{
		//Standart
		case 1: 
			$("#id_guests2").html(SELECTED2);
			$("#id_extra_bed2").val("");
			$("#id_extra_bed2").attr("disabled","disabled");
			$("#id_extra_bed2 option:contains(----)").attr("selected", "selected")
			break;
		//Deluxe
		case 2: 
			$("#id_guests2").html(SELECTED2);
			$("#id_extra_bed2").removeAttr("disabled");
			break;
		//Apartament
		case 3: 
			$("#id_guests2").html(SELECTED4);
			$("#id_extra_bed2").val("");
			$("#id_extra_bed2").attr("disabled","disabled");
			$("#id_extra_bed2 option:contains(----)").attr("selected", "selected")
			break;
		}
		update_price();
	}).change();
	$("#jq_extra_room_form").hide();
	$("#id_extra_room").click(function(){
		$("#jq_extra_room_form").toggle();
		update_price();
		if($("#id_extra_room:checked").length)
		{
			$("#id_arrival_date2").val($("#id_arrival_date").val());
			$("#id_departure_date2").val($("#id_departure_date").val());
			set_date('arrival_date2',$("#id2_arrival_date").val());
			set_date('departure_date2',$("#id2_departure_date").val());			
		}
	});
	if($("#id_extra_room:checked").length>0)
	{
		$("#jq_extra_room_form").show();
	}
	
	$(".jq_info").mouseover(function(){
		$(".jq_offer_info strong.jq_show").hide();
		$("strong.jq_show",$(this)).show();
	});
	$(".jq_info").mouseout(function(){
		$(".jq_offer_info strong.jq_show").hide();
		$("strong.jq_show",this).hide();
	});
	$(".jq_info strong.jq_show").hide();
	
	$("#id_use_transfer").click(function (){
		$("#jq_transfer").toggle();
	});
	if($("#id_use_transfer:checked").length>0)
	{
		$("#jq_transfer").show();
	}else{
		$("#jq_transfer").hide();
	}
	
	$("#id_visa_support_count").change(update_price);
	
	$("#id_guests").change(update_price);
	$("#id_guests2").change(update_price);
	$("#id_extra_bed").change(update_price);
	$("#id_extra_bed2").change(update_price);
	$("input[type=radio]").change(update_price);
	$("#id_use_transfer").change(update_price);
//	$("#jq_booking_form select").change(update_price);
	
	update_nights();
	update_nights2();
});

