var f;
var counting=false;
var start_time,now_time,timer_id=null;
var input_msg,escape_msg,escape_msg1;
var i,max_len,no_of_Eng_Char,no_of_Chi_Char,total_no_of_char,chi_position,no_of_Eng_Char2,no_of_Chi_Char2,total_no_of_char2;
var max_eng_char=60,max_chi_char=30;

function update_counter(form) {
        input_msg=form.title.value;
        escape_msg=escape(input_msg);
        chi_position=0;
        no_of_Eng_Char=0;
        no_of_Chi_Char=0;

//For Textarea one
        for (i=0;i<escape_msg.length;i++) {
                if (escape_msg.charAt(i)=="%") {
                        if (escape_msg.charAt(i+1)=="u" || escape_msg.charAt(i+1)=="A" || escape_msg.charAt(i+1)=="B" || escape_msg.charAt(i+1)=="C" || escape_msg.charAt(i+1)=="D" || escape_msg.charAt(i+1)=="E" || escape_msg.charAt(i+1)=="F") {
                                no_of_Chi_Char++;
                                if (chi_position==0) { chi_position=no_of_Eng_Char+no_of_Chi_Char; }
                                if (escape_msg.charAt(i+1)=="u") { i+=5; }
                                else {
                                        i+=3;
                                        if (escape_msg.charAt(i)=="%") { i+=2; }
                                }
                        } else {
                                no_of_Eng_Char++;
                                i+=2;
                        }
                } else { no_of_Eng_Char++; }
                if ((no_of_Eng_Char+no_of_Chi_Char)==(max_chi_char)) { escape_msg1=escape_msg.substring(0,i+1); }
        }
        total_no_of_char=no_of_Eng_Char+no_of_Chi_Char;

        if (no_of_Chi_Char>0) {
                form.remLen.value=Math.max(max_chi_char-total_no_of_char,0);
                if (total_no_of_char+total_no_of_char2>max_chi_char) {
                        if (chi_position>(max_chi_char)) { form.title.value=input_msg.substring(0,chi_position-1); }
                                else { form.title.value=unescape(escape_msg1); }
                }
        } else {
                form.remLen.value=Math.max(max_eng_char-total_no_of_char,0);
                if (total_no_of_char>(max_eng_char)) {
                        form.title.value=input_msg.substring(0,max_eng_char);
                }
        }
        if (counting) {
                start_time=new Date();
                start_time=start_time.getTime();
                delay_counter(form);
        }
}

function delay_counter(form) {
        now_time=new Date();
        now_time=now_time.getTime();
        if (now_time-start_time<=500) {
                f=form;
                if (counting) { timer_id=setTimeout("delay_counter(f)",500); }
        } else {
                if (navigator.appName=="Netscape") {
                        form.remLen.focus();
                        form.title.focus();
                }
                update_counter(form);
        }
}

function start_counter(form) {
        counting=true;
        update_counter(form);
}

function stop_counter(form) {
        counting=false;
        clearTimeout(timer_id);
        update_counter(form);
} 

