function getXMLHttpRequest() {
	var xhr = null;
	
	if (window.XMLHttpRequest || window.ActiveXObject) {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
		} else {
			xhr = new XMLHttpRequest(); 
		}
	} else {
		alert("Erreur : Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
		return null;
	}
	
	return xhr;
}
function actuNews(id)
{
	var xhr = getXMLHttpRequest();
	xhr.onreadystatechange = function() {
	if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
	       	readActu(xhr.responseText, id);   }
		else if(xhr.readyState < 4)
		{
		}
		};
 	xhr.open("GET", "newsActu.php?id="+encodeURIComponent(id), true);
	xhr.send(null);
	return false;
}
function readActu(data, id)
{
	document.getElementsByClassName("contenu")[0].innerHTML = data;
	var timer = setTimeout("actuNews("+id+");", 20000);
}
function pageTopJeux(page)
{
	var xhr = getXMLHttpRequest();
	xhr.onreadystatechange = function() {
	if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
	       	readTopJeu(xhr.responseText);   }
		else if(xhr.readyState < 4)
		{
		}
		};
 	xhr.open("GET", "pageTopJeu.php?page="+encodeURIComponent(page), true);
	xhr.send(null);
	return false;
}
function readTopJeu(data)
{
	document.getElementById("tutos").innerHTML = data;
}
function liste(id_jeu, type)
{
	if(type == 1)
	{
		if(document.getElementById("lienCollec").innerHTML == '[Masquer]')
		{
			document.getElementById("lienCollec").innerHTML = '[Voir la liste]';
			$("#listeCollec").slideUp("slow");
			return false;
		}
	 }
	 else if(type == 2)
	 {
	 	if(document.getElementById("lienSouhait").innerHTML == '[Masquer]')
		{
			document.getElementById("lienSouhait").innerHTML = '[Voir la liste]';
			$("#listeSouhait").slideUp("slow");
			return false;
		}
	}
	var xhr = getXMLHttpRequest();
	xhr.onreadystatechange = function() {
	if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
	       	readListe(xhr.responseText, type);   }
		else if(xhr.readyState < 4)
		{
		}
		};
 	xhr.open("GET", "listeMembre.php?id_jeu="+encodeURIComponent(id_jeu)+"&type="+encodeURIComponent(type), true);
	xhr.send(null);
	return false;
}
function readListe(data, type)
{
	if(type == 1)
	{
		if(document.getElementById("listeSouhait").style.display != "none")
			$("#listeSouhait").slideUp("slow");
		$("#listeCollec").slideDown("slow");
		document.getElementById("lienSouhait").innerHTML = '[Voir la liste]';
		document.getElementById("lienCollec").innerHTML = '[Masquer]';
		document.getElementById("listeCollec").innerHTML = data;
	}
	else if(type == 2)
	{
		if(document.getElementById("listeCollec"))
		{
			if(document.getElementById("listeCollec").style.display != "none")
				$("#listeCollec").slideUp("slow");
			document.getElementById("lienCollec").innerHTML = '[Voir la liste]';
		}
		$("#listeSouhait").slideDown("slow");
		document.getElementById("lienSouhait").innerHTML = '[Masquer]';
		document.getElementById("listeSouhait").innerHTML = data;
	}
}
function changerPage(page)
{
	var xhr = getXMLHttpRequest();
        xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
               	readData(xhr.responseText);   }
		else if(xhr.readyState < 4)
		{
		}
		};
	var id_cur_news = document.getElementById("cur_news_input").value;
 	xhr.open("GET", "/changerPageCom.php?page="+encodeURIComponent(page)+"&id="+encodeURIComponent(id_cur_news), true);
	xhr.send(null);
	return false;
}
function readData(data)
{
	// Mis en commentaire pour corriger le problème d'appel Ajax
	// On recherchait un div de pub qui n'existe plus dans la v3
	//var pubimg = document.getElementById("pubimg").innerHTML;
	//document.getElementById("pubimg").innerHTML = pubimg;
	document.getElementById("listeCom").innerHTML = data;
	$(".box_comment").fadeIn("slow");
}
function addEvent(obj, event, fct) {
    if (obj.attachEvent)
        obj.attachEvent("on" + event, fct);
    else
        obj.addEventListener(event, fct, true);
}
function valueInput(text, evenement)
{
	if(evenement == 'focus')
	{
		if(text.value == document.getElementById("value_initiale").value)
		{
			text.value = "";
		}
	}
	else
	{
		if(text.value == "")
		{
			text.value = document.getElementById("value_initiale").value;
		}
	}
	
}
function rechercheBouton(text)
{
	if(text.value == "")
		document.getElementById("result").disabled = true;
	else
		document.getElementById("result").disabled = false;
}
function ajouterJeu(type_collec, id_jeu)
{
	var xhr = getXMLHttpRequest();
        type_jeu = type_collec;
        xhr.onreadystatechange = function() {
        	if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                        readJeu(xhr.responseText);
			document.getElementById("loader_ajout").style.display = "none";   
		}
		else if(xhr.readyState < 4)
		{
			document.getElementById("loader_ajout").style.display = "inline";
		}
		};
	xhr.open("GET", "ajouterJeu.php?jeu="+encodeURIComponent(id_jeu)+"&type_collection="+encodeURIComponent(type_collec), true);
        xhr.send(null);
	return false;
}
function readJeu(data)
{
	if(type_jeu == 1)
		document.getElementById("ajout").innerHTML = data;
	else if(type_jeu == 2)
		document.getElementById("ajout").innerHTML = data;
}
function supprimerJeu(type_collec, id_jeu)
{
	var xhr = getXMLHttpRequest();
        type_jeu = type_collec;
        xhr.onreadystatechange = function() {
        	if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                        readJeu(xhr.responseText);
			document.getElementById("loader_ajout").style.display = "none";   
		}
		else if(xhr.readyState < 4)
		{
			document.getElementById("loader_ajout").style.display = "inline";
		}
		};
	xhr.open("GET", "supprimerJeu.php?jeu="+encodeURIComponent(id_jeu)+"&type_collection="+encodeURIComponent(type_collec), true);
        xhr.send(null);
	return false;
}
function readJeu(data)
{
	if(type_jeu == 1)
		document.getElementById("ajouts").innerHTML = data;
	else if(type_jeu == 2)
		document.getElementById("ajouts").innerHTML = data;
}
function Compter(chaine, textareaid, showid)
{
	var reg = new RegExp('[a-zA-Z0-9éèêëàáâäóòôöíìîïçÉÈÊËÀÁÂÄÒÓÔÖÌÍÎÏÇ-]+', 'g');

	var tabNom = chaine.match(reg);

	if(tabNom == null) 
	{
		document.getElementById(showid).innerHTML = 'Nombre de mots = 0';
		document.getElementById("boutton_avis").disabled = true;
	} 
	else 
	{
		document.getElementById(showid).innerHTML = 'Nombre de mots = '+tabNom.length;
		if(tabNom.length < 50)
		{
			document.getElementById("boutton_avis").disabled = true;
		}
		else
		{
			document.getElementById("boutton_avis").disabled = false;
		}
	}

}
function afficherMasquerAvis(titre)
{
	if(document.getElementById("formAvis").style.display == 'none')
	{
		$("#formAvis").slideDown("slow");
		document.getElementById("lienTitre").innerHTML = "Masquer";
	}
	else
	{
		$("#formAvis").slideUp("slow");
		var titre_slashes = titre.replace(/\\'/g, "\'");
		document.getElementById("lienTitre").innerHTML = "Rédiger un avis sur le jeu "+titre_slashes;
	}
	return false;
}	
function afficherMasquerAvisMenu()
{
	if(document.getElementById("formAvis").style.display == 'none')
	{
		$("#formAvis").slideDown("slow");
		document.getElementById("avis_lien").innerHTML = "Masquer";
	}
	else
	{
		$("#formAvis").slideUp("slow");
		document.getElementById("avis_lien").innerHTML = "Ajouter un avis";
	}
	return false;
}	
function avisJeu(form, jeu)
{
	var exp = new RegExp('[a-zA-Z0-9éèêëàáâäóòôöíìîïçÉÈÊËÀÁÂÄÒÓÔÖÌÍÎÏÇ-]+', 'g');

	var tabNom = form.avis.value.match(exp);

	if(tabNom == null) {
		var nb = 0;
	} else {
		var nb = tabNom.length;
	}
	var xhr = getXMLHttpRequest();
        xhr.onreadystatechange = function() {
        	if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                        readAvis(xhr.responseText);
			document.getElementById("loader_avis").style.display = "none";   
		}
		else if(xhr.readyState < 4)
		{
			document.getElementById("loader_avis").style.display = "inline";
		}
		};
	xhr.open("GET", "ajouterAvis.php?add="+encodeURIComponent(jeu)+"&note="+encodeURIComponent(form.note.options[form.note.selectedIndex].value)+"&avis="+encodeURIComponent(form.avis.value)+"&nb="+nb, true);
        xhr.send(null);
	return false;
}
function readAvis(data)
{
	document.getElementById("pagination").background = 'none';
	document.getElementById("pagination").style.background = 'none';
	document.getElementsByClassName('titreCommentaire')[0].style.display = 'none';
	document.getElementsByClassName('titreCommentaire')[0].innerHTML = '';
	document.getElementById("formAvis").innerHTML = data;
}
function sondageQuestion()
{
	var xhr = getXMLHttpRequest();
        xhr.onreadystatechange = function() {
        	if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                        readQuestion(xhr.responseText);  
		}
		else if(xhr.readyState < 4)
		{
		}
		};
	xhr.open("GET", "reponseSondage.php", true);
        xhr.send(null);
	return false;
}
function readQuestion(data)
{
	document.getElementById("sondage-choix").style.display = "block";
	document.getElementById("sondage-resultats").style.display = "none";
	document.getElementById("sondage-choix").innerHTML = data;
}
function sondageResultat()
{
	var xhr = getXMLHttpRequest();
        xhr.onreadystatechange = function() {
        	if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                        readResultat(xhr.responseXML);  
		}
		else if(xhr.readyState < 4)
		{
		}
		};
	xhr.open("GET", "resultatSondage.php", true);
        xhr.send(null);
}
function readResultat(data)
{
	document.getElementById("sondage-choix").style.display = "none";
	document.getElementById("sondage-resultats").style.display = "block";
	var question = data.getElementsByTagName("question");
	var total_votants = data.getElementsByTagName("votant");
	var reponses = data.getElementsByTagName("reponse");
	var innerHTML = "";
	document.getElementById("sondage-choix").innerHTML = "";
	innerHTML += '<div id="display-sondage" style="display:none;"><p  class="question"> '+question[0].getAttribute("name")+' </p>';
	var coef = 100 / parseInt(total_votants[0].getAttribute("value"));
	for(var i = 0, c = reponses.length; i < c; i++)
	{
		var pourcent = reponses[i].getAttribute("value") * coef;
		innerHTML += '<p class="radio" style="clear:left;margin:10px;margin-bottom:-10px;">'+reponses[i].getAttribute("name")+' ('+reponses[i].getAttribute("value")+' votants)</p><br/><div id="div'+i+'" style="float:left;height:15px;width:4px;background-color:#197ed4;-moz-border-radius:2px;border-radius:2px;-webkit-border-radius:2px;color:white;font-size:12px;margin-top:0;margin-right:3px;margin-left:5px;margin-bottom:10px;"><div id="vote'+i+'" style="display:none;">'+reponses[i].getAttribute("value")+'</div><p id="'+i+'" style="text-align:center;">'; 
		if(Math.floor(pourcent) > 7) 
		{
			innerHTML += Math.round(pourcent)+'%';
		}
		innerHTML += '</p></div>';
		if(Math.floor(pourcent) != 0 && Math.round(pourcent) <= 7)
			innerHTML += '<div id="pourcentage" style="color:#787878;font-size:12px;">'+Math.round(pourcent)+'%</div>';
		if(Math.floor(pourcent) < 1){
			var pourcent_string = pourcent.toString();
			innerHTML += '<div id="pourcentage" style="color:#787878;font-size:12px;">'+pourcent_string.substr(0, 3)+'%</div>';}
	}
	innerHTML += '<br/><br/><p style="text-align:center;"> <strong>Total : '+total_votants[0].getAttribute("value")+' vote'; if(total_votants[0].getAttribute("value") != 1) { innerHTML += 's'; } innerHTML += '</strong></p></div>';
	document.getElementById("sondage-resultats").innerHTML = innerHTML;
	$("#display-sondage").slideDown("slow",function () {for(var i = 0, c = reponses.length; i < c; i++)
	{
		var pourcent = reponses[i].getAttribute("value") * coef + 2;
		if(document.getElementById(i).offsetWidth > (pourcent * 1.6)) {
			pourcent = document.getElementById(i).offsetWidth + 2;}
		$("#div"+i).animate({width: pourcent * 1.6+'px'}, 1500);
	}});

}
function sondageVote()
{
	var choix = "";
	var cases = document.getElementById("sondage").sondage_reponses;
	for(var i = 0, c = cases.length; i < c; i++)
	{
		if(cases[i].checked)
			var choix = cases[i].value;
	}
	$("#loading_sondage").fadeIn("slow");
	$("#sondage-choix").fadeTo("slow", 0.25);
	var xhr = getXMLHttpRequest();
        xhr.onreadystatechange = function() {
        	if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                        readVote(xhr.responseText);  
		}
		else if(xhr.readyState < 4)
		{
		}
		};
	xhr.open("GET", "voteSondage.php?choix="+encodeURIComponent(choix), true);
        xhr.send(null);
	return false;
}
function readVote(data)
{
	if(data != 'Votre vote a bien été comptabilisé.')
	{
		document.getElementById("retour").innerHTML = data;
		document.getElementById("loading_sondage").style.display = "none";
	}
	else
	{
		document.getElementById("loading_sondage").style.display = "none";
		sondageResultat();
	}
}
function autoCompletionRecherche(champ)
{
	if(champ.length >= 3)
	{
		var xhr = getXMLHttpRequest();
        	xhr.onreadystatechange = function() {
        		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                	        readCompletion(xhr.responseText);  
			}
			else if(xhr.readyState < 4)
			{
			}
			};
		xhr.open("GET", "/completion.php?texte="+encodeURIComponent(champ), true);
        	xhr.send(null);
		return false;
	}
	else
	{
		$("#completion").fadeOut("normal");
	}
}
function readCompletion(data)
{
	if(data != '')
	{
		document.getElementById("completion").innerHTML = data;
		var completion = document.getElementById("completion");
		var entrees = completion.getElementsByTagName("li");
		var nb_entree = entrees.length;
		if(document.getElementById("rows").value != 0)
			$("#completion").fadeIn("slow");
		else
			document.getElementById("completion").style.display = "none";
		if(nb_entree > 7)
		{
			document.getElementById("completion").style.height = "150px";
			document.getElementById("completion").style.overflow = "auto";
		}
		else
		{
			document.getElementById("completion").style.height = "";
			document.getElementById("completion").style.overflow = "";
		}

	}
	else
	{
		document.getElementById("completion").style.display = "none";
	}
}
function addMur(data, id) {
	if(data != '') {
		var xhr = getXMLHttpRequest();
        xhr.onreadystatechange = function() {
        	if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                        readMur(xhr.responseText);  
		}
		else if(xhr.readyState < 4)
		{
		}
		};
	xhr.open("GET", "addMur.php?data="+encodeURIComponent(data)+"&id="+encodeURIComponent(id), true);
    xhr.send(null);
	}
}
function readMur(data) {
	document.getElementById('coms_list').innerHTML = data+''+document.getElementById('coms_list').innerHTML;
	document.getElementById('box_comment').innerHTML = '';
	document.getElementById('box_comment').style.background = 'none';
	$('#last_com').fadeIn('slow');
}
function supprMur(id) {
	var xhr = getXMLHttpRequest();
        xhr.onreadystatechange = function() {
        	if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                        readSupprMur(id, xhr.responseText);  
		}
		else if(xhr.readyState < 4)
		{
		}
		};
	xhr.open("GET", "supprMur.php?id="+encodeURIComponent(id), true);
    xhr.send(null);
}
function readSupprMur(id, data) {
	if(data != '' || !data)
		$('#'+id).fadeOut("slow");
	else
		document.getElementById(id).innerHTMl = data;
}