/**
 * File contains JS Library for Poll Panel Control
 *
 * JavaScript  version 1
 * @category   JavaScript Libraries
 * @author     ZFort Group
 * @copyright  (c) 2004-2009 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();

    /**
     * PHP2Controls.PollPanel is the namespace and JS Class for Poll control.
     *
     * @author   ZFort Group
     * @version  $Id: pollpanel.common.js, v 2.3.0 2006/09/19 $
     * @access   public
     * @package  php2
     */
    PHP2Controls.PollManager = function(owner, ajaxEventUID)
    {
        /**
         * Poll Panel Nesting Lavel
         *
         * @var  integer
         */
        this.owner            = owner;
    	this.ajaxEventUID     = ajaxEventUID;
        this.answerId         = null;

        // --- Setting Controls IDs --- //
        var bsubmitControlId   = this.owner + '_btnAddVote';
        var bresultsControlId  = this.owner + '_btnResults';
        var bodyControlId      = this.owner + '_body';
        var currentObject      = this;

        /**
         * Button Submit Control
         *
         * @var  HTMLElement
         */
        this.btnSubmitControl  = document.getElementById(bsubmitControlId);
        this.btnResultsControl = document.getElementById(bresultsControlId);
        this.bodyControl       = document.getElementById(bodyControlId);

        this.btnSubmitControl.onclick  = function() { currentObject.doAddVote(1); };
        this.btnResultsControl.onclick = function() { currentObject.doAddVote(0); };


    }

    /**
     * Send Request to the Web Server to Add Vote
     */
    PHP2Controls.PollManager.prototype.doAddVote = function(votesCount)
    {
        if (this.validateAnswer(votesCount) || (votesCount==0))
        {
            this.bodyControl.innerHTML = '';

            this.showLoading();

            this.serverResponse = new PHP2Ajax.JSONRequest(selfUrl);
            this.serverResponse.call('doAddVote');            
            this.serverResponse.add('votesCount', votesCount);
            this.serverResponse.add('answerId',   this.answerId);
            this.serverResponse.add(this.owner + '__ajaxEventUID', this.ajaxEventUID);
            this.serverResponse.setHandler(this.onVoteAdded);
            this.serverResponse.onResponseError = this.onResponseError;
            this.serverResponse.currentObject  = this;
            this.serverResponse.execute();
        }
        else
        {
            alert('Please select answer!');
        }
    }

    /**
     * Loads Responsed HTML Code to the
     *
     * @param AjaxRequest currentObject
     */
    PHP2Controls.PollManager.prototype.onVoteAdded = function()
    {
        if ((typeof(showAJAXDebugInfo) != 'undefined') && this.response.PHPAJAXDebug != null) 
        {
            showAJAXDebugInfo(this.response.PHPAJAXDebug.Info, this.response.PHPAJAXDebug.Owner);
        }
        
        if (this.response.Response.Error.Code > 0)
        {
            alert(this.response.Response.Error.Message);
        }
        else
        {
            this.currentObject.bodyControl.innerHTML = this.response.Response.Results;
        }

        this.currentObject.hideLoading();

        return true;
    }

    /**
     * On Response Error Method
     *
     */
    PHP2Controls.PollManager.prototype.onResponseError = function()
    {
        alert("Error: " + this.response.Error.Code + ". " + this.response.Error.Message);

        this.currentObject.hideLoading();
    }

    /**
     * Show Loading Div element
     *
     * @param  HTMLElement htmlObject
     */
    PHP2Controls.PollManager.prototype.showLoading = function()
    {
        // --- Setting Loading Data Element --- //
        if (!document.getElementById('pollManagerLoading_' + this.owner))
        {
            this.bodyControl.innerHTML = '<img src="' + this.sRootUrl + 'images/ajax/ajax-loader.gif" /><b>Please wait for a votes count ...</b>';
        }
        if (navigator.appName == 'Microsoft Internet Explorer') 
        {
            offset = -100;
        }
        else 
        {
            offset = 50;
        }

        /*this.pollManagerLoadingPanel = document.getElementById('pollManagerLoading_' + this.owner);
        this.pollManagerLoadingPanel.style.left  = HTMLElement.findPosX(this.bodyControl) + this.bodyControl.offsetWidth/2 + offset + "px";
        this.pollManagerLoadingPanel.style.top   = HTMLElement.findPosY(this.bodyControl) + this.bodyControl.offsetHeight/2 - 50 + "px";
        this.pollManagerLoadingPanel.style.display  = 'inline';*/
    }

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

   /**
    * Validate Answer
    */
    PHP2Controls.PollManager.prototype.validateAnswer = function(votesCount)
    {
        if(votesCount)
        {
            var checked = false;

            var rbl = document.getElementsByName(this.owner + '_answer');

            for(i = 0; i < rbl.length; i++)
            {
                if (rbl[i].type == 'radio' && (rbl[i].checked))
                {
                    checked  = true;
                    this.answerId = rbl[i].value;
                }
            }
            return checked;
        }
        else
        {
            return true;
        }
    }
