/*
 * limitsize.js by Mark Rosenstein, 5/22/2007
 * this will add a character counter to textareas, and enforce a maximum
 * number of characters.
 * To use it, include this javascript, then add these attributes to your
 * <textarea> tags:
 * 	rel="limitsize"
 * 	maxsize="500"
 *	sizedisplay="sizeinputid"
 * also, have an <input> field with the specified ID to display the size
 * You also must include prototype.js
 */


var LimitSize = Class.create();

LimitSize.prototype = {
  initialize: function() {
    if (!document.getElementsByTagName){ return; }
    var tas = document.getElementsByTagName('textarea');
    for (var i = 0; i < tas.length; i++) {
      var ta = tas[i];
      var relAttribute = String(ta.getAttribute('rel'));
      if (relAttribute.toLowerCase().match('limitsize')) {
	ta.onkeydown = function() { doclick(this); }
	ta.onkeyup = function() { doclick(this); }
	ta.onclick = function() { doclick(this); }
	doclick(ta, this);
      }
    }
  }
}

  function doclick(e) {
    var m = String(e.getAttribute('maxsize'));
    var l = e.value.length;
    if (l > m) {
      e.value = e.value.substring(0, m);
      l = m;
    }
    var sd = e.getAttribute('sizedisplay');
    var f = $(sd);
    if (f) {
      f.value = l;
    }
  }



function initLimitSize() { myLimitSize = new LimitSize(); }
Event.observe(window, 'load', initLimitSize, false);
