
//-----------------------------------------------------------------------
// Copyright (C) Motorwebs Corporation. All rights reserved.
//-----------------------------------------------------------------------
// Contact Form Class

Type.registerNamespace('Motorwebs.UI');

Motorwebs.UI.ContactForm = function(element) {
  Motorwebs.UI.ContactForm.initializeBase(this, [element]);

  this._firstNameElement = null;
  this._lastNameElement = null;
  this._emailElement = null;
  this._phoneElement = null;
  this._commentsElement = null;
  this._sendElement = null;
  this._overlayElement = null;
  this._loaderElement = null;
  this._formName = "";
  this._messageTitle = "";
  this._showLoader = false;
  this._loaderImagePath = "";

}

Motorwebs.UI.ContactForm.prototype = {

    initialize: function() {
        Motorwebs.UI.ContactForm.callBaseMethod(this, 'initialize');

        $addHandlers(this._firstNameElement, { click: this._onElementClick }, this._firstNameElement);
        $addHandlers(this._lastNameElement, { click: this._onElementClick }, this._lastNameElement);
        $addHandlers(this._emailElement, { click: this._onElementClick }, this._emailElement);
        $addHandlers(this._phoneElement, { click: this._onElementClick }, this._phoneElement);
        $addHandlers(this._commentsElement, { click: this._onElementClick }, this._commentsElement);
        $addHandlers(this._sendElement, { click: this._onSendClick }, this);
    },

    dispose: function() {
        $clearHandlers(this._firstNameElement);
        $clearHandlers(this._lastNameElement);
        $clearHandlers(this._emailElement);
        $clearHandlers(this._phoneElement);
        $clearHandlers(this._commentsElement);
        $clearHandlers(this._sendElement);

        Motorwebs.UI.ContactForm.callBaseMethod(this, 'dispose');
    },

    // event handlers ////////////////////////////////////////////////////////////////
    _onElementClick: function(e) {
        if (this.title + ":" == this.value) this.value = "";
    },

    _onSendClick: function(e) {

        if (!this._validateContactForm()) return false;

        var widgetBounds = $common.getBounds(this._element);
        var ole = this._overlayElement;
        ole.style.width = widgetBounds.width + "px";
        ole.style.height = widgetBounds.height + "px";
        $common.setLocation(ole, { x: widgetBounds.x, y: widgetBounds.y });
        $common.setElementOpacity(ole, .6);
        ole.style.display = "block";

        this._loaderShow(widgetBounds);

        // create form post
        var names = "FormName|Section_Contact|FirstName|LastName|Email|Phone|Comments";

        var sb = new Sys.StringBuilder();
        sb.append(this._formName);
        sb.append("|");
        sb.append(this._messageTitle);
        sb.append("|");
        sb.append(this._firstNameElement.value);
        sb.append("|");
        sb.append(this._lastNameElement.value);
        sb.append("|");
        sb.append(this._emailElement.value);
        sb.append("|");
        sb.append(this._phoneElement.value);
        sb.append("|");
        sb.append(this._commentsElement.value);
        var values = sb.toString();

        //contactFormSend(names, values, this._formName);
        Widgets.ContactFormSend(names, values, this._formName, onSuccess, onFailure, "contactFormSend");
    },

    // private methods //////////////////////////////////////////////////////////////
    _validateContactForm: function() {
        if ($common.isNotEmpty(this._firstNameElement)) {
            if ($common.isNotEmpty(this._lastNameElement)) {
                if ($common.isEmailAddr(this._emailElement)) {
                    if ($common.isNotEmpty(this._phoneElement)) {
                        return true;
                    }
                }
            }
        }
        return false;
    },

    _loaderShow: function(widgetBounds) {
        if (this._loaderElement != null && this._showLoader) {

            var loaderWidth = $common.getCurrentStyle(this._loaderElement, "width");
            loaderWidth = $common.parseUnit(loaderWidth);

            var loaderHeight = $common.getCurrentStyle(this._loaderElement, "height");
            loaderHeight = $common.parseUnit(loaderHeight);

            var xOffset = Math.round(widgetBounds.width / 2) - Math.round(loaderWidth.size / 2);
            var yOffset = Math.round(widgetBounds.height / 2) - Math.round(loaderHeight.size / 2);

            $common.setLocation(this._loaderElement, { x: widgetBounds.x + xOffset, y: widgetBounds.y + yOffset });
            this._loaderElement.style.display = "block";
        }
    },

    _hideLoader: function() {
        if (this._loaderElement != null && this._showLoader) {
            this._loaderElement.style.display = "none";
        }
    },

    // public methods ////////////////////////////////////////////////////////////////
    onServiceSuccess: function(response) {
        this._hideLoader();
        var el = this._element;
        el.innerHTML = "<p style='font-weight: bold; font-size: 12px; padding: 15px;'>" + response + "<br /><br /><a href='default.aspx'>Send Another Message</a></p>";
    },

    // public properties //////////////////////////////////////////////////////////////
    get_firstNameElement: function() {
        return this._firstNameElement;
    },

    set_firstNameElement: function(value) {
        this._firstNameElement = value;
    },

    get_lastNameElement: function() {
        return this._lastNameElement;
    },

    set_lastNameElement: function(value) {
        this._lastNameElement = value;
    },

    get_emailElement: function() {
        return this._emailElement;
    },

    set_emailElement: function(value) {
        this._emailElement = value;
    },

    get_phoneElement: function() {
        return this._phoneElement;
    },

    set_phoneElement: function(value) {
        this._phoneElement = value;
    },

    get_commentsElement: function() {
        return this._commentsElement;
    },

    set_commentsElement: function(value) {
        this._commentsElement = value;
    },

    get_sendElement: function() {
        return this._sendElement;
    },

    set_sendElement: function(value) {
        this._sendElement = value;
    },

    get_overlayElement: function() {
        return this._overlayElement;
    },

    set_overlayElement: function(value) {
        this._overlayElement = value;
    },

    get_loaderElement: function() {
        return this._loaderElement;
    },

    set_loaderElement: function(value) {
        this._loaderElement = value;
    },

    get_formName: function() {
        return this._formName;
    },

    set_formName: function(value) {
        this._formName = value;
    },

    get_messageTitle: function() {
        return this._messageTitle;
    },

    set_messageTitle: function(value) {
        this._messageTitle = value;
    },

    get_showLoader: function() {
        return this._showLoader;
    },

    set_showLoader: function(value) {
        this._showLoader = value;
    },

    get_loaderImagePath: function() {
        return this._loaderImagePath;
    },

    set_loaderImagePath: function(value) {
        this._loaderImagePath = value;
    }
}

Motorwebs.UI.ContactForm.registerClass('Motorwebs.UI.ContactForm', Sys.UI.Control);