
/**
 * 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.ShareForm 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.ShareForm = function(objectName, wsUrl, objectId)
    {
        this.objectId = objectId;
        /**
         * Web Service Url
         */
        this.wsUrl          = (wsUrl != null) ? wsUrl : 'wsdl/shareform.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.artistName      = '';
        this.albumTitle      = '';
        this.country         = '';
        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 + '_shareBody';
        var senderControlId          = objectName + '_shareSender';
        var senderNameControlId      = objectName + '_shareSenderName';
        var artistNameControlId      = objectName + '_shareArtistName';
        var albumTitleControlId      = objectName + '_shareAlbumTitle';
        var countryControlId         = objectName + '_shareCountry';
        var cityControlId            = objectName + '_shareCity';
        var zipControlId             = objectName + '_shareZip';
        var toControlId              = objectName + '_shareTo';
        var ccControlId              = objectName + '_shareCc';
        var bccControlId             = objectName + '_shareBcc';
        var subjectControlId         = objectName + '_shareSubject';
        var mailBodyControlId        = objectName + '_shareMailBody';
        var responseMessageControlId = objectName + '_shareResponseMessage';
        var loadingControlId         = objectName + '_shareLoading';
        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.txtAlbumTitleControl  = document.getElementById(albumTitleControlId);

        this.txtArtistNameControl = document.getElementById(artistNameControlId);

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

        /**
         * 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.ShareForm.prototype.setOffsetX = function(offsetX)
    {
        this.offsetX = offsetX;
    }

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

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

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

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

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

    /**
     * Set City
     *
     * @param string city
     */
    PHP2Controls.ShareForm.prototype.setCountry = function(country)
    {
        this.country = country;
    }

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

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

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

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

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

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

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

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

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

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

    /**
     * Show EnquiryForm Body
     */
    PHP2Controls.ShareForm.prototype.showEnquiryFormBody = function()
    {
        $('.relative_releases').removeClass('relative_releases');

        this.init();

        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.id).center();

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

    /**
     * Show EnquiryForm Body
     */
    PHP2Controls.ShareForm.prototype.init = function()
    {
        this.txtSenderControl.value              = this.sender;
        this.txtSenderNameControl.value          = this.senderName;
        this.txtAlbumTitleControl.value           = this.albumTitle;
        this.txtArtistNameControl.value         = this.artistName;
        this.txtCountryControl.value             = this.country;
        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.ShareForm.prototype.hideEnquiryFormBody = function()
    {
        this.objBodyControl.style.display = 'none';
    }

    /**
     * Send Request to the Web Server to Send Mail
     *
     * @param integer categoryId
     */
    PHP2Controls.ShareForm.prototype.sendMail = function()
    {
        this.serverResponse = new PHP2Ajax.JSONRequest(this.wsUrl);
        this.serverResponse.call('doSendMail');
        this.serverResponse.add("objectId",       this.objectId);
        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("txtArtistName",  this.txtArtistNameControl.value);
        this.serverResponse.add("txtAlbumTitle",  this.txtAlbumTitleControl.value);
        this.serverResponse.add("txtCountry",     this.txtCountryControl.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.ShareForm.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.ShareForm.prototype.hideLoading = function()
    {
        this.objLoadingControl.style.display  = 'none';
    }

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

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



