﻿function char_counter_OnTextChanged(textControl, maxCharCount, remainingCharsControlClientId, totalCharsControlClientId)
{
    if (textControl != null)
    {
        var charCount = (textControl.value != null) ? textControl.value.length : 0;
        
        var remainingCharsControl = null;
        try { remainingCharsControl = document.getElementById(remainingCharsControlClientId); } catch(e) {}
    
        var totalCharsControl = null;
        try { totalCharsControl = document.getElementById(totalCharsControlClientId); } catch(e) {}
    
        if (remainingCharsControl != null)
        {
            var remainingCharsCount = maxCharCount - charCount;
            if (remainingCharsCount < 0) 
            {
                remainingCharsCount = 0;
            }
            
            char_counter_SetText(remainingCharsControl, remainingCharsCount);
        }
        
        if (totalCharsControl != null)
        {
            char_counter_SetText(totalCharsControl, charCount);
        }
    }        
}

function char_counter_SetText(control, text)
{
    if (control == null) return;    
    if (control.textContent != null) control.textContent = text;
    if (control.innerText != null) control.innerText = text;    
}