﻿// JScript File


var req = null;

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(key.length < 3)
    {
        HideDiv("ac_container");
        return;
    }

    Initialize();
    
    //var url="search.aspx?search-term=" + key;                                     //production URL
    var url="http://localhost/dsiWebSite/search.aspx?search-term=" + key;           //dev URL

    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("ac_container");
            else
            {
                
                //document.getElementById("autocomplete").innerHTML = req.responseText;
                document.getElementById("ac_container").innerHTML = req.responseText;
                ShowDiv("ac_container");
            }            
         }
         else
         {
                document.getElementById("ac_container").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("ac_container");
    document.getElementById('txtSearchPhrase').focus();    
}


function DelayDropDownResultsHideOnBlur()
{
    setTimeout("HideDiv('ac_container')", 300);
}


function ServiceTypeLink(homePageURL, businessPageURL, searchPhrase)
{  
    if(homePageURL.length > 0 && businessPageURL.length > 0)
        window.location.href = 'search.aspx?homeservicesurl=' + homePageURL + '&businessservicesurl=' + businessPageURL;
    else if(homePageURL.length > 0)
        window.location.href = homePageURL;
    else if(businessPageURL.length > 0)
        window.location.href = businessPageURL;
}

function UserFocusSearchTextBox(textInSearchBox)
{
    if(textInSearchBox.length >= 3)
    {
        ShowDiv("ac_container");
        return;
    }
}
