/*  pt_autocomplete.js, using jQuery UI:s autocomplete */
$(function() {
	// Browser's own autocomplete gets even higher z-index and blocks the jQuery autocomplete, so disable them
	// $('#text_fly_from').attr('autocomplete', 'off');
	// $('#text_fly_to').attr('autocomplete', 'off');
	// $('#hotel_city').attr('autocomplete', 'off');
	// $('#fh_text_from').attr('autocomplete', 'off');
	// $('#fh_text_to').attr('autocomplete', 'off');
	// Fixed in a wrong way, for now, by adding attribute "autocomplete='off'" to input fields
		
	var json_data = "E"; // By default, the result set from DB is empty
	
	$("#text_fly_from").keyup(function() { // every time key is released from input field, do this
		var user_input = $("#text_fly_from").val(); // store string that user input to user_input
		if (user_input.length < 3) { // if input is too short, do not try to find match from DB
			json_data = "E"; // reset result set
			$("#from_IATA").val('');
			$("#text_fly_from").autocomplete("close"); // close autocomplete menu if it happens to be open
		} else if (json_data == "E") { // Now the length of user_input is three characters
			// Result set is still empty, so start DB lookup
			json_data = "X"; // make sure this JS will not create another simultaneous DB lookup 
			$.ajax({
				url: "db_locations3.php?term=" + user_input,
				dataType: "json",
				success: function(responselist) {
					// The rows above use AJAX to query the cities that match user_input from DB.
					// json_data = responselist; // result set from DB is stored to json_data
					
					$("#text_fly_from").autocomplete({ // Call autocomplete function that is a part of JQuery UI
						source: responselist, // autocomplete thinks that the data source is local (text) array
						minLength: 3, // show autocomplete menu only when there are at least 3 characters
						delay: 0, // result set is already retrieved with AJAX, no need to delay here anymore
						select: function( event, ui ) {
							// Select automatically sets the input field's value to ui.item.value
							// Let's change the hidden field for IATA's as well
							$("#from_IATA").val(ui.item.id);
						}
					});
					// Last thing is to make sure the autocomplete menu is visible even slower DB lookup
					$("#text_fly_from").autocomplete( "search" );
				}
			});
		}
	});
	$("#text_fly_to").keyup(function() { // Autocomplete for flight destination text field
		var user_input = $("#text_fly_to").val();
		if (user_input.length < 3) {
			json_data = "E"; // reset result set
			$("#to_IATA").val('');
			$("#text_fly_to").autocomplete("close"); // close autocomplete menu if it happens to be open
		} else if (json_data == "E") { // Now the length of user_input is three characters
			json_data = "X"; // make sure this JS will not create another simultaneous DB lookup 
			$.ajax({
				url: "db_locations3.php?term=" + user_input,
				dataType: "json",
				success: function(responselist) {
					$("#text_fly_to").autocomplete({ // Call autocomplete function that is a part of JQuery UI
						source: responselist, minLength: 3, delay: 0, 
						select: function( event, ui ) {		$("#to_IATA").val(ui.item.id);		}	});
					$("#text_fly_to").autocomplete( "search" );
				}
			});
		}
	});
	
	$("#hotel_city").keyup(function() { // every time key is released from input field, do this
		var user_input = $("#hotel_city").val(); // store string that user input to user_input
		if (user_input.length < 3) { // if input is too short, do not try to find match from DB
			json_data = "E"; // reset result set
			$("#hotel_country_code").val('');
			$("#hotel_city").autocomplete("close"); // close autocomplete menu if it happens to be open
		} else if (json_data == "E") { // Now the length of user_input is three characters
			// Result set is still empty, so start DB lookup
			json_data = "X"; // make sure this JS will not create another simultaneous DB lookup 
			$.ajax({
				url: "db_cities.php?term=" + user_input,
				dataType: "json",
				success: function(responselist) {
					// The rows above use AJAX to query the cities that match user_input from DB.
					// json_data = responselist; // result set from DB is stored to json_data
					
					$("#hotel_city").autocomplete({ // Call autocomplete function that is a part of JQuery UI
						source: responselist, // autocomplete thinks that the data source is local (text) array
						minLength: 3, // show autocomplete menu only when there are at least 3 characters
						delay: 0, // result set is already retrieved with AJAX, no need to delay here anymore
						select: function( event, ui ) {
							// Select automatically sets the input field's value to ui.item.value
							// change the hidden hotel_country_code to country code
							$("#hotel_country_code").val(ui.item.id);
						}
					});
					// Last thing is to make sure the autocomplete menu is visible even slower DB lookup
					$("#hotel_city").autocomplete( "search" );
				}
			});
		}
	});
	
	$("#fh_text_from").keyup(function() { // Autocomplete for flight destination text field, in combo searches
		var user_input = $("#fh_text_from").val();
		if (user_input.length < 3) {
			json_data = "E"; // reset result set
			$("#fh_from_IATA").val('');
			$("#fh_text_from").autocomplete("close"); // close autocomplete menu if it happens to be open
		} else if (json_data == "E") { // Now the length of user_input is three characters
			json_data = "X"; // make sure this JS will not create another simultaneous DB lookup 
			$.ajax({
				url: "db_locations3.php?term=" + user_input,
				dataType: "json",
				success: function(responselist) {
					$("#fh_text_from").autocomplete({ // Call autocomplete function that is a part of JQuery UI
						source: responselist, minLength: 3, delay: 0, 
						select: function( event, ui ) {		$("#fh_from_IATA").val(ui.item.id);		}	});
					$("#fh_text_from").autocomplete( "search" );
				}
			});
		}
	});
	$("#fh_text_to").keyup(function() { // Autocomplete for flight destination text field, in combo searches
		var user_input = $("#fh_text_to").val();
		if (user_input.length < 3) {
			json_data = "E"; // reset result set
			$("#fh_to_IATA").val('');
			$("#fh_text_to").autocomplete("close"); // close autocomplete menu if it happens to be open
		} else if (json_data == "E") { // Now the length of user_input is three characters
			json_data = "X"; // make sure this JS will not create another simultaneous DB lookup 
			$.ajax({
				url: "db_locations3.php?term=" + user_input,
				dataType: "json",
				success: function(responselist) {
					$("#fh_text_to").autocomplete({ // Call autocomplete function that is a part of JQuery UI
						source: responselist, minLength: 3, delay: 0, 
						select: function( event, ui ) {		$("#fh_to_IATA").val(ui.item.id);		}	});
					$("#fh_text_to").autocomplete( "search" );
				}
			});
		}
	});
});

