function whatsRemaining(obj, limit) {
    var len = limit - obj.value.length;
    if (len < 0) {
        obj.value = obj.value.substr(0, limit);
        len = 0;
    }
    document.getElementById('remaining').innerHTML = len;
}

forms_onload = window.onload;
window.onload = function() {
    if (forms_onload) {
        forms_onload();
    }
    if (document.getElementById('ch_description')) {
        document.getElementById('ch_description').onkeyup = function() {
            whatsRemaining(this, 1000);
        }
    }
    if (document.getElementById('ch_registry')) {
        document.getElementById('ch_registry').onkeyup = function() {
            whatsRemaining(this, 300);
        }
    }
    if (document.getElementById('ch_ticketContent')) {
        document.getElementById('ch_ticketContent').onkeyup = function() {
            whatsRemaining(this, 1000);
        }
    }
    if (document.getElementById('ch_cmtContent')) {
        document.getElementById('ch_cmtContent').onkeyup = function() {
            whatsRemaining(this, 300);
        }
    }
    
    var lst = new Array();
    lst[0] = document.getElementsByTagName('input');
    lst[1] = document.getElementsByTagName('select');
    lst[2] = document.getElementsByTagName('textarea');

    for (var i = 0 ; i < lst.length ; i++) {
        for (var j = 0 ; j < lst[i].length ; j++) {
            lst[i][j].onfocus = function() {
                addClass('focus', this);
            }
            lst[i][j].onblur = function() {
                remClass('focus', this);
            }
        }
    }
    
    if (document.getElementById('messagesList')) {
        var trs = document.getElementById('messagesList').getElementsByTagName('tr');
        for (var i = 0 ; i < trs.length ; i++) {
            if (trs[i].id.indexOf('msg_') != -1) {
                trs[i].onmouseover = function() {
                    addClass('hover', this);
                }
                trs[i].onmouseout = function() {
                    remClass('hover', this);
                }
                trs[i].onclick = function() {
                    var id = this.id.substr(4, this.id.length);
                    document.location = 'message-'+id+'.html';
                }
            }
        }
    }
}

