$(document).ready(function() {
	$('#BOOK_from').change(function() {
		updateArrival();
	});
    $('#BOOK_from2').change(function() {
        document.getElementById('BOOK_from').options[$("#BOOK_from2").attr("selectedIndex")].selected = true ;
		updateArrival();
	});
	$('#BOOK_to').change(function() {
		updatePrice();
	});
    $('#BOOK_to2').change(function() {
        document.getElementById('BOOK_to').options[$("#BOOK_to2").attr("selectedIndex")].selected = true ;
		updatePrice();
	});
	$('#BOOK_firstname').blur(function() {
		testvalide();
	});
	$('#BOOK_lastname').blur(function() {
		testvalide();
	});
	$('#BOOK_email').blur(function() {
		testvalide();
	});
	$('#BOOK_phone').blur(function() {
		testvalide();
	});
       
    $('#BOOK_car').change(function() {
		updatePrice();
	});
	$("#BOOK_car").before('<div id="layer"></div>');
	
});



function updateArrival(val)
{
       
	$.ajax({
		method: "get",url:"/book_ajax.php",data:"from="+$("#BOOK_from").val()+"&too="+val,
		beforeSend: function(){$("#loading").css('display','inline');},
		complete: function(){$("#loading").css('display','none');},
		success: function(html){
			$("#BOOK_to").html(html);
                        if( $("#BOOK_to2"))
                        $("#BOOK_to2").html(html);
			updatePrice();
		}
	});
}
function updatePrice()
{
 testvalide();
 idcar = $('#BOOK_car').val();
 if(idcar)
 $('#carimage').attr('src', 'images/'+cararray[idcar]);
 idfrom = $('#BOOK_from').val();
 if(idfrom)
 $('#fromimage').attr('src', 'images/'+placearray[idfrom]);
 idto = $('#BOOK_to').val();
 if(idto)
 $('#toimage').attr('src', 'images/'+placearray[idto]);
         var list = document.getElementById('BOOK_from2');
         if(list)
       list.options[$("#BOOK_from").attr("selectedIndex")].selected = true ;
         var listto = document.getElementById('BOOK_to2');
         if(listto)
       listto.options[$("#BOOK_to").attr("selectedIndex")].selected = true ;
        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(),
			success: function(p) {
				
                                if(p)
				{
					if($("#BOOK_roundtrip_roundtrip").attr("checked") && p!="") p = p*0.95*2;
					n = parseFloat(p).toFixed(2);
					p = parseFloat(p).toFixed(2) + " &euro;";
				}
				else{
				p="Contact us";
				n = 0;
				}
                                priceoption = updateoption();
                                price = parseFloat(n) +  parseFloat(priceoption);
				                $("#BOOK_price").html(price+" &euro;");
                                if($("#BOOK_price2"))
                                $("#BOOK_price2").html(price+" &euro;");
                                $("#BOOK_price_car").html(p);
                                $("#price").val(price) ;
                                updateaffichagedestination();
			}
		});
	}
}


function updateoption(){
      price = 0;
      optionchamp = new Array();
      $('#optionaffiche').html('<tr ><td colspan="3"><hr/></td></tr>');
      $("select").each(function () {
          if($(this).attr("id") == 'optionlist'){

          if($(this).val() != 'free'){
          price += parseInt($(this).val());
          }
          if($(this).val() != 0){

          val = $(this).val()+' &euro;';
          optionchamp.push($(this).attr("name")+':'+$(this).val());
          if($(this).val() == 'free'){
          val = 'free';
          }
          $('#optionaffiche').prepend('<tr><td width="10"></td><td>'+$(this).attr("class")+'<br/></td><td width="50" align="right"><span>'+val+'</span></td></tr>') ;
          }
          }
      });
                
     
      $("input:checkbox:checked").each(function () {
          if($(this).attr("id") == 'optioncheck')

         
          optionchamp.push($(this).attr("name")+':'+$(this).val());
          if($(this).val() == "free"){
           priceaff = $(this).val();
          }else{
           priceaff = $(this).val()+' &euro;';
           price += parseInt($(this).val());
          }

          $('#optionaffiche').prepend('<tr><td width="10"></td><td>'+$(this).attr("class")+'<br/></td><td width="50" align="right"><span>'+priceaff+'</span></td></tr>') ;
        
      });
     $('#optionchamp').val(optionchamp.join(";"));
     return price;



}

function updateaffichagedestination()
{
 var from = document.getElementById('BOOK_from').options[$("#BOOK_from").attr("selectedIndex")].text;
 var to = document.getElementById('BOOK_to').options[$("#BOOK_to").attr("selectedIndex")].text;
 var car = document.getElementById('BOOK_car').options[$("#BOOK_car").attr("selectedIndex")].text;
 $("#fromaff").html(from);
 $("#toaff").html(to);
 $("#caraff").html(car);
 $("#fromaff1").html(from);
 $("#toaff1").html(to);
 $("#caraff1").html(car);
}

function testvalide(){
        var ok=true;
		imageok = '<img src="images/ok.png" style="height:16px; vertical-align: middle;"" />';
		imagenot = '<img src="images/not.png" style="height:16px; vertical-align: middle;"" />';
		if($("#BOOK_from").val() == "" || !$("#BOOK_from").val() ) {$("#BOOK_from_res").html(imagenot);ok=false;}
		else{$("#BOOK_from_res").html(imageok);}		
		if($("#BOOK_to").val() == "" || !$("#BOOK_to").val()) {$("#BOOK_to_res").html(imagenot);ok=false;}
		else{$("#BOOK_to_res").html(imageok);}
		if($("#BOOK_car").val() == "" || !$("#BOOK_car").val()) {$("#BOOK_car_res").html(imagenot);ok=false;}
		else{$("#BOOK_car_res").html(imageok);}
		if($("#BOOK_firstname").val() == firstnamevalue || !$("#BOOK_firstname").val()) {$("#BOOK_firstname_res").html(imagenot);ok=false;}
		else{$("#BOOK_firstname_res").html(imageok);}
		if($("#BOOK_lastname").val() == lastnamevalue || !$("#BOOK_lastname").val()) {$("#BOOK_lastname_res").html(imagenot);ok=false;}
		else{$("#BOOK_lastname_res").html(imageok);}
		if($("#BOOK_email").val() == "ex : youemail@provider.com" || !$("#BOOK_email").val() || !$("#formstep3").validate().element( "#BOOK_email" )) {$("#BOOK_email_res").html(imagenot);ok=false;}
		else{$("#BOOK_email_res").html(imageok);}
		if($("#BOOK_phone").val() == "ex : 1-800-905-905 (With international code please)" || !$("#BOOK_phone").val()) {$("#BOOK_phone_res").html(imagenot);ok=false;}
		else{$("#BOOK_phone_res").html(imageok);}
		if(ok)
		$('#submitbuttom').css({backgroundColor: '#009900'});
		else
		$('#submitbuttom').css({backgroundColor: '#B50002'});
		
		return ok;

}
