/****************** Devy.UI.Forms.Fields.Base **********************/
Type.registerNamespace('Devy.UI.Forms.Fields');

Devy.UI.Forms.Fields.Base = function () {
    Devy.UI.Forms.Fields.Base.initializeBase(this);

    //Miembros
    this._Parent = null;
    this._Container = null;

    this._FieldContainer = null;

    //Si las clases hijas no lo setearon
    if (!this._FieldContainerCSSClass) this._FieldContainerCSSClass = "GenericField";

    this._ValidationErrorMessagesContainer = null;
    this._Label = null;

    this._fieldIDSufix = "";

    this._BusinessObject = null;
    this._PropertyName = "";
    this._HeaderText = "";
    this._Description = "";

    this._Validators = new Array();
    this._IsValid = true;
    this._AutoValidate = true;

    this._ReadOnly = false;
    this._Enabled = true;

    this._events = null;

    this._InterfaceReady = false;   //Las clases derivadas tienen quye establecerlo a true
    //cuando terminen de inicializar la interface
}

Devy.UI.Forms.Fields.Base.prototype = {
    //*********************************************************************
    //Publicos
    set_Parent: function (value) { this._Parent = value; },
    get_Parent: function () { return this._Parent; },

    set_Container: function (value) { this._Container = value; this._ensureInterface(); },
    get_Container: function () { return this._Container; },

    set_FieldIDSufix: function (value) { this._fieldIDSufix = value },
    get_FieldIDSufix: function () { return this._fieldIDSufix; },

    set_BusinessObject: function (value) {
        this._BusinessObject = value;
        if (this._InterfaceReady) {
            this.ApplyAuthorization();
            this._updateField()
        }
    },
    get_BusinessObject: function () { return this._BusinessObject; },

    set_PropertyName: function (value) { this._PropertyName = value; this._ensureInterface(); },
    get_PropertyName: function () { return this._PropertyName; },

    set_Description: function (value) { this._Description = value },
    get_Description: function () { return this._Description; },

    set_HeaderText: function (value) {
        if (this._HeaderText != value) {
            this._HeaderText = value;
            if (this._Label)
                $(this._Label).text(value);
        }
    },
    get_HeaderText: function () {
        if (this._HeaderText)
            return this._HeaderText;
        else
            return this._PropertyName;
    },

    set_Validators: function (value) {
        if (value) {
            this._Validators = value;
            for (var i = 0; i < this._Validators.length; i++) {
                this._Validators[i].set_FieldToValidate(this);
            }
        }
        else {
            this._Validators = new Array();
        }
    },
    get_Validators: function () { return this._Validators; },
    RegisterValidator: function (validator) {
        validator.set_FieldToValidate(this);
        Array.add(this._Validators, validator);
    },

    get_IsValid: function () { return this._IsValid; },

    set_AutoValidate: function (value) {
        if (this._AutoValidate != value) {
            this._AutoValidate = value;
            this._toogleAutoValidationEventsHooking();
        }
    },
    get_AutoValidate: function () { return this._AutoValidate; },

    set_ReadOnly: function (value) {
        if (this._ReadOnly != value) {
            this._ReadOnly = value;

            if (this._ReadOnly)
                this.set_Enabled(false);
            else
                this.ApplyAuthorization();
        }
    },
    get_ReadOnly: function () { return this._ReadOnly; },

    set_Enabled: function (value) {
        if (this._Enabled != value) {
            this._Enabled = value;

            this._SafeSetEnabled(value);
        }
    },
    get_Enabled: function () { return this._Enabled; },

    set_Properties: function (value) {
        if (value) {
            //POr cada propiedad en value, la copiamos en las nuestras
            for (var propertyName in value)
                this["_" + propertyName] = value[propertyName];

        }
    },

    initialize: function () {
        Devy.UI.Forms.Fields.Base.callBaseMethod(this, 'initialize');
        this._ensureInterface();
    },

    dispose: function () {
        Devy.UI.Forms.Fields.Base.callBaseMethod(this, 'dispose');
    },

    Validate: function () {
        var finalResult = true;

        var sbErrorsSumary = new Sys.StringBuilder();

        for (var i = 0; i < this._Validators.length; i++) {
            result = this._Validators[i].Validate();
            if (!result) {
                finalResult = false;

                sbErrorsSumary.appendLine(
                    String.format('<li class="ValidationError">{0}</li>',
                    Devy.Util.HTMLEncode(this._Validators[i].get_ErrorMessage()))
                );
            }
        }

        if (this._ValidationErrorMessagesContainer) {
            var $valcontainer = $(this._ValidationErrorMessagesContainer);

            $valcontainer.hide();
            $valcontainer.empty();

            if (!finalResult) {
                var ul = $('<ul class ="ValidationErrorSumary">');
                ul.append(sbErrorsSumary.toString());
                $valcontainer.append(ul);
                $valcontainer.show();
            }
        }

        this._set_IsValid(finalResult);

        return finalResult;
    },
    _set_IsValid: function (value) {
        if (this._IsValid != value) {
            this._IsValid = value;

            if (this._FieldContainer) {
                var errCss = 'ValidationError';

                if (this._IsValid)
                    $(this._FieldContainer).removeClass(errCss);
                else
                    $(this._FieldContainer).addClass(errCss);
            }

            this.on_isValidChange();
        }
    },

    ApplyAuthorization: function () {
        if (this._BusinessObject) {
            if (this._BusinessObject.Authorization) {
                this._SafeSetEnabled(this._BusinessObject.Authorization["CanWrite_" + this._PropertyName]);
            }
            else
                this._SafeSetEnabled(true);
        }
        else
            this._SafeSetEnabled(false);
    },

    PrepareToShow: function () {
        /*El form llama este metodo para preparar el control para ser mostrado, si es necesario*/
    },

    UpdateBusinessObject: function () {
        /*POr defecto, pero puede llegar a ser sobreescrita*/
        if (this._BusinessObject) {
            this._BusinessObject[this._PropertyName] = this.get_Value();
        }
    },
    _updateField: function () {
        /*POr defecto, pero puede llegar a ser sobreescrita*/
        if (this._BusinessObject) {
            this.set_Value(this._BusinessObject[this._PropertyName]);
        } else {
            this.clear_Value();
        }
    },


    _SafeSetEnabled: function (value) {
        try {
            this._setEnabled(value && !this._ReadOnly);
        } catch (e) {
            /*do nothing*/
        }
    },

    _ensureInterface: function () {
        if (!this._InterfaceReady) {
            if (this._Container && this._PropertyName) {

                var HeaderText = this.get_HeaderText();
                var fieldID = 'fld_' + this._PropertyName + this._fieldIDSufix;

                this._FieldContainer = $('<div id = "' + fieldID + '-container" class="Field ' + this._PropertyName + " " + this._FieldContainerCSSClass + '" ></div>');
                $(this._Container).append(this._FieldContainer);

                if (HeaderText) {
                    this._Label = $('<label class="FieldLabel" for="' + fieldID + '">' + Devy.Util.HTMLEncode(HeaderText) + '</label>')[0];
                    this._FieldContainer.append(this._Label);
                }
                this._createFieldControl(this._FieldContainer, fieldID);

                if (this._Description) {
                    this._FieldContainer.append('<p class="Description">' + Devy.Util.HTMLEncode(this._Description) + "</p>");
                }

                this._ValidationErrorMessagesContainer = $('<div class="ValidationErrorSumaryContainer">');
                this._ValidationErrorMessagesContainer.hide();
                this._ValidationErrorMessagesContainer = this._ValidationErrorMessagesContainer[0];
                this._FieldContainer.append(this._ValidationErrorMessagesContainer);

                this._FieldContainer = this._FieldContainer[0];

                this._toogleAutoValidationEventsHooking();

                this._SafeSetEnabled(!this._ReadOnly);

                this._updateField();

                this._InterfaceReady = true;
            }
        }
    },

    /*Abstract methods*/
    _toogleAutoValidationEventsHooking: function () {
        throw ("_toogleAutoValidationEventsHooking no implementado");
    },
    _createFieldControl: function (container, fieldID) {
        throw ("_createFieldControl no implementado");
    },
    _setEnabled: function (value) {
        throw ("_setEnabled no implementado");
    },
    clear_Value: function () {
        throw ("clear_Value no implementado");
    },
    get_Value: function () {
        throw ("get_Value no implementado");
    },
    set_Value: function (value) {
        throw ("set_Value no implementado");
    },
    /* /Abstract methods*/

    //Mis eventos    
    get_events: function () {
        if (!this._events) {
            this._events = new Sys.EventHandlerList();
        }
        return this._events;
    },

    add_isValidChange: function (handler) {
        this.get_events().addHandler('isValidChange', handler);
    },
    remove_isValidChange: function (handler) {
        this.get_events().removeHandler('isValidChange', handler);
    },
    on_isValidChange: function () {
        this._raiseEvent("isValidChange");
    },

    add_valueChange: function (handler) {
        this.get_events().addHandler('valueChange', handler);
    },
    remove_valueChange: function (handler) {
        this.get_events().removeHandler('valueChange', handler);
    },
    on_valueChange: function () {
        this._raiseEvent("valueChange");
    },

    _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);
        }
    }
}

