function limitTextEntered(textid, limit, infodiv) {
    var text = $('#'+textid).val();
    var textlength = text.length;
    if (textlength > limit) {
        $("#"+infodiv).css( {"color": "red",
                             "font-weight" : "bold" });
        $("#"+infodiv).html("You have exceeded the limit of " + limit + " characters.");
        $("#"+textid).val(text.substr(0,limit));
        return false;
    } else {
        $("#"+infodiv).css( {"color": "blue",
                             "font-weight" : "normal" });
        $("#"+infodiv).html((limit - textlength) + " characters left.");
    }
    return true;
}


// first argument is the input element's css ID.
// 2nd argument is the id of the element that will display
// the remaining character count.
// 3rd argument (optional) is the numeric limit of how many characters can be
// entered. Default to 250.
function register_char_limit(inputID, counterID, limit) {
    if (arguments.length == 2) {
        limit = 1024;
    } else {
        if (arguments.length != 3) {
            throw "Invalid arguments in call to register_char_limit."; 
        }
    }

    $(document).ready( function() {
        $("#" + inputID).keyup( function() {
            limitTextEntered(inputID, limit, counterID);
        });
        $("#" + inputID).focus( function() {
            $("#" + counterID).css("color", "blue");
            limitTextEntered(inputID, limit, counterID);
        });
    });
}

