﻿// JScript File
var oCategoriesCombo = null;
var oSearchTextBox = null;
var sLastSearch = "";
var bMovingFocusToListBox = false;
var oFreeSearchText = null;

/*--------------------------------------------------------------------------------------*/
function InitSearch()
{
	var oFreeSearchText = uctlRegionSearch_txtFreeSearch;
	oFreeSearchText.onkeyup = __SP_Search;
	oFreeSearchText.onblur = __SP_SearchBoxBlur;
}
/*--------------------------------------------------------------------------------------*/
function __SP_Search(oEvent) 
{
   	var iKeycode = -1;
   	var oTextBox = null;
   	
   	if (!oEvent)
   		oEvent = window.event; // Explorer
   	
	iKeycode = oEvent.keyCode;
	
	oTextBox = oEvent.target; // FireFox
	if (!oTextBox)
		oTextBox = oEvent.srcElement; // Explorer
	
	oSearchTextBox = oTextBox;
	if (sLastSearch != oTextBox.value) 
	{
		sLastSearch = oTextBox.value;
		var oFrame = document.getElementById('ifrGetTextSearch');
		oFrame.contentWindow.location.replace("ifrGetCities.aspx?Text=" + encodeURIComponent(oTextBox.value));
	}

	if (iKeycode == 40 && oSearchTextBox) 
	{
		// event is not fired for this key in Opera, so it won't work there. Whatever.
		var oDestList = __SP_GetListBoxRef();
		
		if (oDestList.style.display == "inline") {
			bMovingFocusToListBox = true;
			
			try 
			{
				oDestList.setActive(); // Explorer
			}
			catch(e) {}
			
			oDestList.focus();
			
			oEvent.returnValue = false;	
		}
	}
	
	if (iKeycode  == 13 && oTextBox.value.length > 0) 
	{
		event.returnValue = false;
		__SP_GetFreeSearchButtonRef().click();
	}
}
/*--------------------------------------------------------------------------------------*/
function __SP_GetListBoxRef() {
	return oSearchTextBox.parentNode.children[1];
}
/*--------------------------------------------------------------------------------------*/
function __SP_GetFreeSearchButtonRef() {
	return uctlRegionSearch_cmdSearch;
}
/*--------------------------------------------------------------------------------------*/
function __SP_DisplayAutocomplete(sListClientID) {
	if (oSearchTextBox) 
	{
		var oFrame = document.getElementById('ifrGetTextSearch');
		var oSrcList = oFrame.contentWindow.document.getElementById(sListClientID);
		var oDestList = __SP_GetListBoxRef();
		var iWidth = oSearchTextBox.clientWidth;

		if (document.all) 
		{
			// Explorer
			oDestList.outerHTML = "<select id=\"lstAutocomplete\" size=\"10\" " + 
				" style=\"position:absolute;direction:rtl;z-index:100;display:inline;width:" + iWidth + "px;top:22px;left:0;\" " + 
				">" + oSrcList.innerHTML + "</select>";
		}
		else 
		{
			//FireFox and others
			oDestList.innerHTML = oSrcList.innerHTML;
			oDestList.style.display = "inline";
			oDestList.style.width = iWidth + "px";
		}
		
		oDestList = __SP_GetListBoxRef(); // once again, yep - the reference may become invalid after setting outerHTML
		
		oDestList.onblur = __SP_HideListBox;
		oDestList.onclick = __SP_SearchListBox_Click;
		oDestList.onkeydown = __SP_SearchListBox_KeyDown;
		oDestList.onmousedown = __SP_SearchListBox_MouseDown;
	}
}
//--------------------------------------------------------------------------------------
function Utils_AddComboOption(oCombo, sText, sID) {
	var oOption = document.createElement("OPTION");
	
	oCombo.options[oCombo.options.length] = new Option(sText, sID);
}
//--------------------------------------------------------------------------------------
function __SP_HideListBox() {
	if (oSearchTextBox) 
	{
		var oDestList = __SP_GetListBoxRef();
		oDestList.style.display = "none";
	}
}
/*--------------------------------------------------------------------------------------*/
function __SP_SearchBoxBlur(oEvent) {
	if (oSearchTextBox) 
	{
		var oDestList = __SP_GetListBoxRef();
		
		if (!bMovingFocusToListBox)
			__SP_HideListBox();
	}
	
	bMovingFocusToListBox = false;
}
/*--------------------------------------------------------------------------------------*/
function __SP_SearchListBox_Click() {
	if (oSearchTextBox) 
	{
		var oDestList = __SP_GetListBoxRef();
		
		if (oDestList.selectedIndex >= 0) 
		{
			oSearchTextBox.value = Utils_Trim(oDestList.options[oDestList.selectedIndex].text);
				
			__SP_HideListBox();

			__doPostBack(uctlRegionSearch_cmdSearch.name,'');
		}
	}
}
/*--------------------------------------------------------------------------------------*/
function __SP_SearchListBox_KeyDown(oEvent) {
	if (!oEvent)
		oEvent = window.event; // Explorer
		 
	var iKeyCode = oEvent.keyCode;

	if (iKeyCode == 13) 
	{
		__SP_SearchListBox_Click();
	}
}
/*--------------------------------------------------------------------------------------*/
function __SP_SearchListBox_MouseDown() {
	bMovingFocusToListBox = true;
}
//---------------------------------------------------------------------------------------
function Utils_LTrim(str)
{
	return str.replace( /^\s*/, "" );
}
/*--------------------------------------------------------------------------------------*/
function Utils_RTrim(str)
{
	return str.replace( /\s*$/, "" );
}
/*--------------------------------------------------------------------------------------*/
function Utils_Trim(str)
{
	return Utils_RTrim(Utils_LTrim(str));
}
/*--------------------------------------------------------------------------------------*/
function ClearSearch()
{
      oFreeSearchText.value = "";
}
/*--------------------------------------------------------------------------------------*/
function Area_Changed(sDefaultValue) 
{
	if (uctlRegionSearch_txtFreeSearch.value != sDefaultValue)
		uctlRegionSearch_txtFreeSearch.value = "";
}
/*--------------------------------------------------------------------------------------*/
function SearchTextBox_OnFocus(sDefaultValue)
{
	if (uctlRegionSearch_txtFreeSearch.value == sDefaultValue)
	{
		uctlRegionSearch_txtFreeSearch.value = "";
		uctlRegionSearch_txtFreeSearch.style.color = "black";
	}
}
/*--------------------------------------------------------------------------------------*/