Devy.UI.Forms.Fields.Base.registerClass('Devy.UI.Forms.Fields.Base', Sys.Component);

/****************** Devy.UI.Forms.Fields.ReadOnlyHtml **********************/
Devy.UI.Forms.Fields.ReadOnlyHtml = function () {
    Devy.UI.Forms.Fields.ReadOnlyHtml.initializeBase(this);

    this._Content = null;
}

Devy.UI.Forms.Fields.ReadOnlyHtml.prototype = {
    initialize: function () {
        this._FieldContainerCSSClass = "ReadOnlyHtml";
        Devy.UI.Forms.Fields.ReadOnlyHtml.callBaseMethod(this, 'initialize');

    },

    _createFieldControl: function (container, fieldID) {
        this._Content = $('<div name="' + fieldID + '"  id="' + fieldID + '" />');

        $(container).append(this._Content);
    },

    clear_Value: function () {
        this._Content.empty()
    },
    get_Value: function () {
        return this._Content.html();
    },
    set_Value: function (value) {
        this._Content.html(value);
    },

    _toogleAutoValidationEventsHooking: function () {
        /*do nothing*/
    },

    _setEnabled: function (value) {
        /*do nothing*/
    }

}

Devy.UI.Forms.Fields.ReadOnlyHtml.registerClass('Devy.UI.Forms.Fields.ReadOnlyHtml', Devy.UI.Forms.Fields.Base);

Devy.UI.Forms.Fields.ReadOnlyHtml.CreateObject = function () {
    var ctrl = $create(Devy.UI.Forms.Fields.ReadOnlyHtml,
        {},
        {},
        {});

    return ctrl;
}

/****************** Devy.UI.Forms.Fields.Input **********************/
Devy.UI.Forms.Fields.Input = function () {
    Devy.UI.Forms.Fields.Input.initializeBase(this);

    this._inputField = null;
}

Devy.UI.Forms.Fields.Input.prototype = {
    dispose: function () {
        Devy.UI.Forms.Fields.Input.callBaseMethod(this, 'dispose');

        if (this._inputField)
            $clearHandlers(this._inputField);
    },

    _toogleAutoValidationEventsHooking: function () {
        if (!this._AutoValidationDelegate)
            this._AutoValidationDelegate = Function.createDelegate(this, this.Validate);

        if (this._AutoValidationEventsHooked && !this._AutoValidate) {
            //Esta hookeado, lo eliminamos
            $removeHandler(this._inputField, "blur", this._AutoValidationDelegate);
            this._AutoValidationEventsHooked = false;
        }
        else if (this._AutoValidate && !this._AutoValidationEventsHooked) {
            if (this._inputField) {
                $addHandler(this._inputField, "blur", this._AutoValidationDelegate);
                this._AutoValidationEventsHooked = true;
            }
        }
    },
    _setEnabled: function (value) {

        if (value)
            $(this._inputField).removeAttr("disabled");
        else {
            $(this._inputField).attr("disabled", "disabled");
        }

    },

    get_InnerInputField: function () {
        return this._inputField;
    }
}

Devy.UI.Forms.Fields.Input.registerClass('Devy.UI.Forms.Fields.Input', Devy.UI.Forms.Fields.Base);

/****************** Devy.UI.Forms.Fields.Text **********************/
Devy.UI.Forms.Fields.Text = function () {
    Devy.UI.Forms.Fields.Text.initializeBase(this);

    this._Multiline = false;
}

Devy.UI.Forms.Fields.Text.prototype = {
    /*Abstract methods*/
    initialize: function () {
        if (!this._FieldContainerCSSClass) this._FieldContainerCSSClass = "TextField";

        Devy.UI.Forms.Fields.Text.callBaseMethod(this, 'initialize');
    },

    _createFieldControl: function (container, fieldID) {
        if (this._Multiline)
            this._inputField = $('<textarea name="' + fieldID + '" id="' + fieldID + '" cols="50" rows="5" />')[0];
        else
            this._inputField = $('<input name="' + fieldID + '" type="text" id="' + fieldID + '" size="70" />')[0];

        $(container).append(this._inputField);
    },

    clear_Value: function () {
        Devy.Util.SetFormTextFieldValue(this._inputField, "");
    },
    get_Value: function () {
        return Devy.Util.GetFormTextFieldValue(this._inputField);
    },
    set_Value: function (value) {
        Devy.Util.SetFormTextFieldValue(this._inputField, value);
    }
}

Devy.UI.Forms.Fields.Text.registerClass('Devy.UI.Forms.Fields.Text', Devy.UI.Forms.Fields.Input);

