


function textAreaMaxLength(){
	if (!document.getElementById) return false; 
	
	// for each textarea
	var formTextareas = document.getElementsByTagName("textarea");	
 	for (var i=0; i < formTextareas.length; i++) { 
	
		
		// find the maxlength from the span 
		var siblings = formTextareas[i].nextSibling;
    	while (siblings) { 		
			for( var x = 0; siblings.childNodes[x]; x++ ){	
		 		 if (siblings.childNodes[x].className=="maxlength") {		
					
					
					formTextareas[i].onchange = function(){
						
						// get the maxlength value from this textareas note
						var siblings2 = this.nextSibling;
						while (siblings2) { 	
							for( var j = 0; siblings2.childNodes[j]; j++ ){	
						 		if (siblings2.childNodes[j].className=="maxlength") {	

								 	maxKeys = siblings2.childNodes[j].firstChild.nodeValue;
								}
							}
							siblings2 = siblings2.nextSibling;		
						}			
						
						var theInput = new String(this.value);
						
						if(theInput.length > maxKeys){
							alert(keysLimtString + maxKeys + keysLimitEnd);
							this.value = this.value.substring(0,maxKeys);
							startstr = startstr;
							endstr = endstr;
							swapPTag(this,maxKeys,startstr,endstr,0);
						}
					} 
					
					
					// assign a function to the keyup event handler for this textarea
					formTextareas[i].onkeyup = function(){
						
						
						// get the maxlength value from this textareas note
						var siblings2 = this.nextSibling;
						while (siblings2) { 	
							for( var j = 0; siblings2.childNodes[j]; j++ ){	
						 		if (siblings2.childNodes[j].className=="maxlength") {	

								 	maxKeys = siblings2.childNodes[j].firstChild.nodeValue;
								}
							}
							siblings2 = siblings2.nextSibling;		
						}			
						
						// create the output string
						var str = new String(this.value);
						var remaining = maxKeys - str.length;
						if(remaining <= 0){
							//alert('none left');
							this.value = this.value.substring(0,maxKeys);
							
						}
						//var showstr = len + " characters of ";
						var endstr = remaining + textareaMaxCharsMsg;
						if (remaining < 0){ endstr = textareaLimitMsg + maxKeys + textareaLimitEnd;}
						
						swapPTag(this,maxKeys,'',endstr,1);
					}	
				}
			}	
			siblings = siblings.nextSibling;	
	  	}
     }
}
function swapPTag(currentItem,maxKeys,startString,endString,hideThis){
	
	// find p.note and swap it for a new one containg the output string and maxlength (hidden)
	var temp = currentItem.parentNode.getElementsByTagName("p");	
	for( var k = 0; temp[k]; k++ ){	
 					if (temp[k].className.indexOf('maxLength') != -1) {
						//alert(temp[k].className);
			var theNewParagraph = document.createElement('p');
			theNewParagraph.className = 'note maxLength';
			
			var theOpeningText = document.createTextNode(startString);
			theNewParagraph.appendChild(theOpeningText);
			
			var theNewSpan = document.createElement('span');
			theNewSpan.className = "maxlength";
			if(hideThis){
				theNewSpan.style.display = 'none';
			}
			var theSpan = document.createTextNode(maxKeys);
			theNewSpan.appendChild(theSpan);
			
			theNewParagraph.appendChild(theNewSpan);
			
			var theClosingText = document.createTextNode(endString);
			theNewParagraph.appendChild(theClosingText);
			
			temp[k].parentNode.replaceChild(theNewParagraph,temp[k]);
		}	
	}
}



function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(textAreaMaxLength);
