var bTopAjaxElemSelected = false;
var objForm = null;
var bHasFocusFreetext = false;
var bHasFocusLocationChoices = false;
var bIsValidLocationSelected = false;

function keyupAutoComplete( evt )
{
	if (evt) 
	{		
		bHasFocusFreetext = true;
		bIsValidLocationSelected = false;

    	if(evt.keyCode == 9) /* tab */
    	{	
			
    	}
    	else if(evt.keyCode == 40 ) /* arrow down */
    	{
        	objForm.locationChoices.selectedIndex = 0;
        	objForm.locationChoices.focus();
		}
		else if(evt.keyCode == 38) /* arrow up */
		{

		}
		else if(evt.keyCode == 13) /* enter */
		{
			
		}
		else if((bHasFocusFreetext || bHasFocusLocationChoices) && evt.keyCode == 27) /* escape */
		{
			doSetValue("");
		}
		else
		{
			doAJAXSearch(idRadiusLocation.value);
		}
	}
}

function keyupLocationChoices( evt )
{
	if (evt) 
	{
		bHasFocusLocationChoices = true;
		
    	if(evt.keyCode == 9) /* tab */
    	{	
			
    	}
    	else if(evt.keyCode == 40 ) /* arrow down */
    	{
			//bTopAjaxElemSelected = false;
		}
		else if(evt.keyCode == 38 && objForm.locationChoices.selectedIndex == 0) /* arrow up */
		{
			if (bTopAjaxElemSelected) {
				objForm.locationChoices.selectedIndex = -1;
	        	objForm.locationChoices.blur();
		        objForm.sRadiusLocation.focus();
		        bTopAjaxElemSelected = false;
			} else {
				bTopAjaxElemSelected = true;
			}
		}
		else if(evt.keyCode == 13) /* enter */
		{
			if(objForm.locationChoices.options.length > 0)
			{
				doSetValue(objForm.locationChoices.options[objForm.locationChoices.selectedIndex].value);
			}
		}
		else if((bHasFocusFreetext || bHasFocusLocationChoices) && evt.keyCode == 27) /* escape */
		{
			doSetValue("");
		}
	}
}

function clickLocationChoices( evt )
{
	bIsValidLocationSelected = true;
}

function blurAutoComplete()
{
	bHasFocusFreetext = false;
}

function blurLocationChoices()
{
	bHasFocusLocationChoices = false;
}

function doSetValue(thisValue)	
{
	if(thisValue != "" && thisValue.indexOf("unknown zip") == -1  && thisValue.indexOf("no location found") == -1)	
	{
		bIsValidLocationSelected = true;
		objForm.sRadiusLocation.value = thisValue;
		doShowHide("hide");	/* User has selected location so hide drop-down */
		objForm.sRadiusLocation.focus();	/* focus on text box to allow user to type in something else */
	}
	else	
	{
		bIsValidLocationSelected = false;
		objForm.sRadiusLocation.value = "";	/* invalid data, so clear text box */
		doShowHide("hide");	/* User has selected location so hide drop-down */
		objForm.sRadiusLocation.focus();	/* focus on text box to allow user to type in something else */
	}
}

function doAJAXSearch(thisString)
{
	if((isNaN(thisString) && thisString.length > 3) || thisString.length > 4)     
	{
		var thisUrl = "../queries/qFindLocations_US.cfm?locationString=" + escape(thisString);
		doAjax(thisUrl);
	}
	else if	(thisString == "")
	{
		doSetValue("");	
	}
}

function doAjax(url)	
{
	if ( window.XMLHttpRequest )
    	ajaxObj = new XMLHttpRequest();
	else if ( window.ActiveXObject ) {
		try {
        	ajaxObj = new ActiveXObject('Msxml2.XMLHTTP');
		} catch(err) {
			ajaxObj = new ActiveXObject('Microsoft.XMLHTTP');
		}
	}
	
	ajaxObj.onreadystatechange = doPopulateSelectBox;
	ajaxObj.open("GET", url, true);
	ajaxObj.send(null);
}

function doPopulateSelectBox(thisList)
{
	if (ajaxObj.readyState == 4)
	{
		if (ajaxObj.status == 200) {
			ajaxElemList = ajaxObj.responseText;
			PopulateSelectBox(ajaxElemList);
		}
		else
		{
			//alert ( "Unable to retrieve data." );
		}
	}
}

function PopulateSelectBox(thisList)
{
	objForm.locationChoices.options.length = 0;

	if(typeof thisList != "undefined" && thisList != "")	{
		var thisArray = thisList.split("|");
		if(thisArray.length > 0)	{
			doShowHide("show");	/* Ajax has returned some results so display drop-down */
			for(i=0; i < thisArray.length; i++)	{
				thisOption = new Option(thisArray[i], thisArray[i]);
				objForm.locationChoices.options[i] = thisOption;
			}
		}
		else	{
			doShowHide("hide");	/* No results from Ajax so hide drop-down */
		}
	}
	else	{
		doShowHide("hide");	/* No results from Ajax so hide drop-down */
	}
}

function doShowHide(whatAction)
{
	locationDv = document.getElementById("locationDiv");

	if(whatAction == "show")
	{
		locationDv.style.display = "inline";
	}
	else
	{
		locationDv.style.display = "none";
	}
}

function registerForm(theForm) 
{
	objForm	= theForm;
}

function attachEventListener(target, eventType, functionRef, capture)
{
	if (typeof target.addEventListener != "undefined")
	{
		target.addEventListener(eventType, functionRef, capture);
	}
	else if (typeof target.attachEvent != "undefined")
	{
		target.attachEvent("on" + eventType, functionRef);
	}

	return true;
}




