
/**
 * File contains JS Library for Category Select Panel Control
 *
 * JavaScript  version 1
 * @category   JavaScript Libraries
 * @author     Eugene A. Kalosha <aristarch@zfort.net>
 * @copyright  (c) 2004-2006 by ZFort Group
 * @version    SVN: $Id: 206$
 * @link       http://www.zfort.net
 * @since      File available since Release 2.3.0
 */
if (typeof(PHP2Controls) == 'undefined') PHP2Controls = new Object();

    String.prototype.trim = function() {
        a = this.replace(/^\s+/, '');
        return a.replace(/\s+$/, '');
    };

    /**
     * PHP2Controls.CategorySelectPanel is the namespace and JS Class for Category Select control.
     *
     * @author   Eugene A. Kalosha <aristarch@zfort.net>
     * @version  $Id: categoryselectpanel.common.js, v 2.3.0 2006/09/19 $
     * @access   public
     * @package  php2
     */
    PHP2Controls.FAQManager = function(wsUrl, arrAnswerId, lnkAnswer, lnkAnswerAll, loadingScript, classInactive, classActive)
    {
        /**
         * Category Select Panel Nesting Lavel
         *
         * @var  integer
         */
        this.wsUrl          = (wsUrl != null) ? wsUrl : '/wsdl/main.ajax.php';
        this.sRootUrl       = '/';
        var currentObject      = this;

        var linkAnswer     = (lnkAnswer != null) ? lnkAnswer : 'lnkAnswer';
        var linkAnswerAll  = (lnkAnswerAll != null) ? lnkAnswerAll : 'lnkAnswerAll';
        this.linkAnswer    = linkAnswer;
        this.linkAnswerAll = linkAnswerAll;
        this.loadingScript = loadingScript;
        this.arrAnswerId   = arrAnswerId;
        for(i in arrAnswerId)
        {
            var currId = arrAnswerId[i];
            document.getElementById(linkAnswer+currId).onclick = function(){currentObject.doShowAnswer(this);}
        }
        document.getElementById(this.linkAnswerAll).onclick = function(){currentObject.doShowAnswer(this);}
        this.classInactive = classInactive;
        this.classActive   = classActive;
    }

    PHP2Controls.FAQManager.prototype.doShowAnswer = function(link)
    {
        var divMessage;
        var setRequest;
        var showAnswerId = new Array(0);
        var objAnswerAll = document.getElementById(this.linkAnswerAll);

        if (link.id == objAnswerAll.id)
        {
            showAnswerId = showAnswerId.concat(this.arrAnswerId);
        }
        else
        {
            var str = new String(link.id);
            var showAnswerId = new Array(1);
            showAnswerId[1] = str.substring(this.linkAnswer.length,str.length);
        }
        setRequest = 0;
        var nameAll = objAnswerAll.name;
        var valueAll = objAnswerAll.innerHTML;
        var bSetAll = false;
        var classAll = objAnswerAll.className;
        for(i in showAnswerId)
        {
            divMessage = document.getElementById('answer'+showAnswerId[i]);
            questionLink = document.getElementById('lnkAnswer'+showAnswerId[i]);

            if (link.id == objAnswerAll.id)
            {
                if (objAnswerAll.name == 'showAll')
                {
                   divMessage.style.display = 'block';
                   questionLink.className = this.classActive;

                   if (!bSetAll)
                   {
                       nameAll  = 'hideAll';
                       classAll = 'btn_hide_all';
                       valueAll = 'Hide All';
                       bSetAll = true;
                   }
                }
                else
                {
                   divMessage.style.display = 'none';
                   questionLink.className = this.classInactive;
                   if (!bSetAll)
                   {
                       nameAll = 'showAll';
                       classAll = 'btn_show_all';
                       valueAll = 'Show All';
                       bSetAll = true;
                   }
                }
            }
            var innerDiv = new String(divMessage.innerHTML);
            // --- Trim space --- //
            innerDiv.trim();
            if (innerDiv.length > 0 && divMessage.innerHTML != this.loadingScript)
            {
                if (link.id != objAnswerAll.id)
                {
                    if (divMessage.style.display == 'none')
                    {
                        divMessage.style.display = 'block';
                        questionLink.className   = this.classActive;
                    }
                    else
                    {
                        divMessage.style.display = 'none';
                        questionLink.className   = this.classInactive;
                    }
                }
            }
            else setRequest = 1;
        }
        objAnswerAll.name      = nameAll;
        //objAnswerAll.innerHTML = valueAll;
        objAnswerAll.className = classAll;
        //if (valueAll) objAnswerAll.className  = valueAll;

        if (setRequest == 1)
        {
            this.serverResponse = new PHP2Ajax.JSONRequest(this.wsUrl);
            this.serverResponse.call('doShowFAQ');
            this.serverResponse.add("answerId", showAnswerId.join());
            this.serverResponse.setHandler(this.onShowAnswerChecked);
            this.serverResponse.onResponseError = this.onResponseError;
            this.serverResponse.currentObject  = this;
            this.serverResponse.execute();

            for(i in showAnswerId)
            {
                divMessage = document.getElementById('answer'+showAnswerId[i]);
                this.showLoading(divMessage);
            }
        }
    }

    /**
     * Loads Responsed HTML Code to the
     *
     * @param AjaxRequest currentObject
     */
    PHP2Controls.FAQManager.prototype.onShowAnswerChecked = function()
    {
        var divMessage;
        var countAnswer = this.response.Response.Answer.CountAnswer;
        for(i = 1; i <= countAnswer; i++)
        {
            eval('id    = this.response.Response.Answer.Id_' + i + ';');
            eval('title = this.response.Response.Answer.Title_' + i + ';');
            divMessage = document.getElementById('answer'+id);
            if (divMessage)
            {
                divMessage.style.display = 'block';
                divMessage.innerHTML = title;
            }
            questionLink = document.getElementById('lnkAnswer'+id);
            if (questionLink)
            {
               questionLink.className = this.currentObject.classActive;
            }
        }
        return true;
    }

    /**
     * Set Skin Area Root Url
     *
     * @param string sRootUrl
     */
    PHP2Controls.FAQManager.prototype.setSRootUrl = function(sRootUrl)
    {
        this.sRootUrl = sRootUrl;
    }

    /**
     * On Response Error Method
     *
     */
    PHP2Controls.FAQManager.prototype.onResponseError = function()
    {
        // alert("Error: " + this.response.Error.Code + ". " + this.response.Error.Message);
        this.currentObject.alert = new PHP2Controls.Alert("Error: " + this.response.Error.Code + ". " + this.response.Error.Message);
        this.currentObject.hideLoading();
    }

    /**
     * Show Loading Div element
     *
     * @param  HTMLElement htmlObject
     */
    PHP2Controls.FAQManager.prototype.showLoading = function(divMessage)
    {
        // --- Setting Loading Data Element --- //
        if (divMessage)
        {
            this.faqManagerLoadingPanel = divMessage;
            this.faqManagerLoadingPanel.innerHTML   = this.loadingScript;
            this.faqManagerLoadingPanel.style.display = 'block';
        }
    }

    /**
     * Hide Loading Div element
     *
     * @param  HTMLElement htmlObject
     */
    PHP2Controls.FAQManager.prototype.hideLoading = function()
    {
        // --- Setting Loading Data Command --- //
        if (this.faqManagerLoadingPanel != null) this.faqManagerLoadingPanel.style.display  = 'none';
    }

    /**
     * Alert Error Message to browser
     *
     * @param  HTMLElement htmlObject
     */
    PHP2Controls.FAQManager.prototype.alert = function(errorMessage, errorCode)
    {
        this.currentAlert = new PHP2Controls.Alert(errorMessage);
    }
