var canHide = true;
var timer = null;

function InitSearchBoxes(searchString, txtGSearchClientID)
{												
	if (!searchString) {        
		searchString = "Search";    
	}
	inputs = document.getElementById(txtGSearchClientID);
	
	//if (inputs.value.length == 0) {
		inputs.value = searchString;
	//}
	inputs.onfocus = function() {
		if (this.value == searchString) {                  
			this.value = "";    
		}
		//HideDiv("autoComplete_completionListElem");                   
	}                
	inputs.onblur = function() {
		if (this.value.length == 0) {
			this.value = searchString;
		}     
		if(canHide == true)
		{           
			HideDiv("autoComplete_completionListElem");  
		}
	}
	document.getElementById(txtGSearchClientID).focus();
	document.getElementById(txtGSearchClientID).blur();
}

function Initialize()
{
    try
    {
        req=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            req=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(oc)
        {
            req=null;
        }
    }

    if(!req&&typeof XMLHttpRequest!="undefined")
    {
        req=new XMLHttpRequest();
    }
}

function SendQuery(key)
{
	if(timer != null)
	{
		clearTimeout(timer);
		timer = null;
	}
	
	if(key.length > 2)
	{
		timer = setTimeout(function(){ExecuteQuery(key)}, 300);
    }
}

function ExecuteQuery(key)
{
		Initialize();
		
		var url="searchService.aspx?key="+key;
		
		if(req!=null)
		{
			req.onreadystatechange = Process;
			req.open("GET", url, true);
			req.send(null);
		}
}

function Process()
{
    if (req.readyState == 4)
        {
        // only if "OK"
            if (req.status == 200)
            {
                if(req.responseText=="")
                    HideDiv("autoComplete_completionListElem");
                else
                {
                    ShowDiv("autoComplete_completionListElem");
                    document.getElementById("autoComplete_completionListElem").innerHTML = req.responseText;
                }
            }
            else
            {
                //document.getElementById("autoComplete_completionListElem").innerHTML= "There was a problem retrieving data:<br>" + req.statusText;
            }
        }
}

function ShowDiv(divid)
{
   if (document.layers) document.layers[divid].visibility="show";
   else document.getElementById(divid).style.visibility="visible";
}

function HideDiv(divid)
{
   if (document.layers) document.layers[divid].visibility="hide";
   else document.getElementById(divid).style.visibility="hidden";
}


function BodyLoad()
{
    HideDiv("autoComplete_completionListElem");
    document.form1.txtGSearch.focus();

}
function SwitchStyleOn(elem)
{
	elem.className = "autocomplete_highlighted_listitem";
	canHide = false;
}
function SwitchStyleOut(elem)
{
	elem.className = "autocomplete_listItem";
	canHide = true;
}
function SetSuggestion(elem)
{
	//document.getElementById("txtGSearch").value = elem.innerHTML;
	window.location.href = '/department.aspx?mode=search&cps=0&key=' +elem.innerHTML;
	HideDiv("autoComplete_completionListElem");
}
function SetSuggestionDesigner(elem)
{
	//document.getElementById("txtGSearch").value = elem.innerHTML;
	window.location.href = elem;
	HideDiv("autoComplete_completionListElem");
}

function SetSuggestionProduct(elem)
{
	//document.getElementById("txtGSearch").value = elem.innerHTML;
	window.location.href = elem;
	HideDiv("autoComplete_completionListElem");
}