﻿Type.registerNamespace('V_Ent.FlowersByNet.Web.Controls');

V_Ent.FlowersByNet.Web.Controls.TextBoxCounterBehavior = function(element) {

    V_Ent.FlowersByNet.Web.Controls.TextBoxCounterBehavior.initializeBase(this, [element]);

    this._targetLabelIDValue = null;
    this._outputFormatValue = null;
    this._counterTypeValue = null;
    this._maxLengthValue = null;
    
    this._keyup_blur_handler = null;
}

V_Ent.FlowersByNet.Web.Controls.TextBoxCounterBehavior.prototype = {
    _registerHandlers : function() {
        this._keyup_blur_handler = Function.createDelegate(this, this._onkeyup_blur);
    
        $addHandler(this.get_element(), 'keyup', this._keyup_blur_handler);
        $addHandler(this.get_element(), 'blur', this._keyup_blur_handler);
    },    
    
    initialize : function() {
        V_Ent.FlowersByNet.Web.Controls.TextBoxCounterBehavior.callBaseMethod(this, 'initialize');

        this._registerHandlers();
        this._onkeyup_blur();
        
        // this.registerPartialUpdateEvents();
    },
    
    dispose : function() {
        if (this._onkeyup_blur_handler) {
            var element = this.get_element();
            
            $removeHandler(element, 'keyup', this._onkeyup_blur_handler);
            $removeHandler(element, 'blur', this._onkeyup_blur_handler);
            this._onkeyup_blur_handler = null;
        }
        
        V_Ent.FlowersByNet.Web.Controls.TextBoxCounterBehavior.callBaseMethod(this, 'dispose');
    },
    
    _partialUpdateEndRequest : function(sender, endRequestEventArgs) {
        this._onkeyup_blur();
        
        V_Ent.FlowersByNet.Web.Controls.TextBoxCounterBehavior.callBaseMethod(this, '_partialUpdateEndRequest', [sender, endRequestEventArgs]);
    },

    _onkeyup_blur : function() {
        var lbl = document.getElementById(this._targetLabelIDValue);
        var element = this.get_element();
        var regex = /\{0\}/g;
        
        if (this._maxLengthValue > 0) { 
            // check if the new value would be too long. if so, cancel.
            if (element.value.length > this._maxLengthValue) {
                element.value = element.value.substr(0, this._maxLengthValue);
            }
        }

        if (lbl) { 
            var countVal = 0;
            if (this._counterTypeValue == V_Ent.FlowersByNet.Web.Controls.CounterTypes.Down) {
                countVal = this._maxLengthValue - element.value.length;
            } 
            else {
                countVal = element.value.length;
            }
            lbl.innerHTML = this._outputFormatValue.replace(regex, countVal); 
        }
    },
    
    get_TargetLabelID : function() {
        return this._targetLabelIDValue;
    },

    set_TargetLabelID : function(value) {
        this._targetLabelIDValue = value;
    },
    
    get_OutputFormat : function() {
        return this._outputFormatValue;
    },
    
    set_OutputFormat : function(value) {
        this._outputFormatValue = value;
    },
    
    get_CounterType : function() {
        return this._counterTypeValue;
    },
    
    set_CounterType : function(value) {
        this._counterTypeValue = value;
    },
    
    get_MaxLength : function() {
        return this._maxLengthValue;
    },
    
    set_MaxLength : function(value) {
        this._maxLengthValue = value;
    }
}

V_Ent.FlowersByNet.Web.Controls.TextBoxCounterBehavior.registerClass('V_Ent.FlowersByNet.Web.Controls.TextBoxCounterBehavior', AjaxControlToolkit.BehaviorBase);

V_Ent.FlowersByNet.Web.Controls.CounterTypes = function() { throw Error.invalidOperation(); }
V_Ent.FlowersByNet.Web.Controls.CounterTypes.prototype = {
    Up   : 0,
    Down : 1
}
V_Ent.FlowersByNet.Web.Controls.CounterTypes.registerEnum('V_Ent.FlowersByNet.Web.Controls.CounterTypes', true);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();