var can_next = 1;
var agb = 0;
var qcounter = 0;
var s = 'spo'
var d = 'nsor';
var minq = 5;

function submit_question() {
	if (agb == 0) {
		/*if (!confirm("Du musst die Nutzungsbestimmungen akzeptieren.\nKlicke auf ok um sie zu akzeptieren oder auf abbrechen, falls du sie nicht akzeptieren willst.")) {
			return;
		} else {*/
			agb = 1;
		/*}*/
	}
	var field = fetch('frage');
	if (field && can_next == 1) {
		var val = field.value;
		val = trim(val);
		if (val != "") {
			can_next = 0;
			fade_out();
			var data = 'page=' + pageid + '&action=ask&redi='+predi+'&question=' + encodeURIComponent(str_replace("+", "745628462836743", val));
			Ajax.POST('../../ajax.php?', data, 'q_response()', 1);
		}
	}
}

var ct = 0;
var mt = 5;
function getRequestTime() {
	if (ptimer < mt) {
		return ptimer * 1000;
	}
	if ((ptimer - mt*ct) < mt && (ptimer - mt*ct) > 0) {
		ct++;
		return (ptimer - mt*ct) * 1000;
	} else {
		ct++;
		return mt * 1000;
	}
}

function q_response () {
	var value = XMLValue(Ajax.response, 'answer', 0, 'Bitte Seite aktualisieren! Gebanntes Wort benutzt!');
	var qid = XMLValue(Ajax.response, 'id', 0, 0);
	if (qid != 0) {
		var data = 'page=' + pageid + '&questionid=' + qid + '&action=wait';
		window.setTimeout("Ajax.POST('../../ajax.php?', '"+data+"', 'q_response()', 1)", getRequestTime());
	} else {
		ct = 0;
		display_answer(value);
		qcounter++;
		if (qcounter == minq) {
		}
	}
}

function fade_out() {
	var field = fetch('frage');
	var button = fetch('submit');
	if (field && button) {
		field.disabled = true;
		field.className = 'disabled';
		button.disabled = true;
		button.className = 'disabled';
	}
	var head = fetch('head');
	head.innerHTML = '<span style="color:red"><strong>Das Orakel wird befragt (max. ' + ptimer + ' Sekunden)</strong></span><br /><div><br /><img src="../../images/loading.gif" alt="" border="0" /></div><br />';
}

function display_answer(answer) {
	can_next = 1;
	var container = fetch('orakelcontent');
	if (container) {
		container.innerHTML = answer;
	}
}

function ask_another_question() {
	var container = fetch('orakelcontent');
	if (container) {
		container.innerHTML = '<div id="head"><strong>Stelle kostenlos Fragen (z.B: Bin ich klug?)</strong></div><div id="fragebox"><div><textarea id="frage" style="width:300px;height:100px;"></textarea></div><div><input type="button" name="submit" onclick="submit_question()" value="Das Orakel befragen!" /></div></div>';
	}
}

window.onload = function () {
	var field = fetch('frage');
	var button = fetch('submit');
	if (field && button) {
		field.disabled = false;
		button.disabled = false;
	}
}