window.onload=init;
function init() {
	startList("lequiz");
	courquestion=1;
	lit_question();
}
var totcpt=0;
function startList(n) {
	if (document.getElementById) {
		navRoot = document.getElementById(n);
		node = navRoot;
		compte=0;
		while (node && compte<500 && totcpt<1500) {
			compte++;
			totcpt++;
			node = getnextnode(navRoot,node);
			if (node) {
				if (node.className=="reponse") {
					node.onclick=function() {
						choisir_reponse(this);
					}
				}
			}
		}
	}
	if (totcpt>1500) alert("Rotation totale trop importante");
	if (compte>500) alert("Rotation trop importante");
}

function chercheparent(o,tg) {
	if (!o) return false;
	while (o && o.tagName!=tg) {
		o=o.parentNode;
	}
	return o;
}

function position(o,s) {
	l=0;
	t=0;
	if (o.offsetParent) {
		while (o) {
			l+=o.offsetLeft;
			t+=o.offsetTop;
			o=o.offsetParent;
		}
	}else if (o.x) {
		l=o.x;
		t=o.y;
	}
	r=(s==0) ? l:t;
	return r;
}

function getnextnode(source,o) {
	if (!o) return false;
	oo=false;
	if (o.firstChild) return o.firstChild;
	if (o.nextSibling) return o.nextSibling;
	o=o.parentNode;
	while (!oo && o && (o!=source)) {
		oo=o.nextSibling;
		o=o.parentNode;
	}
	return oo;
}

function cache_select(on) {
	fs=self.document.forms;
	for (f in fs) {
		if (fs[f].elements) {
			for (ee in fs[f].elements) {
				if (fs[f].elements[ee]) if (fs[f].elements[ee].options) {
					fs[f].elements[ee].style.visibility=(on) ? "hidden":"visible";
				}
			}
		}
	}
}


function popup(o,w,h,l,t) {
	if (!w) w=500;
	if (!h) h=500;
	if (!t) t=parseInt(screen.height-h)/2;
	if (!l) l=parseInt(screen.width-w)/2;
	if (!o) return false;
	win=window.open(o.href,o.target,"left="+l+",top="+t+",width="+w+",height="+h);
	win.focus();
}

var courreponse="";
function choisir_reponse(o) {
	if (! o) return false;
	if (! okrep) return false;
	if (o.id==courreponse) {
		courreponse="";
		o.className="reponse";
	}else{
		if (courreponse!="") {
			oo=$(courreponse);
			if (oo) oo.className="reponse";
		}
		courreponse=o.id;
		o.className="reponsechek"
	}
	oo=$("envoireponse");
	if (oo) {
		oo.style.display="block";
		oo.style.visibility=(courreponse!="") ? "visible":"hidden";
	}
	return "";
}

var courajax=0;
function waitajax(sens,r) {
	v=(sens)? "block":"none";
	o=$('systemeAttente');
	if (o) {o.style.display=v;
		if (r) r=$(r);
		if (r) {
			o.style.left=position(r,0)+"px";
			o.style.top=position(r,1)+"px";
		}
	}
}

var courquestion=1;
function lit_question() {

	var url = '/quiz/quiz.php';
	var parametres = 'q='+courquestion;
	waitajax(true);
	var myAjax = new Ajax.Request(
		url,
		{
			method: 'get',
			parameters: parametres,
			onComplete: afficheQuiz
		}
	);
	return false;
}
function afficheQuiz(requete) {
	//alert("attends");
	if (requete.responseText.match(">>>OK")) {
		rep=requete.responseText.split(">>>");
		if (rep[0]=="RESULTAT") {
			waitajax(false);
			self.window.location="/quiz/quiz.php?fin=1";
			return;
		}
		rep=rep[0].split("|");
		if (rep.length>3) {
			o=$("titre_section");
			if (o) o.innerHTML=rep[0];
			o=$("question");
			if (o) o.innerHTML=rep[1];
			doc="";
			for (i=2;i<rep.length;i++) {
				doc+="<a href=\"javascript:;\" onclick=\"choisir_reponse(this);\" class=\"reponse\" id=\"reponse"+(i-1)+"\"><span class=\"repoption\"><img src=\"/imgs/_.gif\" title=\"\" alt=\"\" /></span>"+rep[i]+"</a>\n";
			}
			o=$("reponses");
			if (o) o.innerHTML=doc;
			o=$("envoireponse");
			if (o) {
				o.style.display="block";
				o.style.visibility="hidden";
			}
			o=$("continuez");
			if (o) {
				o.style.display="none";
				o.style.visibility="hidden";
			}
		}
		okrep=true;
	}else if (requete.responseText.match(">>>NOK")) alert(requete.responseText);
	else alert("Raté");

	waitajax(false);
}

