﻿// README
//
// There are two steps to adding a property:
//
// 1. Create a member variable to store your property
// 2. Add the get_ and set_ accessors for your property.
//
// Remember that both are case sensitive!
//
Type.registerNamespace('TextboxLimitExtender');

TextboxLimitExtender.TextboxLimitExtenderBehavior = function(element) {
    TextboxLimitExtender.TextboxLimitExtenderBehavior.initializeBase(this, [element]);

    // initializing property values
    //
    this._TargetCountTextControlId = null;
    this._MaxLength = 1000;
    
//    //initializing handlers
    this._onKeyPressHandler = null;
    this._onBeforePasteHandler = null;
    this._onPasteHandler = null;
    this._onKeyDownHandler = null;
    this._onKeyUpHandler = null;
}

TextboxLimitExtender.TextboxLimitExtenderBehavior.prototype = {

    initialize: function() {
        TextboxLimitExtender.TextboxLimitExtenderBehavior.callBaseMethod(this, 'initialize');

        //init handlers

        this._onKeyPressHandler = Function.createDelegate(this, this._onKeyPress);
        this._onBeforePasteHandler = Function.createDelegate(this, this._onBeforePaste);
        this._onPasteHandler = Function.createDelegate(this, this._onPaste);
        this._onKeyDownHandler = Function.createDelegate(this, this._onKeyDown);
        this._onKeyUpHandler = Function.createDelegate(this, this._onKeyUp);

        //hookup events

        $addHandler(this.get_element(), "keypress", this._onKeyPressHandler);
        $addHandler(this.get_element(), "beforepaste", this._onBeforePasteHandler);
        $addHandler(this.get_element(), "paste", this._onPasteHandler);
        $addHandler(this.get_element(), "keydown", this._onKeyDownHandler);
        $addHandler(this.get_element(), "keyup", this._onKeyUpHandler);

    },

    dispose: function() {

        // Detach event handlers
        if (this._onKeyPressHandler) {
            $removeHandler(this.get_element(), "keypress", this._onKeyPressHandler);
            this._onKeyPressHandler = null;
        }

        if (this._onBeforePasteHandler) {
            $removeHandler(this.get_element(), "beforepaste", this._onBeforePasteHandler);
            this._onKeyPressHandler = null;
        }

        if (this._onPasteHandler) {
            $removeHandler(this.get_element(), "paste", this._onPasteHandler);
            this._onKeyPressHandler = null;
        }

        if (this._onKeyDownHandler) {
            $removeHandler(this.get_element(), "keydown", this._onKeyDownHandler);
            this._onKeyDownHandler = null;
        }

        if (this._onKeyUpHandler) {
            $removeHandler(this.get_element(), "keyup", this._onKeyUpHandler);
            this._onKeyUpHandler = null;
        }

        // call base dispose
        TextboxLimitExtender.TextboxLimitExtenderBehavior.callBaseMethod(this, 'dispose');

    },

    //
    get_TargetCountTextControlId: function() {
        return this._TargetCountTextControlIdValue;
    },

    set_TargetCountTextControlId: function(value) {
        this._TargetCountTextControlIdValue = value;
    },

    get_MaxLength: function() {
        return this._MaxLength;
    },

    set_MaxLength: function(value) {
        if (this._MaxLength != value) {
            this._MaxLength = value;
            this.raisePropertyChanged('MaxLength');
        }
    },
    //    // Handlers
    _onKeyPress: function(e) {
        var control = this.get_element();
        if (control.value.length > this._MaxLength - 1) {

            if (e) {
                e.preventDefault();
            }
            else {
                event.returnValue = false;
            }

        }
        this._refreshCountTextBox();
    },

    _onKeyDown: function() {
        this._refreshCountTextBox();
    },

    _onKeyUp: function() {
        this._refreshCountTextBox();
    },

    _onBeforePaste: function(e) {
        //cancel default behaviour
        if (e) {
            e.preventDefault();
        }
        else {
            event.returnValue = false;
        }

        this._refreshCountTextBox();
    },
    //    
    _onPaste: function(e) {
        var control = this.get_element();
        var maxLength = this.get_MaxLength();
        //cancel default behaviour to override

        if (e) {
            e.preventDefault();
        }
        else {
            event.returnValue = false;
        }
        var oTR = control.document.selection.createRange();
        var insertLength = maxLength - control.value.length + oTR.text.length;
        var copiedData = window.clipboardData.getData("Text").substr(0, insertLength);
        oTR.text = copiedData;

        this._refreshCountTextBox();
    },

    _refreshCountTextBox: function() {

        var control = this.get_element();
        var maxLength = this.get_MaxLength();
        var tbId = this.get_TargetCountTextControlId();
        var countTextBox;
        //var countMode = this.
        if (tbId) {
            countTextBox = $get(tbId);
        }
        else
            return; //nowhere to write.
        
        var innerTextEnabled = (document.getElementsByTagName("body")[0].innerText != undefined) ? true : false;

        if (countTextBox)
        {
            
            if(innerTextEnabled)
            {
                countTextBox.innerText = maxLength - control.value.length;
            }
            else
            {
                countTextBox.value = maxLength - control.value.length;
            }
        }

        //alert(countTextBox.innerText);
    }


}

TextboxLimitExtender.TextboxLimitExtenderBehavior.registerClass('TextboxLimitExtender.TextboxLimitExtenderBehavior', AjaxControlToolkit.BehaviorBase);








if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();