﻿
function fnOnTabClick(label, scope, linkID){
    // build a nice searching message.
    var sMessage = "Retrieving " + label + " Resources...";

    // set the hidden field to the value of the clicked tab.
    // fnCreateSearchXml will pick this value up after the timeout occurs.
    $("#hidScope").val(scope);
    
    // set the active tab;
    $("#current").attr("id", "");
    $("#"+linkID).parent().attr("id", "current");
    
    fnKickOffSearch(sMessage);
}

function fnOnDropdownChange(){
    fnKickOffSearch("Searching Resources...");
}

function fnOnPagingClick(iPageNumber){
    fnKickOffSearch("Searching Resources...", iPageNumber);
}

function fnKickOffSearch(message, iPageNumber){
    $("#divGrid").hide();
    $("#divSearchPrompt").hide();
    $("#divSearching").show();
    $("#spnSearchMessage").html(message);
    
    iPageNumber = (iPageNumber)? iPageNumber : 1;
    
    window.setTimeout("fnCreateSearchXml(" + iPageNumber + ")", 500);
}

function fnCreateSearchXml(iPageNumber){
    var sTopicID = $("#hidTopicID").val();
    var sScope = $("#hidScope").val();
    var sCountyCity = $("#" + $("#hidCountyCityID").val()).val();
    var sType = $("#" + $("#hidTypeID").val()).val();
    var sBusFunction = $("#" + $("#hidBusinessFunctionID").val()).val();
    var sKeywords = $("#txtKeywords").val();

    iPageNumber = (iPageNumber)? iPageNumber : 1;

    var sXml = '<Search>';
        if(sTopicID)sXml += '<TopicID>' + sTopicID + '</TopicID>';
        if(sScope)sXml += '<Scope><![CDATA[' + sScope + ']]></Scope>';
        if(sCountyCity)sXml += '<County>' + sCountyCity + '</County>';
        if(sType)sXml += '<Type>' + sType + '</Type>';
        if(sBusFunction)sXml += '<BusFunction>' + sBusFunction + '</BusFunction>';
        if(sKeywords)sXml += '<Keywords>' + sKeywords + '</Keywords>';
        sXml += '</Search>';

    fnExecuteSearch(sXml, iPageNumber);
}

function fnExecuteSearch(sXml, iPageNumber){    
    iPageNumber = (iPageNumber)? iPageNumber : 1;

    // build the url for the ajax call.
    var blnHasQString = (window.location.toString().indexOf("?") != -1);
    var sUrl = window.location.toString();
    sUrl += (blnHasQString) ? "&" : "?";
    sUrl += "action=getlist&page=" + iPageNumber;

    $.ajax({
        type: "POST",
        url: sUrl,
        data: sXml,
        success: function(html) {
            var sGridID = $("#hidGridID").val();
        
            // insert search results
            $("#divGrid").html(html);
            
            // set the result count
            $("#spnResultCount").html($("#hidResultCount").val());

            // check if the grid exists. If not, that means that no results were return - insert the 'no results' message.
            if(!$("#" + sGridID).get(0)){
                $("#divGrid").html('<div class="list_no_results">No results found</div>');
            }
            
            // hide/show the search ui.
            $("#divGrid").show();
            $("#divSearching").hide();
        },
        error: function(XmlHttpRequest, textStatus, errorThrown) {
            //alert("An error occured while processing your request.");
            var oWin = window.open();
            oWin.document.write(XmlHttpRequest.responseText);

            $("#divGrid").show();
            $("#divSearching").hide();
        }
    });
}