
var previousLocationInput = "";

/****************************************************************************************************************
*	Create a drop down list OR hidden input of location matches.
*	Dependant on gis.cfm which generates the ddl or hidden input
****************************************************************************************************************/
function retrieveLocationMatches()
{
	// Exit if text is not entered into address/intersection field
	if ($F('locationInput') == "")
		return;
	
	// Previous entry is the same as current entry, show location selection w/o calling gis web service
	if (previousLocationInput == $F('locationInput'))
	{
		$('cityList').selectedIndex = 0;
		$('intersectionArea').hide(); 
		$('loading').hide(); 
		$('locationOptions').show();
		try {
			$('locationList').selectedIndex = 0;
		}
		catch (err)
		{}
	}
	else
	{
	
		var myAjax = 
			new Ajax.Updater('locationOptions', 'http://dpw.lacounty.gov/general/survey/gis.cfm',
			{
				method: 'get',
				parameters: {locationInput: $F('locationInput')},
				evalScripts: true,			// Evaluate any scripts that is generated when calling gis.cfm
				onLoading: 	function() 
				{
					$('locationOptions').hide();
					$('loading').show();
					$('intersectionArea').show();
				},
				onLoaded: function()
				{
					$('cityList').selectedIndex = 0;
					$('intersectionArea').hide(); 
					$('loading').hide(); 
					$('locationOptions').show();	
				},
				onException: function() 
				{
					alert('Error');
				}
			});
	}
	
	// Cache current input for later comparison
	previousLocationInput = $F('locationInput');
}

/****************************************************************************************************************
*	Resets the address/intersection input
****************************************************************************************************************/
function resetIntersection() 
{
	$('locationInput').value = "";
	$('locationOptions').hide();
	$('intersectionArea').show();
}

/****************************************************************************************************************
*	Resets the address/intersection input without clearing user input
****************************************************************************************************************/
function editIntersection()
{
	$('locationOptions').hide();
	$('intersectionArea').show();
}

/****************************************************************************************************************
*	select the city from the cities drop down list given an object with a value 
*	@param city - an object where it's value contains a city
****************************************************************************************************************/
function selectCity(city)
{
	$$('#cityList option').each(
		function(n){if (n.text.trim() == city.trim()) n.selected = true;}
		);
}

//================================================================================
// Extend PROTOTYPE's string object and add a trim/ltrim/rtrim function
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
