﻿// ***********************************************************
// 		Fonction Javascript Commune Site Transvolcanique
// ***********************************************************


	// ************************************************
	// Lancement affichage diaporama
			function diaporama(rep)
			{
				url_diapo = "/media/photos/"+rep+"/index.php"
				window.open(url_diapo,"diaporama","adress=0, menuBar=0,toolbar=0, location=0, directories=0, status=0, scrollbars=1, copyhistory=0,  resizable=1, width=500, height=500")
			}
		
	
	//***********************************************
	//ouverture page (dans la même fenetre)
		function open_page(url_page)
		{
			window.location.href=url_page;
		}
	
	
	// ********************************************************
	// Javascript pour se déconnecter du compte
		function deconnexion()
		{
			url_deconnexion = "/inscription/index.php?mode=LOGOUT"
			window.location.href= url_deconnexion
		}



//  *********************************************************** 
// 	Javascript inscription Transvolcanique
//  ***********************************************************

	
//***********************************************
// switch entre l'affichage des 2 formulaires 
// Participant et Accompagnateur
	function view_form(click_btn)
	{
		if(click_btn == "type_1")
		{
			$('#form_accompagnateur').fadeOut('slow', function() 
				{update_prix(); select_club(); $('#form_participant').fadeIn('slow');}
			);
		}
		
		else if(click_btn == "type_2")
		{
			$('#form_participant').fadeOut('slow', function()
				{update_prix(); select_club(); $('#form_accompagnateur').fadeIn('slow');}
			);
		}
		
	}
	
// ********************************************************
// formatage prix avec affichage 2 chiffre aprés la virgule xx,xx€ 
	function formatage_prix(str_prix)
	{
		str_new = new String(str_prix);
		
		// Ajoute les 2 chiffres aprés la virgule
			pos = str_new.indexOf(".")
			if(pos>=0)
			{
				nb_num = str_new.length - pos - 1;
				for (z=0; z<nb_num; z++)
				{str_new = str_new+"0";}
			}
			else{str_new = str_new+".00";}
			
		// Remplace le "." par une ","
			str_new = str_new.replace("\.","\,");
		
		// Ajoute unité euros 
			str_new = str_new+" €";
			
		return str_new;
	}
			
// ********************************************************
// retourne le prix de l'id de l'élément passé en paramètre
	function select_prix(nom_elmt,elmt,licencie)
	{
		
		// Definition Prix élément
			if(elmt == null){prix_elmt=0;}
			else
			{
				info_elmt = elmt.id.split("-")
				prix_l = info_elmt[1];
				prix_nl = info_elmt[2];
			
				if(!elmt.checked){prix_elmt=0}
				else if(licencie){prix_elmt = Number(prix_l)}
				else{prix_elmt = Number(prix_nl)}
			}
				
		// Mise à jour prix dans la liste 
			html_prix = formatage_prix(prix_elmt)
			div_prix = document.getElementById(nom_elmt+"_prix")
			div_prix.innerHTML = html_prix;
		
		// retourne le prix de l'élément	
			return (prix_elmt);
	}


