﻿// This function calls the Web service method and 
// passes the event callback function.
if (!window.jqDCCAJAX)
    jqDCCAJAX = jQuery.noConflict();


function FetchFAQs(_PortalID, _TabID, _ModuleId, _CatID, _BaseCulture, _CurrentCulture, _PageNo, _ThemeName, _NoQMsg, _strQIDToOpen, _strPermalinkTag, _strPermalinkText, _strExpandCollapseAllText, _strFAQAJAXEffect, _strFAQTooltip, _strShowOnlyOneFAQ, _strShowAllFAQExpanded)
{

    DNNCentric.AJAXFAQ.AJAXFAQServicesController.FetchFAQs(_PortalID, _TabID, _ModuleId, _CatID, _BaseCulture, _CurrentCulture, _PageNo, _ThemeName, _NoQMsg, _strQIDToOpen, _strPermalinkTag, _strPermalinkText, _strExpandCollapseAllText, _strFAQAJAXEffect, _strFAQTooltip, _strShowOnlyOneFAQ, _strShowAllFAQExpanded, 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)
{

    
    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);
			    //debugger;
			    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);
			var 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
            addExpandCollapseButton(ModuleId, themenametoprefix, expandCollapseAllText, bshowALLFaqExpanded);
        }
        if (bshowALLFaqExpanded) {

            ShowAllFAQExpanded(ModuleId, themenametoprefix);
        }
	}
}
// 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();