// WARNING- this js module requires prototype.js to be installed and 
// included before calling this script otherwise you get $A not defined

var word = $A();
var tas = $A();
var length;  // Added by Richard Newman June 9 07 to 
	     // fix the issue where wordcount does not
	     // work in IE


function initWordCounts(limit) {
	var limit = limit;
	tas = $A(document.getElementsByTagName('textarea'));
	tas.each(
		function(ta, b) {
			words = document.createElement('span');
			if (ta.value == '') length = 0;
			else length = ta.value.replace(/^[\n\t\s]+|[\n\t\s]+$/, '').replace(/ +/g, ' ').split(/[\s\n\t]+/).length;
			// words.innerHTML = length + ' / ' + limit + ' words';

			words.innerHTML = '<br/>You have used  ' + length + '  of  ' + limit + '  words allowed in this field.';


			ta.parentNode.appendChild(words);
			word.push(words);
		}
	);
}

function updateWordCounts(limit) {
	word.each(
		function(count, ta) {
			if (tas[ta].value == '') length = 0;
			else length = tas[ta].value.replace(/^[\s\n\t]+|[\t\n\s]+$/, '').replace(/ +/g, ' ').split(/[\s\n\t]+/).length;
			// count.innerHTML = length + ' / ' + limit + ' words';

			count.innerHTML = '<br/>You have used  ' + length + '  of  ' + limit + '  words allowed in this field.';

			if (length > limit) {
				count.innerHTML = '<br/>You have EXCEEDED words allowed in this field.<br/>Your submission will be truncated to 150 words.<br/><blink>Please remove some of your text to be sure your message is clear.</blink>';
				count.style.color = 'red';
				count.style.fontWeight = 'bold';
				
			} else {
				count.style.color = '';
				count.style.fontWeight = '';
			}
		}
	);
};

