/**
 * Counts how many chars are remaining to enter.
 *
 * Example of using:
 * <script language="JavaScript" type="text/javascript" src="chars_remain.js"></script>
 * Chars remaining: <span id="chars">128</span><br>
 * <textarea name="text" id="text" onKeyUp="countCharsRemaining(128, 'text', 'chars');"></textarea>
 *
 * @return  void
 * @param   int     max_chars       The maximum quantity of chars allowed to enter
 * @param   string  text_field      The id of the form field with the text
 * @param   string  counter_id      The id of the counter tag
 */
function countCharsRemaining( max_chars, text_field, counter_id )
{

    var newCounterValue = max_chars - document.getElementById(text_field).value.length;

    if (newCounterValue >= 0)
    {
        document.getElementById(counter_id).innerHTML = newCounterValue;
    }
    else
    {
        document.getElementById(text_field).value = document.getElementById(text_field).value.substr(0,max_chars);
        document.getElementById(counter_id).innerHTML = '0';
    }
}