function countCharacter(string) {
	return string.length;
}

function countLine(string) {
	var buf= string;
	//var opSep = " \n\r,;:.!?";
	var linear="";

	if (string.length==0) return 0;	
	while (buf.indexOf("\r") >-1)
	{
		buf= buf.replace("\r","\n");
		buf= buf.replace("\n\n","\n");
	}
	
	linear= buf.split("\n");
	
	return linear.length;
}


function countWord(string) {
	var buf= string;
	//var opSep = " \n\r,;:.!?";
	var count= 0; 
	var wordar="";
	
	while (buf.indexOf("\r") >-1)
	{
		buf= buf.replace("\r","\n");
		buf= buf.replace("\n\n"," ");
	}
	while (buf.indexOf("\n") >-1)
	{
		buf= buf.replace("\n"," ");
	}
	
	wordar= buf.split(" ");
	
	for (var i=0;i<wordar.length;i++)
	{
		if ((wordar[i] !=" ") && (wordar[i] !=""))
		{	count++;	}
	}
	return count;
}

function checkLength(aCheck){
	var bMessageOkay = true;
	for (ct=0; ct<aCheck.length; ct++){
		var sFieldName = aCheck[ct][0];
		var sType = aCheck[ct][1];
		var iLimit = aCheck[ct][2];
		var oTextArea = document.getElementById(sFieldName);
		var sCheckText = oTextArea.value;
		var iUsing = eval('count'+sType+'(sCheckText)');
		document.getElementById(sFieldName+'_'+sType+'_using').innerHTML = iUsing
		if(iUsing > iLimit){
			document.getElementById(sFieldName+'_'+sType+'_limit').style.color = '#c00';
			document.getElementById(sFieldName+'_'+sType+'_using').style.color = '#c00';
			bMessageOkay = false;
		} else {
			document.getElementById(sFieldName+'_'+sType+'_limit').style.color = '';
			document.getElementById(sFieldName+'_'+sType+'_using').style.color = '';
		}
	}
	document.getElementById(aCheck[0][0]).style.color = (bMessageOkay)?'':'#c00';
}

function addCounter(sFieldName, aType, aLimit, iCurrentCount){
/*
This function is called as follows:
	addCounter("tarea", ["Character", "Line"], [200, 5]);
In this publisher, the arrays only ever have one element each. Functionality has been retained in case of later changes.
*/
	// set default current count
	if (!iCurrentCount) iCurrentCount = 0;
	var oTextArea = document.getElementById(sFieldName);
	var sChange = "edflag=true; checkLength([";
	document.write('<table cellpadding="0" cellspacing="0" border="0" class="textInfo"><tr>');
	for (ct=0; ct<aType.length; ct++){
		document.write('<td id="'+sFieldName+'_'+aType[ct]+'">'+aType[ct]+' Limit: <strong id="'+sFieldName+'_'+aType[ct]+'_limit">'+aLimit[ct]+'</strong>' + ((aType.length==1)?'</td><td>':'<br>') + 'Currently Using: <strong id="'+sFieldName+'_'+aType[ct]+'_using">' + iCurrentCount + '</strong></td>');
		if (ct>0) { sChange += "," };
		sChange += "['"+sFieldName+"', '"+aType[ct]+"', "+aLimit[ct]+"]";
	}
	document.write('</tr></table>');
	sChange += "]);";
//	alert(sChange);
	eval("oTextArea.onchange = function (){ "+sChange+" }");
	eval("oTextArea.onkeydown = function (){ "+sChange+" }");
	eval("oTextArea.onkeyup = function (){ "+sChange+" }");
	eval(sChange);
}

function checkLengthMinimum(aCheck){
	var bMessageOkay = true;
	for (ct=0; ct<aCheck.length; ct++){
		var sFieldName = aCheck[ct][0];
		var sType = aCheck[ct][1];
		var iLimit = aCheck[ct][2];
		var oTextArea = document.getElementById(sFieldName);
		var sCheckText = oTextArea.value;
		var iUsing = eval('count'+sType+'(sCheckText)');
		document.getElementById(sFieldName+'_'+sType+'_using').innerHTML = iUsing
		if (iUsing < iLimit){
			document.getElementById(sFieldName+'_'+sType+'_limit').style.color = '#c00';
			document.getElementById(sFieldName+'_'+sType+'_using').style.color = '#c00';
			bMessageOkay = false;
		} else {
			document.getElementById(sFieldName+'_'+sType+'_limit').style.color = '';
			document.getElementById(sFieldName+'_'+sType+'_using').style.color = '';
		}
	}
	document.getElementById(aCheck[0][0]).style.color = (bMessageOkay)?'':'#c00';
}

function addCounterMinimum(sFieldName, aType, aLimit, iCurrentCount){
/*
This function is called as follows:
	addCounter("tarea", ["Character", "Line"], [200, 5]);
In this publisher, the arrays only ever have one element each. Functionality has been retained in case of later changes.
*/
	// set default current count
	if (!iCurrentCount) iCurrentCount = 0;
	var oTextArea = document.getElementById(sFieldName);
	var sChange = "edflag=true; checkLengthMinimum([";
	document.write('<table cellpadding="0" cellspacing="0" border="0" class="textInfo" style="width:350px;"><tr>');
	for (ct=0; ct<aType.length; ct++){
		document.write('<td id="'+sFieldName+'_'+aType[ct]+'"> Minimum '+aType[ct]+' Limit: <strong id="'+sFieldName+'_'+aType[ct]+'_limit">'+aLimit[ct]+'</strong>' + ((aType.length==1)?'</td><td>':'<br>') + 'Currently Using: <strong id="'+sFieldName+'_'+aType[ct]+'_using">' + iCurrentCount + '</strong></td>');
		if (ct>0) { sChange += "," };
		sChange += "['"+sFieldName+"', '"+aType[ct]+"', "+aLimit[ct]+"]";
	}
	document.write('</tr></table>');
	sChange += "]);";
//	alert(sChange);
	eval("oTextArea.onchange = function (){ "+sChange+" }");
	eval("oTextArea.onkeydown = function (){ "+sChange+" }");
	eval("oTextArea.onkeyup = function (){ "+sChange+" }");
	eval(sChange);
}



