
/**
 * File contains JS Library for EnquiryForm 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.EnquiryForm is the namespace and JS Class for EnquiryForm control.
     *
     * @author   Dmitry M. Mashkov <mashkov@zfort.net>
     * @version  $Id: EnquiryForm.js, v 2.3.0 2006/09/19 $
     * @access   public
     * @package  php2
     */
    PHP2Controls.EnquiryForm = function(objectName)
    {
        /**
         * Web Service Url
         */
        this.wsUrl          = '/wsdl/enquiryform.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();

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

        this.sender          = '';
        this.senderName      = '';
        this.productName     = '';
        this.companyName     = '';
        this.phoneNumber     = '';
        this.address1        = '';
        this.address2        = '';
        this.city            = '';
        this.zip             = '';
        this.to              = '';
        this.cc              = '';
        this.bcc             = '';
        this.subject         = '';
        this.mailBody        = '';

        // --- Setting Controls IDs --- //
        var openControlId            = objectName + '_openObject';
        var cancelControlId          = objectName + '_cancelObject';
        var sendControlId            = objectName + '_sendObject';
        var bodyControlId            = objectName + '_enquiryBody';
        var senderControlId          = objectName + '_enquirySender';
        var senderNameControlId      = objectName + '_enquirySenderName';
        var productNameControlId     = objectName + '_enquiryProductName';
        var companyNameControlId     = objectName + '_enquiryCompanyName';
        var phoneNumberControlId     = objectName + '_enquiryPhoneNumber';
        var address1ControlId        = objectName + '_enquiryAddress1';
        var address2ControlId        = objectName + '_enquiryAddress2';
        var cityControlId            = objectName + '_enquiryCity';
        var zipControlId             = objectName + '_enquiryZip';
        var toControlId              = objectName + '_enquiryTo';
        var ccControlId              = objectName + '_enquiryCc';
        var bccControlId             = objectName + '_enquiryBcc';
        var subjectControlId         = objectName + '_enquirySubject';
        var mailBodyControlId        = objectName + '_enquiryMailBody';
        var responseMessageControlId = objectName + '_enquiryResponseMessage';
        var loadingControlId         = objectName + '_enquiryLoading';
        var divContentId             = objectName + '_enquire_content';
        var divThanksId              = objectName + '_thanks';
        var btnCloseThanksId         = objectName + '_closeThanksObject';

        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);

        /**
         * Sender Name Text Control
         *
         * @var  HTMLElement
         */
        this.txtSenderNameControl = document.getElementById(senderNameControlId);

        /**
         * Product Name Text Control
         *
         * @var  HTMLElement
         */
        this.txtProductNameControl = document.getElementById(productNameControlId);

        /**
         * Company Name Text Control
         *
         * @var  HTMLElement
         */
        this.txtCompanyNameControl = document.getElementById(companyNameControlId);

        /**
         * Phone Number Text Control
         *
         * @var  HTMLElement
         */
        this.txtPhoneNumberControl = document.getElementById(phoneNumberControlId);

        /**
         * Address1 Text Control
         *
         * @var  HTMLElement
         */
        this.txtAddress1Control = document.getElementById(address1ControlId);

        /**
         * Address2 Text Control
         *
         * @var  HTMLElement
         */
        this.txtAddress2Control = document.getElementById(address2ControlId);

        /**
         * City Text Control
         *
         * @var  HTMLElement
         */
        this.txtCityControl = document.getElementById(cityControlId);

        /**
         * Zip Text Control
         *
         * @var  HTMLElement
         */
        this.txtZipControl = document.getElementById(zipControlId);

        /**
         * 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);

        /**
         * Div Cintent Control
         *
         * @var  HTMLElement
         */
        this.divContentControl = document.getElementById(divContentId);

        /**
         * Div Thanks Control
         *
         * @var  HTMLElement
         */
        this.divThanksControl = document.getElementById(divThanksId);

        /**
         * Button Thanks Control
         *
         * @var  HTMLElement
         */
        this.btnCloseThanksControl = document.getElementById(btnCloseThanksId);

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

        this.objOpenControl.onclick   = function() { currentObject.showEnquiryFormBody(); };
        this.objCancelControl.onclick = function() { currentObject.hideEnquiryFormBody(); };
        this.btnCloseThanksControl.onclick = function() { currentObject.hideEnquiryFormBody(); };
        this.objSendControl.onclick   = function() { currentObject.sendMail(); };
    }

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

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

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

    /**
     * Set Sender Name
     *
     * @param string sender
     */
    PHP2Controls.EnquiryForm.prototype.setSenderName = function(senderName)
    {
        this.senderName = senderName;
    }

    /**
     * Set Product Name
     *
     * @param string productName
     */
    PHP2Controls.EnquiryForm.prototype.setProductName = function(productName)
    {
        this.productName = productName;
    }

    /**
     * Set Company Name
     *
     * @param string companyName
     */
    PHP2Controls.EnquiryForm.prototype.setCompanyName = function(companyName)
    {
        this.companyName = companyName;
    }

    /**
     * Set Phone Number
     *
     * @param string phoneNumber
     */
    PHP2Controls.EnquiryForm.prototype.setPhoneNumber = function(phoneNumber)
    {
        this.phoneNumber = phoneNumber;
    }

    /**
     * Set Address1
     *
     * @param string address1
     */
    PHP2Controls.EnquiryForm.prototype.setAddress1 = function(address1)
    {
        this.address1 = address1;
    }

    /**
     * Set Address2
     *
     * @param string address2
     */
    PHP2Controls.EnquiryForm.prototype.setAddress2 = function(address2)
    {
        this.address2 = address2;
    }

    /**
     * Set City
     *
     * @param string city
     */
    PHP2Controls.EnquiryForm.prototype.setCity = function(city)
    {
        this.city = city;
    }

    /**
     * Set Zip
     *
     * @param string zip
     */
    PHP2Controls.EnquiryForm.prototype.setZip = function(zip)
    {
        this.zip = zip;
    }

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

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

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

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

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

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

    /**
     * Set Mail Template
     *
     * @param string Mail Template
     */
    PHP2Controls.EnquiryForm.prototype.setMailTemplate = function(mailTemplate)
    {
        this.mailTemplate = mailTemplate;
    }

    /**
     * Add Mail Template Object
     *
     * @param string Object Name
     * @param string Object Value
     */
    PHP2Controls.EnquiryForm.prototype.addMailTemplateObject = function(objectName, objectValue)
    {
        this.tplMailObjects['_' + this.objectName + '_' + objectName] = objectValue;
    }

    /**
     * Show EnquiryForm Body
     */
    PHP2Controls.EnquiryForm.prototype.showEnquiryFormBody = function()
    {
        this.init();

		this.objOpenControl.style.display = 'none';

        this.objResponseMessageControl.innerHTML = '';

//        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';
        
        if(document.getElementById('ddlSize'))     document.getElementById('ddlSize').style.visibility = 'hidden';
        if(document.getElementById('ddlQuantity')) document.getElementById('ddlQuantity').style.visibility = 'hidden';
    }

    /**
     * Show EnquiryForm Body
     */
    PHP2Controls.EnquiryForm.prototype.init = function()
    {
        this.txtSenderControl.value              = this.sender;
        this.txtSenderNameControl.value          = this.senderName;
        this.txtProductNameControl.value         = this.productName;
        this.txtCompanyNameControl.value         = this.companyName;
        this.txtPhoneNumberControl.value         = this.phoneNumber;
        this.txtAddress1Control.value            = this.address1;
        this.txtAddress2Control.value            = this.address2;
        this.txtCityControl.value                = this.city;
        this.txtZipControl.value                 = this.zip;
        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;
        this.divThanksControl.style.display      = 'none';
        this.divContentControl.style.display     = 'block';
    }

    /**
     * Hide EnquiryForm Body
     */
    PHP2Controls.EnquiryForm.prototype.hideEnquiryFormBody = function()
    {
        //this.objBodyControl.style.display  = 'none';
        this.divContentControl.style.display = 'none';
		this.objOpenControl.style.display    = 'block';
		this.divThanksControl.style.display  = 'none';
		
        if(document.getElementById('ddlSize'))     document.getElementById('ddlSize').style.visibility = '';
        if(document.getElementById('ddlQuantity')) document.getElementById('ddlQuantity').style.visibility = '';
    }

    /**
     * Send Request to the Web Server to Send Mail
     *
     * @param integer categoryId
     */
    PHP2Controls.EnquiryForm.prototype.sendMail = function()
    {
        this.serverResponse = new PHP2Ajax.JSONRequest(this.wsUrl);
        this.serverResponse.call('doSendMail');
        this.serverResponse.add("objectName",     this.objectName);
        this.serverResponse.add("contentType",    this.contentType);
        this.serverResponse.add("mailTemplate",   this.mailTemplate);

        for (var key in this.tplMailObjects)
        {
            this.serverResponse.add(key, this.tplMailObjects[key]);
        }

        this.serverResponse.add("txtSender",      this.txtSenderControl.value);
        this.serverResponse.add("txtSenderName",  this.txtSenderNameControl.value);
        this.serverResponse.add("txtProductName", this.txtProductNameControl.value);
        this.serverResponse.add("txtCompanyName", this.txtCompanyNameControl.value);
        this.serverResponse.add("txtPhoneNumber", this.txtPhoneNumberControl.value);
        this.serverResponse.add("txtAddress1",    this.txtAddress1Control.value);
        this.serverResponse.add("txtAddress2",    this.txtAddress2Control.value);
        this.serverResponse.add("txtCity",        this.txtCityControl.value);
        this.serverResponse.add("txtZip",         this.txtZipControl.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.EnquiryForm.prototype.showLoading = function()
    {
        this.objLoadingControl.style.left  = this.txtMailBodyControl.style.left + 225 + "px";
        this.objLoadingControl.style.top   = this.txtMailBodyControl.style.top  + 290 + "px";
        this.objLoadingControl.style.display  = 'block';
    }

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

    /**
     * Loads Responsed HTML Code to the
     *
     * @param AjaxRequest currentObject
     */
    PHP2Controls.EnquiryForm.prototype.onSendMail = function()
    {
        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();

            this.currentObject.divContentControl.style.display = 'none';
            this.currentObject.divThanksControl.style.display  = 'block';
        }

        return true;
    }

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

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



