qrichtig = 0;
qfehler = 0;
qgesamtfertig = 0;
qgesamt = 0;
fehlerfrage = 0;

function getAnzahlFragen()
{
	pb = '&action=getanzahlfragen';
	new Ajax.Request('/ajax/wuerzburg.php', { asynchronous: true, method: 'post', postBody: pb, onSuccess: anzahlFragenLiegtVor, onFailure: updateFailure});
}

function anzahlFragenLiegtVor(t)
{ /* ergebnis pruefung */
	var xmldoc =  t.responseXML;
	var root_node = xmldoc.childNodes[1];
	var nAjaxUpdate = xmldoc.getElementsByTagName('info')[0];
	var anzahl = nAjaxUpdate.getElementsByTagName('anzahl')[0].childNodes[0].nodeValue;
	qgesamt = anzahl;
	$('qgesamt').innerHTML = qgesamt;
}


function getNewObject()
{
	pb = '&action=getadresse';
	new Ajax.Request('/ajax/wuerzburg.php', { asynchronous: true, method: 'post', postBody: pb, onSuccess: frageLiegtVor, onFailure: updateFailure});
}

function frageLiegtVor(t)
{ /* ergebnis pruefung */
	var xmldoc =  t.responseXML;
	var root_node = xmldoc.childNodes[1];
	var nAjaxUpdate = xmldoc.getElementsByTagName('question')[0];
	var id = nAjaxUpdate.getElementsByTagName('id')[0].childNodes[0].nodeValue;
	var objekt = nAjaxUpdate.getElementsByTagName('objekt')[0].childNodes[0].nodeValue;

	$('objektname').innerHTML = objekt;

	$('objektid').value = id;
	$('strasse').value = '';
	$('ortsteil').value = '';

	$('strasse').style.borderColor = '#cccccc';
	$('ortsteil').style.borderColor = '#cccccc';
	qgesamtfertig += 1;
	$('qgesamtfertig').innerHTML = qgesamtfertig;
	$('ortsteil').focus();
}


function checkObject()
{ /* prueft */
	objektid = $F('objektid');
	ortsteil = $F('ortsteil');
	strasse = $F('strasse');
	pb = '&action=checkadresse&id='+objektid+'&ortsteil='+encodeURIComponent(ortsteil)+'&strasse='+encodeURIComponent(strasse);
	new Ajax.Request('/ajax/wuerzburg.php', { asynchronous: true, method: 'post', postBody: pb, onSuccess: ergebnisLiegtVor, onFailure: updateFailure});
}

function ergebnisLiegtVor(t)
{ /* ergebnis pruefung */
	var xmldoc =  t.responseXML;
	var root_node = xmldoc.childNodes[1];
	var nAjaxUpdate = xmldoc.getElementsByTagName('check')[0];
	var ortsteil = nAjaxUpdate.getElementsByTagName('ortsteil')[0].childNodes[0].nodeValue;
	var strasse = nAjaxUpdate.getElementsByTagName('strasse')[0].childNodes[0].nodeValue;
	var id = nAjaxUpdate.getElementsByTagName('id')[0].childNodes[0].nodeValue;
	if (strasse == "wrong") { strbordc = '#cc0000'; }
	else { strbordc = '#00cc00'; }
	$('strasse').style.borderColor = strbordc;

	if (ortsteil == "wrong") { ortbordc = '#cc0000'; }
	else { ortbordc = '#006600'; }
	$('ortsteil').style.borderColor = ortbordc;

	if (ortsteil == 'ok' && strasse == 'ok') {
		if (fehlerfrage != id) { qrichtig += 1; }
		$('qrichtig').innerHTML = qrichtig;
		getNewObject();
	}
	else
	{
		if (fehlerfrage != id)
		{
			qfehler += 1; $('qfehler').innerHTML = qfehler;
			fehlerfrage = id;
		}
	}
	if (qgesamtfertig == qgesamt)
	{
		$('xcontent').innerHTML = '<h3>Geschafft!</h3><p>Möchten Sie eine neue Runde starten?<br /><a href="/wuerzburg">Ja, weiter gehts!</a></p>';
		resetQuestions();
	}
}


function resetQuestions()
{
	pb = '&action=resetquestions';
	new Ajax.Request('/ajax/wuerzburg.php', { asynchronous: true, method: 'post', postBody: pb, onSuccess: doNothing, onFailure: updateFailure});
}

function doNothing(t)
{
	return 1;
}

function getLoesung()
{ /* prueft */
	objektid = $F('objektid');
	pb = '&action=getsolution&id='+objektid;
	new Ajax.Request('/ajax/wuerzburg.php', { asynchronous: true, method: 'post', postBody: pb, onSuccess: loesungLiegtVor, onFailure: updateFailure});
}


function loesungLiegtVor(t)
{ /* ergebnis pruefung */
	var xmldoc =  t.responseXML;
	var root_node = xmldoc.childNodes[1];
	var nAjaxUpdate = xmldoc.getElementsByTagName('solution')[0];
	var ortsteil = nAjaxUpdate.getElementsByTagName('ortsteil')[0].childNodes[0].nodeValue;
	var strasse = nAjaxUpdate.getElementsByTagName('strasse')[0].childNodes[0].nodeValue;
	var id = nAjaxUpdate.getElementsByTagName('id')[0].childNodes[0].nodeValue;

	$('strasse').style.borderColor = '#ff6600';
	$('ortsteil').style.borderColor = '#ff6600';
	$('strasse').value = strasse
	$('ortsteil').value = ortsteil;

	if (fehlerfrage != id)
	{
		qfehler += 1; $('qfehler').innerHTML = qfehler;
		fehlerfrage = id;
	}
}

function updateFailure(t)
{
	alert('Fehler beim Laden: ' + t.status + ' -- ' + t.statusText);
}


