var f;
var counting=false;
var counter_truncate=true;
var start_time,now_time,timer_id=null;
var msg,escape_msg,esacpe_msg_b,escape_msg_bm;
var i,max_len,no_of_Eng_Char,no_of_Chi_Char,total_no_of_char,chi_position;
var max_eng_char=70,max_chi_char=70;
var error_msg="Number of characters exceeds the limitation";

function setCounterTruncate(val) {
	//alert("Counter Truncate "+val);
	counter_truncate = val;
}

function counter_refresh(form, multipart) {

	max_eng_char = form.max_eng_char.value || max_eng_char;
	max_chi_char = form.max_chi_char.value || max_chi_char;
	max_eng_char_multi = max_eng_char - 7;
	max_chi_char_multi = max_chi_char - 3;
	msg=form.msg.value;

	escape_msg=escape(msg);
	chi_position=0;
	no_of_Eng_Char=0;
	no_of_Chi_Char=0;

	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_msg_b=escape_msg.substring(0,i+1); }
		if ( (no_of_Eng_Char+no_of_Chi_Char)==max_chi_char_multi*multipart ) { escape_msg_bm=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.lang_type.value='C';
		if ( total_no_of_char > max_chi_char ) {
			if ( multipart ) {
				char_left = max_chi_char_multi - (total_no_of_char-1) % max_chi_char_multi - 1;
				msg_count = Math.ceil(total_no_of_char / max_chi_char_multi);
				if ( msg_count > multipart ) {
					//form.msg.value=msg.substring(0,max_chi_char_multi*multipart-1);
					form.msg.value=unescape(escape_msg_bm);
					char_left = 0;
					msg_count = multipart;
					alert(error_msg);
				}
			} else if ( counter_truncate ) {
				char_left = 0;
				if (chi_position>max_chi_char) {
					form.msg.value=msg.substring(0,chi_position-1);
				} else {
					form.msg.value=unescape(escape_msg_b);
				}
				alert(error_msg);
			}
		} else {
			char_left = max_chi_char-total_no_of_char;
			if ( multipart ) { msg_count = 1 }
		}
	} else {
		form.lang_type.value='E';
		if ( total_no_of_char > max_eng_char ) {
			if ( multipart ) {
				char_left = max_eng_char_multi - (total_no_of_char-1) % max_eng_char_multi - 1;
				msg_count = Math.ceil(total_no_of_char / max_eng_char_multi);
				if ( msg_count > multipart ) {
					form.msg.value=msg.substring(0,max_eng_char_multi*multipart);
					char_left = 0;
					msg_count = multipart;
					alert(error_msg);
				}
			} else if ( counter_truncate ) {
				char_left = 0;
				form.msg.value=msg.substring(0,max_eng_char);
				alert(error_msg);
			}
		} else {
			char_left = max_eng_char-total_no_of_char;
			if ( multipart ) { msg_count = 1 }
		}
	}
	if (counting) {
		start_time=new Date();
		start_time=start_time.getTime();
		counter_delay(form, multipart);
	}

	//update shown counter value from hidden form field value
	if(form.name=='sms_form'){
		update_sms_message_counter(char_left, 'web_sms_message_counter');
		update_sms_message_counter(msg_count, 'web_sms_package_counter');
	} else if(form.name=='sms_form'){
		update_sms_message_counter(char_left, 'sms_leftCounter');
	} else {
		if (form.leftCounter && form.leftCounter.value){
			leftCounterId = form.leftCounter.value;
			//below is equal to the function update_sms_message_counter(n, id) in web_sms.js
			var counterObj = document.getElementById(leftCounterId);
			if(counterObj) counterObj.firstChild.nodeValue = char_left;
		}
	}
}

function counter_delay(form, multipart) {

	now_time=new Date();
	now_time=now_time.getTime();
	if (now_time-start_time<=500) {
		f=form;
		if (counting) { timer_id=setTimeout("counter_delay(f, "+multipart+")",500); }
	} else {
		if (navigator.appName=="Netscape") {
			form.char_left.focus();
			form.msg.focus();
		}
		counter_refresh(form, multipart);
	}
}

function counter_on(form, multipart) {
	counting=true;
	counter_refresh(form, multipart);
}

function counter_off(form, multipart) {
	counting=false;
	clearTimeout(timer_id);
	counter_refresh(form, multipart);
}