Devy.UI.Forms.Fields.Text.CreateObject = function () {
    var ctrl = $create(Devy.UI.Forms.Fields.Text,
        {},
        {},
        {});

    return ctrl;
}


/****************** Devy.UI.Forms.Fields.MultilineText **********************/
Devy.UI.Forms.Fields.MultilineText = function () {
    Devy.UI.Forms.Fields.MultilineText.initializeBase(this);
}

Devy.UI.Forms.Fields.MultilineText.prototype = {
    /*Abstract methods*/
    initialize: function () {
        this._Multiline = true;
        this._FieldContainerCSSClass = "MultilineTextField";

        Devy.UI.Forms.Fields.MultilineText.callBaseMethod(this, 'initialize');
    }
}

Devy.UI.Forms.Fields.MultilineText.registerClass('Devy.UI.Forms.Fields.MultilineText', Devy.UI.Forms.Fields.Text);

Devy.UI.Forms.Fields.MultilineText.CreateObject = function () {
    var ctrl = $create(Devy.UI.Forms.Fields.MultilineText,
        {},
        {},
        {});

    return ctrl;
}

/****************** Devy.UI.Forms.Fields.Password **********************/
Devy.UI.Forms.Fields.Password = function () {
    Devy.UI.Forms.Fields.Password.initializeBase(this);
}

Devy.UI.Forms.Fields.Password.prototype = {
    /*Abstract methods*/
    initialize: function () {
        this._FieldContainerCSSClass = "PasswordField";

        Devy.UI.Forms.Fields.Password.callBaseMethod(this, 'initialize');

    },

    _createFieldControl: function (container, fieldID) {
        this._inputField = $('<input name="' + fieldID + '" type="password" id="' + fieldID + '" size="20" />')[0];

        $(container).append(this._inputField);
    },

    clear_Value: function () {
        Devy.Util.SetFormTextFieldValue(this._inputField, "");
    },
    get_Value: function () {
        return Devy.Util.GetFormTextFieldValue(this._inputField);
    },
    set_Value: function (value) {
        Devy.Util.SetFormTextFieldValue(this._inputField, value);
    }
}

Devy.UI.Forms.Fields.Password.registerClass('Devy.UI.Forms.Fields.Password', Devy.UI.Forms.Fields.Input);

Devy.UI.Forms.Fields.Password.CreateObject = function () {
    var ctrl = $create(Devy.UI.Forms.Fields.Password,
        {},
        {},
        {});

    return ctrl;
}


/****************** Devy.UI.Forms.Fields.Date **********************/
Devy.UI.Forms.Fields.Date = function () {
    Devy.UI.Forms.Fields.Date.initializeBase(this);
}

Devy.UI.Forms.Fields.Date.prototype = {
    /*Abstract methods*/
    initialize: function () {
        this._FieldContainerCSSClass = "DateField";

        Devy.UI.Forms.Fields.Date.callBaseMethod(this, 'initialize');


    },

    _createFieldControl: function (container, fieldID) {
        this._inputField = $('<input name="' + fieldID + '" type="text" id="' + fieldID + '" size="10" style="text-align:right"/>')[0];
        Devy.Util.SetDatePicker(this._inputField);

        $(container).append(this._inputField);
    },

    clear_Value: function () {
        Devy.Util.SetFormTextFieldValue(this._inputField, "");
    },
    get_Value: function () {
        return Devy.Util.GetFormTextFieldValue(this._inputField);
    },
    set_Value: function (value) {
        if (value) {
            var partes = value.split(" ");

            Devy.Util.SetFormTextFieldValue(this._inputField, partes[0]);
        }
        else {
            Devy.Util.SetFormTextFieldValue(this._inputField, "");
        }
    }
}

Devy.UI.Forms.Fields.Date.registerClass('Devy.UI.Forms.Fields.Date', Devy.UI.Forms.Fields.Input);

Devy.UI.Forms.Fields.Date.CreateObject = function () {
    var ctrl = $create(Devy.UI.Forms.Fields.Date,
        {},
        {},
        {});

    return ctrl;
}

/****************** Devy.UI.Forms.Fields.Time **********************/
Devy.UI.Forms.Fields.Time = function () {
    Devy.UI.Forms.Fields.Time.initializeBase(this);
}

Devy.UI.Forms.Fields.Time.prototype = {
    /*Abstract methods*/
    initialize: function () {
        this._FieldContainerCSSClass = "TimeField";

        Devy.UI.Forms.Fields.Time.callBaseMethod(this, 'initialize');


    },

    _createFieldControl: function (container, fieldID) {
        this._inputField = $('<input name="' + fieldID + '" type="text" id="' + fieldID + '" size="8" style="text-align:right"/>')[0];

        $(container).append(this._inputField);
    },

    clear_Value: function () {
        Devy.Util.SetFormTextFieldValue(this._inputField, "");
    },
    get_Value: function () {
        return Devy.Util.GetFormTextFieldValue(this._inputField);
    },
    set_Value: function (value) {
        if (value) {
            var partes = value.split(" ");

            Devy.Util.SetFormTextFieldValue(this._inputField, partes[0]);
        }
        else {
            Devy.Util.SetFormTextFieldValue(this._inputField, "");
        }
    }
}

Devy.UI.Forms.Fields.Time.registerClass('Devy.UI.Forms.Fields.Time', Devy.UI.Forms.Fields.Input);

Devy.UI.Forms.Fields.Time.CreateObject = function () {
    var ctrl = $create(Devy.UI.Forms.Fields.Time,
        {},
        {},
        {});

    return ctrl;
}

/****************** Devy.UI.Forms.Fields.DateTime **********************/
Devy.UI.Forms.Fields.DateTime = function () {
    Devy.UI.Forms.Fields.DateTime.initializeBase(this);

    this._inputFieldHora = null;
}