//***********************************************
// Mise à jour des prix du formulaire selctionné
	function update_prix()
	{
		form_usr = document.getElementById("form_fiche_participant");
		btn = document.getElementById("btn_valider_form_fiche_inscription");
		
		// Definition Type participation
			participation_select = null
			participation_select = recup_val_radio(form_usr.type_participation)

		// ****************************************************
		// Gestion calcul prix >> formulaire Participant
		if(participation_select==1)
		{
			// Afficahge bouton valier
				btn.style.display="block";
			
			// Definition Type licence
				licence_select = form_usr.participant_licence
				licence_val = licence_select.value;
				if(licence_val=="0" || licence_val=="1" || licence_val==""){licencie=false}else{licencie=true}

			// recup element parcours selectionne 
				liste_parcours = form_usr.parcours
				parcour_select = null
				for(x=0; x<liste_parcours.length; x++)
				{if(liste_parcours[x].checked){parcour_select=liste_parcours[x];}}
				
			
			// Calcul prix de chaque element
				prix_parcours = select_prix("parcours",parcour_select,licencie);
				prix_petit_dej = select_prix("petit_dejeuner_participant",form_usr.petit_dejeuner_participant,licencie);
				prix_hebergement = select_prix("hebergement_participant",form_usr.hebergement_participant,licencie);
			
			// calcul prix total
				prix_total = prix_parcours+prix_petit_dej+prix_hebergement;
			
			// Affichage resultat 
				div_prix_total = document.getElementById("prix_total_participant")
				div_prix_total.innerHTML =  formatage_prix(prix_total);
		}
		
		// ****************************************************
		// Gestion calcul prix >> formulaire Accompagnateur
		else if(participation_select==2)
		{
				licencie=false;
				
			// Afficahge bouton valier
				btn.style.display="block";
				
			// Calcul prix de chaque element
				prix_petit_dej = select_prix("petit_dejeuner_accompagnateur",form_usr.petit_dejeuner_accompagnateur,licencie);
				prix_repas_samedi = select_prix("repas_samedi",form_usr.repas_samedi,licencie);
				prix_repas_dimanche = select_prix("repas_dimanche",form_usr.repas_dimanche,licencie);
				prix_hebergement = select_prix("hebergement_accompagnateur",form_usr.hebergement_accompagnateur,licencie);
			
			// calcul prix total	
				prix_total = prix_petit_dej+prix_repas_samedi+prix_repas_dimanche+prix_hebergement;
			
			// Affichage resultat 
				div_prix_total = document.getElementById("prix_total_accompagnateur")
				div_prix_total.innerHTML =  formatage_prix(prix_total);
		}
		
		// ****************************************************
		// pas de type participation selectionné >> masque btn valider
		else
		{			
			btn.style.display="none";
		}
			

	}
	
// ********************************************************
// Affiche ou maque la zone club
	function select_club()
	{
		lst = document.getElementById("lst_participant_licence");
		str_val = lst.value
		z_club = document.getElementById("zone_club");
		
		// >> Masque zone club
		if(str_val=="0" || str_val=="1" ||  str_val=="")
		{
			z_club.style.display="none";
			chp_club = document.getElementById("chp_participant_club");
			// chp_club.value =""
		}
		
		// >> affiche zone club
		else
		{z_club.style.display="inline";}
		
		// Lancement calcul prix participation
		update_prix()
		
	}


