function setMaxLength() {
	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 = 'Zeichen: <span>0</span>/'+x[i].getAttribute('maxlength');
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength)
		this.relatedElement.className = 'toomuch';
	else
		this.relatedElement.className = '';
	//this.relatedElement.firstChild.nodeValue = currentLength;
	
	this.value = this.value.substring(0,maxLength)
	var currentLength = this.value.length;
	this.relatedElement.firstChild.nodeValue = currentLength;
	
	// not innerHTML
}

// fw: New Char-counter
function CountLeft(field, count, max) {
	// if the length of the string in the input field is greater than the max value, trim it
	if (field.value.length > max)
		field.value = field.value.substring(0, max);
	else
	// calculate the remaining characters
	count.value = max - field.value.length;
}


jQuery.fn.tagName = function() {
    if(this.get(0) == undefined) { return false; }
    return this.get(0).tagName.toLowerCase();
}

jQuery.fn.maxLength = function(max){

		var p = { counterElement: "display_count" };
		var id = $(this).attr("id");
				
		//Get the type of the matched element
		var type = $(this).tagName();
		
		//If the type property exists, save it in lower case
		
    //Check if is a input type=text OR type=password
		if(type == "input" && $(this).attr("type") == "text" || $(this).attr("type") == "password"){
		  var max = $(this).attr("maxlength");
		  var x = max - $(this).attr("value").length;
      
      var l = "noch " + x + " Zeichen zur Verf&uuml;gung";
		  jQuery('#'+id+'_'+p.counterElement).html(l);
			
			$(this).keyup(function(event){
				//If the keypress fail and allow write more text that required, this event will remove it
				if($(this).attr("value").length > max){
					$(this).attr("value") = $(this).attr("value", $(this).attr("value").substring(0,max));
				}	

				var x = max - this.value.length;
				var l = "noch " + x + " Zeichen zur Verf&uuml;gung";
				jQuery('#'+id+'_'+p.counterElement).html(l);
				
			});

		} else if(type == "textarea"){
		  var x = max - $(this).attr("value").length;     
      var l = "noch " + x + " Zeichen zur Verf&uuml;gung";
		  jQuery('#'+id+'_'+p.counterElement).html(l);

			$(this).keyup(function(event){
				//If the keypress fail and allow write more text that required, this event will remove it
				if($(this).attr("value").length > max){
					$(this).attr("value") = $(this).attr("value", $(this).attr("value").substring(0,max));
				}	

				var x = max - this.value.length;
				var l = "noch " + x + " Zeichen zur Verf&uuml;gung";
				jQuery('#'+id+'_'+p.counterElement).html(l);
				
			});		  


		}


};

$(document).ready(function() {
	$("#hbesonderheiten").maxLength(280);
	$("#attributesById_6_23_value").maxLength(180);
	$("#attributesById_6_27_value").maxLength(180);
});
