﻿// 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('CharCounter');

CharCounter.CharCounterBehavior = function(element) {

    CharCounter.CharCounterBehavior.initializeBase(this, [element]);

    // TODO : (Step 1) Add your property variables here
    //
    this._MaxLength = null;
    this._TargetLabelID = null;
}

CharCounter.CharCounterBehavior.prototype = {

    initialize : function() {
        CharCounter.CharCounterBehavior.callBaseMethod(this, 'initialize');

        // TODO: add your initalization code here
        $addHandler(this.get_element(), 'keyup',Function.createDelegate(this, this._onkeyup));
        this._onkeyup();
    },

  
    dispose : function() {
        // TODO: add your cleanup code here

        CharCounter.CharCounterBehavior.callBaseMethod(this, 'dispose');
    },
    
    
    _onkeyup : function() {    
    var e = $get(this._TargetLabelID);
    
    if (e) 
        {
            if (this.get_element().value.length > this._MaxLength) {
		        this.get_element().value = this.get_element().value.slice(0, this._MaxLength);
	        }
	        e.innerHTML  = this.get_element().value.length + ' of ' + this._MaxLength;            
        }
    },
    
    
    // TODO: (Step 2) Add your property accessors here
    //
    get_MaxLength : function() {
        return this._MaxLength;
    },

    set_MaxLength : function(value) {
        this._MaxLength = value;
    },
    
    get_TargetLabelID : function() {
        return this._TargetLabelID;
    },

    set_TargetLabelID : function(value) {
        this._TargetLabelID = value;
    }
    
}

CharCounter.CharCounterBehavior.registerClass('CharCounter.CharCounterBehavior', AjaxControlToolkit.BehaviorBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();