

$(document).ready(function() {
    // serivice area rectangle south west: 42.293564, -10.546875
    // serivice area rectangle north east: 58.676938,  15.073242
    var s_sw   = new google.maps.LatLng(42.293564, -10.546875);
    var s_ne   = new google.maps.LatLng(58.676938,  15.073242);
    var s_area = new google.maps.LatLngBounds(s_sw, s_ne);

    geocoder = new google.maps.Geocoder();
	
    // from airport selection
    $('#originating-flight-from-city-input').autocomplete({
        source: function(request, response) {
		
            geocoder.geocode(
                { address: request.term, bounds: s_area },
                function(results, status) {
                response($.map(results, function(item) {
                    if (s_area.contains(item.geometry.location)) {
                        return {
                            label: item.formatted_address,
                            value: item.formatted_address,
                            latitude: item.geometry.location.lat(),
                            longitude: item.geometry.location.lng()
                        }
                    }
                }));
            })
        },
        select: function(event, ui) {
			var optionValue = $("#originating-flight-from-airport-select option[value='0']").text();
            $('#originating-flight-from-airport-select').empty().append('<option selected="selected" value="0">'+optionValue+'</option>');
            $('#originating-flight-from-airport-select').attr('disabled', 'disabled');
            $.ajax({
                type: "POST",
                url: "/lib/ajax-list-airports.php",
                data: "lat=" + ui.item.latitude + "&lng=" + ui.item.longitude,
                success: function(msg) {
                    var str = $.trim(msg);
					firstAirport = true;
                    var arr = str.split('|');
                    if ($.isArray(arr)) {
                        $.each(arr, function(index, value) {
                            var airport = value.split('###');
                            if ($.isArray(airport)) {
								if(firstAirport)
								{
									$('#originating-flight-from-airport-select').append($('<option selected="selected"></option>').val(airport[0]).html(airport[1]));
								}
								else
								{
									$('#originating-flight-from-airport-select').append($('<option ></option>').val(airport[0]).html(airport[1]));
								}
								firstAirport = false;
                            }
                        });
                        if (arr.length)
                            $('#originating-flight-from-airport-select').removeAttr('disabled');
							if($('#originating-flight-to-city-input').val() == "Enter a valid destination city" || $('#originating-flight-to-airport-select').val() == "Enter a valid destination city")
							{
								$('#originating-flight-to-city-input').focus();
								$('#originating-flight-to-city-input').select();
							}
                    }
                }
            });
        }
    });

    // to airport selection
    $('#originating-flight-to-city-input').autocomplete({
        source: function(request, response) {
            geocoder.geocode(
                {address: request.term, bounds: s_area },
                function(results, status) {
                response($.map(results, function(item) {
                    if (s_area.contains(item.geometry.location)) {
                        return {
                            label: item.formatted_address,
                            value: item.formatted_address,
                            latitude: item.geometry.location.lat(),
                               longitude: item.geometry.location.lng()
                        }
                    }
                }));
            })
        },
        select: function(event, ui) {
			var optionValue = $("#originating-flight-to-airport-select option[value='0']").text();
            $('#originating-flight-to-airport-select').empty().append('<option selected="selected" value="0">'+optionValue+'</option>');
            $('#originating-flight-to-airport-select').attr('disabled', 'disabled');
            $.ajax({
                type: "POST",
                url: "/lib/ajax-list-airports.php",
                data: "lat=" + ui.item.latitude + "&lng=" + ui.item.longitude,
                success: function(msg) {
                    var str = $.trim(msg);
					firstAirport = true;
                    var arr = str.split('|');
                    if ($.isArray(arr)) {
                        $.each(arr, function(index, value) {
                            var airport = value.split('###');
                            if ($.isArray(airport)) {
				if(firstAirport)
				{
					$('#originating-flight-to-airport-select').append($('<option selected="selected"></option>').val(airport[0]).html(airport[1]));
				}
				else
				{
					$('#originating-flight-to-airport-select').append($('<option ></option>').val(airport[0]).html(airport[1]));
				}
				firstAirport = false;
                            }
                        });
                        if (arr.length)
                            $('#originating-flight-to-airport-select').removeAttr('disabled');
							if($('#originating-flight-from-airport-select').val() == "Enter a valid departure city" || $('#originating-flight-from-airport-select').val() == "Voer plaats van vertrek in")
							{
								$('#originating-flight-from-city-input').focus();
								$('#originating-flight-from-city-input').select();
							}
                    }
                }
            });
        }
    });
}
);
/////////////////////////////////////
$(document).ready(function() {
    // serivice area rectangle south west: 42.293564, -10.546875
    // serivice area rectangle north east: 58.676938,  15.073242
    var s_sw   = new google.maps.LatLng(42.293564, -10.546875);
    var s_ne   = new google.maps.LatLng(58.676938,  15.073242);
    var s_area = new google.maps.LatLngBounds(s_sw, s_ne);

    geocoder = new google.maps.Geocoder();

    // from airport selection
    $('#return-flight-from-city-input').autocomplete({
        source: function(request, response) {
            geocoder.geocode(
                { address: request.term, bounds: s_area },
                function(results, status) {
                response($.map(results, function(item) {
                    if (s_area.contains(item.geometry.location)) {
                        return {
                            label: item.formatted_address,
                            value: item.formatted_address,
                            latitude: item.geometry.location.lat(),
                            longitude: item.geometry.location.lng()
                        }
                    }
                }));
            })
        },
        select: function(event, ui) {
			var optionValue = $("#return-flight-from-airport-select option[value='0']").text();
            $('#return-flight-from-airport-select').empty().append('<option  value="0">'+optionValue+'</option>');
            $('#return-flight-from-airport-select').attr('disabled', 'disabled');
            $.ajax({
                type: "POST",
                url: "/lib/ajax-list-airports.php",
                data: "lat=" + ui.item.latitude + "&lng=" + ui.item.longitude,
                success: function(msg) {
                    var str = $.trim(msg);
					firstAirport = true;
                    var arr = str.split('|');
                    if ($.isArray(arr)) {
                        $.each(arr, function(index, value) {
                            var airport = value.split('###');
                            if ($.isArray(airport)) {
								if(firstAirport)
								{
									$('#return-flight-from-airport-select').append($('<option selected="selected"></option>').val(airport[0]).html(airport[1]));
								}
								else
								{
									$('#return-flight-from-airport-select').append($('<option ></option>').val(airport[0]).html(airport[1]));
								}
								firstAirport = false;
                            }
                        });
                        if (arr.length)
                            $('#return-flight-from-airport-select').removeAttr('disabled');
							
							$('#return-flight-to-city-input').focus();
							$('#return-flight-to-city-input').select();
                    }
                }
            });
        }
    });

    // to airport selection
    $('#return-flight-to-city-input').autocomplete({
        source: function(request, response) {
            geocoder.geocode(
                {address: request.term, bounds: s_area },
                function(results, status) {
                response($.map(results, function(item) {
                    if (s_area.contains(item.geometry.location)) {
                        return {
                            label: item.formatted_address,
                            value: item.formatted_address,
                            latitude: item.geometry.location.lat(),
                               longitude: item.geometry.location.lng()
                        }
                    }
                }));
            })
        },
        select: function(event, ui) {
			var optionValue = $("#return-flight-to-airport-select option[value='0']").text();
            $('#return-flight-to-airport-select').empty().append('<option value="0">'+optionValue+'</option>');
            $('#return-flight-to-airport-select').attr('disabled', 'disabled');
            $.ajax({
                type: "POST",
                url: "/lib/ajax-list-airports.php",
                data: "lat=" + ui.item.latitude + "&lng=" + ui.item.longitude,
                success: function(msg) {
                    var str = $.trim(msg);
					firstAirport = true;
                    var arr = str.split('|');
                    if ($.isArray(arr)) {
                        $.each(arr, function(index, value) {
                            var airport = value.split('###');
                            if ($.isArray(airport)) {
								if(firstAirport)
								{
									$('#return-flight-to-airport-select').append($('<option selected="selected"></option>').val(airport[0]).html(airport[1]));
								}
								else
								{
									$('#return-flight-to-airport-select').append($('<option ></option>').val(airport[0]).html(airport[1]));
								}
								firstAirport = false;
                            }
                        });
                        if (arr.length)
                            $('#return-flight-to-airport-select').removeAttr('disabled');
							$('#return-flight-from-city-input').focus();
							$('#return-flight-from-city-input').select();
                    }
                }
            });
        }
    });
}


);
////////////////////////////////////////
$(document).ready(function() {
    // serivice area rectangle south west: 42.293564, -10.546875
    // serivice area rectangle north east: 58.676938,  15.073242
    var s_sw   = new google.maps.LatLng(42.293564, -10.546875);
    var s_ne   = new google.maps.LatLng(58.676938,  15.073242);
    var s_area = new google.maps.LatLngBounds(s_sw, s_ne);

    geocoder = new google.maps.Geocoder();
	
    // from airport selection
    $('#from-city-input').autocomplete({
        source: function(request, response) {
            geocoder.geocode(
                { address: request.term, bounds: s_area },
                function(results, status) {
                response($.map(results, function(item) {
                    if (s_area.contains(item.geometry.location)) {
                        return {
                            label: item.formatted_address,
                            value: item.formatted_address,
                            latitude: item.geometry.location.lat(),
                            longitude: item.geometry.location.lng()
                        }
                    }
                }));
            })
        },
        select: function(event, ui) {
			var optionValue = $("#from-airport-select").text();
            $('#from-airport-select').empty().append('<option  value="0">'+optionValue+'</option>');
            $('#from-airport-select').attr('disabled', 'disabled');
            $.ajax({
                type: "POST",
                url: "/lib/ajax-list-airports.php",
                data: "lat=" + ui.item.latitude + "&lng=" + ui.item.longitude,
                success: function(msg) {
                    var str = $.trim(msg);
					firstAirport = true;
                    var arr = str.split('|');
                    if ($.isArray(arr)) {
                        $.each(arr, function(index, value) {
                            var airport = value.split('###');
                            if ($.isArray(airport)) {
                                if(firstAirport)
								{
									$('#from-airport-select').append($('<option selected="selected"></option>').val(airport[0]).html(airport[1]));
								}
								else
								{
									$('#from-airport-select').append($('<option ></option>').val(airport[0]).html(airport[1]));
								}
								firstAirport = false;
                            }
                        });
                        if (arr.length)
                            $('#from-airport-select').removeAttr('disabled');
							if($('#to-city-input').val() == "Enter a valid destination city" || $('#to-city-input').val() == "Voer plaats van bestemming in")
							{
								$('#to-city-input').focus();
								$('#to-city-input').select();
							}
                    }
                }
            });
        }
    });

    // to airport selection
    $('#to-city-input').autocomplete({
        source: function(request, response) {
            geocoder.geocode(
                {address: request.term, bounds: s_area },
                function(results, status) {
                response($.map(results, function(item) {
                    if (s_area.contains(item.geometry.location)) {
                        return {
                            label: item.formatted_address,
                            value: item.formatted_address,
                            latitude: item.geometry.location.lat(),
                               longitude: item.geometry.location.lng()
                        }
                    }
                }));
            })
        },
        select: function(event, ui) {
			var optionValue = $("#to-airport-select").text();
            $('#to-airport-select').empty().append('<option value="0">'+optionValue+'</option>');
            $('#to-airport-select').attr('disabled', 'disabled');
            $.ajax({
                type: "POST",
                url: "/lib/ajax-list-airports.php",
                data: "lat=" + ui.item.latitude + "&lng=" + ui.item.longitude,
                success: function(msg) {
                    var str = $.trim(msg);
					firstAirport = true;
                    var arr = str.split('|');
                    if ($.isArray(arr)) {
                        $.each(arr, function(index, value) {
                            var airport = value.split('###');
                            if ($.isArray(airport)) {
							
								if(firstAirport)
								{
									$('#to-airport-select').append($('<option selected="selected"></option>').val(airport[0]).html(airport[1]));
								}
								else
								{
									$('#to-airport-select').append($('<option ></option>').val(airport[0]).html(airport[1]));
								}
								firstAirport = false;
                            }
                        });
                        if (arr.length)
                            $('#to-airport-select').removeAttr('disabled');
							if($('#from-city-input').val() == "Enter a valid departure city" || $('#from-city-input').val() == "Voer plaats van vertrek in")
							{
								$('#from-city-input').focus();
								$('#from-city-input').select();
							}
					
                    }
                }
            });
        }
    });
}


);

$(document).ready(function() {
    $('input[type="text"]').focus(function() {
        if (this.value == "Enter a valid departure city" || this.value == "Enter a valid destination city" || this.value == "Voer plaats van vertrek in" || this.value == "Voer plaats van bestemming in"){
            this.value = '';
        }
            if(this.value != this.defaultValue){
                this.select();
            }
    });
    $('input[type="text"]').blur(function() {
        if ($.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
        }
    });
});

