var bCutMessage = false;

window.onload = function() {
    if (document.getElementById('sRingingMessage')) {
        countText(document.getElementById('sRingingMessage'), 'sRingingMessageCharsCount_label', document.getElementById('iMaxRingMsgChars').value);
    }
    if (document.getElementById('sAnswerMessage')) {
        countText(document.getElementById('sAnswerMessage'), 'sAnswerMessageCharsCount_label', document.getElementById('iMaxAnswerMsgChars').value);
    }
    if (document.getElementById('sUUIMessage')) {
        countText(document.getElementById('sUUIMessage'), 'sUUIMessageCharsCount_label', document.getElementById('iMaxUUIMsgChars').value);
    }
}

function countText(objTxt, lblCounter, iMax) {
    var iTextLength = objTxt.value.length;
    var sMax = '/'+iMax;
    // cut message if it was exceed
    if (bCutMessage && iTextLength > iMax) {
        objTxt.value = objTxt.value.substr(0, iMax);
        iTextLength = objTxt.value.length;
    }
    document.getElementById(lblCounter).innerHTML = iTextLength+sMax;
    showHideWarning(objTxt.name+'Warn', iTextLength, iMax);  // reached this number, then show warning
}

function showHideWarning(sWarnId, iLength, iMax) {
    if (document.getElementById(sWarnId)) {
        if (iLength > iMax) {
            document.getElementById(sWarnId).style.display = 'block';
        }
        else {
            document.getElementById(sWarnId).style.display = 'none';
        }
    }
}
