
/**
 * File contains JS Library for Mailer Control
 *
 * JavaScript  version 1
 * @category   JavaScript Libraries
 * @author     Dmitry M. Mashkov <mashkov@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();

    /**
     * PHP2Controls.PopupForm is the namespace and JS Class for PopupForm control.
     *
     * @author   Dmitry M. Mashkov <mashkov@zfort.net>
     * @version  $Id: mailer.js, v 2.3.0 2006/09/19 $
     * @access   public
     * @package  php2
     */
    PHP2Controls.PopupForm = function(objectName)
    {
        /**
         * Web Service Url
         */
        this.wsUrl          = '/wsdl/popupform.ajax.php';

        /**
         * OffsetX
         */
        this.offsetX        = 10;
        this.offsetY        = -150;

        /**
         * Content Type
         */
        this.contentType    = 'text/plain';

        /**
         * Mail template
         */
        this.mailTemplate   = '';

        /**
         * Mail Template Objects
         */
        this.tplMailObjects = new Array();

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

        this.first          = '';
        this.last              = '';
        this.company              = '';
        this.phone             = '';
        this.email         = '';
        this.leads        = '';
        this.comments        = '';

        // --- Setting Controls IDs --- //
        var openControlId            = objectName + '_openObject';
        var cancelControlId          = objectName + '_cancelObject';
        var sendControlId            = objectName + '_sendObject';
        var bodyControlId            = objectName + '_popupformBody';
        var firstNameControlId       = objectName + '_FirstName';
        var lastNameControlId        = objectName + '_LastName';
        var companyControlId         = objectName + '_Company';
        var phoneControlId           = objectName + '_Phone';
        var emailControlId           = objectName + '_Email';
        var leadsControlId         	 = objectName + '_Leads';
        var commentsControlId        = objectName + '_Comments';
        var responseMessageControlId = objectName + '_popupformResponseMessage';
        var responseMessageBoxControlId = objectName + '_popupformResponseMessageBox';
        var loadingControlId         = objectName + '_popupformLoading';
        var currentObject            = this;

        this.objOpenControl = document.getElementById(openControlId);
        this.objCancelControl = document.getElementById(cancelControlId);
        this.objBodyControl = document.getElementById(bodyControlId);
        this.objSendControl = document.getElementById(sendControlId);

        this.txtFirstNameControl = document.getElementById(firstNameControlId);
        this.txtLastNameControl = document.getElementById(lastNameControlId);
        this.txtCompanyControl = document.getElementById(companyControlId);
        this.txtPhoneControl = document.getElementById(phoneControlId);
        this.txtEmailControl = document.getElementById(emailControlId);
        this.ddlLeadsControl = document.getElementById(leadsControlId);
        this.txtCommentsControl = document.getElementById(commentsControlId);
        
        this.objResponseMessageControl = document.getElementById(responseMessageControlId);
        this.objResponseMessageBoxControl = document.getElementById(responseMessageBoxControlId);
        this.objLoadingControl = document.getElementById(loadingControlId);

        this.objOpenControl.onclick   = function() { currentObject.showPopupFormBody(); };
        this.objCancelControl.onclick = function() { currentObject.hidePopupFormBody(); };
        this.objSendControl.onclick   = function() { currentObject.sendForm(); };
    }

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

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

    /**
     * Set Sender
     *
     * @param string sender
     */
    PHP2Controls.PopupForm.prototype.setFirst = function(first)
    {
        this.first = first;
    }

    /**
     * Set To
     *
     * @param string to
     */
    PHP2Controls.PopupForm.prototype.setLast = function(last)
    {
        this.last = last;
    }

    /**
     * Set CC
     *
     * @param string cc
     */
    PHP2Controls.PopupForm.prototype.setCompany = function(company)
    {
        this.company = company;
    }

    /**
     * Set BCC
     *
     * @param string Bcc
     */
    PHP2Controls.PopupForm.prototype.setPhone = function(phone)
    {
        this.phone = phone;
    }

    /**
     * Set Subject
     *
     * @param string Subject
     */
    PHP2Controls.PopupForm.prototype.setEmail = function(email)
    {
        this.email = email;
    }

    /**
     * Set Mail Body
     *
     * @param string Mail Body
     */
    PHP2Controls.PopupForm.prototype.setLeads = function(leads)
    {
        this.leads = leads;
    }

    PHP2Controls.PopupForm.prototype.setComments = function(comments)
    {
        this.comments = comments;
    }

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

   
    /**
     * Show PopupForm Body
     */
    PHP2Controls.PopupForm.prototype.showPopupFormBody = function()
    {
        this.init();

        this.objResponseMessageControl.innerHTML = '';
		this.objResponseMessageBoxControl.style.display = 'none';
        this.objBodyControl.style.left  = HTMLElement.findPosX(this.objOpenControl) + this.objOpenControl.offsetWidth + this.offsetX + "px";
        this.objBodyControl.style.top   = HTMLElement.findPosY(this.objOpenControl) + this.offsetY + "px";

        this.objBodyControl.style.display = 'block';
    }

    /**
     * Show PopupForm Body
     */
    PHP2Controls.PopupForm.prototype.init = function()
    {
        this.txtFirstNameControl.value              = this.first;
        this.txtLastNameControl.value              = this.last;
        this.txtCompanyControl.value                  = this.company;
        this.txtPhoneControl.value                  = this.phone;
        this.txtEmailControl.value                 = this.email;
        this.ddlLeadsControl.value             = this.leads;
        this.txtCommentsControl.value            = this.comments;
        
       
    }

    /**
     * Hide PopupForm Body
     */
    PHP2Controls.PopupForm.prototype.hidePopupFormBody = function()
    {
        this.objResponseMessageBoxControl.style.display = 'none';
        this.objBodyControl.style.display = 'none';
    }

    /**
     * Send Request to the Web Server to Send Mail
     *
     * @param integer categoryId sendMail
     */
    PHP2Controls.PopupForm.prototype.sendForm = function()
    {
        this.serverResponse = new PHP2Ajax.JSONRequest(this.wsUrl);

        this.serverResponse.call('doSendForm');
        this.serverResponse.add("objectName",     this.objectName);
        this.serverResponse.add("contentType",    this.contentType);
      
        for (var key in this.tplMailObjects)
        {
            this.serverResponse.add(key, this.tplMailObjects[key]);
        }

        this.serverResponse.add("txtFirstName",      this.txtFirstNameControl.value);
        this.serverResponse.add("txtLastName",          this.txtLastNameControl.value);
        this.serverResponse.add("txtCompany",          this.txtCompanyControl.value);
        this.serverResponse.add("txtPhone",         this.txtPhoneControl.value);
        this.serverResponse.add("txtEmail",     this.txtEmailControl.value);
        this.serverResponse.add("ddlLeads",     (this.ddlLeadsControl.selectedIndex+1));
        this.serverResponse.add("txtComments",    this.txtCommentsControl.value);

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

        this.showLoading();
    }

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

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

    /**
     * Loads Responsed HTML Code to the
     *
     * @param AjaxRequest currentObject
     */
    PHP2Controls.PopupForm.prototype.onSendForm = function()
    {
        this.currentObject.hideLoading();
        
        if (this.response.Response.ResponseMessage.ErrorCode == 0)
        	this.currentObject.objResponseMessageBoxControl.className = 'popupformThank';
		
		this.currentObject.objResponseMessageBoxControl.style.display = 'block';

        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.PopupForm.prototype.onResponseError = function()
    {
        this.currentObject.hideLoading();
this.objResponseMessageBoxControl.style.display = 'block';
        this.currentObject.objResponseMessageControl.innerHTML = this.response.Error.Message;
    }
	
