﻿var req = null;
var timr = null;
var suggtmr = null;

function submitSearch(searchBox) {
    keyValue = document.getElementById(searchBox).value;
    if (keyValue != "") {
        document.location.href = "/search.aspx?q=" + escape(keyValue);
    }
}

function GetSearchSuggestions(pressevent) {
    keyValue = document.getElementById("search_box").value;


    if (keyValue != "") {
        //alert(keyValue)


        var charCode = (pressevent.which) ? pressevent.which : (event.keyCode);

        // alert(charCode);

        // Send to the Server Side Method to get the string

        if (charCode >= 65 && charCode <= 90 || charCode >= 97 && charCode <= 122) {
            if (timr != null) {
                clearTimeout(timr);
            }
            timr = setTimeout("GetSearchResults()", 500);
        }

        // if the backspace key (8) is pressed and 48 is for the delete button

        else if (charCode == 8 || charCode == 48) {

            // Reset the count

            _highlightSuggestionIndex = -2;

            if (timr != null) {
                clearTimeout(timr);
            }
            timr = setTimeout("GetSearchResults()", 500);

        }

        // when the down arrow key is pressed

        else if (charCode == 40) {

            if ((_highlightSuggestionIndex + 2) <= document.getElementById("SearchSuggestionsDisplay").childNodes.length) {

                _highlightSuggestionIndex = _highlightSuggestionIndex + 2;

            }

            Highlight(_highlightSuggestionIndex);

        }

        // When the up arrow key is pressed

        else if (charCode == 38) {
            if ((_highlightSuggestionIndex - 2) >= 0) {

                _highlightSuggestionIndex = _highlightSuggestionIndex - 2;

            }

            Highlight(_highlightSuggestionIndex);
        }
    }
    else {
        //document.getElementById("SearchResultsContainer").style.visibility = "invisible";
        document.getElementById("SearchResultsContainer").style.display = "none";
    }
}

function KeepSearchSuggestions() {
    if (suggtmr != null) {
        clearTimeout(suggtmr);
        suggtmr = null;
    }
}

function HideSearchSuggestions() {
    suggtmr = setTimeout("HideSearchSuggestionsNow()", 3000);
}

function HideSearchSuggestionsNow() {
    if (suggtmr != null) {
        document.getElementById("SearchResultsContainer").style.display = "none";
    }
}

function GetSearchResults() {
    keyValue = document.getElementById("search_box").value;
    AjaxGetSearchData('/SiteSphereHandler.ashx?search=' + escape(keyValue), resultHandler);
}

function resultHandler() {
    try {
        //readyState of 4 or 'complete' represents  
        //that data has been returned  
        if (req.readyState == 4 ||
            req.readyState == 'complete') {
            word = req.responseText;
            //document.location.href = document.location.href;
            //alert(req.responseText);

            //document.getElementById("SearchResultsContainer").style.visibility = "visible";
            document.getElementById("SearchResultsContainer").style.display = "block";

            document.getElementById("SearchSuggestionsDisplay").innerHTML = "<div><ul>" + word.substring(0, word.length - 4) + "</ul></div>";

        }
    }
    catch (e) {
        alert('Error in Ajax respone');
    }
}

function AjaxGetSearchData(url, responseHandler) {
    if (window.XMLHttpRequest) {
        // browser has native support for XMLHttpRequest object 
        req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        // try XMLHTTP ActiveX (Internet Explorer) version 
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (req) {
        req.onreadystatechange = responseHandler;
        req.open('GET', url, true);
        req.setRequestHeader("content-type", "application/x-www-form-urlencoded");
        req.send('');
    }
    else {
        alert('Your browser does not seem to support XMLHttpRequest.');
    }
}

function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        //Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