Devy.UI.Forms.Fields.DateTime.prototype = {
    /*Abstract methods*/
    initialize: function () {
        this._FieldContainerCSSClass = "DateTimeField";

        Devy.UI.Forms.Fields.DateTime.callBaseMethod(this, 'initialize');


    },

    _createFieldControl: function (container, fieldID) {
        this._inputField = $('<input name="' + fieldID + '" type="text" id="' + fieldID + '" size="10" style="text-align:right"/>')[0];
        Devy.Util.SetDatePicker(this._inputField);

        $(container).append(this._inputField);

        this._inputFieldHora = $('<input name="' + fieldID + '_time" type="text" id="' + fieldID + '_time" size="8" style="text-align:right"/>')[0];
        $(container).append(' <label class="Time"><span>Hora</span></label>');
        $(container).append(this._inputFieldHora);
    },

    clear_Value: function () {
        Devy.Util.SetFormTextFieldValue(this._inputField, "");
        Devy.Util.SetFormTextFieldValue(this._inputFieldHora, "");
    },
    get_Value: function () {
        return Devy.Util.StringTrim(Devy.Util.GetFormTextFieldValue(this._inputField) + ' ' +
         Devy.Util.GetFormTextFieldValue(this._inputFieldHora));
    },
    set_Value: function (value) {
        if (value) {
            var partes = value.split(" ");

            Devy.Util.SetFormTextFieldValue(this._inputField, partes[0]);

            if (partes.length > 1)
                Devy.Util.SetFormTextFieldValue(this._inputFieldHora, partes[1]);
            else
                Devy.Util.SetFormTextFieldValue(this._inputFieldHora, "");
        }
        else {
            Devy.Util.SetFormTextFieldValue(this._inputField, "");
            Devy.Util.SetFormTextFieldValue(this._inputFieldHora, "");
        }
    },

    set_DatePartValue: function (value) {
        if (value) {
            var partes = value.split(" ");

            Devy.Util.SetFormTextFieldValue(this._inputField, partes[0]);
        }
        else {
            Devy.Util.SetFormTextFieldValue(this._inputField, "");
        }
    },
    get_DatePartValue: function () {
        return Devy.Util.GetFormTextFieldValue(this._inputField);
    },

    _setEnabled: function (value) {
        if (value) {
            $(this._inputField).removeAttr("disabled");
            $(this._inputFieldHora).removeAttr("disabled");
        }
        else {
            $(this._inputField).attr("disabled", "disabled");
            $(this._inputFieldHora).attr("disabled", "disabled");
        }
    }
}

Devy.UI.Forms.Fields.DateTime.registerClass('Devy.UI.Forms.Fields.DateTime', Devy.UI.Forms.Fields.Input);

Devy.UI.Forms.Fields.DateTime.CreateObject = function () {
    var ctrl = $create(Devy.UI.Forms.Fields.DateTime,
        {},
        {},
        {});

    return ctrl;
}

/****************** Devy.UI.Forms.Fields.Numeric **********************/
Devy.UI.Forms.Fields.Numeric = function () {
    Devy.UI.Forms.Fields.Numeric.initializeBase(this);
}

Devy.UI.Forms.Fields.Numeric.prototype = {
    /*Abstract methods*/
    initialize: function () {
        this._FieldContainerCSSClass = "NumericField";

        Devy.UI.Forms.Fields.Numeric.callBaseMethod(this, 'initialize');


    },

    _createFieldControl: function (container, fieldID) {
        this._inputField = $('<input name="' + fieldID + '" type="text" id="' + fieldID + '" size="20" style="text-align:right" />')[0];
        Devy.Util.SetNumericTextBox(this._inputField);

        $(container).append(this._inputField);
    },

    clear_Value: function () {
        Devy.Util.SetFormTextFieldValue(this._inputField, "");
    },
    get_Value: function () {
        return Devy.Util.GetFormNumericFieldValue(this._inputField);
    },
    set_Value: function (value) {
        Devy.Util.SetFormNumericFieldValue(this._inputField, value);
    }
}

Devy.UI.Forms.Fields.Numeric.registerClass('Devy.UI.Forms.Fields.Numeric', Devy.UI.Forms.Fields.Input);

Devy.UI.Forms.Fields.Numeric.CreateObject = function () {
    var ctrl = $create(Devy.UI.Forms.Fields.Numeric,
        {},
        {},
        {});

    return ctrl;
}


/****************** Devy.UI.Forms.Fields.Boolean **********************/
Devy.UI.Forms.Fields.Boolean = function () {
    Devy.UI.Forms.Fields.Boolean.initializeBase(this);
}

Devy.UI.Forms.Fields.Boolean.prototype = {
    /*Abstract methods*/
    initialize: function () {
        this._FieldContainerCSSClass = "BooleanField";

        Devy.UI.Forms.Fields.Boolean.callBaseMethod(this, 'initialize');


    },

    _createFieldControl: function (container, fieldID) {
        this._inputField = $('<input name="' + fieldID + '" type="checkbox" id="' + fieldID + '" />')[0];

        $(container).append(this._inputField);
    },

    clear_Value: function () {
        Devy.Util.SetFormTextFieldValue(this._inputField, "");
    },
    get_Value: function () {
        return Devy.Util.GetFormBooleanFieldValue(this._inputField);
    },
    set_Value: function (value) {
        Devy.Util.SetFormBooleanFieldValue(this._inputField, value);
    }
}

Devy.UI.Forms.Fields.Boolean.registerClass('Devy.UI.Forms.Fields.Boolean', Devy.UI.Forms.Fields.Input);

Devy.UI.Forms.Fields.Boolean.CreateObject = function () {
    var ctrl = $create(Devy.UI.Forms.Fields.Boolean,
        {},
        {},
        {});

    return ctrl;
}

/****************** Devy.UI.Forms.Fields.SelectFromDialogBase **********************/
Devy.UI.Forms.Fields.SelectFromDialogBase = function () {
    Devy.UI.Forms.Fields.SelectFromDialogBase.initializeBase(this);

    this._PropertyDisplayName = "";
    this._SelectedObjectPropertyName = "";
    this._SelectedObjectPropertyDisplayName = "";

    this._SelectedValue = null;
    this._SelectedValueDisplay = "";

    this._DialogTitle = "Seleccione un valor";
    this._DialogClass = "devy-SelectValueDlg";
    this._DialogFrmClass = "SelectValueDlgFrm";
    this._DialogWidth = $(window).width() - 80;
    this._DialogHeight = $(window).height() - 80;

    this._cmdSelect = null;
    this._cmdClear = null;

    this._lblDisplay = null;

    this._dlgContainer = null;
}

