// JavaScript Document
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;

	
}

function selectItem(li) {
	findValue(li);
}

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

function lookupAjax(){
	var oSuggest = $("#CityAjax")[0].autocompleter;

	oSuggest.findValue();

	return false;
}

function lookupLocal(){
	var oSuggest = $("#CityLocal")[0].autocompleter;

	oSuggest.findValue();

	return false;
}

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

	$("#CityLocal").autocompleteArray(
		[
			"Providence, Rhode Island", 
			"Cranston, Rhode Island", 
			"Central Falls, Rhode Island", 
			"Newport, Rhode Island", 
			"Barrington, Rhode Island", 
			"Bristol, Rhode Island", 
			"Burrillville, Rhode Island", 
			"Charlestown, Rhode Island", 
			"Coventry, Rhode Island", 
			"Cumberland, Rhode Island", 
			"East Greenwich, Rhode Island", 
			"East Providence, Rhode Island", 
			"Attleboro, Massachusetts", 
			"Exeter, Rhode Island", 
			"Hopkinton, Rhode Island", 
			"Jamestown, Rhode Island", 
			"Johnston, Rhode Island", 
			"Lincoln, Rhode Island", 
			"Little Compton, Rhode Island", 
			"Narragansett, Rhode Island", 
			"North Kingstown, Rhode Island", 
			"Pawtucket, Rhode Island", 
			"North Providence, Rhode Island", 
			"North Smithfield, Rhode Island", 
			"Portsmouth, Rhode Island", 
			"Richmond, Rhode Island", 
			"Scituate, Rhode Island", 
			"Smithfield, Rhode Island", 
			"South Kingstown, Rhode Island", 
			"Tiverton, Rhode Island", 
			"Warren, Rhode Island", 
			"Warwick, Rhode Island", 
			"West Warwick, Rhode Island", 
			"Westerly, Rhode Island", 
			"Woonsocket, Rhode Island", 
			"Taunton, Massachusetts", 
			"Abington, Massachusetts", 
			"Webster, Massachusetts", 
			"Whitinsville, Massachusetts", 
			"Grafton, Massachusetts", 
			"Fall River, Massachusetts", 
			"Swansea, Massachusetts", 
			"Westport, Massachusetts", 
			"Dartmouth, Massachusetts", 
			"New Bedford, Massachusetts", 
			"Freetown, Massachusetts", 
			"Seekonk, Massachusetts", 
			"Rehoboth, Massachusetts", 
			"Raynham, Massachusetts", 
			"Lakeville, Massachusetts", 
			"Middleborough, Massachusetts", 
			"Norton, Massachusetts", 
			"Plainville, Massachusetts", 
			"Wrentham, Massachusetts", 
			"Foxboro, Massachusetts", 
			"Bridgewater, Massachusetts", 
			"East Bridgewater, Massachusetts", 
			"West Bridgewater, Massachusetts", 
			"Brockton, Massachusetts", 
			"Whitman, Massachusetts", 
			"Easton, Massachusetts", 
			"Hanson, Massachusetts", 
			"Halifax, Massachusetts", 
			"Oxford, Massachusetts", 
			"Southbridge, Massachusetts", 
			"Millbury, Massachusetts", 
			"Medfield, Massachusetts", 
			"Medway, Massachusetts", 
			"Millis, Massachusetts", 
			"Walpole, Massachusetts", 
			"Canton, Massachusetts", 
			"Stoughton, Massachusetts", 
			"Randolph, Massachusetts", 
			"Holbrook, Massachusetts", 
			"Avon, Massachusetts", 
			"Rockland, Massachusetts", 
			"Hanover, Massachusetts", 
			"Plympton, Massachusetts", 
			"Plymouth, Massachusetts", 
			"Carver, Massachusetts", 
			"Rochester, Massachusetts", 
			"Fairhaven, Massachusetts", 
			"Marion, Massachusetts", 
			"Mattapoisett, Massachusetts", 
			"Norwood, Massachusetts", 
			"Bellingham, Massachusetts", 
			"Milford, Massachusetts", 
			"Hopedale, Massachusetts", 
			"Mendon, Massachusetts", 
			"Uxbridge, Massachusetts", 
			"Upton, Massachusetts", 
			"Hopkinton, Massachusetts", 
			"Holliston, Massachusetts", 
			"Boston, Massachusetts", 
			"Dover, Massachusetts", 
			"North Attleboro, Massachusetts", 
			"Millville, Massachusetts", 
			"Blackstone, Massachusetts", 
			"Wakefield, Rhode Island", 
			"Quincy, Massachusetts", 
			"Braintree, Massachusetts", 
			"Boston, Massachusetts", 
			"Mansfield, Massachusetts", 
			"Cambridge, Massachusetts", 
			"South Boston, Massachusetts", 
			"Somerville, Massachusetts", 
			"Dorchester, Massachusetts", 
			"Brookline, Massachusetts", 
			"Jamaica Plain, Massachusetts", 
			"Newton, Massachusetts", 
			"Chestnut Hill, Massachusetts", 
			"Waltham, Massachusetts", 
			"Watertown, Massachusetts", 
			"Belmont, Massachusetts", 
			"Weymouth, Massachusetts", 
			"Needham, Massachusetts", 
			"Wellesley, Massachusetts", 
			"Dedham, Massachusetts", 
			"Somerset, Massachusetts", 
			"Salem, Massachusetts", 
			"Marblehead, Massachusetts", 
			"Beverly, Massachusetts", 
			"Swampscott, Massachusetts", 
			"Franklin, Massachusetts", 
			"Brimfield, Massachusetts", 
			"Middletown, Rhode Island", 
			"Lowell, Massachusetts", 
			"S Dartmouth, Massachusetts", 
			"Foster, Rhode Island", 
			"Gloucester, Rhode Island", 
			"Melrose, Massachusetts"
		],
		{
			delay:10,
			minChars:1,
			matchSubset:1,
			onItemSelect:selectItem,
			onFindValue:findValue,
			autoFill:true,
			maxItemsToShow:10
		}
	);
});