//=================================================================================================
var countTimerID;				    // Used to track countdown timer used to delay counts while typing...
var countTimerInterval = 750	    // Millisecs for timer interval...
var countFieldID;			        // (Temp var: ID of field we're counting characters/words in)...
var countDisplayFieldID;            // (Temp var: ID of field we'll write count to)...
var countDisplayPrefix;             // (Temp var: Prefix string to display with actual count)...
var countDisplaySuffix;             // (Temp var: Suffix string to display with actual count)...

//=================================================================================================
// charCounter function counts number of chars entered in text field and displays in another field.
// Function should work with multiple forms, and includes a param to determine whether function is
// a countdown or count-up counter (if True, then countdown, else count-up).
// 20071128 Daryl Smith (ISM) Modified function to use field ID's rather than actual field object
//          references...
// 20071128 Daryl Smith (ISM) added psPrefix and psSuffix params...
function charCounter(psFieldID, psCountFieldID, psPrefix, psSuffix, plMaxChars, pbCountdown, pbLimit)
{
    var poField = document.getElementById(psFieldID);
    var poCountField = document.getElementById(psCountFieldID);
    
//--If field is too long, trim it to max characters...
	if (pbLimit && (poField.value.length > plMaxChars))
		poField.value = poField.value.substring(0, plMaxChars);

//--Assign char count text to <span> "field"...
	if (pbCountdown)
		poCountField.innerHTML = psPrefix + String(plMaxChars - poField.value.length) + psSuffix;
	else
		poCountField.innerHTML = psPrefix + String(poField.value.length) + psSuffix;
}
//=================================================================================================
// charCounterStatus function counts number of chars entered in text field and displays in the browser's
// status bar.
// Function should work with multiple forms, and includes a param to determine whether function is
// a countdown or count-up counter (if True, then countdown, else count-up)...
// 20071128 Daryl Smith (ISM) Modified function to use a field ID to reference the field we're counting
//          rather than an actual field object reference...
function charCounterStatus(psField, plMaxChars, pbCountdown, pbLimit)
{
    var poField = document.getElementById(psField);
    
//--If field is too long, trim it to max characters...
	if (pbLimit && (poField.value.length > plMaxChars))
		poField.value = poField.value.substring(0, plMaxChars);

//--Update message in browser's status bar...
	if (pbCountdown)
		window.status = "Word Count:  " + (plMaxChars - poField.value.length);
	else
		window.status = "Word Count:  " + poField.value.length;
}
//=================================================================================================
// wordCounter function works similarly to the textCounter function (above), but counts words
// (non-white-space char groups)...
// 20071128 Daryl Smith (ISM) Modified function to use field ID's rather than actual field object
//          references...
// 20071128 Daryl Smith (ISM) added psPrefix and psSuffix params...
function wordCounter(psFieldID, psWordCountFieldID, psPrefix, psSuffix, plMaxWords, pbCountdown, pbLimit)
{
	var lIndex
	var lWordCount = 0
	var bWhitespace = true		// Will be toggled true/false as we scan through the field; initially true...
	var poField = document.getElementById(psFieldID);
    var poWordCountField = document.getElementById(psWordCountFieldID);

//--Count words in field...
	for (lIndex = 0; lIndex < poField.value.length; lIndex++)
	{
		switch (poField.value.charAt(lIndex))
		{
			case " ":
			case "\n":
			case "\r":
			case "\t":
			case "\b":
				if ((lIndex > 0) && (!bWhitespace))								// If we're at the first whitespace after a word...
				{
					lWordCount++;
					if (pbLimit && (lWordCount == plMaxWords))
						poField.value = poField.value.substring(0, lIndex + 1);	// This also causes next loop comparison to fail...
				}
				bWhitespace = true;												// In any case, this is a whitespace...
				break;
			default:
				bWhitespace = false;
		}			
	}

//--If last char of field is non-white-space, then increment word counter so it gets counted.
	if (!bWhitespace)
		lWordCount++;
		
//--Make sure we haven't exceeded max words...		
	if (pbLimit && (lWordCount == plMaxWords))
		poField.value = poField.value.substring(0, lIndex + 1);	
		
//--Assign word count to form field...
	if (pbCountdown) 
		poWordCountField.innerHTML = psPrefix + String(plMaxWords - lWordCount) + psSuffix;
	else
		poWordCountField.innerHTML = psPrefix + String(lWordCount) + psSuffix;
}
//=================================================================================================
// wordCounterStatus function works similarly to the textCounterStatus function (above), but counts words
// (non-white-space char groups)...
// 20071128 Daryl Smith (ISM) Modified function to use a field ID to reference the field we're counting
//          rather than an actual field object reference...
function wordCounterStatus(psField, plMaxWords, pbCountdown, pbLimit)
{
	var lIndex
	var lWordCount = 0
	var bWhitespace = true		// Will be toggled true/false as we scan through the field; initially true...
    var poField = document.getElementById(psField);
    
//--Count words in field...
	for (lIndex = 0; lIndex < poField.value.length; lIndex++)
	{
		switch (poField.value.charAt(lIndex))
		{
			case " ":
			case "\n":
			case "\r":
			case "\t":
			case "\b":
				if ((lIndex > 0) && (!bWhitespace))								// If we're at the first whitespace after a word...
				{
					lWordCount++;
					if (pbLimit && (lWordCount == plMaxWords))
						poField.value = poField.value.substring(0, lIndex + 1);	// This also causes next loop comparison to fail...
				}
				bWhitespace = true;												// In any case, this is a whitespace...
				break;
			default:
				bWhitespace = false;
		}			
	}

//--If last char of field is non-white-space, then increment word counter so it gets counted.
	if (!bWhitespace)
		lWordCount++;
		
//--Make sure we haven't exceeded max words...		
	if (pbLimit && (lWordCount == plMaxWords))
		poField.value = poField.value.substring(0, lIndex + 1);	
		
//--Assign word count to form field...
	if (pbCountdown) 
		window.status = "Word count:  " + (plMaxWords - lWordCount);
	else
		window.status = "Word count:  " + lWordCount;
}

