function findValue(li) {
		if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	return sValue;
}

function selectItem(li) {
	// do nothing, just handle the callback
}

function formatItem(row) {
//	return row[0] + " (id: " + row[1] + ")";
	return row[0];
}

function lookupFlyDeparture(){
	var oSuggest = $("#flyDeparture").attr("value");
	return getBetween(oSuggest, '[', ']');
}

function lookupFlyArrival(){
	var oSuggest = $("#flyArrival").attr("value");

	return getBetween(oSuggest, '[', ']');
}

function getBetween(s, prefix, suffix) {
	var i = s.indexOf(prefix);
	if (i >= 0) {
		s = s.substring(i + prefix.length);
	} else {
		return '';
	}

	if (suffix) {
		i = s.indexOf(suffix);
		
		if (i >= 0) {
			s = s.substring(0, i);
		} else {
			return '';
		}
	}

	return s;
}

$(document).ready(function() {
		$("#flyDeparture").autocomplete(
			"airports",
			{
				delay:10,
				minChars:2,
				matchSubset:1,
				matchContains:1,
				cacheLength:10,
				onItemSelect:selectItem,
				onFindValue:findValue,
				formatItem:formatItem,
				autoFill:true
			}
		);

		$('#flyArrival').autocomplete(
			"airports",
			{
				delay:10,
				minChars:2,
				matchSubset:1,
				matchContains:1,
				cacheLength:10,
				onItemSelect:selectItem,
				onFindValue:findValue,
				formatItem:formatItem,
				autoFill:true
			}
		);
	}
);