// ####################################################
// COMMENTS
// ####################################################

// ####################################################
//        I. VARIABLES
// ####################################################
        // ####################################################
        // 1. GLOBAL VARIABLES
        // ####################################################
        // var maxKeys = 100;
        var maxKeys;
        var keysSoFar = 0;
        var alerted = false;

// ####################################################
// II. FUNCTIONS
// ####################################################
// ####################################################
// function change(what)
//                -
//                what -
// ####################################################
function change(what, whatValue, maxKeys) {
    if (!alerted) alert('Сообщение ' + maxKeys + 'characters please.');
        if ((maxKeys-1) < 0) {
            what.value = "0";
        }
        else {
            what.value = what.value.substring(0,maxKeys-1); // chop after maxlength
        }
    alerted = true;
}

// ####################################################
// function keyup(what,whatValue)
//                 -
//                 what -
//                whatValue -
// ####################################################
function keyup(what,whatValue, maxKeys) {
    keysSoFar++;
    if (keysSoFar >= maxKeys) {
        if (!alerted) alert('Сообщение превышает ' + maxKeys + ' символов.');
                if ((maxKeys-1) < 0) {
                    what.value = "0";
                }
                else {
                what.value = what.value.substring(0,maxKeys-1); // chop the last typed char
                }
        alerted = true;

                // DISPLAY MESSAGE
                change(what,whatValue, maxKeys);
    }
        else {
        alerted = false;
        }

        // DISPLAY LENGTH
        if ((maxKeys - whatValue.length - 1) > 0) {
                document.forms['form1'].elements['counter'].value = (maxKeys - whatValue.length - 1);
        }
        else {
                document.forms['form1'].elements['counter'].value = "0";
        }
        keysSoFar = whatValue.length;
}
