﻿// Keep user from entering more than maxLength characters
function doKeyUpForTextArea(control)
{
    maxLength = control.attributes["maxLength"].value;
    value = control.value;
     if(value.length > maxLength)
     {
          //event.returnValue = false;
         control.value = control.value.substring(0, maxLength);
         doUpdateCharactersLeft(control);
     }
     else
     {
         doUpdateCharactersLeft(control);
     }
}

// Cancel default behavior
function doBeforePasteForTextArea(control)
{
    maxLength = control.attributes["maxLength"].value;
     if(maxLength)
     {
          event.returnValue = false;
     }
}

// Cancel default behavior and create a new paste routine
function doPasteForTextArea(control)
{
    maxLength = control.attributes["maxLength"].value;
    value = control.value;
     if(maxLength)
     {
          event.returnValue = false;
          maxLength = parseInt(maxLength);
          var oTR = control.document.selection.createRange();
          var iInsertLength = maxLength - value.length + oTR.text.length;
          var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
          oTR.text = sData;
     }
}

//do update
function doUpdateCharactersLeft(control)
{
    var maxLength = control.attributes["maxLength"].value;
    var counterChars = maxLength - control.value.length;
    var controlId = 'charactersLeft' + control.id;
    var charactersLeftControl = document.getElementById(controlId);
                
    if (counterChars > 0)
    {
        charactersLeftControl.value = counterChars + ' characters left (max ' + maxLength + ' characters)';
    }
    else
    {
        charactersLeftControl.value = 'no characters left (max ' + maxLength + ' characters)';
    }  
}