Type.registerNamespace('Devy.UI');

Devy.UI.ContactForm = function () {
    Devy.UI.ContactForm.initializeBase(this);

    //Miembros
    this._Container = null;
    this._CSSClass = "";

    this._ContactInfoIndex = 1;

    this._FieldDescriptors = null;
    this._FormFieldValues = {};

    this._ServiceBussy = false;
    this._ServiceLastResult = "";
    this._ServiceLastError = "";
}

Devy.UI.ContactForm.prototype = {
    //*********************************************************************
    //Publicos
    set_Container: function (value) { this._Container = value; },
    get_Container: function () { return this._Container; },

    set_CSSClass: function (value) { this._CSSClass = value; },
    get_CSSClass: function () { return this._CSSClass; },

    set_ContactInfoIndex: function (value) { this._ContactInfoIndex = value; },
    get_ContactInfoIndex: function () { return this._ContactInfoIndex; },

    set_FieldDescriptors: function (value) { this._FieldDescriptors = value; },
    get_FieldDescriptors: function () { return this._FieldDescriptors; },


    initialize: function () {
        Devy.UI.ContactForm.callBaseMethod(this, 'initialize');

        this._ServicePortal = Devy.UI.Contacto.ContactoService;
        
        var contexto = this;
        setTimeout(function () {
            contexto._initInterface();

            contexto._atachEvents();
        }, 1000);

    },

    dispose: function () {
        this._detachEvents();
        Devy.UI.ContactForm.callBaseMethod(this, 'dispose');
    },

    _atachEvents: function () {
        /**/
    },

    _detachEvents: function () {
        /**/
    },


    //*********************************************************************
    //Event Handlers

    //*********************************************************************
    //Mis eventos    
    get_events: function () {
        if (!this._events) {
            this._events = new Sys.EventHandlerList();
        }
        return this._events;
    },
    add_beginSend: function (handler) {
        this.get_events().addHandler('beginSend', handler);
    },
    remove_beginSend: function (handler) {
        this.get_events().removeHandler('beginSend', handler);
    },
    add_endSend: function (handler) {
        this.get_events().addHandler('endSend', handler);
    },
    remove_endSend: function (handler) {
        this.get_events().removeHandler('endSend', handler);
    },
    _raiseEvent: function (eventName, eventArgs) {
        var handler = this.get_events().getHandler(eventName);

        var theEventArgs = null;
        if (handler) {
            if (!eventArgs) {
                theEventArgs = Sys.EventArgs.Empty;
            }
            else {
                theEventArgs = eventArgs;
            }

            handler(this, theEventArgs);
        }
    },

    _initInterface: function () {
        var css = "Contacto";
        if (this._CSSClass) css += " " + this._CSSClass;

        var tmpContainer = $('<div class="' + css + '">');
        $(this._Container).append(tmpContainer);


        this._Form = $create(Devy.UI.Forms.Form,
        {
            "Parent": this,
            "Container": tmpContainer[0],
            "CSSClass": this._CSSClass,
            "CommandOKText": "Enviar",
            "FieldDescriptors": this._FieldDescriptors,
            "ShowCommandCancel": false
        },
        {
            "OKClick": this._formOKClick
        },
        {});

        this._Form.set_BusinessObject(this._FormFieldValues);
        this._Form.PrepareToShow();
    },

    _formOKClick: function (s, e) {
        s.get_Parent()._BeginSend();
    },

    _BeginSend: function () {
        //Invocamos el servicio
        var ServiceData = new Array();
        ServiceData.Requester = this;

        var fieldsArray = new Array();

        for (var i = 0; i < this._FieldDescriptors.length; i++) {
            var propName = this._FieldDescriptors[i].PropertyName;

            fieldsArray[i] = { "Name": propName, "Value": this._FormFieldValues[propName] };
        }


        Devy.Notifications.ShowLoading("Enviando mensaje...");

        this._ServicePortal.Send(this._ContactInfoIndex, fieldsArray,
                    this._onSendServerSuccess, this._onSendServerError, ServiceData);
    },


    _onSendServerSuccess: function (result, context, methodName) {
        Devy.Notifications.HideLoading();
        Devy.Notifications.ShowMessage("Resultado", result);
    },
    _onSendServerError: function (error, context, methodName) {
        Devy.Notifications.HideLoading();
        Devy.Notifications.ShowError("Error al intentar enviar su mensaje", Devy.Util.HTMLEncode(error.get_message()));
    }
}


Devy.UI.ContactForm.registerClass('Devy.UI.ContactForm', Sys.Component);

