﻿var xmlhttpBrowseSearch;
var handlerPath = document.getElementById("hidAjaxHandlerPath").value;

function postBrowseSearchRequest(pString) {
    postBrowseSearchRequestCalled(pString, true);
}

function postBrowseSearchRequestCalled(pString,toggleSearchInstructions) {

    if (toggleSearchInstructions == true) {
        if (document.getElementById("browseSearchLinkSearchSection") != undefined) {
            document.getElementById("browseSearchLinkSearchSection").style.display = "none";
        }

        if (document.getElementById("jobSearchResultsSection") != undefined) {
            document.getElementById("jobSearchResultsSection").style.display = "none";
        }

        if (document.getElementById("browseSearchInstructions") != undefined) {
            document.getElementById("browseSearchInstructions").style.display = "";
        }
    }
    
    document.getElementById("browseSearchForm").style.display = "none";
    document.getElementById("loadingMessage").style.display = "";
    
    xmlhttpBrowseSearch = null;
    if (window.XMLHttpRequest) {// code for all new browsers
        xmlhttpBrowseSearch = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {// code for IE5 and IE6
        xmlhttpBrowseSearch = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlhttpBrowseSearch != null) {
        xmlhttpBrowseSearch.onreadystatechange = handleBrowseSearchResponse;
        xmlhttpBrowseSearch.open("GET", handlerPath + '?' + pString, true);
        xmlhttpBrowseSearch.send(null);
    }
    else {
        alert("Your browser does not support AJAX.");
    }
}

function handleBrowseSearchResponse() {
    if (xmlhttpBrowseSearch.readyState == 4) {// 4 = "loaded"
        if (xmlhttpBrowseSearch.status == 200) {// 200 = OK
            //Logic Here
            var output = xmlhttpBrowseSearch.responseText;
            document.getElementById("loadingMessage").style.display = "none";
            if (output == "-1") {
                showZeroResultsMessage();
            }
            else {
                document.getElementById("dynamicControls").innerHTML = output;
                document.getElementById("browseSearchForm").style.display = "";
                
                document.getElementById("btnAjaxSearchShowResults").focus();                
            }
        }
        else {
            alert("GetBrowseSearch AJAX call failed: " + xmlhttpBrowseSearch.status + " : " + xmlhttpBrowseSearch.responseText);
        }
    }
}

function showZeroResultsMessage() {
    document.getElementById("browseSearchForm").style.display = "none";
    document.getElementById("loadingMessage").style.display = "none";
    document.getElementById("zeroResultsMessage").style.display = "";
}

function buildRequestParamsString(_optionsCriteria) {
    var _pString = buildDefaultParamsString();

    if (_optionsCriteria == "")
        _pString = _pString.substring(0, _pString.length - 1);
    else
        _pString += _optionsCriteria;

    return _pString;
}

function buildDefaultParamsString() {
    var _pString = "";
    var elemColl = document.getElementsByTagName("input");
    for (i = 0; i < elemColl.length; i++) {
        if (elemColl[i].type == "hidden" && elemColl[i].id.indexOf("hidHandler") >= 0) {
            _pString += elemColl[i].id + "=" + elemColl[i].value + "&";
        }
    }
    return _pString;
}

function showSearchResults() {
    var _resultsURL = document.getElementById("hidShowResultsURL").value;
    window.location.href = _resultsURL;
}

function showDefaultBrowseSearchOptions() {
    var _optionsCriteria = document.getElementById("hidPageOptionsCriteria").value;
    var _paramString = buildRequestParamsString(_optionsCriteria);
    postBrowseSearchRequestCalled(_paramString, false);
}

function showStartAgainBrowseSearchOptions() {
    document.getElementById("zeroResultsMessage").style.display = "none";
    var _paramString = buildRequestParamsString("");
    postBrowseSearchRequest(_paramString);
}

function showOneLevelUpBrowseSearchOptions() {
    document.getElementById("zeroResultsMessage").style.display = "none";
    var _optionsCriteria = "";
    if (document.getElementById("hidShowResultsOptionsString") != undefined
                            && document.getElementById("hidShowResultsOptionsString").value != "") {
        _optionsCriteria = document.getElementById("hidShowResultsOptionsString").value;
    }
    var _paramString = buildRequestParamsString(_optionsCriteria);
    postBrowseSearchRequest(_paramString);
}

function onKeywordClicked() {
    var _keywordDefaultText = document.getElementById("hidHandlerKeywordDefaultText").value;
    var _keywordCurrentText = document.getElementById("hidAjaxSearchKeywordEncoded").value;

    if (_keywordCurrentText == _keywordDefaultText) {
        document.getElementById("txtAjaxSearchKeyword").value = "";
        document.getElementById("hidAjaxSearchKeywordEncoded").value = "";
        document.getElementById("hidAjaxSearchKeywordPrevious").value = "";
    }
}

function postKeywordBrowseSearchRequest(pString) {
    var _keywordPreviousText = document.getElementById("hidAjaxSearchKeywordPrevious").value;
    var _keywordCurrentText = document.getElementById("txtAjaxSearchKeyword").value;
    if (_keywordPreviousText != _keywordCurrentText) {
        if (pString != "")
            pString += "&";
        else
            pString += "?";

        pString += "q=" + encodeURIComponent(document.getElementById("txtAjaxSearchKeyword").value);
        //alert(pString);
        postBrowseSearchRequest(pString);
    }
}

function changeOptionsCriteria(elem) {    
}

showDefaultBrowseSearchOptions();