$(document).ready(function(){
	
//	jQuery("#airport_from").suggest("search.php",{
//		onSelect: function() {alert("You selected: " + this.value)}});
	
	jQuery("#airport_from").autocomplete("search.php", {minChars:3 });
	jQuery("#airport_to").autocomplete("search.php", {minChars:3 });
	
	$("#FlightSearchForm").submit(function(){
		DepartureDate = $("#dep_date").attr("value");
		ArrivalDate = $("#arr_date").attr("value");
		
		DepartureArray = DepartureDate.split("/");
		ArrivalArray = ArrivalDate.split("/");
		
		Departure = new Date(DepartureArray[2], DepartureArray[1], DepartureArray[0]);
		Arrival = new Date(ArrivalArray[2], ArrivalArray[1], ArrivalArray[0]);
		
		if($("#flight_type_round_trip").is(":checked"))
		{
			if(Departure.getTime() > Arrival.getTime())
			{
				alert("Departure Date can not be bigger than arrival date");
				return false;
			}
			else
			{
				if($("#infant").val() > $("#adult").val())
				{
					alert("Infants number must not be bigger then adults number");
					return false;
				}
				else
				{
					if($("#airport_from").attr("value") == '-' || $("#airport_to").attr("value") == '-')
					{
						
						alert("Please select departure and arrival airports");
						return false;
					}
					else if(DepartureDate == '' || ArrivalDate == '')
					{
						alert("Please select departure and arrival date");
						return false;
					}
				}
			}
		}
		else if($("#flight_type_one_way").is(":checked"))
		{
			if($("#infant").val() > $("#adult").val())
			{
				alert("Infants number must not be bigger then adults number");
				return false;
			}
			else
			{
				if($("#airport_from").attr("value") == '-' || $("#airport_to").attr("value") == '-')
				{
					alert("Please select departure and arrival airports");
					return false;
				}
				else if(DepartureDate == '')
				{
					alert("Please select departure date");
					return false;
				}
			}
		}
	});
	
	
	$("#dep_date").datepicker({ firstDay: 1, dateFormat: 'dd/mm/yy', dayNamesMin:['Do','Lu','Ma','Me','Gi','Ve','Sa'], monthNames:['Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'], minDate: new Date(), onSelect: function(){ if($("#arr_date").attr("disabled") == false){date = $(this).datepicker('getDate'); $("#arr_date").datepicker("option","minDate", date);}  } });
	$("#arr_date").datepicker({ firstDay: 1, dateFormat: 'dd/mm/yy', dayNamesMin:['Do','Lu','Ma','Me','Gi','Ve','Sa'], monthNames:['Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'], minDate: new Date() });
	
	$("#aeroport_plecare").click(function(){
		$("#dialog").remove();
		$("body").append('<div style="display:none;" id="dialog" title="Scegli l\'aeroporto di partenza"></div>');
		$("#dialog").append('<span>Country</span><select name="country" id="country_select" onchange="LoadCity(this.value);"><option value="-">Selectati</option></select><span>City</span><select name="city" id="city_select" onchange="LoadAirport(this.value);" disabled><option value="-">Selectati</option></select><span>Airport</span><select onchange="SetAirport(this.value, this[this.selectedIndex].innerHTML, \'from\');" id="airport_select" name="aeroport" disabled><option value="-">Selectati</option></select>');
		
		$.post('scripts/getCountry.php', '', function(data){
			$("#dialog #country_select").append(data);
		});
		
		$("#dialog").dialog({ buttons: [
		                                {
		                                    text: "Close",
		                                    click: function() { $(this).dialog("close"); }
		                                }
		                            ] });
	});
	
	$("#aeroport_sosire").click(function(){
		$("#dialog").remove();
		$("body").append('<div style="display:none;" id="dialog" title="Scegli l\'aeroporto di arrivo"></div>');
		$("#dialog").append('<span>Country</span><select name="country" id="country_select" onchange="LoadCity(this.value);"><option value="-">Selectati</option></select><span>City</span><select name="city" id="city_select" onchange="LoadAirport(this.value);" disabled><option value="-">Selectati</option></select><span>Airport</span><select onchange="SetAirport(this.value, this[this.selectedIndex].innerHTML, \'to\');" id="airport_select" name="aeroport" disabled><option value="-">Selectati</option></select>');
		
		$.post('scripts/getCountry.php', '', function(data){
			$("#dialog #country_select").append(data);
		});
		
		$("#dialog").dialog({ buttons: [
		                                {
		                                    text: "Close",
		                                    click: function() { $(this).dialog("close"); }
		                                }
		                            ] });
	});
});


function LoadCity(city)
{
	if(city !== '-')
	{
		$("#dialog #city_select").empty();
		$("#dialog #city_select").removeAttr('disabled')
		$("#dialog #airport_select").attr('disabled', 'disabled');
		$.post('scripts/getCity.php', {CityCode:city}, function(data){
			$("#dialog #city_select").append(data);
		});
	}
	else
	{
		$("#dialog #city_select").attr('disabled', 'disabled');
		$("#dialog #airport_select").attr('disabled', 'disabled');
	}
}

function LoadAirport(airport)
{
	if(airport !== '-')
	{
		$("#dialog #airport_select").empty();
		$("#dialog #airport_select").removeAttr('disabled')
		$.post('scripts/getAirport.php', {AirportCode:airport}, function(data){
			$("#dialog #airport_select").append(data);
		});
	}
	else
	{
		$("#dialog #airport_select").attr('disabled', 'disabled');
	}
}


function SetAirport(airport, airport_name, type)
{
	$("#airport_" + type).attr("value", airport_name + " (" + airport + ") ");
}



function arr_date_enable(value)
{
	if(value=='true')
	{
		$("#arr_date").attr("disabled", false);
	}
	else if (value=='false')
	{
		$("#arr_date").attr("disabled", true);
	}
}