//**************************************************************************************
//**************************************************************************************


	// ********************************************************
	// Test email retourne TRUE si bien formaté FALSE sinon
		function test_email(str_email)
		{

			// formatage email
				email = str_email.replace(/^\s+/g,'').replace(/\s+$/g,'') 
				info_email = email.split("@")
			

			// test présence d'un seul caractère "@" et d'au moins 1 "."
				if(info_email.length != 2){return(false);}
			
			// recup info email
				nom = info_email[0];
				domaine_email = info_email[1];
				dom_ext = domaine_email.substr(domaine_email.lastIndexOf(".")+1);
				dom_name = domaine_email.substr(0,domaine_email.lastIndexOf("."));

			// test si il y a au moins 1 caractère avant le "@"
				if(nom.length < 1){return(false);}

			// test si il y a au moins 2 caractère aprés le dernier "."
				if(dom_ext.length < 2){return(false);}
			
			// test si il y a au moins 1 caractère dans le nom de domaine
				if(dom_name.length < 1){return(false);}
			
				
				
			// Si toutes les autres vérification OK alors email OK
				return(true);
		}
					
	// ********************************************************
	// Test valeur textfield retourne TRUE si OK FALSE sinon
			function test_tf(tf)
			{
				val_tf = tf.value;
				val_verif = val_tf.replace(/^\s+/g,'').replace(/\s+$/g,'') 
				if(val_verif!=""){return(true);}
				else{return(false);}
			}
				
	// ********************************************************
	// Test valeur validite date anniversaire
			function test_date_anniv(jour,mois,annee)
			{
				date_OK = true;
				
				// Recup annee actuelle
				dt_tmp = new Date(); 
				annee_max = dt_tmp.getFullYear();
				
				if(jour<=0 || jour >31){date_OK=false;}
				if(mois<=0 || mois >12){date_OK=false;}
				if(annee<=1900 || annee > annee_max){date_OK=false;}
				
				if(isNaN(jour) || isNaN(mois) || isNaN(annee)){date_OK=false; alert("NAN")}
				
				return date_OK;
			}
			
	// ********************************************************
	// Test Age participant
		function test_age(jour,mois,annee)
		{
			// Recup annee maxi >> 16 ans minimum
			dt_tmp = new Date(); 
			annee_max = dt_tmp.getFullYear();
			annee_max = annee_max-16;
			
			
			if(annee > annee_max){age_OK = false;}
			else{age_OK = true;}
			
			return age_OK;
		}
		
			
	// ********************************************************
	// retourne la valeur selctionné du bouton radio passé en prm
			function recup_val_radio(rb_list)
			{
				nb_radio = rb_list.length
				for(x=0; x< nb_radio; x++)
				{
					btn_radio = rb_list[x];
					if(btn_radio.checked){return(btn_radio.value);};
				}
				return(false);
			}


	// *************************************************
	// Affichage / Masque Formulaire Info Compte
		function show_hidde_form(prm)
		{
			// init variables
			ctn = document.getElementById("zone_info")
			mode = "";
			
			// Switch automatique Open/Close si pas de parametre fourni
			if(prm=="")
			{
				if(ctn.style.display=="none"){mode = "open"}
				else{mode = "close"}
			}
			
			// Sinon le parametre est prioritaure >> Frocer ouverture ou fermeture
			else{mode = prm;}
			
			// affiche ou masque le contenu
			if(mode =="open")
			{
				//ctn.style.display="block";
				$('#zone_info').slideDown('slow')
			}
			else
			{
				// ctn.style.display="none";
				$('#zone_info').slideUp('slow')
			}
		}	
	
	
	// **************************************
	// Affichage lightbox Formulaire participant
		function open_form_participant(id_compte, num_participant)
		{
			// Definition URL lightbox ajax
			url_form_participant ="/inscription/scripts/fiche_participant.php?id_compte="+id_compte+"&id_participant="+num_participant
			
			// Creation Lightbox
			$("#LB_form_participiant").fancybox({
				'titleShow'		: 'false',
				'transitionIn'		: 'fade',
				'transitionOut'		: 'fade',
				'overlayOpacity'	: '0.75',
				'overlayColor'		: '#000000',
				'type'						: 'ajax',
				'href'						: url_form_participant
			});	
				
			// lansement affichage lightbox
			$('#LB_form_participiant').trigger('click');
		}
		
	
	// **************************************
	//  Affichage lightbox Supression participant
		var id_participant_supprime = null;
		function supression_participant(id_compte, id_participant, lib_participant)
		{
			id_participant_supprime = id_participant
			id_compte_participant_supprime = id_compte
			// Mise à jour ID compte dans lightbox
			ctn = document.getElementById("str_participant");
			ctn.innerHTML = lib_participant;
			
			// lansement affichage lightbox
			$('#popup_confirm_supprimer').trigger('click');
		}
	
	// **************************************
	// Script Valide Form Supression
		function valide_supression_participant()
		{
			// Creation url supression
			url_suppression = "?updateBDD=SUPPRESSION_PARTICIPANT&id_compte="+id_compte_participant_supprime+"&id_participant="+id_participant_supprime
			
			// Validation formualaire
			document.location.href=url_suppression
		}
		
		
		
	
	//**************************************************************************************
	// Validation général du formulaire participant
	
		function valide_form_fiche_participant()
		{
			frm = document.getElementById("form_fiche_participant");
			
			// Test si tous les champs obligatoire sont rempli correctement // user_id
			if(	test_tf(frm.participant_nom)== false || test_tf(frm.participant_prenom)== false ||	test_tf(frm.participant_sexe)== false 
					||	test_tf(frm.participant_tel)== false ||	test_tf(frm.participant_adresse)== false ||	test_tf(frm.participant_cp)== false  
					||	test_tf(frm.participant_ville)== false ||	test_tf(frm.participant_pays)== false )
					{alert("Merci renseigner TOUS les champs obligatoires pour pouvoir enregistrer les informations");}
			else
			{
				type_participation = recup_val_radio(frm.type_participation);
				formulaire_OK = false;
				// ********************************************
				// Validation formulaire PARTICIPANT
				if(type_participation == 1)
				{
					// test Format Date Anniversaire
					if(!test_date_anniv(frm.date_jour.value, frm.date_mois.value, frm.date_annee.value))
					{alert("La Date de naissance n'est pas renseignée correctement.\nExemple : 25/04/1980");}
					
					// test Age Participant
					else if(!test_age(frm.date_jour.value, frm.date_mois.value, frm.date_annee.value))
					{alert("Rappel la Transvolcanique est interdite aux participants agés de moins de 16 ans.");}
					
					// Verification que le nom du club est bien inscrit si licence selctionné
					else if(frm.participant_licence.value!=1 && test_tf(frm.participant_club)== false)
					{alert("Veuillez indiquer le Club dans lequel la personne est licenciée \nou choisir \"Non Licencié\" si elle n'apparatient pas à un club.");}
					
					// Verification qu'au moins 1 type de parcours est sélectionné
					else if(recup_val_radio(frm.parcours)==false)
					{alert("Veuillez sélectionner l'un des \"Types de parcours\"\n ou sinon inscrivez cette personne en tant que \"Accopagnateur\" ")}
					
					// Validation formulaire
					else{formulaire_OK = true;}
								
	
				}
				
				// ********************************************
				// Validation formulaire ACCOMPAGNATEUR
				else if(type_participation == 2)
				{
					if(frm.petit_dejeuner_accompagnateur.checked || frm.repas_samedi.checked || frm.repas_dimanche.checked || frm.hebergement_accompagnateur.checked )
					{formulaire_OK = true;}
					else{alert("Veuillez sélectionner au moins une prestations dans la liste");}
				}
				
				// Validation formulaire
				if(formulaire_OK){frm.submit();}
			}
		}
	
		
		
	//**************************************************************************************
	// Validation général du formulaire de compte transvolcanique
		function valide_form_compte()
		{
			frm = document.getElementById('form_compte')
			
			// Test si tous les champs obligatoire sont rempli correctement // user_id
			if(	test_tf(frm.nom)== false || test_tf(frm.prenom)== false ||	test_tf(frm.tel)== false 
					||	test_tf(frm.email)== false ||	test_tf(frm.adresse)== false ||	test_tf(frm.cp)== false  
					||	test_tf(frm.ville)== false ||	test_tf(frm.pays)== false ||	test_tf(frm.password)== false ||	test_tf(frm.password_verif)== false )
					{alert("Merci renseigner TOUS les champs obligatoires pour pouvoir enregistrer les informations");}
			else
			{
				// Test mot de passe
				if(frm.password.value != frm.password_verif.value)
				{alert("votre 'mot de passe' et le mot de passe pas confirmation sont différents\n Veuillez inscrire des mots de passe identiques dans ces 2 champs")} 
				
				// Test email 
				else if(!test_email(frm.email.value))
				{alert("L'adresse email est incorrecte");}
				
				// si tout est OK >> Validation formulaire
				else
				{frm.submit();}
			}
		}

		
					




	

