﻿                function limitbox(textboxId,outputId,max){
                  this.textboxId = textboxId;
                  this.outputId = outputId;
                  this.max = max;
                  this.remainingText = "";
                  this.init();
                }

                limitbox.prototype = {

                  init : function(){

                    var elem = document.getElementById(this.textboxId);
                    var ref = this;

                    elem.onkeyup = function(){ref.keypress();}
                    elem.onkeydown = function(){ref.keypress();}
                    elem.onkeypress = function(){ref.keypress();}
                    elem.onchange = function(){ref.keypress();}  //catches paste events

                    this.keypress();
                    elem = null;
                  },

                  keypress : function(){
                    var elem = document.getElementById(this.textboxId);
                    if(elem.value.length >= this.max) elem.value = elem.value.substr(0,this.max);
                    document.getElementById(this.outputId).innerHTML = this.remainingText + (this.max - elem.value.length);
                    elem = null;                   
                  }
                }
                var limit1 = new limitbox("body","BodyLimit",300);                
                var limit2 = new limitbox("comment","BodyComment",300);