$(document).ready(function() {
	$('#BOOK_date').datepicker({ duration: 'fast', dateFormat: 'dd/mm/yy', defaultDate: +2, minDate: location.href.indexOf("admin/")==-1 ? new Date() : null });
	$('#BOOK_roundtrip_date').datepicker({ duration: 'fast', dateFormat: 'dd/mm/yy', defaultDate: +2, minDate: location.href.indexOf("admin/")==-1 ? new Date() : null });
	$('#BOOK_from').change(function() {
		updateArrival();
	});
	$('#BOOK_to').change(function() {
		updatePrice();
	});
	$("#BOOK_car").before('<div id="layer"></div>');
	$("#BOOK_pick").val("aboo");
	$("#layer").click(function() {
		$("#carlist").fadeIn();
		document.onclick = function() {
			document.onclick = function() {
				$("#carlist").fadeOut();
				document.onclick = null;
			}
		}
	});
	$("#book :text, #book select").focus(function() {
		$(this).animate({ backgroundColor: "white" }, 1000);
		$("#mandatory_alert").fadeOut("slow");
	});
	$("#book").submit(function () {
		var ok=true;
		if($("#BOOK_firstname").val()=="") { $("#BOOK_firstname").animate({ backgroundColor: "pink" }, 500); ok=false; }
		if($("#BOOK_lastname").val()=="") { $("#BOOK_lastname").animate({ backgroundColor: "pink" }, 500); ok=false; }
		if($("#BOOK_phone").val()=="" || $("#BOOK_phone").val()=="+33.") { $("#BOOK_phone").animate({ backgroundColor: "pink" }, 500); ok=false; }
		if($("#BOOK_email").val()=="" || $("#BOOK_email").val().search(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i)==-1) { $("#BOOK_email").animate({ backgroundColor: "pink" }, 500); ok=false; }
		if($("#BOOK_type_full").attr("checked"))
		{
			if($("#BOOK_date").val().search("^[0-9]{2}/[0-9]{2}/[0-9]{4}$")==-1) { $("#BOOK_date").animate({ backgroundColor: "pink" }, 500); ok=false; }
			if($("#BOOK_from").attr("selectedIndex")<=0) { $("#BOOK_from").animate({ backgroundColor: "pink" }, 500); ok=false; }
			if($("#BOOK_to").attr("selectedIndex")<0) { $("#BOOK_to").animate({ backgroundColor: "pink" }, 500); ok=false; }
			if($("#BOOK_car").attr("selectedIndex")<=0) { $("#BOOK_car").animate({ backgroundColor: "pink" }, 500); ok=false; }
			if($("#BOOK_roundtrip_roundtrip").attr("checked") && $("#BOOK_roundtrip_date").val().search("^[0-9]{2}/[0-9]{2}/[0-9]{4}$")==-1) { $("#BOOK_roundtrip_date").animate({ backgroundColor: "pink" }, 500); ok=false; }
		}
		if(!ok) $("#mandatory_alert").css('display','inline');
		return ok;
	});
	$("#BOOK_type_custom").click(function() {
		if(this.checked) {
			$(".book_collapse").fadeOut("fast");
		}
	});
	$("#BOOK_type_full").click(function() {
		if(this.checked) {
			$(".book_collapse").fadeIn("fast");
		}
	});
	$("#BOOK_roundtrip_oneway").click(function() {
		if(this.checked) {
			$(".book_collapse_roundtrip").fadeOut("fast");
			updatePrice();
		}
	});
	$("#BOOK_roundtrip_roundtrip").click(function() {
		if(this.checked) {
			$(".book_collapse_roundtrip").fadeIn("fast");
			$("#BOOK_roundtrip_date").val($("#BOOK_date").val());
			updatePrice();
		}
	});
	$("#BOOK_passengers").change(function() {
		$("#nb_people").animate({ width: ($("#BOOK_passengers").attr("selectedIndex")+1)*10+"px" },200);
	});
	if($.browser.msie && parseFloat($.browser.version)<7) {//:S
		$("#book :text, #book select").css('backgroundColor','#fff');
		$("#BOOK_car").change(function(){ chooseCar($(this).attr("selectedIndex")); });
	}
});



function updateArrival()
{
	$.ajax({
		method: "get",url:"/book_ajax.php",data:"from="+$("#BOOK_from").val(),
		beforeSend: function(){$("#loading").css('display','inline');},
		complete: function(){ $("#loading").css('display','none');},
		success: function(html){
			$("#BOOK_to").html(html);
			updatePrice();
		}
	});
}
function updatePrice()
{
	if($("#BOOK_car").length && $("#BOOK_car").get(0).selectedIndex!=0)
	{
		$.ajax({
			method: "get",url:"/book_ajax.php",data:"from="+$("#BOOK_from").val()+"&to="+$("#BOOK_to").val()+"&car="+$("#BOOK_car").val(),
			beforeSend: function(){$("#loading").css('display','inline');},
			complete: function(){ $("#loading").css('display','none');},
			success: function(p) {
				if(p)
				{
					if($("#BOOK_roundtrip_roundtrip").attr("checked") && p!="") p = p*0.95*2;
					p = parseFloat(p) + calcOptions();
					p = parseFloat(p).toFixed(2) + "&euro;";
				}
				else p="Contact us";
				$("#BOOK_tr_price").fadeIn();
				$("#BOOK_price").html(p);
			}
		});
	}
}
function calcOptions()
{
	options_price = 0;
	$("#div_options input:checked").each(function(){
		options_price+=parseFloat(options[this.value].price);
	});
	return options_price;
}
function chooseCar(i)
{
	$("#BOOK_car").focus();
	$("#BOOK_car").get(0).selectedIndex=i;
	updatePrice();
}
