// cross platform equiv. to document.getElementById
function gGetElementById(s) {
 var o = (document.getElementById ? document.getElementById(s)
                                  : document.all[s]);
 return ((o == null) ? false : o);
}

function showtitlebar(title,subtitle) {
	gGetElementById('pagetitle').innerHTML= title;
	gGetElementById('pagesubtitle').innerHTML= subtitle;
}

function changetitle(game) {
	gGetElementById('pagetitle').innerHTML= "Jeux ";
	gGetElementById('pagesubtitle').innerHTML= game;
}

function showerror(err) {
	gGetElementById("errormsg").innerHTML = err;
	gGetElementById("errormsg").className="errormsg_on";
}
var Handleruser = {
	clearedOnce : false,
	clear : function ( field ) {
		if (this.clearedOnce == false) {
			field.value = '';
			this.clearedOnce = true;
		}
	}
}

var Handlerpass = {
	clearedOnce : false,
	clear : function ( field ) {
		if (this.clearedOnce == false) {
			field.value = '';
			this.clearedOnce = true;
		}
	}
}


// AJAX PART

function getXhr(){
	var xhr = null; 
	if(window.XMLHttpRequest) // Firefox et autres
	xhr = new XMLHttpRequest(); 

	else if(window.ActiveXObject){ // IE
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else { // Si XMLHttpRequest est non supporté
		alert("Votre navigateur ne supporte pas les fonctions javascripts présente sur ce site. Veuillez mettre à jour votre navigateur."); 
		xhr = false; 
	} 
	return xhr;
}

function captchaliveaudio(){
	var xhr = getXhr();
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4 && xhr.status == 200){
					// On se sert de innerHTML pour rajouter les options a la liste
					gGetElementById("liveaudio").innerHTML = "";
					gGetElementById("liveaudio").innerHTML = '<embed src="'+xhr.responseText+'.wav" hidden="true" autostart="true" >';
					gGetElementById("captcha_code").value="";
					gGetElementById("captcha_code").focus();
					deleteit(xhr.responseText);
			}
		}
	
		// Loading
		gGetElementById("loading").className="loadingon";
		// POST
		xhr.open("GET","securimage_play.php",true);
		xhr.send(null);
}

function deleteit(filename) {
		var xhr2 = getXhr();
		xhr2.onreadystatechange = function(){
			if(xhr2.readyState == 4 && xhr2.status == 200){
				gGetElementById("loading").className="loadingoff";
			}
		}
		// POST
		xhr2.open("POST","erease.php",true);
		xhr2.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr2.send("Filename="+filename);
}

function checksubscribeform() {
		var xhr = getXhr();
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4 && xhr.status == 200){
				if (xhr.responseText == 'true')
				{
					alert("Votre inscription est complétée. Vous pouvez maintenant vous connecter.");
					window.location = "index.php";
				}
				else {
					gGetElementById("errormsg").innerHTML = xhr.responseText;
					gGetElementById("errormsg").className="errormsg_on";
					gGetElementById("loading").className="loadingoff";
				}
			}
		}
		// Loading
		gGetElementById("loading").className="loadingon";
		// POST
		xhr.open("POST","checkform.php",true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		Pseudo =			gGetElementById('pseudo').value;
		Pass =				gGetElementById('password').value;
		PassConfirm =		gGetElementById('passwordconfirm').value;
		Courriel =			gGetElementById('courriel').value;
		PremiereQ =	gGetElementById('PremiereQuestion').selectedIndex;
		Reponse1 =			gGetElementById('reponse1').value;
		DeuxiemeQ =	gGetElementById('DeuxiemeQuestion').selectedIndex;
		Reponse2 =			gGetElementById('reponse2').value;
		Captcha_code =		gGetElementById('captcha_code').value;
		if (gGetElementById('reglements').checked)
		{
			Reglements = "true";
		}
		else {
			Reglements = "false";
		}
		xhr.send("Pseudo="+encodeURIComponent(Pseudo)+"&Pass="+encodeURIComponent(Pass)+"&PassConfirm="+encodeURIComponent(PassConfirm)+"&Courriel="+encodeURIComponent(Courriel)+"&PremiereQuestion="+encodeURIComponent(PremiereQ)+"&Reponse1="+encodeURIComponent(Reponse1)+"&DeuxiemeQuestion="+encodeURIComponent(DeuxiemeQ)+"&Reponse2="+encodeURIComponent(Reponse2)+"&Captcha_code="+Captcha_code+"&Form=Register&Reglements="+Reglements);
}

function checkreinitform() {
		gGetElementById("errormsg").innerHTML = '';
		gGetElementById("errormsg").className="errormsg";
		var xhr = getXhr();
		Courriel =	gGetElementById('courriel').value;
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4 && xhr.status == 200){
				if (xhr.responseText == 'true')
				{
					getquestionslist(Courriel)
				}
				else {
					gGetElementById("errormsg").innerHTML = xhr.responseText;
					gGetElementById("errormsg").className="errormsg_on";
					gGetElementById("loading").className="loadingoff";
				}
			}
		}
		// Loading
		gGetElementById("loading").className="loadingon";
		// POST
		xhr.open("POST","checkform.php",true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send("&Courriel="+encodeURIComponent(Courriel)+"&Form=CheckReinitForm");
}

function getquestionslist(Courriel) {
		gGetElementById("errormsg").innerHTML = '';
		gGetElementById("errormsg").className="errormsg";
		var xhr = getXhr();
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4 && xhr.status == 200){
					gGetElementById("errormsg").className="errormsg_off";
					gGetElementById("pannel").innerHTML = xhr.responseText;
					gGetElementById("loading").className="loadingoff";
			}
		}
		// POST
		xhr.open("POST","checkform.php",true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send("Courriel="+encodeURIComponent(Courriel)+"&Form=GetQuestionsList");
}