﻿Type.registerNamespace('Zextenders');

Zextenders.TextLimit = function(element) 
{
    Zextenders.TextLimit.initializeBase(this,[element]);
    
    this._label = null;
    this._max = null;
    this._keyPressHandler = null;
    this._changeHandler = null;
    this._updateHandler = null;
    this._remainingString = "{0} characters remaining";
}
Zextenders.TextLimit.prototype = 
{
    initialize : function()
    {
        this._keyPressHandler = Function.createDelegate(this, this.keyPress);
        this._changeHandler = Function.createDelegate(this, this.change);
        this._updateHandler = Function.createDelegate(this, this.updateLabel);
        var el = this.get_element();
        $addHandler(el, "keypress", this._keyPressHandler);
        $addHandler(el, "change", this._changeHandler);
        
        this.updateLabel();
        Zextenders.TextLimit.callBaseMethod(this, 'initialize');
    },
    
    dispose : function()
    {
        var el = this.get_element();
        if (this._keyPressHandler)
            $removeHandler(el, "keypress", this._keyPressHandler);
        if (this._changeHandler)
            $removeHandler(el, "change", this._changeHandler);
        Zextenders.TextLimit.callBaseMethod(this, 'dispose');
    },
    
    
    keyPress : function(evt)
    {
        //count the characters in the textarea and display it in the lable.
        if (this._max > 0)
        {
            var scanCode;

            if (evt.rawEvent.keyIdentifier) {
                // Safari
                // Note (Garbin): used the underlying rawEvent insted of the DomEvent instance.
                if (evt.rawEvent.ctrlKey || evt.rawEvent.altKey || evt.rawEvent.metaKey) {
                    return;
                }
                
                if (evt.rawEvent.keyIdentifier.substring(0,2) != "U+") {
                    return;
                }
                
                scanCode = evt.rawEvent.charCode; 
                if (scanCode == 63272 /* Delete */) {
                    return;
                }
            } else {
                scanCode = evt.charCode;
            }  
                
            if (scanCode && ((scanCode == 13) || (scanCode >= 47))) {
                var el = this.get_element();
                var val = el.value;
                
                if (val.length+1 > this._max)
                    evt.preventDefault();
            }
        
            window.setTimeout(this._updateHandler, 50);
        }
    },
    
    updateLabel : function()
    {
        var el = this.get_element();
        var val = el.value;
        
        if (this._max > 0)
        {
            if (this._label)
            {
                if (val.length > this._max)
                {
                    val = val.substring(0,this._max);
                    el.value = val;
                }
                var remaining = this._max - val.length;
                this._label.innerHTML = String.format(this._remainingString, remaining);
            }
        }
    },
    
    change : function(evt)
    {
        window.setTimeout(this._updateHandler, 50);
    },
    
    get_displayLabel : function()
    {
        return this._label;
    },
    
    set_displayLabel : function(value)
    {
        this._label = value;
    },
    
    get_max : function()
    {
        return this._max;
    },
    
    set_max : function(value)
    {
        this._max = value;
    },
    
    get_remainingString : function()
    {
        return this._remainingString;
    },
    
    set_remainingString : function(value)
    {
        this._remainingString = value;
    }
}

Zextenders.TextLimit.registerClass('Zextenders.TextLimit', AjaxControlToolkit.BehaviorBase, Sys.IDisposable);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();