var myShow;
var tabAffiche = ["descriptif", "carte", "photos", "dispo"];

function afficheDetail (id){

	document.getElementById(id).style.display = "block";
	document.getElementById("lien"+id).className = "lienactifrose";
	
	
	//on masque tous les autres 
	for(var i=0; i<=tabAffiche.length; i++){
		if(tabAffiche[i] != id){
			if(document.getElementById(tabAffiche[i])) {
				document.getElementById(tabAffiche[i]).style.display = "none";
				document.getElementById("lien"+tabAffiche[i]).className = "liennoirsousligne";
			}
		}
	}
	
	//map.checkResize();
	if(id == "carte"){
		map.checkResize();
		map.setCenter(point, 13);
	}
	
}

function affichePhoto(num, nbPhotos) {

	for(i=1; i<=nbPhotos; i++){
		if(i == num){
			document.getElementById('slide-'+i).style.display = "block";
			document.getElementById('slide-'+i).style.textalign = "center";
			document.getElementById('slide-'+i).style.margin = "0 auto";
		}else{
			document.getElementById('slide-'+i).style.display = "none";
		}
	}
}



var nbParticipants = 1;
var nbAccompagnateurs = 1;
var nbListes = 0;
var nbParticipantsTotal = 0;
var affects = new Array();

function ajouterParticipant(id){
	
	var prenom;
	var nom;
	var liste_noms = document.getElementById(id);
	var idPersonne;
	var invite;
	
	switch(id){
		case "participants_congres":
			prenom = document.getElementById('prenomP').value;
			nom = document.getElementById('nomP').value;	
			invite = 1;
		break;
		case "accompagnateurs_tab":
			prenom = document.getElementById('prenomA').value;
			nom = document.getElementById('nomA').value;
			invite = 0;
		break;
	}
	// on vérifie que le nom et le prénom de la personne ont bien été saisis
	if(nom == "" || prenom == ""){
		alert("Veuillez saisir le nom et le prénom de la personne à ajouter");
		return "";
	}else{
		switch(id){
			case "participants_congres":	
				document.getElementById('prenomP').value = "";
				document.getElementById('nomP').value = "";
				nbParticipants++;
				
				break;
			case "accompagnateurs_tab":
				document.getElementById('prenomA').value = "";
				document.getElementById('nomA').value = "";
				nbAccompagnateurs++;
				
				break;
		}
				
	}
	var nbRows = liste_noms.getElementsByTagName('tbody')[0].getElementsByTagName('tr').length;
	
	var newRow = liste_noms.getElementsByTagName('tbody')[0].insertRow(nbRows);
	
	var idPersonne = new UUID();

	newRow.id = 'p_'+id+'_'+idPersonne;
	
	var newCell = newRow.insertCell(0);
    newCell.innerHTML = prenom;
	newCell.className = "colonne1";
	
	var newCell = newRow.insertCell(1);
    newCell.innerHTML = nom;
	newCell.className = "colonne2";
	
	var newCell = newRow.insertCell(2);
    newCell.innerHTML = '<a href="#" title="Supprimer ce participant" onclick="supprimerParticipant(\''+id+'\', \''+idPersonne+'\');"><img src="images/bt-suppr-mini.gif" alt="Supprimer" title="Supprimer ce participant" /></a>';
	newCell.className = "colonne3";
	
	ajouterDsListe(nom , prenom, idPersonne);
	
	// remise en etat des styles sur les lignes
	appliquerStylesTableau(id);
	
	$.ajax({
	   type: "POST",
	   url: "majSession.php",
	   data: "action=1&invite="+invite+"&nom="+nom+"&prenom="+prenom+"&idPersonne="+idPersonne,
	   success: function(msg){
		 //alert( "Data Saved: "+   msg );
	   }
	 });
	
}
function supprimerParticipant(id, idPersonne){
	
	var ligneASupprimer = document.getElementById('p_'+id+'_'+idPersonne).rowIndex;
	
	var liste_personnes = document.getElementById(id);
	

	
	$.ajax({
	   type: "POST",
	   url: "majSession.php",
	   data: "action=2&idPersonne="+idPersonne,
	   success: function(msg){
		   
		 	liste_personnes.deleteRow(ligneASupprimer);
	
			// remise en etat des styles sur les lignes
			appliquerStylesTableau(id);
			
			supprimerDsListe(idPersonne);
			
			switch(id){
				
				case "participants_congres":			
					nbParticipants--;
					break;
				case "accompagnateurs_tab":
					nbAccompagnateurs--;
					break;
			}
	   }
	 });
	
}
function appliquerStylesTableau(id){
	var liste_noms = document.getElementById(id);
	var nbRows = liste_noms.getElementsByTagName('tbody')[0].getElementsByTagName('tr').length;
	var odd = 1;
	for(i=0; i<nbRows; i++){
		if(odd == 1){
			liste_noms.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[i].className = "fond_rose_clair";
			odd = 0;
		}else{
			liste_noms.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[i].className = "";
			odd= 1;
		}
	}
};
function ajouterDsListe(nom , prenom, idPersonne){
	
	for(i=0; i< nbListes; i++){
		liste = document.getElementById('liste_'+i);
		liste.options[liste.options.length] = new Option(prenom+' '+nom, idPersonne);
	}

}
function supprimerDsListe(idPersonne){
	
	var indexASupprimer;
	if(nbListes > 0){
		
		liste = document.getElementById('liste_0');
		for(i=0; i<liste.options.length; i++){
			if(idPersonne == liste.options[i].value)
				indexASupprimer = liste.options[i].index;
		}
		for(j=0; j< nbListes; j++){
			liste = document.getElementById('liste_'+j);
			liste.options[indexASupprimer] = null;
			trierListe(liste);
		}
	}
	
}
function trierListe(liste){
	
	Liste = new Array();
	 
	for(i=0; i<liste.options.length; i++){
		Liste[i] = new Array();
		Liste[i][0] = liste.options[i].text;
		Liste[i][1] = liste.options[i].value;
	}
	Liste = Liste.sort();
	 
	for(i=0; i<liste.options.length; i++){
		liste.options[i].text = Liste[i][0];
		liste.options[i].value = Liste[i][1];
	}
 
}
function verifListes(){
	
	if(nbParticipants == 1){
		alert("Aucun participant pour la manifestation n'a été saisi.")
		return false;
	}else{
		
		var erreur = 0;
		for(i=0; i< nbListes; i++){
			liste = document.getElementById('liste_'+i);
			if(liste.options[liste.selectedIndex].value == "-1")
				erreur++
		}
		if(erreur > 0){
			alert("Veuillez sélectionner pour chaque hébergement et chaque activité, les personnes concernées.");
			return false;
		}else{
			// on vérifie que pour chaque groupe de liste, la même personne n'a pas été saisie deux fois
			if(!verifGroupeListe()){
				alert("La même personne ne peut pas être sélectionnée pour une même activité.");
				return false;
			}else{
				envoyerSelection();
				return true;
				//return false;
			}
		}
	}
};