function soumetrep() {
	requete="";
	if (courreponse=="") {
		alert("Pas de réponse sélectionnée");
		return false;
	}
	
	var url = '/quiz/quiz.php';
	var parametres = 'q='+courquestion+'&r='+courreponse.replace("reponse","");
	waitajax(true);
	var myAjax = new Ajax.Request(
		url,
		{
			method: 'get',
			parameters: parametres,
			onComplete: afficheEnvoi
		}
	);
	return false;
}
var okrep=true;
function afficheEnvoi(requete) {
	if (requete.responseText.match(">>>OK")) {
		rep=requete.responseText.split(">>>");
		if (rep[0]=="TEST") {
			waitajax(false);
			continuer();
			return;
		}
		rep=rep[0].split("|");
		o=$("envoireponse");
		if (o) {
			o.style.visibility="hidden";
			o.style.display="none";
		}
		o=$("continuez");
		if (o) {
			oo=$("quizcomment");
			if (oo) oo.innerHTML=rep[0];
			o.style.visibility="visible";
			o.style.display="block";
		}
		okrep=false;
	}else if (requete.responseText.match(">>>NOK")) alert(requete.responseText);
	else alert("Raté");
	waitajax(false);
}

function continuer() {
	courquestion++;
	courreponse="";
	lit_question();
}

function _(o) {
	if (!o) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById(o)) return false;
	return document.getElementById(o);
}

function aaide(mes) {
	if (_('aide')) {
		_('aide').innerHTML=mes;
		_('aide').style.display=(mes=="") ? 'none':'block';
	}
}

function limite(zone,max){
	if(zone.value.length>=max){
		zone.value=zone.value.substring(0,max);
	}
}

function attention(){
	resultat=window.confirm('Etes vous certain de vouloir valider votre projet définitivement ?');
	
	if (resultat==1){
		return true;
	}else{
		return false;
	}
}


function closeIsel() {
	cache_select(false);
	if (_('ifcontenu')) _('ifcontenu').src='/vide.html';
	if (_('iselcont')) _('iselcont').style.display="none";
	if (_('isel')) _('isel').style.display="none";
	return false;
}

function openIsel(uri) {
	cache_select(true);
	o=_("isel");
	o.style.width=document.documentElement.offsetWidth+"px";
	o.style.height=document.documentElement.scrollHeight+"px";
	o.style.zIndex="10";
	o.style.display="block";
	o=_("iselcont");
	o.style.zIndex="11";
	o.style.display="block";
	t=parseInt((document.documentElement.offsetWidth-o.offsetWidth)/2);
	if (t<0) t=0;
	o.style.left=t+"px";
	t=20+document.documentElement.scrollTop;
	if (t+o.offsetHeight>document.documentElement.offsetHeight) t=document.documentElement.scrollTop;
	o.style.top=t+"px";
	_("ifcontenu").src=uri;
}

function cache_select(on) {
	fs=self.document.forms;
	for (f in fs) {
		if (fs[f].elements) {
			for (ee in fs[f].elements) {
				if (fs[f].elements[ee]) if (fs[f].elements[ee].options) {
					fs[f].elements[ee].style.visibility=(on) ? "hidden":"visible";
				}
			}
		}
	}
}

