//Our XmlHttpRequest object to get the auto suggests
var http = getHTTPObject();

//Called from keyup on the search textbox.
function searchSuggest()
{
	document.getElementById('citySuggest').style.visibility = "hidden";	
	
	var searchKey	 = escape(document.getElementById('searchKey').value);
	
	http.open("GET", 'ajaxserverscripts/searchSuggest.php?searchKey=' + searchKey, true);
	http.onreadystatechange = handleSearchSuggest; 
	http.send(null);
}

//Called when the AJAX response is returned.
function handleSearchSuggest()
{
	if (http.readyState == 4)
	{
		var suggestDiv = document.getElementById('citySuggest')
		suggestDiv.innerHTML = '';
		
		var strCities   = http.responseText;
		var arrayCities  = new Array();
		arrayCities  = strCities .split(', ');
		
		for(i=0; i < arrayCities.length - 1; i++)
		{
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
			suggest += 'class="suggestlink">' + arrayCities[i] + '</div>';
			suggestDiv.innerHTML += suggest;
		}
		
		if (arrayCities.length - 1 > 0)
			suggestDiv.style.visibility = "visible";
	}
}

//Mouse over function
function suggestOver(div) {
	div.className = 'suggestlinkover';
}
//Mouse out function
function suggestOut(div) {
	div.className = 'suggestlink';
}
//Click function
function setSearch(selectedCity) {
	document.getElementById('searchKey').value = selectedCity;
	document.getElementById('citySuggest').innerHTML = '';
	document.getElementById('citySuggest').style.visibility = "hidden";
}

function getHTTPObject()
{
  var xmlhttp;
	
	if(window.XMLHttpRequest) // Firefox, Opera 8.0+, Safari
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
		}
		catch(e)
		{
			xmlhttp = false;
		}
	}
	else if(window.ActiveXObject) // Internet Explorer
	{
		try
		{
			xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch(e)
		{
			try
			{
				xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch(e)
			{
				xmlhttp = false;
			}
		}
	}
	
	return xmlhttp;
}