function setStars(m,n) {
	for(j = 1; j <= 5; j++) { document['star'+j+'-'+m].src = starn.src; }

	var votetext = document.getElementById('text-'+ m);
	if(writetemp) { temp = votetext.innerHTML; writetemp = false; }
	if(n == 1) { votetext.innerHTML = 't&ouml;dlich'; }
	if(n == 2) { votetext.innerHTML = 'igitt'; }
	if(n == 3) { votetext.innerHTML = 'okay'; }
	if(n == 4) { votetext.innerHTML = 'gut'; }
	if(n == 5) { votetext.innerHTML = 'super!'; }

	while(n >= 1) {
		document['star'+n+'-'+m].src = starb.src;
		n--;
	}
}

function resetStars(m,n) {
	for(j = 1; j <= 5; j++) { document['star'+j+'-'+m].src = starn.src; }

	if(2*n % 2 == 1) { document['star'+Math.round(n)+'-'+m].src = starh.src; n -= 0.5; }	
	while(n >= 1) {
		document['star'+n+'-'+m].src = star.src;
		n--;
	}

	var votetext = document.getElementById('text-'+ m);
	votetext.innerHTML = temp;
	writetemp = true;
}

function vote(m_id, vote)
{
	ajaxdata("vote.php?m_id=" + m_id + "&vote=" + vote, answer, m_id, vote, null);
}

function answer(output, m_id, vote, nop)
{
	var barpos = output.indexOf("|");
	var stars = output.substr(0,barpos);
	var sumvotes = output.substr(barpos+1);

	var votetext = document.getElementById('text-'+ m_id);
	votetext.innerHTML = sumvotes+" Bew.";
	
	var stararray = document.getElementById('stars-'+ m_id);
	stararray.innerHTML = '';
	for(j = 1; j <= 5; j++) { stararray.innerHTML += '<img name=\"star'+j+'-'+m_id+'" src="img/starn.png" border="0">'; }
	
	var n = Math.round(2*stars)/2;
	if(2*n % 2 == 1) { document['star'+Math.round(n)+'-'+m_id].src = starhg.src; n -= 0.5; }	
	while(n >= 1) {
		document['star'+n+'-'+m_id].src = starg.src; 
		n--;
	}
	
	writetemp = true;
}


function ajax(url, callback, arg1, arg2, arg3)
{

        var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
 
        request.open("GET", "./" + url, true);

 
        request.onreadystatechange = function()
	{
		if(request.readyState == 4 && (request.status == 200 || request.status == 302 || request.status == 301))
		{
			if(request.responseText.indexOf("AJAXOK") != -1)
			{
				callback(arg1, arg2, arg3);
			}
		}
        }
        request.send(null);
}

function notLoggedIn() {
	alert('Um abstimmen zu koennen, musst du dich einloggen. Klicke einfach oben auf Login.');
}

function markMeal(m_id, colora, colorb) {
	ajaxdata("mark.php?m_id=" + m_id, trmark, m_id, colora, colorb);
}

function trmark(status, m_id, colora, colorb) {
	if(status == '1') {
		document.getElementById('tr-'+ m_id).style.backgroundColor=colora;
	} else {
		document.getElementById('tr-'+ m_id).style.backgroundColor=colorb;
	}
}

function ajaxdata(url, callback, arg1, arg2, arg3)
{

        var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
 
        request.open("GET", "./" + url, true);

 
        request.onreadystatechange = function()
	{
		if(request.readyState == 4 && (request.status == 200 || request.status == 302 || request.status == 301))
		{
			callback(request.responseText, arg1, arg2, arg3);
		}
        }
        request.send(null);
}
