﻿// This function calls the Web service method and 
// passes the event callback function.
if (!window.jqDCCAJAX)
    jqDCCAJAX = jQuery.noConflict();

var strSearchMsg = null;
var strfaqSearch = null;
function FetchFAQs(_PortalID, _TabID, _ModuleId, _CatID, _BaseCulture, _CurrentCulture, _PageNo, _ThemeName, _NoQMsg, _strQIDToOpen, _strPermalinkTag, _strPermalinkText, _strExpandCollapseAllText, _strFAQAJAXEffect, _strFAQTooltip, _strShowOnlyOneFAQ, _strShowAllFAQExpanded, _faqSearch, _disableLocalization,_searchMsg) {
    strSearchMsg = _searchMsg;
     strfaqSearch = _faqSearch;
     DNNCentric.AJAXFAQ.AJAXFAQServicesController.FetchFAQs(_PortalID, _TabID, _ModuleId, _CatID, _BaseCulture, _CurrentCulture, _PageNo, _ThemeName, _NoQMsg, _strQIDToOpen, _strPermalinkTag, _strPermalinkText, _strExpandCollapseAllText, _strFAQAJAXEffect, _strFAQTooltip, _strShowOnlyOneFAQ, _strShowAllFAQExpanded, _faqSearch, _disableLocalization, SucceededCallback, FailedCallback);
}

// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object as a parameter.
function SucceededCallback(result, eventArgs) {
    var plchldr = null;
    if (result != null) {
        if (result.FAQs.length > 0) {
            
            var ModuleId = result.ModuleId;
            var thispageno = result.ThisPageNo;
            var totalpages = result.TotalPages;
            var themenametoprefix = result.ThemeName;
            var faqarray = result.FAQs;
            var permalinkTag = result.PermalinkTag;
            var permalinkText = result.PermalinkText;
            var expandCollapseAllText = result.ExpandCollapseAllText;
            var fAQAJAXEffect = result.FAQAJAXEffect;
            var fAQTooltip = result.FAQTooltip;
            var showonlyonefaq = result.ShowOnlyOneFAQ;
            var bshowALLFaqExpanded = result.showALLFAQExpanded;

            var strFAQHTMLMain = "<div id=\"faq" + themenametoprefix + "\">{0}</div>";
            var strFAQHTMLTemplate = ""
            if (permalinkTag.length > 0) {
                strFAQHTMLTemplate = "<span id='QID{2}' style=\"display:none\">{2}</span><h2>{0}</h2><div>{1}<span style=\"position:relative; top:-15px; vertical-align:bottom; float:right\"><a href=\"{3}\">{4}</a></span></div>";
            }
            else {
                strFAQHTMLTemplate = "<span id='QID{2}' style=\"display:none\">{2}</span><h2>{0}</h2><div>{1}</div>";
            }
            var strFAQHTMLTemplateForAdmin = "<h2><a href=\"{2}\">EDIT</a>{0}</h2><div>{1}</div>";

            var strNoQsMsg = result.NoQMsg;
            var strQuestionOpen = result.OpenQID;
            var strFAQList = "";
            var strFAQSingleQ = "";
            var ctr = 0;
            for (ctr = 0; ctr <= faqarray.length - 1; ctr++) {
                //strFAQSingleQ= String.format(strFAQHTMLTemplate, "Q " + faqarray[ctr].ID + ":" + " " + faqarray[ctr].Question, "Ans:-" + " " + faqarray[ctr].Answer);
                strFAQSingleQ = String.format(strFAQHTMLTemplate, " " + faqarray[ctr].Question, " " + faqarray[ctr].Answer, faqarray[ctr].QuestionID, faqarray[ctr].PermaLink, permalinkText);
                strFAQList = strFAQList + strFAQSingleQ;
                strFAQSingleQ = "";
            }
            strFAQList = String.format(strFAQHTMLMain, strFAQList);
            plchldr = jqDCCAJAX('div#dnn_ctr' + ModuleId + '_ModuleContent').find('td#dnn_ctr' + ModuleId + '_ViewFAQ_tdPlaceholder');
            jqDCCAJAX(plchldr).html(strFAQList);
            jqDCCAJAX(plchldr).find('div#faq' + themenametoprefix).makeFAQ({
                indexTitle: "My Index",
                displayIndex: false,
                faqHeader: "h2",
                theme: themenametoprefix,
                effect: fAQAJAXEffect,
                tooltip: fAQTooltip,
                openonlyone: showonlyonefaq
            });
            handlepaging(ModuleId, thispageno, totalpages, 0, themenametoprefix);
            openQuestion(ModuleId, strQuestionOpen, themenametoprefix);
        }
        else {
            var ModuleId = result.ModuleId;
            var strNoQsMsg = result.NoQMsg;
            var themenametoprefix = result.ThemeName;
            var strQuestionOpen = result.OpenQID;

            var plchldr = jqDCCAJAX('div#dnn_ctr' + ModuleId + '_ModuleContent').find('td#dnn_ctr' + ModuleId + '_ViewFAQ_tdPlaceholder');
            jqDCCAJAX(plchldr).empty().html('<h3>' + strNoQsMsg + '</h3>');
            handlepaging(ModuleId, 0, 0, 1, themenametoprefix);
            openQuestion(ModuleId, strQuestionOpen, themenametoprefix);
        }
        if (expandCollapseAllText != '') {
            //blank text here interpreted as not to show the Expand/Collaps All link header
            if (expandCollapseAllText != undefined)
                addExpandCollapseButton(ModuleId, themenametoprefix, expandCollapseAllText, bshowALLFaqExpanded);
        }
        if (bshowALLFaqExpanded) {

            ShowAllFAQExpanded(ModuleId, themenametoprefix);
        }
    }
    if (strfaqSearch != '') {
        
        var parent = jqDCCAJAX('div#dnn_ctr' + ModuleId + '_ModuleContent');
        parent.find('tr[id*=rowVisible]').hide();
        var tdSearchContents = parent.find('td[id*=SearchContents]');
        tdSearchContents.html("");
      //  tdSearchContents.append(strSearchMsg.replace("[searchterm]", strfaqSearch)).append('&nbsp;&nbsp;').append(jqDCCAJAX(' <a href="#" onclick=\'javascript:__doPostBack("","")\' >Clear Results</a>'));

        tdSearchContents.append(strSearchMsg.replace("[searchterm]", strfaqSearch)).append('&nbsp;&nbsp;').append(jqDCCAJAX(' <a href="#top" onclick="ClearAll_' + ModuleId + '(' + ModuleId + ',\'' + themenametoprefix + '\');" >Clear Results</a>'));
    }
    
}
// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameter.
function FailedCallback(error) {
    // Display the error.    
    /*var RsltElem = 
    document.getElementById("ResultId");
    RsltElem.innerHTML = 
    "Service Error: " + error.get_message();*/
    alert("Failed: " + error._message + "; Status Code: " + error._statusCode + "; Timed Out: " + error._timedOut);
}

// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object, the user context, and the 
// calling method name as parameters.
function SucceededCallbackWithContext(result, userContext, methodName) {
    if (userContext == "XmlDocument") {
        GetWebServiceXMLResult(result);
    }

}




//every external Javascript file that is being referenced from ScriptManager must have a call to Sys.Application.
if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