Devy.UI.Forms.Fields.SelectFromDialogBase.prototype = {
    set_SelectedObjectPropertyName: function (value) { this._SelectedObjectPropertyName = value; },
    get_SelectedObjectPropertyName: function () { return this._SelectedObjectPropertyName; },

    set_SelectedObjectPropertyDisplayName: function (value) { this._SelectedObjectPropertyDisplayName = value; },
    get_SelectedObjectPropertyDisplayName: function () { return this._SelectedObjectPropertyDisplayName; },

    set_PropertyDisplayName: function (value) { this._PropertyDisplayName = value; },
    get_PropertyDisplayName: function () { return this._PropertyDisplayName; },

    initialize: function () {
        this._FieldContainerCSSClass = "SelectableValueField";

        Devy.UI.Forms.Fields.SelectFromDialogBase.callBaseMethod(this, 'initialize');


    },

    dispose: function () {
        Devy.UI.Forms.Fields.SelectFromDialogBase.callBaseMethod(this, 'dispose');

        if (this._cmdSelect)
            $clearHandlers(this._cmdSelect);

        if (this._cmdClear)
            $clearHandlers(this._cmdClear);
    },

    UpdateBusinessObject: function () {
        if (this._BusinessObject) {
            this._BusinessObject[this._PropertyName] = this._SelectedValue;

            if (this._PropertyDisplayName)
                this._BusinessObject[this._PropertyDisplayName] = this._SelectedValueDisplay;
        }
    },

    _updateField: function () {
        if (this._BusinessObject) {
            var value = this._BusinessObject[this._PropertyName];

            var display = "";
            if (this._PropertyDisplayName)
                display = this._BusinessObject[this._PropertyDisplayName];

            this._setSelectedValueAndDisplay(value, display);


        }
        else {
            this.clear_Value();
        }
    },

    _createFieldControl: function (container, fieldID) {
        var $container = $(container);

        var tmp = $('<div class = "ValueSelector" />');
        $container.append(tmp);

        this._lblDisplay = $('<label class="ValueDisplay" />')[0];
        tmp.append(this._lblDisplay);

        this._cmdSelect = $('<a href="#" class="Command SelectCmd" title="' + this._DialogTitle + '"><span>Seleccionar</span></a>')[0]; ;
        tmp.append(this._cmdSelect);

        var contexto = this;
        $addHandler(this._cmdSelect, "click", function (evt) {
            evt.preventDefault();

            if (contexto._Enabled)
                contexto._showDialogForm();
        });

        this._cmdClear = $('<a href="#" class="Command ClearCmd" title="Limpiar"><span>Limpiar</span></a>')[0]; ;
        tmp.append(this._cmdClear);

        $addHandler(this._cmdClear, "click", function (evt) {
            evt.preventDefault();

            if (contexto._Enabled)
                contexto.clear_Value();
        });
    },

    _showDialogForm: function () {
        //Mostramos el dialogo
        this._getDlg().dialog({ dialogClass: this._DialogClass,
            modal: true,
            title: this._DialogTitle,
            height: this._DialogHeight,
            width: this._DialogWidth,
            closeOnEscape: true,
            close: function () { $(this).dialog('destroy'); }
        });
    },

    _setSelectedObject: function (SelectedObject) {
        this._getDlg().dialog('close');

        var value = null;
        if (this._SelectedObjectPropertyName) {
            value = SelectedObject[this._SelectedObjectPropertyName];
        }
        else {
            value = SelectedObject;
        }

        var display = "";
        if (this._SelectedObjectPropertyDisplayName)
            display = SelectedObject[this._SelectedObjectPropertyDisplayName];

        this._setSelectedValueAndDisplay(value, display);

        this.Validate();
    },
    _setSelectedValueAndDisplay: function (value, display) {
        this._SelectedValue = value;
        this._SelectedValueDisplay = display;

        if (!this._SelectedValueDisplay)
            this._SelectedValueDisplay = this._SelectedValue;

        if (this._SelectedValueDisplay) {
            this._setLabelText(this._SelectedValueDisplay);
        }
        else {
            this._setLabelText("Seleccione...");
        }
    },

    _setLabelText: function (text) {
        $(this._lblDisplay).text(text);
    },

    _getDlg: function () {
        if (!this._dlgContainer) {
            this._dlgContainer = $('<div class="SelectValueFrm"></div>');
            var frm = this._createSelectObjectFrm(Function.createDelegate(this, this._setSelectedObject));

            this._dlgContainer.append(frm);
        }
        return this._dlgContainer;
    },

    /*Abstract methods*/
    _createSelectObjectFrm: function (CompleteCallBack) {
        throw ("_createSelectObjectFrm no implementado");
    },

    _toogleAutoValidationEventsHooking: function () {
        /*do nothing*/
    },
    clear_Value: function () {
        this._setSelectedValueAndDisplay(0, "");
    },

    _setEnabled: function (value) {
        if (value)
            $(this._cmdSelect).show();
        else {
            $(this._cmdSelect).hide();
        }
    },

    get_Value: function () {
        return this._SelectedValue;
    },
    set_Value: function (value) {
        this._setSelectedValueAndDisplay(value, ""); //debieramos tener info de Display, pero aca no la tenemos
    }
}

Devy.UI.Forms.Fields.SelectFromDialogBase.registerClass('Devy.UI.Forms.Fields.SelectFromDialogBase', Devy.UI.Forms.Fields.Base);

/****************** Devy.UI.Forms.Fields.ImageURL **********************/
Devy.UI.Forms.Fields.ImageURL = function () {
    Devy.UI.Forms.Fields.ImageURL.initializeBase(this);

    this._SelectedValue = null;

    this._cmdSelect = null;
    this._txtDisplay = null;

    this._img = $('<img />').hide();
    this._lblSinImagen = $('<p class="SinImagen"><span>SIN IMAGEN</span></p>').hide();
}

Devy.UI.Forms.Fields.ImageURL.prototype = {
    initialize: function () {
        this._FieldContainerCSSClass = "ImageURLField";

        Devy.UI.Forms.Fields.ImageURL.callBaseMethod(this, 'initialize');


    },

    dispose: function () {
        Devy.UI.Forms.Fields.ImageURL.callBaseMethod(this, 'dispose');

        if (this._cmdSelect)
            $clearHandlers(this._cmdSelect);

        if (this._txtDisplay)
            $clearHandlers(this._txtDisplay);
    },

    UpdateBusinessObject: function () {
        if (this._BusinessObject) {
            this._BusinessObject[this._PropertyName] = this._SelectedValue;
        }
    },

    _updateField: function () {
        if (this._BusinessObject) {
            var value = this._BusinessObject[this._PropertyName];


            this._setSelectedValueAndDisplay(value);
        }
        else {
            this.clear_Value();
        }
    },

    _createFieldControl: function (container, fieldID) {
        var $container = $(container);
        var contexto = this;

        var tmp = $('<div class = "URLSelector" />');
        $container.append(tmp);

        //Thumbnail
        var tbContainer = $('<div class="ThumbNailContainer"></div>');
        tbContainer.append(this._img);
        tbContainer.append(this._lblSinImagen);
        tmp.append(tbContainer);

        tmp.append('<p class="FieldDescription">Ingrese URL o seleccione una imagen de su carpeta de recursos</p>');

        this._txtDisplay = $('<input name="' + fieldID + '" type="text" id="' + fieldID + '" size="50" />')[0];
        tmp.append(this._txtDisplay);
        $addHandler(this._txtDisplay, "blur", function (evt) {
            evt.preventDefault();

            if (contexto._Enabled)
                this._SelectedValue = contexto._txtDisplay.value;
        });

        var fieldToEdit = this._txtDisplay;

        this._txtDisplay.onchange = function () {
            contexto._SelectedValue = fieldToEdit.value;
            contexto._updateImg();
        };

        this._cmdSelect = $('<a href="#" class="Command SelectCmd" title="Seleccionar una imagen de mi carpeta de recursos"><span>Seleccionar</span></a>')[0]; ;
        tmp.append(this._cmdSelect);
        $addHandler(this._cmdSelect, "click", function (evt) {
            evt.preventDefault();

            if (contexto._Enabled)
                Devy.UI.Admin.Recursos.RecursosManager.Dialog.SelectImage(fieldToEdit);
        });

        $container.append(tmp);
    },


    _setSelectedValueAndDisplay: function (value) {
        if (value) {
            this._SelectedValue = value;
            this._txtDisplay.value = this._SelectedValue;
        }
        else {
            this._SelectedValue = "";
            this._txtDisplay.value = "";
        }

        this._updateImg();
    },

    _updateImg: function () {
        if (this._SelectedValue) {
            this._img.attr("src", Devy.Util.ThumbNailURL(this._SelectedValue, 70, 70));
            this._img.attr("title", this._SelectedValue);

            this._lblSinImagen.hide();
            this._img.show();
        }
        else {
            this._img.hide();
            this._lblSinImagen.show();
        }
    },

    _toogleAutoValidationEventsHooking: function () {
        /*do nothing*/
    },
    clear_Value: function () {
        this._setSelectedValueAndDisplay(0, "");
    },

    _setEnabled: function (value) {
        if (value) {
            $(this._cmdSelect).show();
            $(this._txtDisplay).removeAttr("disabled");
        }
        else {
            $(this._cmdSelect).hide();
            $(this._txtDisplay).attr("disabled", "disabled");
        }
    },

    get_Value: function () {
        return this._SelectedValue;
    },
    set_Value: function (value) {
        this._setSelectedValueAndDisplay(value, "");
    }
}