function envoyerSelection(){
	
	$.ajax({
		   type: "POST",
		   url: "majSession.php",
		   data: "action=4",
		   success: function(msg){}
		 });
	
	for(i=0; i<affects.length; i++){
	
		liste = document.getElementById('liste_'+i);
		idP = liste.options[liste.selectedIndex].value;
		
		//alert("action=3&type="+affects[i]['type']+"&id="+affects[i]['id']+"&idPersonne="+idP+"&idUnique="+affects[i]['idUnique']);
		$.ajax({
		   type: "POST",
		   url: "majSession.php",
		   async:false,
		   data: "action=3&type="+affects[i]['type']+"&id="+affects[i]['id']+"&idPersonne="+idP+"&idUnique="+affects[i]['idUnique'],
		   success: function(msg){
			
		   }
		 });
	}
	//return true;
};

function verifGroupeListe(){
	
	for(j=0; j<affects.length; j++){
		if(affects[j]['type'] == 2){
			liste = document.getElementById('liste_'+j);
			idP = liste.options[liste.selectedIndex].value;
			if(chercheMemePersonne(j, affects[j]['idUniqueV'],idP)){
				//alert("personne trouvée dans une autre liste");
				return false;
				break;
			}
		}
			
	}
	return true;
	
};
function chercheMemePersonne(index, id, idPO){
	
	
	for(h=0; h<affects.length; h++){
		if(affects[h]['type'] == 2){
			liste = document.getElementById('liste_'+h);
			idP = liste.options[liste.selectedIndex].value;
		//	alert("h "+h+" index "+index+" idO "+id+" id" +affects[h]['idUnique']+" idPo "+idPO+" idP "+idP);
			if(index != h && affects[h]['idUniqueV'] == id && idPO == idP){
				return true;
				break;
			}
		}
		
	}
	//alert("personne pas trouvée dans une autre liste");
	return false;
}