/**
* Copyright (c) 2007 by CES/ServiceSPAN, Inc. All rights reserved.
* $Id: jquery-maxlength.js,v 1.1 2008/02/12 17:21:57 gleb Exp $
*
* Limits length of an input element (edit box or text area). Optionally updates
* the specified element with the number of characters remaining.
*/

jQuery.fn.maxlength = function(maxlength, counter) {
  this.each(function() {
    jQuery(counter).text("" + (maxlength - this.value.length));
  });
  this.keypress(
    function(e) {
      if (this.value.length >= maxlength) {
        e.preventDefault();
      }
    }
  ).keyup(
    function(e) {
      jQuery(counter).text("" + (maxlength - this.value.length));
      if (this.value.length > maxlength) {
          this.value = this.value.substr(0, maxlength);
      }
    }
  );
};
