var running = false;
var endTime = null;
var timerID = null;

var countCallBack = null;

function startTimer() {
	running = true;
	now = new Date();
	now = now.getTime();
	endTime = now + (1000 *  totalSeconds);
	showCountDown();
}

function showCountDown() {
	var now = new Date();
	now = now.getTime();
	if (endTime - now <= 0) {
		clearTimeout(timerID);
		running = false;
		document.getElementById('counter').innerHTML = "00:00 min";
		if (countCallBack != null && totalSeconds > 0) {
			// aufruf des Callbacks wenn die Zeit abgelaufen ist
			// und nur, wenn es eine restzeit gab
			countCallBack();
		}
	} else {
		var delta = new Date(endTime - now);
		var theMin = delta.getMinutes();
		var theSec = delta.getSeconds();
		var theTime = theMin;
		theTime += ((theSec < 10) ? ":0" : ":") + theSec;
		document.getElementById('counter').innerHTML = theTime + " min";
		if (running) {
			timerID = setTimeout("showCountDown()",900);
		}
	}
}

function initTextArea() {
	checkTextArea(document.getElementById("quest1"), 1000);
}

function checkTextArea(obj, maxLength) {
	var textArea = obj;
	var length = textArea.value.length;
	if(length > maxLength) {
		textArea.value = textArea.value.substr(0, maxLength);
	}
	var length = maxLength - textArea.value.length;
	$("#charCount").html(length);
}