var textMaxLength=0; // to dynamically change maxlength

function textCounter(field,counter,maxlimit,linecounter) {
  if(textMaxLength!=0)maxlimit=textMaxLength;

	// text width//
	var fieldWidth =  parseInt(field.offsetWidth)-6;
	var charcnt = field.value.length;        

	// trim the extra text
	if (charcnt > maxlimit) { 
	  field.value = field.value.substring(0, maxlimit);
	}else { 
  	  var percentage = parseInt(100 - (( maxlimit - charcnt) * 100)/maxlimit) ;
	  document.getElementById(counter).style.width =  parseInt((fieldWidth*percentage)/100)+"px";
	  // color correction on style from CCFFF -> CC0000
	  setcolor(document.getElementById(counter),percentage,"background-color");
	}
}

function setcolor(obj,percentage,prop){
	obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
}

