﻿// This function calls the Web service method and 
// passes the event callback function.  

function FetchFAQs(_PortalID,_TabID, _ModuleId,_CatID,_BaseCulture,_CurrentCulture,_PageNo, _ThemeName, _NoQMsg,_strQIDToOpen, _strPermalinkTag, _strPermalinkText)
{

    DNNCentric.AJAXFAQ.AJAXFAQServicesController.FetchFAQs(_PortalID,_TabID,_ModuleId,_CatID,_BaseCulture,_CurrentCulture,_PageNo, _ThemeName, _NoQMsg,_strQIDToOpen,_strPermalinkTag,_strPermalinkText, 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 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 = "";
			//jqDCCAJAX = jqDCCAJAX.noConflict(true);
			
			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
				});
			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);
        }
	}
}
// 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();
