// JavaScript Document
window.onload = function () {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var counterClone = counter.cloneNode(true);
			   
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength')+' Characters';
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];
            
            //x[i].onkeydown = checkValue;
            x[i].onkeypress =  x[i].onblur = ismaxlength;
		//	x[i].onkeyup = x[i].onchange = checkMaxLength;
		//	x[i].onkeyup();
			x[i].onkeypress();
		}
	}
}

function ismaxlength(){

    var maxlength = this.getAttribute ? parseInt(this.getAttribute("maxlength")) : ""
    if (this.getAttribute && this.value.length > maxlength)
    {
        this.value=this.value.substring(0,maxlength)        		
    }
	else
	{
		this.relatedElement.className = '';
		this.relatedElement.firstChild.nodeValue = this.value.length;
		
	}        

}

function checkMaxLength() {

	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	var currentValue = this.value;
	if (currentLength > maxLength)
	{
	   // return false;
		//this.relatedElement.className = 'validateBAD';
		//this.relatedElement.firstChild.nodeValue = currentLength + ' maximum limit exceeded';
		return false;
	}
	else
	{
		this.relatedElement.className = '';
		this.relatedElement.firstChild.nodeValue = currentLength;
		return true;
	}
	//this.relatedElement.firstChild.nodeValue = currentLength;
	// not innerHTML
}

