var target = null;
var cnt = 0;
var n = 0;

function checkIfNull(until)
{
	until = until.split(";");
	var i = 0;
	for each(var tmp in until) until[i++] = parseInt(tmp);
	target = new Date(until[0],until[1]-1,until[2],until[3],until[4],until[5]);
	startDatum=new Date();

	if(startDatum>target) return true;
	return false;
}
function countdown(until,id, color)
{
	untilo = until;
	if(document.getElementById(id) != null)
	{
		until = until.split(";");
		var i = 0;
		for each(var tmp in until) until[i++] = parseInt(tmp);
		target = new Date(until[0],until[1]-1,until[2],until[3],until[4],until[5]);
		startDatum=new Date();

		if(startDatum<target)
		{
			var jahre=0, monate=0, tage=0, stunden=0, minuten=0, sekunden=0;

			while(startDatum<target)
			{
				jahre++;
				startDatum.setFullYear(startDatum.getFullYear()+1);
			}
			startDatum.setFullYear(startDatum.getFullYear()-1);
			jahre--;

			while(startDatum<target)
			{
				monate++;
				startDatum.setMonth(startDatum.getMonth()+1);
			}
			startDatum.setMonth(startDatum.getMonth()-1);
			monate--;

			while(startDatum.getTime()+(24*60*60*1000)<target)
			{
				tage++;
				startDatum.setTime(startDatum.getTime()+(24*60*60*1000));
			}

			stunden=Math.floor((target-startDatum)/(60*60*1000));
			startDatum.setTime(startDatum.getTime()+stunden*60*60*1000);

			minuten=Math.floor((target-startDatum)/(60*1000));
			startDatum.setTime(startDatum.getTime()+minuten*60*1000);

			sekunden=Math.floor((target-startDatum)/1000);

			time = parseInt((jahre*31556926)+(monate*2629743.83)+(tage*86400)+(stunden*3600)+(minuten*60)+sekunden);
		}
		else time = 0;

		var from = 7889232;	// 3 monate
		var p = parseInt((1-(time/from))*100);
		document.getElementById(id).style.backgroundColor = color;
		document.getElementById(id).style.width = p+"%";
		document.getElementById(id).innerHTML = time+" sec - "+p+"%";
	}
	setTimeout('countdown(\''+untilo+'\',\''+id+'\', \''+color+'\')',200);
}

function createCD(until, id)
{
	if(!checkIfNull(until))
	{
		var colors = new Array("lightgreen", "lightblue", "orange", "green", "silver", "yellow", "purpur", "lightgrey", "pink", "red", "gold", "brown", "maroon");
		var progress = id+"_progress";
		if(document.getElementById(id) == null)	document.getElementById('cnt').innerHTML += "<tr><td id=\""+id+"\"></td></tr>";
		document.getElementById(id).innerHTML = "<div id=\""+progress+"_super\" style=\"text-align:left;font-weight:bold;line-height:90%;font-family:Calibri, Helvetica, sans-serif;margin:5px;margin-bottom:15px;padding:0;border:2px solid silver;width:500px;height:15px;\"><div id=\""+progress+"\" style=\"float:left;text-align:right;font-weight:bold;line-height:90%;font-family:Calibri, Helvetica, sans-serif;margin:0;padding:0;border:0px solid white;height:100%;width:0%;background-color:lightblue;cursor:pointer;\" onclick=\"document.getElementById('"+progress+"_super').style.height = '10px';document.getElementById('"+progress+"_super').innerHTML = document.getElementById('"+progress+"_super').innerHTML.substring(0,document.getElementById('"+progress+"_super').innerHTML.length-"+id.length+");\"></div>&nbsp;"+id+"</div>";
		countdown(until,progress, colors[cnt++]);
	}
	else n++;
}

function getFinished()
{
	document.getElementById("ct_timer").innerHTML = " - " + n + " finished appointment" + ((n != 1)?"s":"");
}