﻿// *********************************************************************
// * NeptuneLoader.js
// *
// * Copyright 2008 CCH, a Wolters Kluwer business
// *********************************************************************
function NeptuneLoader_createPager(curPage, maxPage, requestFunc)
{
    var rfString = new Function("p", "t", "return $(\"<a></a>\").attr(\"href\", \"javascript:window[\'" + this.winId + "\']." + requestFunc + "(\" + p + \")\").text(t);");
    var retVal = $("<table id=\"pager" + requestFunc + "\" class=\"pager\"></table>");
    var row = $("<tr></tr>");
    retVal.append(row);
        
    if (curPage > 1)
    {
        row.append($("<td></td>").append(rfString(0, "[First]")));
    }
    
    if (curPage > 0)
    {
        row.append($("<td></td>").append(rfString(curPage - 1, "[Prev]")));
    }

    row.append("<td>Page " + (curPage + 1) + " of " + (maxPage + 1) + "</td>");
    
    if (maxPage > curPage)
    {
        row.append($("<td></td>").append(rfString(curPage + 1, "[Next]")));
    }
    
    if ((curPage + 1) < maxPage)
    {
        row.append($("<td></td>").append(rfString(maxPage, "[Last]")));
    }
    
    return retVal;
}
// **********
// * Browse
// **********
function NeptuneLoader_requestBrowse(page)
{
    this.browsePageNum = page;
    if (this.browseCached || this.initialPage)
    {
        $("#All").block({message: "<p>Loading documents&hellip;</p>", css: {border: "3px solid #EE014C", padding: "5px"}});
    }
    else
    {
        $("#All").block({message: "<p>Loading documents&hellip;</p><p>This may take a while, but will be faster in subsequent pages.</p>", css: {border: "3px solid #EE014C", padding: "5px"}});
        this.browseCached = true;
    }
    this.initialPage = false;
    var sendParams = new Object();
    sendParams.data = {path: this.path, page: page, format: "xml", cluster: "DefaultCluster"};
    sendParams.dataType = "xml";
    sendParams.error = new Function("request", "textStatus", "window[\"" + this.winId + "\"].browseError(request, textStatus);");
    sendParams.success = new Function("data", "textStatus", "window[\"" + this.winId + "\"].browseResult(data, textStatus);");
    sendParams.url = this.browsePage;
    $.ajax(sendParams);
}
function NeptuneLoader_browseResult(data, textStatus)
{
    this.browseDocCount = parseInt($("documentCount", data).text());
    this.browsePageCount = Math.ceil(this.browseDocCount / this.pageSize);
    var allDiv = $("#All");
    allDiv.unblock();
    allDiv.empty().append(this.createPager(this.browsePageNum, this.browsePageCount - 1, "requestBrowse"));
    $("<ol id=\"resultList\" start=\"" + ((this.browsePageNum * this.pageSize) + 1) + "\"></ol>").appendTo(allDiv);
    
    $("result result", data).each(function(i)
    {
        var title = $("title", this).text();
        var solutionId = $("solutionId", this).text();
        var itm = $("<li></li>").append($("<a></a>").append(title).attr("href", "solution.aspx?id=" + encodeURIComponent(solutionId) + "&" + window.urlSuffix)).append(" (" + solutionId + ")");
        $("#resultList").append(itm);
    });
    
    if ($("message text", data)[0])
    {
        allDiv.empty().append("<p>There are no solutions associated with this product.</p>");
        var tIdx = $("#TabList").tabs("length") - 1;
        $("#TabList").tabs("disable", tIdx);
    }
}
function NeptuneLoader_browseError(request, textStatus)
{
    var allDiv = $("#All");
    allDiv.unblock();
    allDiv.empty();
    $("<p>An error has occurred in contacting the search server. Please try again later.</p><p>Error: " + textStatus + "</p>").appendTo(allDiv);
}
// **********
// * Search
// **********
function NeptuneLoader_requestSearch(p)
{
    this.searchPageNum = p;
    $("#Search").block({message: "<p>Searching&hellip;</p>", css: {border: "3px solid #EE014C", padding: "5px"}});
    var sendParams = new Object();
    this.currentSearch = $("#txtSearch").attr("value");
    sendParams.data = {path: this.path, page: p, format: "xml", searchTerm: this.currentSearch, cluster: "DefaultCluster"};
    sendParams.dataType = "xml";
    sendParams.error = new Function("request", "textStatus", "window[\"" + this.winId + "\"].searchError(request, textStatus);");
    sendParams.success = new Function("data", "textStatus", "window[\"" + this.winId + "\"].searchResult(data, textStatus);");
    sendParams.url = this.searchPage;
    $.ajax(sendParams);
}
function NeptuneLoader_searchResult(data, textStatus)
{
    var resultList = $("<ol id=\"SearchResultList\"></ol>");
    this.searchDocCount = parseInt($("answer", data).attr("responseCount"));
    this.searchPageCount = Math.ceil(this.searchDocCount / this.searchPageSize);
    if (this.searchDocCount > 0)
    {
        resultList.attr("start", "" + ((this.searchPageNum * this.searchPageSize) + 1));
        $("response document", data).each(function(i)
        {
            var li = $("<li></li>");
            var a = $("<a></a>").appendTo(li);
            a.append($("title", this).text());
            var solutionId = $("meta[name='$solution_id']", this).attr("content");
            if (solutionId)
            {
                li.append(" (" + solutionId + ")");
                a.attr("href", "solution.aspx?id=" + encodeURIComponent(solutionId) + "&" + window.urlSuffix);
            }
            resultList.append(li);
        });
        $("#SearchResults").empty().append(this.createPager(this.searchPageNum, this.searchPageCount - 1, "requestSearch")).append(resultList);
    }
    else
    {
        $("#SearchResults").empty().append($("<p>No results were found for </p>").append("\"").append(this.currentSearch).append("\""));
    }
    $("#Search").unblock();
}
function NeptuneLoader_searchError(request, textStatus)
{
    $("#Search").unblock();
    $("#SearchResults").empty().append("<p>An error has occurred contacting the search server. Please try again later.</p>");
}
function NeptuneLoader_newSearchFunction( )
{
    return new Function("window[\"" + this.winId + "\"].requestSearch(0);");
}
// **********
// * Solution
// **********
function NeptuneLoader()
{
    if (window[this.winId])
    {
        return window[this.winId];
    }
    window[this.winId] = this;
    this.path = window.prodInfo.path;
    this.browsePageNum = 0;
    this.browsePageCount = 0;
    this.browseDocCount = 0;
    this.searchPageNum = 0;
    this.searchPageCount = 0;
    this.searchDocCount = 0;
    this.currentSearch = $("#txtSearch").attr("value");
    this.browseCached = false;
    this.initialPage = true;
    if (this.currentSearch != "")
    {
        this.requestSearch(0);
        $(document).tabs("select", $(document).tabs("length") - 1);
    }
    window.urlSuffix = "prodpath=" + encodeURIComponent(this.path) + "&prodname=" + encodeURIComponent(window.prodInfo.name);
}
NeptuneLoader.prototype.winId = "E2407107915C47B99EC1A940D63B5E86";
NeptuneLoader.prototype.browsePage = "/neptune/browse.do";
NeptuneLoader.prototype.searchPage = "/neptune/search.do";
NeptuneLoader.prototype.solutionPage = "/neptune/solution.do";
NeptuneLoader.prototype.browseResult = NeptuneLoader_browseResult;
NeptuneLoader.prototype.browseError = NeptuneLoader_browseError;
NeptuneLoader.prototype.requestBrowse = NeptuneLoader_requestBrowse;
NeptuneLoader.prototype.requestSearch = NeptuneLoader_requestSearch;
NeptuneLoader.prototype.searchResult = NeptuneLoader_searchResult;
NeptuneLoader.prototype.searchError = NeptuneLoader_searchError;
NeptuneLoader.prototype.createPager = NeptuneLoader_createPager;
NeptuneLoader.prototype.newSearchFunction = NeptuneLoader_newSearchFunction;
NeptuneLoader.prototype.pageSize = 20;
NeptuneLoader.prototype.searchPageSize = 20;