﻿// This function calls the Web service method and 
// passes the event callback function.  

function FetchFAQs(_PortalID,_TabID, _ModuleId,_CatID,_BaseCulture,_CurrentCulture,_PageNo, _ThemeName, _NoQMsg)
{
    //debugger;
    DNNCentric.AJAXFAQ.AJAXFAQServicesController.FetchFAQs(_PortalID,_TabID,_ModuleId,_CatID,_BaseCulture,_CurrentCulture,_PageNo, _ThemeName, _NoQMsg, 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 strFAQHTMLMain = "<div id=\"faq"+themenametoprefix+"\">{0}</div>";
			var strFAQHTMLTemplate = "<h2>{0}</h2><div>{1}</div>";
			var strFAQHTMLTemplateForAdmin = "<h2><a href=\"{2}\">EDIT</a>{0}</h2><div>{1}</div>";
			
			var strNoQsMsg=result.NoQMsg;
			
			var strFAQList = "";
			var strFAQSingleQ = "";
			jQuery.noConflict();
			
			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);
				strFAQList = strFAQList + strFAQSingleQ;
				strFAQSingleQ = "";
			}
			strFAQList = String.format(strFAQHTMLMain, strFAQList);
			var plchldr=jQuery('div#dnn_ctr' + ModuleId + '_ModuleContent').find('td#dnn_ctr' + ModuleId + '_ViewFAQ_tdPlaceholder'); 
			jQuery(plchldr).html(strFAQList);
			jQuery(plchldr).find('div#faq'+themenametoprefix).makeFAQ({
					indexTitle: "My Index",
					displayIndex: false,
					faqHeader: "h2",
					theme:themenametoprefix
				});
			handlepaging( ModuleId, thispageno, totalpages, 0, themenametoprefix ) ;
        }
        else
        {
            var ModuleId= result.ModuleId;
            var strNoQsMsg=result.NoQMsg;
            var themenametoprefix= result.ThemeName;
			
            
			var plchldr=jQuery('div#dnn_ctr' + ModuleId + '_ModuleContent').find('td#dnn_ctr' + ModuleId + '_ViewFAQ_tdPlaceholder'); 
			jQuery(plchldr).empty().html('<h3>'+strNoQsMsg+'</h3>');
			handlepaging( ModuleId, 0, 0, 1, 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.toString());
}

// 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();
