function voter(vote, imgId, ipMachineHoste){ //lancement de la fonction
//div corespond au nom de la div pour afficher si la valeur existe déjà ou non
//liens corespond au liens sur le PHP
	var xhr;
	if(window.XMLHttpRequest || window.ActiveXObject) {
	        if(window.XMLHttpRequest) {
	                xhr = new XMLHttpRequest(); 
	        }
	        else { // Internet Explorer <7
	                try {
	                        xhr = new ActiveXObject("Msxml2.XMLHTTP");
	                } catch(e) {
	                        xhr = new ActiveXObject("Microsoft.XMLHTTP");
	                }
	        }
	}
	else {
	        alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
	        return;
	}

	xhr.onreadystatechange = function() { alert_ajax(xhr); };	//appelle la fonction lors des 5 changement d'état de l'objet xhr

	liens = "include/voter.php?imageId=" + imgId + "&vote=" + vote + "&ipHoste=" + ipMachineHoste; //ajoute une variable de type GET au liens
	
	//on appelle le fichier reponses.php
	xhr.open("GET", liens, true);
	xhr.send(null);

}
	
function alert_ajax(xhr)	//fonction appelée à chaque changement d'état
{
    if (xhr.readyState==4) //lorsque l'état sera à 4 (dérnier changement)
    {
    	var docXML = xhr.responseXML;
    	var nombreVote = docXML.getElementsByTagName("nombreVote").item(0).firstChild.data;
    	var note = docXML.getElementsByTagName("note").item(0).firstChild.data;
    	var test = docXML.getElementsByTagName("testReussi").item(0).firstChild.data;
    	var validite = docXML.getElementsByTagName("validite").item(0).firstChild.data;
		if(validite){
			note = Math.round(note / nombreVote * 100) / 100;
			
			document.getElementById('nbrVote').innerHTML = nombreVote;
			document.getElementById('noteVote').innerHTML = note;
		}else{
			alert("Votre IP a déjà été utilisé pour voter durant les 24 précédentes heures.");
		}
    }
}

function selectionnerMembre(precedeNom, valeurCommission){
	alert (precedeNom + valeurCommission);
	
	alert (document.getElementById["lapin"].selected);
	
	//document.getElementById[precedeNom + valeurCommission].selected = true;
}

function afficherDiv(nomDiv, posX, posY){
	var divCourant = document.getElementById(nomDiv);
	divCourant.style.left = posX + "ex";
	divCourant.style.top = posY + "ex";
	divCourant.style.display = "block";
}

function cacherDiv(nomDiv){
	var divCourant = document.getElementById(nomDiv).style;
	divCourant.display = "none";
}

function afficheCache(elemCheck, elemDiv, content){
	if(document.getElementById(elemCheck).checked){
		document.getElementById(elemDiv).style.visibility = "visible";
		document.getElementById(elemDiv).innerHTML = '<h3>' + content + '</h3>';
	}else{
		document.getElementById(elemDiv).style.visibility = "hidden";
	}
}

function masqueDisplay(nomDiv){
	var divCourant = document.getElementById(nomDiv).style;

	if(divCourant.display == "block"){
		document.getElementById(nomDiv).style.display = 'none';
	}else{
		divCourant.display = 'block';
	}
}

function menuSql(nomMenu, nomDiv, nbrCols){
	
	contenuHtm = document.getElementById(nomDiv).innerHTML;
	
	if(nomMenu != 0 && nomMenu != null){
		var xhr;

		if(window.XMLHttpRequest || window.ActiveXObject) {
			if(window.XMLHttpRequest) {
					xhr = new XMLHttpRequest(); 
				}
			else { // Internet Explorer <7
				try {
					xhr = new ActiveXObject("Msxml2.XMLHTTP");
				}catch(e) {
					xhr = new ActiveXObject("Microsoft.XMLHTTP");
				}
			}
		}
		else {
				alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
				return;
		}
		
		xhr.open("POST", "include/menuSql.php", true);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xhr.send("nomMenu=" + nomMenu);

		xhr.onreadystatechange = function() {
				if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
					
					information = xhr.responseXML.getElementsByTagName("INFORMATION")[0].firstChild.nodeValue;
					
					if(information == 1){
						listeChamp = xhr.responseXML.getElementsByTagName("CHAMP");
						
						var listeChampDetail = '';
						
						var noCols = 0;
						
						listeChampDetail += '<table width="100%" border="1">'
												+ '<tr>';
						
						for(var index=0; index < nbrCols; index++){
							listeChampDetail += '<td>'
													+ 'Champ'
												+ '</td>'
												+ '<td>'
													+ 'Affich.'
												+ '</td>'
												+ '<td>'
													+ 'Dist'
												+ '</td>';
						}
						
						listeChampDetail += '</tr>';
						
						for(var index=0; index < listeChamp.length; index++){
							
							var champFinal = listeChamp[index].firstChild.nodeValue;
							champFinal = '<td>'
											+ champFinal
										+ '</td>'
										+ '<td>'
											+ '<input type="checkbox" name="' + champFinal + '" value="1">'
										+ '</td>'
										+ '<td>'
											+ '<input type="checkbox" name="distinct_' + champFinal + '">'
										+ '</td>';
							
							noCols++;
							if(noCols == 1){
								listeChampDetail += '<tr>'
														+ champFinal;
							}else {
								if(noCols == nbrCols){
									listeChampDetail += champFinal
													+ '</tr>';
									noCols = 0;
								}else{
									listeChampDetail += champFinal;
								}
							}
							
						}
						
						listeChampDetail += '</table>';
						
						listeChampDetail += '<input type="submit" value="Envoyer">';
						
						document.getElementById(nomDiv).innerHTML = contenuHtm + listeChampDetail;
					}else{
						document.getElementById(nomDiv).innerHTML = contenuHtm + 'Erreur AJAX';
					}

		        }else{
					document.getElementById(nomDiv).innerHTML = contenuHtm + 'Chargement en cours...';
				}
		}
	}
}

$(document).ready(function() {
	$("#but_aperc_new").click(function() {
		$("#ctt_aperc").slideUp("slow", function() {
			$("#ctt_aperc").empty().load("include/apercu.php",{ctt_aperc:  $("#id_textNews").val(), tit_aperc: $("#id_titreNews").val(), type: 'news'}, function (){
				$("#ctt_aperc").slideDown("slow");
			});
		});
	});
	
	$("#but_aperc_dyn").click(function() {
		$("#ctt_aperc").slideUp("slow", function() {
			$("#ctt_aperc").empty().load("include/apercu.php",{ctt_aperc:  $("#id_textNews").val(), type: 'dyn'}, function (){
				$("#ctt_aperc").slideDown("slow");
			});
		});
	});
});
