

var TextareaLimiter = function(max_chars, max_words, form_name, field_name, chars_typed, chars_left, words_typed, words_left) {
  // Value One:
  // Specify the maximum number of characters the form field
  // may contain. If you have no maximum, specify 0 (zero).
  
  this.MaximumCharacters = max_chars;
  
  // Value Two:
  // Specify the maximum number of words the form field may
  // contain. If you have no maximum, specify 0 (zero).
  
  this.MaximumWords = max_words;
  
  // Value Three:
  // Specify the form's name (provided by the name="_____"
  // attribute in the FORM tag).
  
  this.FormName = form_name;
  
  // Value Four:
  // Specify the name of the text field being monitored
  // (provided by the name="_____" attribute in the
  // INPUT or TEXTARE tag).
  
  this.TextFieldName = field_name;
  
  // Value Five:
  // Specify the field name where where is to be displayed
  // the number of characters the user has typed. Make
  // it blank (nothing between the quotation marks) if
  // you aren't displaying the number of characters typed.
  
  this.CharactersTypedFieldName = chars_typed;
  
  // Value Six:
  // Specify the field name where where is to be displayed
  // the number of characters left that may be typed.
  // Make it blank (nothing between the quotation marks)
  // if you aren't displaying the number of characters
  // left.
  
  this.CharactersLeftFieldName = chars_left;
  
  // Value Seven:
  // Specify the field name where where is to be displayed
  // the number of words the user has typed. Make it
  // blank (nothing between the quotation marks) if you
  // aren't displaying the number of words typed.
  
  this.WordsTypedFieldName = words_typed;
  
  // Value Eight:
  // Specify the field name where where is to be displayed
  // the number of words left that may be typed. Make it
  // blank (nothing between the quotation marks) if you
  // aren't displaying the number of words left.
  
  this.WordsLeftFieldName = words_left;
  
  //////////////////////////////////////////////////////
  //                                                  //
  //  No modfications are required below this point.  //
  //                                                  //
  //////////////////////////////////////////////////////
  
  this.WordsMonitor = 0;
  this.MaxWords = parseInt(this.MaximumWords);
  this.MaxChars = parseInt(this.MaximumCharacters);
  this.textfield = 'document.' + this.FormName + '.' + this.TextFieldName + '.value';
  
  this.WordLengthCheck = function(s,l) {
    this.WordsMonitor = 0;
    var f = false;
    var ts = new String();
    for(var vi = 0; vi < s.length; vi++) {
      vs = s.substr(vi,1);
      if((vs >= 'A' && vs <= 'Z') || (vs >= 'a' && vs <= 'z') || (vs >= '0' && vs <= '9')) {
        if(f == false)  {
          f = true;
          this.WordsMonitor++;
          if((l > 0) && (this.WordsMonitor > l)) {
            s = s.substring(0,ts.length);
            vi = s.length;
            this.WordsMonitor--;
          }
        }
      }
      else { f = false; }
      ts += vs;
    }
    return s;
  }; // function WordLengthCheck()
  
  this.CharLengthCheck = function(s,l) {
    if(s.length > l) { s = s.substring(0,l); }
    return s;
  }; // function CharLengthCheck()
  
  this.InputCharacterLengthCheck = function() {
    if(this.MaxChars <= 0) { return; }
    var currentstring = new String();
    eval('currentstring = ' + this.textfield);
    var currentlength = currentstring.length;
    eval('currentstring = this.CharLengthCheck(' + this.textfield + ',' + this.MaxChars + ')');
    if(this.CharactersLeftFieldName.length > 0) {
      var left = 0;
      eval('left = ' + this.MaxChars + ' - ' + this.textfield + '.length');
      if(left < 0) { left = 0; }
      eval('document.' + this.FormName + '.' + this.CharactersLeftFieldName + '.value = ' + left);
      if(currentstring.length < currentlength) { eval(this.textfield + ' = currentstring.substring(0)'); }
    }
    if(this.CharactersTypedFieldName.length > 0) {
      eval('document.' + this.FormName + '.' + this.CharactersTypedFieldName + '.value = ' + this.textfield + '.length');
      if(currentstring.length < currentlength) { eval(this.textfield + ' = currentstring.substring(0)'); }
    }
  }; // function InputCharacterLengthCheck()
  
  this.InputWordLengthCheck = function() {
    if(this.MaxWords <= 0) { return; }
    var currentstring = new String();
    eval('currentstring = ' + this.textfield);
    var currentlength = currentstring.length;
    eval('currentstring = this.WordLengthCheck(' + this.textfield + ',' + this.MaxWords + ')');
    if (this.WordsLeftFieldName.length > 0) {
      var left = this.MaxWords - this.WordsMonitor;
      if(left < 0) { left = 0; }
      eval('document.' + this.FormName + '.' + this.WordsLeftFieldName + '.value = ' + left);
      if(currentstring.length < currentlength) { eval(this.textfield + ' = currentstring.substring(0)'); }
    }
    if (this.WordsTypedFieldName.length > 0) {
      eval('document.' + this.FormName + '.' + this.WordsTypedFieldName + '.value = ' + this.WordsMonitor);
      if(currentstring.length < currentlength) { eval(this.textfield + ' = currentstring.substring(0)'); }
    }
  }; // function InputWordLengthCheck()
  
  this.InputLengthCheck = function() {
    this.InputCharacterLengthCheck();
    this.InputWordLengthCheck();
  }; // function InputLengthCheck()

}; // end TextareaLimiter

