var ns6=document.getElementById&&!document.all?1:0;
function checkMaxLength(callObj)
{
	maxLength = callObj.getAttribute("maxlength");
	//callObj.innerHTML=maxLength;
	if (maxLength && callObj.value.length > maxLength){
		callObj.value = callObj.value.substr(0,maxLength);
	}
	//set up Post-Code
	var showPost = true;
	if (callObj.getAttribute("countmaxlength"))
	{	
		if (callObj.getAttribute("countmaxlength").toLowerCase() == "true")
			showPost=true;
		else
			showPost=false;
	}
	if (showPost)
		document.getElementById(callObj.name + "_post").innerHTML=(maxLength-callObj.value.length);
}


function doKeyPress(obj,evt)
{
	maxLength = obj.getAttribute("maxlength");
	var e = window.event ? event.keyCode : evt.which;
	if ( (e == 32) || (e == 13) || (e > 47))
	{ //IE
		if (maxLength && (obj.value.length > maxLength-1)) 
		{
            		if (window.event)
	               		window.event.returnValue = null;
			else
			{
               			evt.cancelDefault;
                		return false;
            		}
        	}
    	}
}


function doTAPaste(myObj)
{
	setTimeout("preCheckMaxLength('"+myObj.name+"')",1);
}

function preCheckMaxLength(myObjName)
{
	checkMaxLength(document.getElementsByName(myObjName)[0]);

}

function initTextareaMaxLengths()
{
	var TAs=document.getElementsByTagName("textarea");
	for (var a=0; a<TAs.length; a++)
	{
		if (TAs.item(a) && TAs.item(a).getAttribute("maxlength") && TAs.item(a).getAttribute("name"))		//Must include a name and a maxlength for this to work.
		{
			var newPre=document.createElement("span");
			var newPost=document.createElement("span");
			var thisTA=TAs.item(a);
			TAs.item(a).onkeyup= function(){checkMaxLength(this)};
			TAs.item(a).onpaste= function(){doTAPaste(this)};
			TAs.item(a).onkeypress= function(){doKeyPress(this)};

			//set up Pre-Code
			var showPre = true;
			if (TAs.item(a).getAttribute("echomaxlength"))
			{	
				if (TAs.item(a).getAttribute("echomaxlength").toLowerCase() == "true")
					showPre=true;
				else
					showPre=false;
			}
			if (showPre)
			{
				newPre.setAttribute("id", thisTA.name+"_pre");
				newPre.innerHTML="Limit "+TAs.item(a).getAttribute("maxlength")+" Characters.  ";
				thisTA.parentNode.insertBefore(newPre, thisTA);
			}
			//set up Post-Code
			var showPost = true;
			if (TAs.item(a).getAttribute("countmaxlength"))
			{	
				if (TAs.item(a).getAttribute("countmaxlength").toLowerCase() == "true")
					showPost=true;
				else
					showPost=false;
			}
			if (showPost)
			{
				newPost.setAttribute("id", thisTA.name+"_post");
				newPost.innerHTML="--";
				var charsRemaining = document.createElement("span");
				var bridgeReturn = document.createElement("br");
				charsRemaining.innerHTML="Chars Remaining:";
				thisTA.parentNode.insertBefore(charsRemaining,thisTA); //charsRemaining,thisTA.nextSibling);
				thisTA.parentNode.insertBefore(newPost, thisTA);  //newPost, thisTA.nextSibling.nextSibling);
				thisTA.parentNode.insertBefore(bridgeReturn,thisTA);
			}
			checkMaxLength(thisTA);
		}
	}	
}

window.onload=initTextareaMaxLengths;
