/* limitInput.js - client-side script for textarea size limit.

   v001 24/10/07 Simon Sellick	- initial version, adapted from Trax

   To use this:

   - include this file in the page header:

     <script src="Includes/limitInput.js"></script>

   - add a place for the progress bar to be displayed, near the input field:

     <div id="bar" class="progress"></div>

   - call the function from the input field's onKeyUp event:

     <textarea ... onKeyUp="limitInput(this,\'bar'\,limit)" ...>

   Note the reference to the "bar" area in the function call.  If you do not
   provide this reference, you get the limit behaviour but without a display.
*/

  function limitInput(field, limit, bar) {                      // Show <bar> as % of <limit> input in <field>

    var n = field.value.length;                                 // typed so far
    if (n > limit) {                                            // too many chars..
    	field.value = field.value.substring(0, limit);            // ..lose surplus
    }
    if (typeof bar == 'undefined') {                            // no display wanted
      return;
    }
    if (n <= limit) {
      var b = document.getElementById(bar);                     // ref to display area
      var dw = parseInt(field.offsetWidth);                     // bar display width (assumes underneath or above)
      var pu = parseInt(100 * n / limit);                       // calc % used
      var bw = parseInt(dw * pu / 100);                         // bar width..
      b.style.width = bw + "px";                                // ..set
      b.innerHTML = pu + "% used";                              // progress message
    }
  }

// end
