﻿
function validateLengthOnBlur(label, textBox, maxLength) {
    var textControl = document.getElementById(textBox);
    var labelControl = document.getElementById(label);

    var currentLength = textControl.value.length;

    if (currentLength > (parseInt(maxLength) - 1)) {

        if (textControl.innerText != undefined) {
            //IE
            textControl.innerText = textControl.innerText.substring(0, parseInt(maxLength));
        }
        else {
            //Firefox & Safari
            textControl.value = textControl.value.substring(0, parseInt(maxLength));
        }

        displayCurrentLength(label, textBox, maxLength);

    }

}

function validateLengthOnPress(label, textBox, maxLength) {
    var textControl = document.getElementById(textBox);
    var labelControl = document.getElementById(label);

    var currentLength = textControl.value.length;

    if (currentLength > (parseInt(maxLength) - 1)) {

        if (textControl.innerText != undefined) {
            //IE
            textControl.innerText = textControl.innerText.substring(0, parseInt(maxLength) - 1);
        }
        else {
            //Firefox & Safari
            textControl.value = textControl.value.substring(0, parseInt(maxLength) - 1);
        }

        displayCurrentLength(label, textBox, maxLength);

    }

}

function displayCurrentLength(label, textBox, maxLength) {
    var textControl = document.getElementById(textBox);
    var labelControl = document.getElementById(label);

    var currentLength = textControl.value.length;

    labelControl.innerHTML = "(" + currentLength + "/" + maxLength + ") <br /> characters remaining";

}
