﻿// JScript File
//<![CDATA[

var strPath;

    function TriggerSearch(event)
        {
             var intKeyCode;
            intKeyCode = event.keyCode;
            if (intKeyCode == 13)
            {
                Search();                
                return false;
            } else {
	            return true;
			}
        }
       
        function getMatches(event, iId, strApPath, lang)
        {
            if (event.keyCode != 13)
            {        
                strPath=strApPath;
                txtSearch = document.getElementById(iId).value;   
                setTimeout("getNextWord('" + iId + "','" + txtSearch.replace("'","\\'") + "'," + event.keyCode + ",'" + strApPath + "','" + lang +"' )", 500);
            }
        }

        function searchButtonClicked(strApPath,iId, lang)
        {   
            strPath=strApPath;        
            //document.getElementById('searchbutton').style.background  = "url(" + strApPath + "_ui/images/btn-go-loader.gif) top left no-repeat";
            txtSearch =  document.getElementById(iId).value;
            getNextWord(iId, txtSearch.replace("'", "\\'"), null, strApPath, lang);
        }

          function Search() 
            {            
                if(document.getElementById('divList').innerHTML.replace(/^\s*/, "").replace(/\s*$/, "") != "")
                {
                    var firstmatch = document.getElementById('divList').getElementsByTagName("li")[0];
                    if(firstmatch.innerHTML != "No match found")
                    {
                        window.location = firstmatch.getElementsByTagName("a")[0].href;
                    }
                }
                return false;
            }   
            
            
        function OnTimeOut(arg) 
        {
            alert("TimeOut encountered when calling web service.");
            document.getElementById('searchbutton').style.background  = "url(" + strApPath + "_ui/images/btn-go.gif) top left no-repeat";            
        }

        function OnError(arg) 
        {
            alert("Error : " + arg);
            document.getElementById('searchbutton').style.background  = "url(" + strApPath + "_ui/images/btn-go.gif) top left no-repeat";            
        }
        
        function hideList()
        {
            document.getElementById('dropdowndiv').className = "dropdown showing";
            document.getElementById('divList').style.display = "none";
            document.getElementById('divTop').style.display = "none";
            document.getElementById('divBottom').style.display = "none";
        }
        
        document.body.onclick = hideList;        

      
        
        function ChangeColor(div, flag)
        {
            if(flag == 1)
            {
                div.style.backgroundColor = "LightGrey";
                div.style.cursor = "pointer";
            }
            else
            {
                div.style.backgroundColor = "Transparent";            
                div.style.cursor = "default";                
            }
        }  
        
        function getNextWord(iId, oldSearch, intKeyCode, strApPath, lang) 
        {
            txtSearch = document.getElementById(iId).value.replace("'", "\\'");   
                        
            if(txtSearch == oldSearch)
            {
                if  (((intKeyCode >= 48) && (intKeyCode <= 57)) || // Numbers 0-9
                    ((intKeyCode >= 65) && (intKeyCode <= 90)) || // Upper case A-Z
                    ((intKeyCode >= 97) && (intKeyCode <= 122)) ||  // Lower case a-z
                    (intKeyCode == 189) || // Hyphen
                    (intKeyCode == 32) || // Space
                    (intKeyCode == 8) || // Backspace
                    (intKeyCode == null)  // search button
                    )        
                {
                    if(txtSearch != "" && txtSearch.length > 2)
                    {
                        //strOldText = txtSearch;                       
                        document.getElementById('searchbutton').style.background  = "url(" + strApPath + "_ui/images/btn-go-loader.gif) top left no-repeat";
                        ret = AutoComplete.GetMatches(txtSearch, lang, OnComplete, OnError, OnTimeOut);
                        return (true);
                    }
                    else
                    {
                        document.getElementById('divList').style.display = "none";
                        document.getElementById('divTop').style.display = "none";
                        document.getElementById('divBottom').style.display = "none";
                    }        
                }
            }
            return (false);
        }
        
        function OnComplete(arg) 
        {    
            var divList = document.getElementById('divList');
            var divTop = document.getElementById('divTop');
            var divBottom = document.getElementById('divBottom');
            divList.innerHTML = "";               
       
            divList.innerHTML = arg;
            
            if(divList.innerHTML != "")
            {
                divList.style.display = "";
                divTop.style.display = "";
                divBottom.style.display = "";
                divList.style.width = null;
                if(divList.clientWidth < 200)
                {
                    divList.style.width = "200px";
                }        
            }
            else
            {
                divList.style.display = "none";
                divTop.style.display = "none";
                divBottom.style.display = "none";
				document.getElementById('dropdowndiv').className = "dropdown showing";
            }       

            document.getElementById('searchbutton').style.background  = "url(" + strPath + "_ui/images/btn-go.gif) top left no-repeat";
            // Select the text inside the textbox (except the prefix)
            //SelectText (strOldText.length, arg.length);
        }


  //]]>