jQuery.fn.MaxLengths = function() {
    jQuery.each(jQuery(this), function() {
        var box = jQuery(this);
        var counterId = "counter" + box.attr("id");
        var limit = box.attr("limit");
        box.after("<br /><span id='" + counterId + "' class='maxlength-counter'></span>");
        box.bind("keyup", function() { limiter(box, counterId, limit); });
        box.bind("mouseover", function() { limiter(box, counterId, limit); });
        limiter(box, counterId, limit);
    });

    function limiter(box, counterId, limit) {
        var counter = $("#" + counterId);
        if (box.val().length > limit) {
            box.val(box.val().substring(0, limit));
        }
        var diff = limit - box.val().length;
        if (diff == 1) {
            counterTxt = diff + " character remaining / " + limit;
        } else if (diff > 1) {
            counterTxt = diff + " characters remaining / " + limit;
        } else if (diff == 0) {
            counterTxt = "You have reached the limit of " + limit + " characters";
        } else {
            counterTxt = "<span class=\"error\">You have entered more than " + limit + " characters. Please revise your existing text to fit within the limit.</span>";
        }
        counter.html(counterTxt);
    }

    return this;
};