var counting=false;
var start_time,now_time,timer_id=null;

var html_tag = new Array("a", "abbr", "acronym", "address","applet","area", "b", "base", "basefont", "bdo","bgsound","big","blockquote","body","br","button","button-live","caption","center","cite","code","col","colgroup","dd","del","dfn","div","dl","dt","em","embed","fieldset","font","form","frame","frameset","h1","h2","h3","h4","h5","h6","head","hr","html","i","iframe","ilayer","img","input","ins","kbd","label","layer","legend","li","link","map","marquee","meta","multicol","noframes","noscript","object","ol","optgroup","option","p","param","pre","q","samp","script","select","small","sound","spacer","span","strong","style","sub","sup","table","tbody","td","textarea","tfoot","th","thead","title","tr","tt","u","ul","var","\n*");
var special_ch = new Array("&nbsp;","&amp;","&lt;","&gt;","&quot;","&copy;","&reg;");
var special_rep = new Array(" ","&","<",">","\"","&amp;copy;","&amp;reg;");

function removeTag(msg)
{
	for(i = 0; i < special_ch.length; i++)
	{
		objregex = new RegExp(special_ch[i],"g");
		msg = msg.replace(objregex,special_rep[i]); 
	}

	//Raymond 050629 Editor
	objregex = new RegExp("<!---->","g");
	msg = msg.replace(objregex,"");
	//Raymond 050629 Editor End

	for(i = 0; i < html_tag.length; i++)
		msg = replaceTag(html_tag[i], msg, "");
	
	objregex = new RegExp("\r\n","g");
	msg = msg.replace(objregex,"");
	objregex = new RegExp("\r","g");
	msg = msg.replace(objregex,"");
	objregex = new RegExp("\n","g");
	msg = msg.replace(objregex,"");
	
	objregex = new RegExp(" ","g");
	msg = msg.replace(objregex,"");
	
	objregex = new RegExp("<\\?xml[^>]*/>","g");
	msg = msg.replace(objregex,"");
	objregex = new RegExp("<\\?XML[^>]*/>","g");
	msg = msg.replace(objregex,"");
	
	objregex = new RegExp("<o:p>","g");
	msg = msg.replace(objregex,"");
	objregex = new RegExp("</o:p>","g");
	msg = msg.replace(objregex,"");
	objregex = new RegExp("<O:P>","g");
	msg = msg.replace(objregex,"");
	objregex = new RegExp("</O:P>","g");
	
	msg = trim(msg);
	return msg;
}

function trim(str)
{
	return str.replace(/^\s*|\s*$/g,"");
}

function replaceTag(tag, msg, replaceElement)
{
	tag = tag.toLowerCase();
	msg = msg.toLowerCase();
	replaceElement = replaceElement.toLowerCase();
	objregex = new RegExp("<" + tag + " [^>]*>","g");
	msg = msg.replace(objregex,replaceElement);
	objregex = new RegExp("<" + tag + ">","g");
	msg = msg.replace(objregex,replaceElement);
	objregex = new RegExp("</" + tag + ">","g");
	msg = msg.replace(objregex,replaceElement);
	
	return msg;
}

function update_counter(message_field,counter_field,max_chr) {
 	var exceed = false;
	var msg = removeTag(message_field.value);
		
	var total_no_of_char = msg .length; 	
 	if(total_no_of_char >max_chr)
	{	message_field.value = msg.substr(0,max_chr);
		exceed = true;
		alert("Only " + max_chr + " characters allowed in this box.  Please edit your passage and submit again.");
	}
	else 
	{
		exceed = false;
		counter_field.value = max_chr - total_no_of_char;
	}
	
  if (counting) {
    start_time=new Date();
    start_time=start_time.getTime();
    delay_counter(message_field,counter_field,max_chr);
  }
  return exceed;
}  // end function update_counter

function delay_counter(message_field,counter_field,max_chr) {
  now_time=new Date();
  now_time=now_time.getTime();
  
  if (now_time-start_time<=20) {
    m=message_field;
	c=counter_field;
	x=max_chr;
	
    if (counting) { 
      timer_id=setTimeout("delay_counter(m,c,x)",max_chr); }
    }
    else {
      if (navigator.appName=="Netscape") {
        counter_field.focus();
        message_field.focus();
      }
    update_counter(message_field,counter_field,max_chr);
  }
}

function start_counter(message_field,counter_field,max_chr) {
  counting=true;
  update_counter(message_field,counter_field,max_chr);
}

function stop_counter(message_field,counter_field,max_chr) {
  counting=false;
  clearTimeout(timer_id);
  return update_counter(message_field,counter_field,max_chr);
}