Devy.UI.Forms.Fields.ImageURL.registerClass('Devy.UI.Forms.Fields.ImageURL', Devy.UI.Forms.Fields.Base);

Devy.UI.Forms.Fields.ImageURL.CreateObject = function () {
    var ctrl = $create(Devy.UI.Forms.Fields.ImageURL,
        {},
        {},
        {});

    return ctrl;
}


//*** TagAsociations ******************************************************************
Devy.UI.Forms.Fields.TagAsociations = function () {
    Devy.UI.Forms.Fields.TagAsociations.initializeBase(this);

    this._TagsTreeContainer = null;
    this._TagAsociationsContainer = null;

    this._boTagsAsociations = new Array();
}

Devy.UI.Forms.Fields.TagAsociations.prototype = {
    initialize: function () {
        this._FieldContainerCSSClass = "TagAsociationsField";
        Devy.UI.Forms.Fields.TagAsociations.callBaseMethod(this, 'initialize');


    },

    UpdateBusinessObject: function () {
        if (this._BusinessObject) {
            var newTags = new Array();

            var sb = new Sys.StringBuilder();

            for (var i = 0; i < this._boTagsAsociations.length; i++) {
                sb.append(this._boTagsAsociations[i].Tag + ", ");
                Array.add(newTags, this._boTagsAsociations[i]);
            }

            var TagsString = sb.toString();
            if (TagsString.length > 0) {
                TagsString.substring(0, TagsString.length - 3);
            }
            this._BusinessObject.TagsString = TagsString;

            this._BusinessObject[this._PropertyName] = newTags;

        }
    },

    _updateField: function () {
        if (this._BusinessObject) {
            Array.clear(this._boTagsAsociations);
            Array.addRange(this._boTagsAsociations, this._BusinessObject[this._PropertyName]);

            this._updateboTagsAsociations();
        }
        else {
            this.clear_Value();
        }
    },


    _createFieldControl: function (container, fieldID) {
        var $container = $(container);
        var contexto = this;


        var mainContainer = $('<div class="TagAsociations"></div>');
        $container.append(mainContainer);

        var tmp = $('<div class="BusinessObjectTagsContainer"><h4>Tags asociados</h4></div>')
        mainContainer.append(tmp);

        this._TagAsociationsContainer = $('<ul class="BusinessObjectTags"></ul>');
        tmp.append(this._TagAsociationsContainer);

        this._TagsTreeContainer = $('<div class="TagsTreeContainer">');

        this._TagsTreeContainer.append('<h4>Tags disponibles. Doble click en un Tag para agregarlo</h4>');

        mainContainer.append(this._TagsTreeContainer);

        $create(Devy.UI.Admin.Tags.TagsEdit,
        {
            "Parent": this,
            "Container": this._TagsTreeContainer[0],
            "Selectable": true
        },
        {
            "tagClick": this._onTagClick
        },
        {});
        this._updateboTagsAsociations();

    },

    _onTagClick: function (s, e) {
        if (s.get_Parent()._Enabled)
            s.get_Parent()._addTag(e.BusinessObject.Id, e.BusinessObject.NombreCompleto);
    },

    _updateboTagsAsociations: function () {
        this._TagAsociationsContainer.empty();

        if (this._boTagsAsociations.length == 0) {
            this._TagAsociationsContainer.append('<li class="BusinessObjectTag"><em>No hay Tags asignados</em></li>');
        }
        else {
            var contexto = this;

            for (var i = 0; i < this._boTagsAsociations.length; i++) {
                var item = $('<li class="BusinessObjectTag"><span>' + this._boTagsAsociations[i].Tag + '</span></li>');

                if (this._Enabled) {
                    var deleteCmd = $('<a href="#" class="Command DeleteCmd" title="Eliminar este Tag"><span>Eliminar</span></a>')[0];

                    deleteCmd.__IDTag = this._boTagsAsociations[i].IDTag;

                    $addHandler(deleteCmd, "click", function (evt) {
                        evt.preventDefault();
                        contexto._removeTag(this.__IDTag);
                    });

                    item.append(deleteCmd);
                }

                this._TagAsociationsContainer.append(item);
            }
        }
    },


    _addTag: function (IDTag, Tag) {
        if (this.get_Enabled()) {
            //ver que no lo tenga ya
            for (var i = 0; i < this._boTagsAsociations.length; i++) {
                if (this._boTagsAsociations[i].IDTag == IDTag) {
                    Devy.Notifications.ShowError('Error', 'Ya se agrego el Tag "' + Tag + '".');
                    return;
                }
            }

            var objeto = { "IDTag": IDTag, "Tag": Tag };
            Array.add(this._boTagsAsociations, objeto);
            this._updateboTagsAsociations();
        }
    },

    _removeTag: function (IDTag) {
        if (this.get_Enabled()) {
            for (var i = 0; i < this._boTagsAsociations.length; i++) {
                if (this._boTagsAsociations[i].IDTag == IDTag) {
                    Array.removeAt(this._boTagsAsociations, i);

                    this._updateboTagsAsociations();
                    break;
                }
            }
        }
    },

    _toogleAutoValidationEventsHooking: function () {
        /*do nothing*/
    },

    clear_Value: function () {
        this._boTagsAsociations = new Array();
        this._updateboTagsAsociations();
    },

    _setEnabled: function (value) {
        if (value) {
            $(this._TagsTreeContainer).show();
        }
        else {
            $(this._TagsTreeContainer).hide();
        }
    },

    get_Value: function () {
        return this._boTagsAsociations;
    },
    set_Value: function (value) {
        this._boTagsAsociations = value;
        this._updateboTagsAsociations();
    }
}

