if(typeof Prototype == undefined) throw("LimitTextArea requires the Prototype library");
if (Object.isUndefined(LimitTextArea)) { var LimitTextArea = {};};

LimitTextArea = Class.create({
    initialize:function(options){
        this.options = options;
        if(this.options.textArea != null){
            this.checkKeyUp();
            document.observe("dom:loaded", this.limitCharacters());
        }        
    },

    checkKeyUp:function(){
        document.observe("keyup", function(e){
            this.limitCharacters();
        }.bind(this));
    },

    limitCharacters:function(){
        if(this.options.textArea.value.length > this.options.limit){
            this.options.message.innerHTML = 'You cannot write more then '+this.options.limit+' characters!';
            this.options.textArea.value =  this.options.textArea.value.substr(0,this.options.limit);
            return false;
        } else {
            this.options.message.innerHTML = 'You have '+ (this.options.limit - this.options.textArea.value.length) +' characters left.';
            return true;
        }
    }
});



