	
addLoadEvent(initializeLength)

function initializeLength()
		{
			if(typeof window.disableLengthCounter == "undefined")
				{
				 addLengthValidation();  
				}
		}



function addLengthValidation()
		{
		 var existingFunction;
		 //check all fields -- add a counter and event handler to any fields with enforcelength defined
		 for(var i=0;i<document.forms.length;i++)
			{
				
			 for(var j=0;j<document.forms[i].elements.length;j++)
			 	{
			 	 if (document.forms[i].elements[j].getAttribute('enforcelength') && 
			 	 	 (!document.forms[i].elements[j].hasLengthValidation ||
			 	 	  document.forms[i].elements[j].hasLengthValidation != true))
			 	 	{	
			 	 		document.forms[i].elements[j].hasLengthValidation = true;
						addLengthCounter(document.forms[i].elements[j]);
			 	 	 	addEvent(document.forms[i].elements[j],"keyup",checkLength);	
						addEvent(document.forms[i].elements[j],"change",checkLength);	
						addEvent(document.forms[i].elements[j],"mouseout",checkLength);	
						// trigger the event once in case the form element
						// has pre-populated text;
						triggerEvent(document.forms[i].elements[j],"mouseout");
			 	 	}
			 	 }
			
			}
		}			
					
		
function checkLength()
	{
	 this.myCounter.firstChild.nodeValue = this.getAttribute('enforcelength') - this.value.length;
	 	
	 if (this.myCounter.firstChild.nodeValue < 0)
	 	{
		 this.myCounter.style.backgroundColor = "#FF3030";
		 this.value = this.value.substr(0,this.getAttribute('enforcelength'));	
		 this.myCounter.firstChild.nodeValue = this.getAttribute('enforcelength') - this.value.length;	
	 	}
	 else if (this.myCounter.firstChild.nodeValue == 0)
	 	{
		 this.myCounter.style.backgroundColor = "#FF3030";		 	
	 	}
	 	
	 else if (this.getAttribute('enforcelength')> 100 && this.myCounter.firstChild.nodeValue < 0.25*this.getAttribute('enforcelength'))
	 	{
		 this.myCounter.style.backgroundColor = "#FFFF00";
	 	}
	 	
	 else
	 	{		 
		 this.myCounter.style.backgroundColor = this.myCounter.defaultBackground;
	 	}
	}	
		
function addLengthCounter(fieldRef)
	{
	 var newHolder = document.createElement("span");	 
	 var newSpan = document.createElement("span");
	 newSpan.myField = fieldRef;
	 
	
	 if(fieldRef.getAttribute("counterclass"))
	 	{
		 	newSpan.className=fieldRef.getAttribute("counterclass");
			newHolder.className="counterHolder";
		}
	 else if(document.getElementById("lengthJS") && document.getElementById("lengthJS").getAttribute("counterclass"))
	 	{			
   		 	newSpan.className = document.getElementById("lengthJS").getAttribute("counterclass");		
			newHolder.className="counterHolder";					
		}
	 else
	 	{	 		
	 		
	 		newSpan.style.backgroundColor="#EFEFDD";
			newSpan.style.fontSize="9pt";
			newSpan.style.fontFamily="arial";
			newSpan.style.padding="3px";
			newSpan.style.fontWeight="bold";
				
			
	 		newSpan.className="counter";
	 		newHolder.className="counterHolder";
	 		
					 
	 	}

	 var newText = document.createTextNode(" ");
	 
	 parentNode = fieldRef.parentNode;
	 newHolder.appendChild(newSpan)
	 newSpan.appendChild(newText);
	 
	 newSpan.firstChild.nodeValue=fieldRef.getAttribute('enforcelength');

	 if (fieldRef.getAttribute('combinetools') && fieldRef.getAttribute('combinetools')=="yes" && fieldRef.toolholder)
	 	{  
			var fieldHolder=fieldRef.toolholder;
			fieldHolder.appendChild(newHolder);
			 try
				 {
				 	newHolder.style.display="table-cell";		 	
				 }
			 catch(e)
				{
					newHolder.style.display="inline-block";
				}			
		}
	 else
	 	{
			var parentNode = fieldRef.parentNode;
	 		parentNode.insertBefore(newHolder, fieldRef.nextSibling);			
		}
		
	 //parentNode.insertBefore(newHolder, fieldRef.nextSibling)
	 fieldRef.myCounter=newSpan;
	 
	 //whatever the background color is, set it as the default, so we can go back to it
	 
	 if(getStyle(newSpan,"backgroundColor"))
	 	{
			newSpan.defaultBackground = getStyle(newSpan,"backgroundColor");			
		}
	 //mozilla uses real CSS names
	 else if (getStyle(newSpan,"background-color"))
		{
			newSpan.defaultBackground = getStyle(newSpan,"background-color");	
		}


	}		
		