Devy.UI.Forms.Fields.TagAsociations.registerClass('Devy.UI.Forms.Fields.TagAsociations', Devy.UI.Forms.Fields.Base);

Devy.UI.Forms.Fields.TagAsociations.CreateObject = function () {
    var ctrl = $create(Devy.UI.Forms.Fields.TagAsociations,
        {},
        {},
        {});

    return ctrl;
}

//*** RichText ******************************************************************
Devy.UI.Forms.Fields.RichText = function () {
    Devy.UI.Forms.Fields.RichText.initializeBase(this);

    this._RichTextEditor = null;
    this._AutoValidationEventsHooked = false;
}

Devy.UI.Forms.Fields.RichText.prototype = {

    initialize: function () {
        this._FieldContainerCSSClass = "RichTextField";
        Devy.UI.Forms.Fields.RichText.callBaseMethod(this, 'initialize');


    },

    _createFieldControl: function (container, fieldID) {
        this._RichTextEditor = $create(Devy.UI.RichTextEditor,
        {
            "Container": container,
            "Parent": this
        },
        {},
        {});
    },

    PrepareToShow: function () {
        if (this._RichTextEditor) this._RichTextEditor.PrepareToShow();
    },

    _richTextBlurHandler: function (e) {
        this.Validate();
    },

    _toogleAutoValidationEventsHooking: function () {
        if (!this._AutoValidationDelegate)
            this._AutoValidationDelegate = Function.createDelegate(this, this._richTextBlurHandler);

        if (this._AutoValidationEventsHooked && !this._AutoValidate) {
            //Esta hookeado, lo eliminamos
            this._RichTextEditor.remove_blur(this._AutoValidationDelegate);
            this._AutoValidationEventsHooked = false;
        }
        else if (this._AutoValidate && !this._AutoValidationEventsHooked) {
            if (this._RichTextEditor) {
                this._RichTextEditor.add_blur(this._AutoValidationDelegate);
                this._AutoValidationEventsHooked = true;
            }
        }
    },

    clear_Value: function () {
        this.set_Value("");
    },

    _setEnabled: function (value) {
        //????
    },

    get_Value: function () {
        return this._RichTextEditor.get_Value();
    },
    set_Value: function (value) {
        this._RichTextEditor.set_Value(value);
    }
}

Devy.UI.Forms.Fields.RichText.registerClass('Devy.UI.Forms.Fields.RichText', Devy.UI.Forms.Fields.Base);


Devy.UI.Forms.Fields.RichText.CreateObject = function () {
    var ctrl = $create(Devy.UI.Forms.Fields.RichText,
        {},
        {},
        {});

    return ctrl;
}

//*** Usuario ******************************************************************
Devy.UI.Forms.Fields.Usuario = function () {
    Devy.UI.Forms.Fields.Usuario.initializeBase(this);
}

Devy.UI.Forms.Fields.Usuario.prototype = {
    //*********************************************************************
    //Publicos


    initialize: function () {
        this._FieldContainerCSSClass += " Usuario";
        this._DialogTitle = "Seleccione un usuario";

        this._PropertyDisplayName = "Usuario";
        this._SelectedObjectPropertyName = "Id";
        this._SelectedObjectPropertyDisplayName = "NombreCompleto";

        this._DialogFrmClass += " SelectUsuarioFrm";

        Devy.UI.Forms.Fields.Usuario.callBaseMethod(this, 'initialize');
    },

    _createSelectObjectFrm: function (CompleteCallBack) {
        if (!this._SelectUsuarioFrm) {
            this._SelectUsuarioFrm = $('<div class="' + this._DialogFrmClass + '"></div>');

            this._SelectUsuarioFrm.append('<p class="FieldDescription">Doble click en un usuario para seleccionarlo...</p>');

            $create(Devy.UI.Admin.Usuarios.UsuariosEdit,
            {
                "Parent": this,
                "Container": this._SelectUsuarioFrm[0],
                "Selectable": true
            },
            {
                "rowClick": function (s, e) { CompleteCallBack(e.BusinessObject) }
            },
            {});

            this._SelectUsuarioFrm = this._SelectUsuarioFrm[0];
        }

        return this._SelectUsuarioFrm;
    }
}

Devy.UI.Forms.Fields.Usuario.registerClass('Devy.UI.Forms.Fields.Usuario', Devy.UI.Forms.Fields.SelectFromDialogBase);


Devy.UI.Forms.Fields.Usuario.CreateObject = function () {
    var ctrl = $create(Devy.UI.Forms.Fields.Usuario,
        {},
        {},
        {});

    return ctrl;
}

//*** Rol ******************************************************************
Devy.UI.Forms.Fields.Rol = function () {
    Devy.UI.Forms.Fields.Rol.initializeBase(this);
}

Devy.UI.Forms.Fields.Rol.prototype = {
    //*********************************************************************
    //Publicos


    initialize: function () {
        this._FieldContainerCSSClass += " Rol Role";
        this._DialogTitle = "Seleccione un Rol";


        this._PropertyDisplayName = "Rol";
        this._SelectedObjectPropertyName = "Id";
        this._SelectedObjectPropertyDisplayName = "Nombre";

        this._DialogFrmClass += " SelectRolFrm";

        Devy.UI.Forms.Fields.Rol.callBaseMethod(this, 'initialize');
    },

    _createSelectObjectFrm: function (CompleteCallBack) {
        if (!this._SelectRolFrm) {
            this._SelectRolFrm = $('<div class="' + this._DialogFrmClass + '"></div>');

            this._SelectRolFrm.append('<p class="FieldDescription">Doble click en un Rol para seleccionarlo...</p>');

            $create(Devy.UI.Admin.Usuarios.RolesEdit,
            {
                "Parent": this,
                "Container": this._SelectRolFrm[0],
                "Selectable": true
            },
            {
                "rowClick": function (s, e) { CompleteCallBack(e.BusinessObject) }
            },
            {});

            this._SelectRolFrm = this._SelectRolFrm[0];
        }

        return this._SelectRolFrm;
    }
}

Devy.UI.Forms.Fields.Rol.registerClass('Devy.UI.Forms.Fields.Rol', Devy.UI.Forms.Fields.SelectFromDialogBase);


Devy.UI.Forms.Fields.Rol.CreateObject = function () {
    var ctrl = $create(Devy.UI.Forms.Fields.Rol,
        {},
        {},
        {});

    return ctrl;
}


//*** Autor ******************************************************************
Devy.UI.Forms.Fields.Autor = function () {
    Devy.UI.Forms.Fields.Autor.initializeBase(this);
}

