function limitChars(textid, limit, infodiv, countMode, validCSS, lblId)
{
    if (jQuery('#' + textid).length > 0) {
        var text = jQuery('#' + textid).val();
        var textlength = text.length;
        var mode = countMode;

        if (textlength > limit) {
            //jQuery('#' + infodiv).html("<= ValidationMessage %>");
            if (mode == "CountDown") { //countdown
                jQuery('#' + infodiv).html("0");
            }
            else {
                jQuery('#' + infodiv).html(textlength);
            }
            jQuery('#' + textid).val(text.substr(0, limit));
            jQuery('#' + infodiv).addClass(validCSS);
            return false;
        }
        else {
            jQuery('#' + infodiv).removeClass(validCSS);
            if (mode == "CountDown") { //countdown
                jQuery('#' + infodiv).html((limit - textlength));
            } //+ ' characters remaining'
            else {
                if (textlength > 0) {
                    jQuery('#' + infodiv).html(textlength);
                } //+ ' of ' + limit + ' characters remaining'); 
                else {
                    jQuery('#' + infodiv).html("0");
                }

                jQuery("#" + lblId).html("/" + limit);
            }

            return true;
        }
    }
}



function initCharcount(tbId,limit,infodivId,countMode,validCSS,lblId){

    jQuery("#" + tbId).keyup(function(event) {
        var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
        if (keyCode == 13) return false; //|| keyCode == 17

        limitChars(tbId, limit, infodivId, countMode, validCSS, lblId);
    });

    jQuery("#" + tbId).blur(function() {
        limitChars(tbId, limit, infodivId, countMode, validCSS, lblId);
    });

    limitChars(tbId, limit, infodivId, countMode, validCSS, lblId);
}
