
/**
 * File contains JS Library for Mailer 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.Mailer is the namespace and JS Class for Mailer control.
     *
     * @author   ZFort Group
     * @version  $Id: mailer.js, v 2.3.0 2006/09/19 $
     * @access   public
     * @package  php2
     */
    PHP2Controls.Mailer = function(objectName)
    {
        /**
         * OffsetX
         */
        this.offsetX        = false;
        this.offsetY        = false;

        /**
         * Owner Depend
         */
        this.offsetOwnerDepend    = false;
        
        /**
         * Draggable
         */
        this.draggable    = false;
        
        /**
         * Show Speed
         */
        this.showSpeed    = '';

        /**
         * Mailer Nesting Level
         *
         * @var  integer
         */
        this.objectName      = objectName;

        this.sender          = '';
        this.to              = '';
        this.cc              = '';
        this.bcc             = '';
        this.subject         = '';
        this.mailBody        = '';
        this.ajaxEventUID    = null;

        // --- Setting Controls IDs --- //
        var openControlId            = objectName + '_openObject';
        var cancelControlId          = objectName + '_cancelObject';
        var sendControlId            = objectName + '_sendObject';
        var bodyControlId            = objectName + '_mailerBody';
        var senderControlId          = objectName + '_mailerSender';
        var toControlId              = objectName + '_mailerTo';
        var ccControlId              = objectName + '_mailerCc';
        var bccControlId             = objectName + '_mailerBcc';
        var subjectControlId         = objectName + '_mailerSubject';
        var mailBodyControlId        = objectName + '_mailerMailBody';
        var responseMessageControlId = objectName + '_mailerResponseMessage';
        var loadingControlId         = objectName + '_mailerLoading';
        var currentObject            = this;

        /**
         * Open Control
         *
         * @var  HTMLElement
         */
        this.objOpenControl = document.getElementById(openControlId);

        /**
         * Cancel Control
         *
         * @var  HTMLElement
         */
        this.objCancelControl = document.getElementById(cancelControlId);

        /**
         * Body Control
         *
         * @var  HTMLElement
         */
        this.objBodyControl = document.getElementById(bodyControlId);

        /**
         * Send Control
         *
         * @var  HTMLElement
         */
        this.objSendControl = document.getElementById(sendControlId);

        /**
         * To Text Control
         *
         * @var  HTMLElement
         */
        this.txtToControl = document.getElementById(toControlId);

        /**
         * Cc Text Control
         *
         * @var  HTMLElement
         */
        this.txtCcControl = document.getElementById(ccControlId);

        /**
         * Bcc Text Control
         *
         * @var  HTMLElement
         */
        this.txtBccControl = document.getElementById(bccControlId);

        /**
         * Sender Text Control
         *
         * @var  HTMLElement
         */
        this.txtSenderControl = document.getElementById(senderControlId);

        /**
         * Subject Text Control
         *
         * @var  HTMLElement
         */
        this.txtSubjectControl = document.getElementById(subjectControlId);

        /**
         * Mail Body Text Control
         *
         * @var  HTMLElement
         */
        this.txtMailBodyControl = document.getElementById(mailBodyControlId);

        /**
         * Response Message Text Control
         *
         * @var  HTMLElement
         */
        this.objResponseMessageControl = document.getElementById(responseMessageControlId);

        /**
         * Loading Control
         *
         * @var  HTMLElement
         */
        this.objLoadingControl = document.getElementById(loadingControlId);

        this.objOpenControl.onclick   = function() { currentObject.showMailerBody(); };
        this.objCancelControl.onclick = function() { currentObject.hideMailerBody(); };
        this.objSendControl.onclick   = function() { currentObject.sendMail(); };
    }

    /**
     * Set OffsetX
     *
     * @param integer offsetX
     */
    PHP2Controls.Mailer.prototype.setOffsetX = function(offsetX)
    {
        this.offsetX = eval(offsetX);
    }

    /**
     * Set OffsetY
     *
     * @param integer offsetY
     */
    PHP2Controls.Mailer.prototype.setOffsetY = function(offsetY)
    {
        this.offsetY = eval(offsetY);
    }

    /**
     * Set Sender
     *
     * @param string sender
     */
    PHP2Controls.Mailer.prototype.setSender = function(sender)
    {
        this.sender = sender;
    }

    /**
     * Set To
     *
     * @param string to
     */
    PHP2Controls.Mailer.prototype.setTo = function(to)
    {
        this.to = to;
    }

    /**
     * Set CC
     *
     * @param string cc
     */
    PHP2Controls.Mailer.prototype.setCC = function(cc)
    {
        this.cc = cc;
    }

    /**
     * Set BCC
     *
     * @param string Bcc
     */
    PHP2Controls.Mailer.prototype.setBCC = function(bcc)
    {
        this.bcc = bcc;
    }

    /**
     * Set Subject
     *
     * @param string Subject
     */
    PHP2Controls.Mailer.prototype.setSubject = function(subject)
    {
        this.subject = subject;
    }

    /**
     * Set Mail Body
     *
     * @param string Mail Body
     */
    PHP2Controls.Mailer.prototype.setMailBody = function(mailBody)
    {
        this.mailBody = mailBody;
    }

    /**
     * Set Content Type
     *
     * @param string Content Type
     */
    PHP2Controls.Mailer.prototype.setContentType = function(contentType)
    {
        this.contentType = contentType;
    }

    /**
     * Set AJAX Event UID
     *
     * @param string sender
     */
    PHP2Controls.Mailer.prototype.setAJAXEventUID = function(uid)
    {
        this.ajaxEventUID = uid;
    }
    
    /**
     * Show Mailer Body
     */
    PHP2Controls.Mailer.prototype.showMailerBody = function()
    {
        this.init();

        this.objResponseMessageControl.innerHTML = '';

        if ((this.offsetX == false) && (this.offsetY == false))
        {
    	   $('#' + this.objectName + '_mailerBody').center();            
        }
        else if (this.offsetOwnerDepend)
        {
           var offsetX = (this.offsetX == false) ? 0 : this.offsetX;
           var offsetY = (this.offsetY == false) ? 0 : this.offsetY;
           this.objBodyControl.style.left  = HTMLElement.findPosX(this.objOpenControl) + offsetX + "px";
           this.objBodyControl.style.top   = HTMLElement.findPosY(this.objOpenControl) + offsetY + "px";
        }
        else
        {
           var offsetX = (this.offsetX == false) ? 0 : this.offsetX;
           var offsetY = (this.offsetY == false) ? 0 : this.offsetY;
           this.objBodyControl.style.left  = offsetX + "px";
           this.objBodyControl.style.top   = offsetY + "px";            
        }
        
    	if (this.showSpeed == 'slow' || this.showSpeed == 'normal' || this.showSpeed == 'fast')
    	{
    	   $('#' + this.objectName + '_mailerBody').slideDown(this.showSpeed);
    	}
    	else
    	{
    	   $('#' + this.objectName + '_mailerBody').show();    	       
    	}
    	
        if (this.draggable)
        {
    	   $('#' + this.objectName + '_mailerBody').draggable();
    	   $('#' + this.objectName + '_mailerBody').css("cursor", "move");    	               
        }        
    }

    /**
     * Show Mailer Body
     */
    PHP2Controls.Mailer.prototype.init = function()
    {
        this.txtSenderControl.value              = this.sender;
        this.txtToControl.value                  = this.to;
        this.txtCcControl.value                  = this.cc;
        this.txtBccControl.value                 = this.bcc;
        this.txtSubjectControl.value             = this.subject;
        this.txtMailBodyControl.value            = this.mailBody;
    }

    /**
     * Hide Mailer Body
     */
    PHP2Controls.Mailer.prototype.hideMailerBody = function()
    {
    	if (this.draggable)
    	{
    	   $('#' + this.objectName + '_mailerBody').draggable("destroy");            
    	   $('#' + this.objectName + '_mailerBody').css("cursor", "auto");    	               
    	}
    	if (this.showSpeed == 'slow' || this.showSpeed == 'normal' || this.showSpeed == 'fast')
    	{
    	   $('#' + this.objectName + '_mailerBody').slideUp(this.showSpeed);
    	}
    	else
    	{
    	   $('#' + this.objectName + '_mailerBody').hide();    	       
    	}
    }

    /**
     * Send Request to the Web Server to Send Mail
     *
     * @param integer categoryId
     */
    PHP2Controls.Mailer.prototype.sendMail = function()
    {
        this.serverResponse = new PHP2Ajax.JSONRequest(selfUrl);
        this.serverResponse.call('doSendMail');                
        this.serverResponse.add(this.objectName + '__ajaxEventUID', this.ajaxEventUID);
        this.serverResponse.add("txtSender",      this.txtSenderControl.value);
        this.serverResponse.add("txtTo",          this.txtToControl.value);
        this.serverResponse.add("txtCc",          this.txtCcControl.value);
        this.serverResponse.add("txtBcc",         this.txtBccControl.value);
        this.serverResponse.add("txtSubject",     this.txtSubjectControl.value);
        this.serverResponse.add("txtMailBody",    this.txtMailBodyControl.value);

        this.serverResponse.setHandler(this.onSendMail);
        this.serverResponse.onResponseError = this.onResponseError;
        this.serverResponse.currentObject  = this;
        this.serverResponse.execute();

        this.showLoading();
    }

    /**
     * Show Loading Div element
     *
     * @param  HTMLElement htmlObject
     */
    PHP2Controls.Mailer.prototype.showLoading = function()
    {
        this.objLoadingControl.style.left  = this.txtMailBodyControl.style.left + 165 + "px";
        this.objLoadingControl.style.top   = this.txtMailBodyControl.style.top  + 270 + "px";
        this.objLoadingControl.style.display  = 'block';
    }

    /**
     * Hide Loading Div element
     *
     * @param  HTMLElement htmlObject
     */
    PHP2Controls.Mailer.prototype.hideLoading = function()
    {
        this.objLoadingControl.style.display  = 'none';
    }

    /**
     * Loads Responsed HTML Code to the
     *
     * @param AjaxRequest currentObject
     */
    PHP2Controls.Mailer.prototype.onSendMail = function()
    {
        if ((typeof(showAJAXDebugInfo) != 'undefined') && this.response.PHPAJAXDebug != null) 
        {
            showAJAXDebugInfo(this.response.PHPAJAXDebug.Info, this.response.PHPAJAXDebug.Owner);
        }
        
        this.currentObject.hideLoading();

        if (this.response.Response.ResponseMessage.Message != '')
        {
            this.currentObject.objResponseMessageControl.innerHTML = this.response.Response.ResponseMessage.Message;
        }

        if (this.response.Response.ResponseMessage.ErrorCode == 0)
        {
            this.currentObject.init();
        }

        return true;
    }

    /**
     * On Response Error Method
     *
     */
    PHP2Controls.Mailer.prototype.onResponseError = function()
    {
        this.currentObject.hideLoading();

        this.currentObject.objResponseMessageControl.innerHTML = this.response.Error.Message;
    }