Devy.UI.Forms.Fields.Autor.prototype = {
    initialize: function () {
        Devy.UI.Forms.Fields.Autor.callBaseMethod(this, 'initialize');

        this._FieldContainerCSSClass += " Autor";
        this._PropertyDisplayName = "Autor";
        this._DialogFrmClass += " SelectAutorFrm";
    }
}

Devy.UI.Forms.Fields.Autor.registerClass('Devy.UI.Forms.Fields.Autor', Devy.UI.Forms.Fields.Usuario);


Devy.UI.Forms.Fields.Autor.CreateObject = function () {
    var ctrl = $create(Devy.UI.Forms.Fields.Autor,
        {},
        {},
        {});

    return ctrl;
}

/****************** Devy.UI.Forms.Fields.URLViewer **********************/
Devy.UI.Forms.Fields.URLViewer = function () {
    Devy.UI.Forms.Fields.URLViewer.initializeBase(this);

    this._valueLabel = null;
    this._linkVer = null;

    this._valueLabelText = "";
}

Devy.UI.Forms.Fields.URLViewer.prototype = {
    /*Abstract methods*/
    initialize: function () {
        this._FieldContainerCSSClass = "URLViewerField";
        this.set_ReadOnly(true);

        Devy.UI.Forms.Fields.URLViewer.callBaseMethod(this, 'initialize');


    },

    _toogleAutoValidationEventsHooking: function () {
        /*do nothing*/
    },

    _setEnabled: function (value) {
        /*do nothing*/
    },

    _createFieldControl: function (container, fieldID) {
        this._valueLabel = $('<label class="URLViewerLabel" /> ');
        this._linkVer = $('<a class="URLViewerLink" href="#" target="_blank" ><span>[Ver]</span></a>');
        this._linkVer.hide();

        $(container).append(this._valueLabel);
        $(container).append(this._linkVer);
    },

    clear_Value: function () {
        this._valueLabelText = "";
        this._updateLabel();
    },
    get_Value: function () {
        return this._valueLabelText;
    },
    set_Value: function (value) {
        this._valueLabelText = value;
        this._updateLabel();
    },

    _updateLabel: function () {
        this._valueLabel.text(this._valueLabelText);

        if (this._valueLabelText) {
            this._linkVer.attr("href", this._valueLabelText);
            this._linkVer.show();
        }
        else {
            this._linkVer.attr("href", "#");
            this._linkVer.hide();
        }
    }
}

Devy.UI.Forms.Fields.URLViewer.registerClass('Devy.UI.Forms.Fields.URLViewer', Devy.UI.Forms.Fields.Base);

Devy.UI.Forms.Fields.URLViewer.CreateObject = function () {
    var ctrl = $create(Devy.UI.Forms.Fields.URLViewer,
        {},
        {},
        {});

    return ctrl;
}


Devy.UI.Forms.Fields.OptionList = function () {
    Devy.UI.Forms.Fields.OptionList.initializeBase(this);

    this._DataSource = null;
    this._initialized = false;

    this._selectedValue = null;
}

Devy.UI.Forms.Fields.OptionList.prototype = {
    /*Abstract methods*/
    initialize: function () {
        this._FieldContainerCSSClass = "OptionsList";

        Devy.UI.Forms.Fields.OptionList.callBaseMethod(this, 'initialize');


    },

    get_DataSource: function () {
        return this._DataSource;
    },
    set_DataSource: function (value) {
        this._DataSource = value;

        if (this._initialized)
            this._populateList();
    },

    _createFieldControl: function (container, fieldID) {

        this._inputField = $('<select name="' + fieldID + '" id="' + fieldID + '" />')[0];

        $(container).append(this._inputField);

        this._initialized = true;

        this._populateList();
    },

    _populateList: function () {
        var jqField = $(this._inputField);

        jqField.empty();

        if (this._DataSource) {
            for (var i = 0; i < this._DataSource.length; i++) {
                var item = $('<option></option>')[0];

                item.value = this._DataSource[i].Value;
                item.text = this._DataSource[i].Text;

                jqField.append(item);
            }
        }

        if (this._selectedValue) jqField.val(this._selectedValue);
    },

    clear_Value: function () {

    },
    get_Value: function () {
        this._selectedValue = $(this._inputField).val();

        return this._selectedValue;
    },
    set_Value: function (value) {
        this._selectedValue = value;
        $(this._inputField).val(value);
    }
}

Devy.UI.Forms.Fields.OptionList.registerClass('Devy.UI.Forms.Fields.OptionList', Devy.UI.Forms.Fields.Input);

Devy.UI.Forms.Fields.OptionList.CreateObject = function (DataSource) {

    var ds = null;
    if (DataSource) ds = DataSource;

    var ctrl = $create(Devy.UI.Forms.Fields.OptionList,
        {
            "DataSource": ds
        },
        {},
        {});

    return ctrl;
}

/****************** Devy.UI.Forms.Fields.Captcha **********************/
Devy.UI.Forms.Fields.Captcha = function () {
    Devy.UI.Forms.Fields.Captcha.initializeBase(this);

    this._imgContainer = null;
    this._CaptchaProcessID = "captchaEmptyProcessID";
}

Devy.UI.Forms.Fields.Captcha.prototype = {
    /*Abstract methods*/
    initialize: function () {
        this._FieldContainerCSSClass = "CaptchaField";

        Devy.UI.Forms.Fields.Captcha.callBaseMethod(this, 'initialize');


    },

    _createFieldControl: function (container, fieldID) {
        this._imgContainer = $('<div class="CaptchaImageContainer" />');
        $(container).append(this._imgContainer);

        this._inputField = $('<input name="' + fieldID + '" type="text" id="' + fieldID + '" size="20" />')[0];
        $(container).append(this._inputField);
    },

    PrepareToShow: function () {
        /*El form llama este metodo para preparar el control para ser mostrado, si es necesario*/
        //regeneramos la imagen
        this._imgContainer.html("");
        this._imgContainer.html('<img src="/_devy/captcha.aspx?processid=' + this._CaptchaProcessID + '&rndfakeparam=' + Math.floor(Math.random() * 999999) + '" />');
    },

    clear_Value: function () {
        Devy.Util.SetFormTextFieldValue(this._inputField, "");
    },
    get_Value: function () {
        return Devy.Util.GetFormTextFieldValue(this._inputField);
    },
    set_Value: function (value) {
        Devy.Util.SetFormTextFieldValue(this._inputField, value);
    },

    get_CaptchaProcessID: function () {
        return this._CaptchaProcessID;
    },
    set_CaptchaProcessID: function (value) {
        this._CaptchaProcessID = value;
    }
}

Devy.UI.Forms.Fields.Captcha.registerClass('Devy.UI.Forms.Fields.Captcha', Devy.UI.Forms.Fields.Input);

Devy.UI.Forms.Fields.Captcha.CreateObject = function () {
    var ctrl = $create(Devy.UI.Forms.Fields.Captcha,
        {},
        {},
        {});

    return ctrl;
}
