	
addLoadEvent(initializeLength)

function initializeLength()
		{
		 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'))
			 	 	{	
						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);					
			 	 	}
			 	 }
			
			}
		}			
					
		
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;
	 
	 //newSpan.disabled=true;
	 if(fieldRef.getAttribute("counterclass"))
	 	{
		 newSpan.className=fieldRef.getAttribute("counterclass");
		}
	 else if(document.getElementById("lengthJS") && document.getElementById("lengthJS").getAttribute("counterclass"))
	 	{			
   		 newSpan.className = document.getElementById("lengthJS").getAttribute("counterclass");							
		}
	 else
	 	{
		 newSpan.style.backgroundColor="#EFEFDD";
		 newSpan.style.fontSize="9pt";
		 newSpan.style.fontFamily="arial";
		 newSpan.style.padding="3px";
		 newSpan.style.fontWeight="bold";		 
	 	}
		
	 var newText = document.createTextNode(" ");
	 
	 parentNode = fieldRef.parentNode;
	 newHolder.appendChild(newSpan)
	 newSpan.appendChild(newText);
	 
	 newSpan.firstChild.nodeValue=fieldRef.getAttribute('enforcelength');
	 
	 //parentNode.appendChild(newSpan);
	 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");	
		}


	}		
		


