/*Charactercounter*/

function characterCounter() {
	this.id; //id of textarea
	this.name; //fallback - name of textarea if no id exists
	this.maxChar = '200'; //maximum number of characters in textarea

	this.element; //textarea object - user input
	var that = this; //helper variable - because of ecma script bug for inner functions
	
	this.init = function() { 
		if(this.id == undefined) {
			document.getElementsByName(this.name)[0].id = this.name;
			this.id = this.name;
		}

		this.element = document.getElementById(this.id);
		//this.element.onkeypress=this.countCharacters(); // does not work, because of private/public object model
		this.element.onkeydown = function() { that.countCharacters(); };
		this.element.onkeyup = function() { that.countCharacters(); };
		this.element.onkeypress = function() { that.countCharacters(); };
		this.element.onfocus = function() { that.countCharacters(); };
	}

	this.countCharacters = function() {
		charCount = this.element.value.length;
		if(charCount > this.maxChar) { //cut text if too long
			this.element.value = this.element.value.substring(0, this.maxChar);
		}
	}
}

