// JavaScript Document
function aff(wnom)
{
  if(document.getElementById(wnom)){
  if(document.getElementById(wnom).style.display!="block")
  {
    document.getElementById(wnom).style.display="block";
    if(document.getElementById('b_'+wnom)){document.getElementById('b_'+wnom).className="bt_moins";};
  }else{
    document.getElementById(wnom).style.display="none";
    if(document.getElementById('b_'+wnom)){document.getElementById('b_'+wnom).className="bt_plus";};
  }}
}
function dates_seances(id_spec,datedeb,datefin)
{
  if(datedeb==datefin)
  {
    document.getElementById(id_spec).innerHTML="Le "+datedeb;
  }else{
    document.getElementById(id_spec).innerHTML="Du "+datedeb+" au "+datefin;
  }
};
function ajoute_resa(wprix,wid_tarif,diff_qte,pl_maxi,wid_seance,wid_zone,wpl_reste){
	var newqte;
	if(diff_qte==0){// qd on clique sur poubelle (panier)
		newqte=0;
	}else{ // ajoute ou soustrait de la qte
		newqte=parseInt(document.getElementById('qte_'+wid_tarif).value)+diff_qte;
	}
	if(document.getElementById("zone_"+wid_zone) && newqte>=0){
		if(parseInt(document.getElementById("zone_"+wid_zone).value)+diff_qte>wpl_reste && diff_qte>=0){ // sauf si on sousrait depuis panier
			alert("Pas assez de places disponibles");
			return;			
		}
		if(parseInt(document.getElementById("zone_"+wid_zone).value)+diff_qte<0){
			document.getElementById("zone_"+wid_zone).value=0;
		}else{
			document.getElementById("zone_"+wid_zone).value=parseInt(document.getElementById("zone_"+wid_zone).value)+diff_qte;			
		}
	}
	if(document.getElementById("seance_"+wid_seance) && document.getElementById("seance_tot_"+wid_seance) && newqte>=0){
		if(parseInt(document.getElementById("seance_"+wid_seance).value)+parseInt(document.getElementById("seance_tot_"+wid_seance).value)+diff_qte>pl_maxi ){
			alert("Vous avez atteint le nombre de place maximum pour cette seance : "+pl_maxi);
			return;			
		}
		if(parseInt(document.getElementById("seance_"+wid_seance).value)+diff_qte<0){
			document.getElementById("seance_"+wid_seance).value=0;
		}else{
			document.getElementById("seance_"+wid_seance).value=parseInt(document.getElementById("seance_"+wid_seance).value)+diff_qte;			
		}
	}	
   	if(newqte<=0){
   		newqte=0;
   		if(document.getElementById('poub_'+wid_tarif)){// on a clique sur poubelle dans panier
   			document.getElementById('poub_'+wid_tarif).value=1;
   		}
   	}
   	// tout est ok on mets a jour
    document.getElementById('qte_'+wid_tarif).value=newqte;
    document.getElementById('total_'+wid_tarif).innerHTML=newqte*parseFloat(wprix)/100;
    if(document.getElementById('total_panier')){// si on est dans la panier, on valide le formulaire
		document.formulaire.submit();
	}
}

function ajoute_abo(wprix,wid_abo,diff_qte,pl_maxi){
	var newqte;
	if(diff_qte==0){ newqte=0;
	}else{ newqte=parseInt(document.getElementById('abo_'+wid_abo).value)+diff_qte;}
	// a supprimer
	if(newqte>pl_maxi){
		alert("Vous avez atteint le nombre de place maximum");
		return;
	}
	// 
	if(newqte<=0){
    	newqte=0;
    	if(document.getElementById('nb_select_'+wid_abo)){document.getElementById('nb_select_'+wid_abo).value=0;}
    	if(document.getElementById('montant_'+wid_abo)){document.getElementById('montant_'+wid_abo).value=0;}
   		if(document.getElementById('poub_'+wid_abo)){document.getElementById('poub_'+wid_abo).value=1;
   		}
    }
   	var tab_id=new Array;
   	var tab_val=new Array;
   	var cpt=0;
   	var lib_retour="";
	var inputs = document.getElementById(wid_abo).getElementsByTagName("input");
	for (var i=0;i<inputs.length;i++){
    	var input = inputs[i];
		if(newqte==0){
	    	if(input.name.substr(0,8)=="chk_abo_"){
	    		if(!document.getElementById('total_panier')){input.checked=false}
	    	};
	    	if(input.name.substr(0,4)=="qte_"){input.value=0};
	    	if(input.name.substr(0,5)=="poub_"){input.value=1};
		}else{
			if(input.name.substr(0,8)=="chk_abo_"){
				if(input.checked){
					var id_lig_abo=input.id.substr(12,20);
					tab_id[cpt]="qte_"+id_lig_abo;
					tab_val[cpt]=newqte;
					if( parseInt(document.getElementById('seance_tot_'+document.getElementById('id_seance_'+id_lig_abo).value).value)+
						parseInt(document.getElementById('seance_'+document.getElementById('id_seance_'+id_lig_abo).value).value)+
						diff_qte>pl_maxi){
						lib_retour+="Vous avez atteint le nombre maximum de places pour cette seance";
					}
					if(document.getElementById('total_panier') && diff_qte>parseInt(document.getElementById('pl_reste_'+id_lig_abo).value)){
						lib_retour+="Pas assez de places disponibles";
					}
					if(!document.getElementById('total_panier') && newqte>parseInt(document.getElementById('pl_reste_'+id_lig_abo).value)){
						lib_retour+="Pas assez de places disponibles";
					}
					cpt++;
	    		}
	    	}
		}
	}
	if(lib_retour==""){
    	document.getElementById('abo_'+wid_abo).value=newqte;
    	if(document.getElementById('total_'+wid_abo)){document.getElementById('total_'+wid_abo).innerHTML=newqte*parseFloat(wprix)/100;}
		for(var i=0;i<tab_id.length;i++){
			document.getElementById(tab_id[i]).value=tab_val[i];
		}
		if(document.getElementById('total_panier')){document.formulaire.submit()}
    }else{
    	alert(lib_retour);
    }
}
function check_lig_abo(wid_abo,wid_ligabo,wprix,pl_maxi){
	if(parseInt(document.getElementById('abo_'+wid_abo).value)==0){
		document.getElementById('chk_lig_abo_'+wid_ligabo).checked=false;
		ajoute_abo(0, wid_abo, 1, pl_maxi);
//		alert('Vous devez ajouter au moins un abonnement');
		return;
	}
	
	if(document.getElementById('chk_lig_abo_'+wid_ligabo).checked==true){
		if(parseInt(document.getElementById('nb_select_'+wid_abo).value)+1 > parseInt(document.getElementById('maxi_'+wid_abo).value)){
			document.getElementById('chk_lig_abo_'+wid_ligabo).checked=false;
			alert('Vous avez déjà sélectionné le nombre maximum de places');
			return;
		}		
		var seance_act = document.getElementById('id_seance_'+wid_ligabo).value;		// id de la seance du tarif en cours
		var inputs = document.getElementById(wid_abo).getElementsByTagName("input");
		for (var i=0;i<inputs.length;i++){												// parcourir la liste des entrées à la recherche de libellés pour la même séance et formule
			var input = inputs[i];
			if((input.name.substr(0,8)=="chk_abo_") && input.checked) {
				var id_lig_abo=input.id.substr(12,20);
				if((id_lig_abo != wid_ligabo) && (document.getElementById('id_seance_'+id_lig_abo).value==seance_act)) {
					document.getElementById('chk_lig_abo_'+wid_ligabo).checked=false;
					alert("Vous ne pouvez choisir qu'un seul libellé par séance");
					return;
				}
			}
		}				
		var increment=1;			// init sur une seule place par séance
		if(document.getElementById('increment_'+wid_abo)){increment=parseInt(document.getElementById('increment_'+wid_abo).value)};
		if(parseInt(document.getElementById('seance_tot_'+document.getElementById('id_seance_'+wid_ligabo).value).value)+parseInt(document.getElementById('abo_'+wid_abo).value)>pl_maxi){
			document.getElementById('chk_lig_abo_'+wid_ligabo).checked=false;
			alert("Vous avez atteint le nombre maximum de places pour cette seance");
			return;
		} 
		document.getElementById('qte_'+wid_ligabo).value=document.getElementById('abo_'+wid_abo).value;
		document.getElementById('nb_select_'+wid_abo).value=parseInt(document.getElementById('nb_select_'+wid_abo).value)+1;
		document.getElementById('montant_'+wid_abo).value=parseInt(document.getElementById('montant_'+wid_abo).value)+(wprix*increment);
	}else{
		document.getElementById('qte_'+wid_ligabo).value=0;
		document.getElementById('nb_select_'+wid_abo).value=parseInt(document.getElementById('nb_select_'+wid_abo).value)-1;
		document.getElementById('montant_'+wid_abo).value=parseInt(document.getElementById('montant_'+wid_abo).value)-(wprix*increment);
		if (parseInt(document.getElementById('nb_select_'+wid_abo).value)==0) {
			ajoute_abo(0, wid_abo, -parseInt(document.getElementById('abo_'+wid_abo).value), pl_maxi);
		}
	}
/*	var msg="reste="+document.getElementById('pl_reste_'+wid_ligabo).value;
	msg+=" nb_formules="+document.getElementById('abo_'+wid_abo).value;
	msg+=" nb_select="+document.getElementById('nb_select_'+wid_abo).value;
	msg+=" cumul="+document.getElementById('montant_'+wid_abo).value;
	msg+=" qtt_ligne="+document.getElementById('qte_'+wid_ligabo).value;
	msg+=" increment="+increment;
	alert(msg);
*/
}
function pour_soumission(wid_abo) {
	var mini = parseInt(document.getElementById('mini_'+wid_abo).value);
	if(mini > parseInt(document.getElementById('nb_select_'+wid_abo).value)) {
		alert("vous devez choisir un minimum de "+mini+" séances");
		return;
	}
   	var tab_seances=new Array;
   	var tab_check=new Array;
	var tab_idlig=new Array;
   	var count=0;
	var inputs = document.getElementById(wid_abo).getElementsByTagName("input");
	for (var i=0;i<inputs.length;i++){
    	var input = inputs[i];
    	if(input.name.substr(0,8)=="chk_abo_") {
			var id_lig_abo = input.id.substr(12,20);
			var idseance = document.getElementById('id_seance_'+id_lig_abo).value;
			
			var j = 0;
			while ((j < count) && (tab_seances[j] != idseance)) j++;
			if (j == count) {
				tab_seances[j] = idseance;
				tab_idlig[j] = id_lig_abo;
				tab_check[j] = input.checked;
				count+=1;
			}else{
				if (input.checked) tab_check[j] = true;
			}
		}
	}
	for (var i=0;i<inputs.length;i++){
    	var input = inputs[i];
    	if((input.name.substr(0,7)=="oblige_") && (input.value == true)) {
			var idseance = input.name.substr(7,20);
			var j = 0;
			while ((j < count) && (tab_seances[j] != idseance)) j++;
			if (j == count) {
				alert("la formule d'abonnement est mal configurée, veuillez nous en excuser : "+count+" idseance "+idseance);
				return;
			}else{
				if(!tab_check[j]) {
					var resa = document.getElementsByName('resa_'+tab_idlig[j])[0].value;
					libelles = resa.split('|');
					alert("la seance "+libelles[0]+" du "+libelles[1]+ " doit obligatoirement faire partie de l'abonnement");
					return;
				}
			}
		}
	}
	var inputs = document.getElementsByName('formulaire'+wid_abo);
	inputs[0].submit();
}
function valide_inscription(){
    var re_email = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/ ;
    t_email=re_email.test(document.getElementById("email").value);
	var lib_retour="";
	if(document.getElementById('email').value==""){lib_retour+=" - Email \n";};
	if(document.getElementById('motdepasse').value==""){lib_retour+=" - Mot de passe \n";};
	if(!document.getElementById('civilite1').checked && !document.getElementById('civilite2').checked && !document.getElementById('civilite3').checked){lib_retour+=" - Civilité \n";};
	if(document.getElementById('nom').value==""){lib_retour+=" - Nom \n";};
	if(document.getElementById('prenom').value==""){lib_retour+=" - Prénom \n";};
	if(document.getElementById('adresse1').value==""){lib_retour+=" - Adresse \n";};
	if(document.getElementById('cdp').value==""){lib_retour+=" - Code postal \n";};
	if(document.getElementById('ville').value==""){lib_retour+=" - ville \n";};
	if(document.getElementById('portable').value=="" && document.getElementById('fixe').value==""){lib_retour+=" - Téléphone portable ou fixe \n"}
	if(document.getElementById('memeadr').checked==false){
		if(!document.getElementById('civiliteb1').checked && !document.getElementById('civiliteb2').checked && !document.getElementById('civiliteb3').checked){lib_retour+=" - Livraison : Civilité \n";};
		if(document.getElementById('nomb').value==""){lib_retour+=" - Livraison : Nom \n";};
		if(document.getElementById('prenomb').value==""){lib_retour+=" - Livraison : Prénom \n";};
		if(document.getElementById('adresse1b').value==""){lib_retour+=" - Livraison : Adresse \n";};
		if(document.getElementById('cdpb').value==""){lib_retour+=" - Livraison : Code postal \n";};
		if(document.getElementById('villeb').value==""){lib_retour+=" - Livraison : ville \n";};		
		if(document.getElementById('portableb').value=="" && document.getElementById('fixeb').value==""){lib_retour+=" - Livraison : Téléphone portable ou fixe \n"}
	}
	if(lib_retour!=""){lib_retour="Vous n'avez pas renseigné les champs suivants :\n"+lib_retour;};
	if(!t_email){lib_retour+="\nL'email n'est pas valide\n";};
	if(lib_retour!=""){
		alert(lib_retour);
		return;
	}else{
		if(document.getElementById('motdepasse').value!=document.getElementById('confirme_motdepasse').value){lib_retour+="\nLes champs Mot de passe et \nConfirmation de mot de passe \nne sont pas identiques\n";};
		if(document.getElementById('email').value!=document.getElementById('confirme_email').value){lib_retour+="\nLes champs Email et Confirmation Email \nne sont pas identiques";};
		if(lib_retour!=""){
			alert(lib_retour);
			return;
		}else{
			if(!document.getElementById('info_exact').checked){lib_retour+="\nVous n'avez pas confirmé l'éxactitude des informations\n";};
			if(!document.getElementById('accept_cgv').checked){lib_retour+="\nVous n'avez pas accepté les conditions générales de vente\n";};
			if(lib_retour!=""){
				alert(lib_retour);
				return;
			}else{
				document.inscription.submit();
			};
		};
	};
}
function ajoute_select(combo,cpt,texte,valeur){
	document.formulaire.elements[combo].options.length=parseInt(cpt)+1;
	document.formulaire.elements[combo].options[cpt].text=texte;
	document.formulaire.elements[combo].options[cpt].value=valeur;
}
function plus_infos(wlieu,wchemin,widseance){
    var hauteur=600;
    var largeur=1000;
    var top=(screen.height-hauteur)/2;
    var left=(screen.width-largeur)/2;
    window.open(wchemin+'infos_salle.php?lieu='+wlieu+'&seance='+widseance,'informations','menubar=no, scrollbars=yes, top='+top+', left='+left+', width='+largeur+', height='+hauteur);
}
function placement(wlieu,wchemin,widseance,widzone,widsoc){
    var hauteur=650;
    var largeur=1000;
    var top=(screen.height-hauteur)/2;
    var left=(screen.width-largeur)/2;
    window.open(wchemin+'placement.php?lieu='+wlieu+'&idseance='+widseance+'&idzone='+widzone+'&idsoc='+widsoc,'informations','menubar=no, scrollbars=yes, top='+top+', left='+left+', width='+largeur+', height='+hauteur);
}
function cgv(wchemin){
    var hauteur=600;
    var largeur=1000;
    var top=(screen.height-hauteur)/2;
    var left=(screen.width-largeur)/2;
    window.open(wchemin+'cgv.php','cgv','menubar=no, scrollbars=yes, top='+top+', left='+left+', width='+largeur+', height='+hauteur);
}
function upload_image(rep,champ,img,zone)
{
    var hauteur=660;
    var largeur=600;
    var top=(screen.height-hauteur)/2;
    var left=(screen.width-largeur)/2;
    window.open('choix_image.php?rep='+rep+'&champ='+champ+'&img='+img+'&zone='+zone,'cp','location=no,top='+top+',left='+left+',width='+largeur+', height='+hauteur+', status=no,menubar=no, scrollbars=yes');
}
function etape_panier(wetape)
{
	var lib_retour="";
	if(wetape=="panier"){
		document.getElementById("etape_panier").value="formulaire";
	}else if(wetape=="formulaire"){
		if(document.getElementById('nom').value==""){lib_retour+=" - Nom \n";};
		if(document.getElementById('prenom').value==""){lib_retour+=" - Prénom \n";};
		if(document.getElementById('adresse1').value==""){lib_retour+=" - Adresse \n";};
		if(document.getElementById('cdp').value==""){lib_retour+=" - Code postal \n";};
		if(document.getElementById('ville').value==""){lib_retour+=" - ville \n";};
		if(document.getElementById('portable').value=="" && document.getElementById('fixe').value==""){lib_retour+=" - Téléphone portable ou fixe \n"}
//		if(document.getElementById('portable').value==""){lib_retour+=" - portable \n";};
		if(document.getElementById('memeadr').checked==false){
			if(document.getElementById('nomb').value==""){lib_retour+=" - Livraison : Nom \n";};
			if(document.getElementById('prenomb').value==""){lib_retour+=" - Livraison : Prénom \n";};
			if(document.getElementById('adresse1b').value==""){lib_retour+=" - Livraison : Adresse \n";};
			if(document.getElementById('cdpb').value==""){lib_retour+=" - Livraison : Code postal \n";};
			if(document.getElementById('villeb').value==""){lib_retour+=" - Livraison : ville \n";};		
		}
		if(lib_retour!=""){lib_retour="Vous n'avez pas renseigné les champs suivants :\n"+lib_retour;};
		if(!document.getElementById('info_exact').checked){lib_retour+="\nVous n'avez pas confirmé l'éxactitude des informations\n";};
		if(!document.getElementById('accept_cgv').checked){lib_retour+="\nVous n'avez pas accepté les conditions générales de vente\n";};
		if(lib_retour!=""){
			alert(lib_retour);
			return;
		}else{
			document.getElementById("etape_panier").value="paiement";
		}
	}else if(wetape=="paiement"){
		var lib_paiement="\nVous n'avez pas choisi de mode de paiement\n";
		var lib_reception="\nVous n'avez pas choisi de mode de reception\n";
		
		if(document.getElementById("cb")){if(document.getElementById("cb").checked){lib_paiement=""}};
		if(document.getElementById("cheque")){if(document.getElementById("cheque").checked){lib_paiement=""}};
		
		if(document.getElementById("recommande")){if(document.getElementById("recommande").checked){lib_reception=""}};
		if(document.getElementById("courrier_simple")){if(document.getElementById("courrier_simple").checked){lib_reception=""}};
		if(document.getElementById("eticket")){if(document.getElementById("eticket").checked){lib_reception=""}};
		if(document.getElementById("retrait")){if(document.getElementById("retrait").checked){lib_reception=""}};
		if(document.getElementById("reception_autre")){if(document.getElementById("reception_autre").checked){lib_reception=""}};
		
		lib_retour=lib_paiement+lib_reception;
		if(lib_retour!=""){
			alert(lib_retour);
			return;
		}
		document.getElementById("etape_panier").value="confirmation";
	}
	document.formulaire.submit();
}
function frais_paiement(wtotalcommande){
	if(document.getElementById('eticket')){
	if(document.getElementById('eticket').checked){
		if(!document.getElementById('cb').checked){
				document.getElementById('eticket').checked=false;
				alert("Les E-tickets ne sont disponibles qu'avec un règlement par CB");
		};
	};
	};
	if(document.getElementById('cb') && document.getElementById('fraisb') && document.getElementById('frais_bancaires')){
	if(document.getElementById('cb').checked){
		wtotalcommande=parseFloat(wtotalcommande)+parseFloat(document.getElementById('fraisb').value);
		document.getElementById('frais_bancaires').value=1;
	}else{
		document.getElementById('frais_bancaires').value=0;
	};
	};
	if(document.getElementById('recommande') && document.getElementById('fraisenvoi') && document.getElementById('frais_recommande')){
	if(document.getElementById('recommande').checked){
		wtotalcommande=parseFloat(wtotalcommande)+parseFloat(document.getElementById('fraisenvoi').value);
		document.getElementById('frais_recommande').value=1;
	}else{
		document.getElementById('frais_recommande').value=0;
	};
	};
	if(document.getElementById('reception_autre') && document.getElementById('frais_reception_autre') && document.getElementById('frais_reception_autre_cmd')){
		if(document.getElementById('reception_autre').checked){
			wtotalcommande=parseFloat(wtotalcommande)+parseFloat(document.getElementById('frais_reception_autre').value);
			document.getElementById('frais_reception_autre_cmd').value=1;
		}else{
			document.getElementById('frais_reception_autre_cmd').value=0;
		};
	};
	document.getElementById('totalcommande').value=wtotalcommande;
	document.getElementById('txt_totalcommande').innerHTML=wtotalcommande/100;
}
function valide_connexion(){
	var lib_retour="";
	if(document.getElementById('connexion_email').value==""){lib_retour+="vous n'avez pas renseigné votre email\n";};
	if(document.getElementById('connexion_mdp').value==""){lib_retour+="vous n'avez pas renseigné votre mot de passe\n";};
	if(lib_retour!=""){
		alert(lib_retour);
		return;
	}else{
		document.connexion.submit();
	}
}
function valide_connexion_formulaire(){
	var lib_retour="";
	if(document.getElementById('connexion_email2').value==""){lib_retour+="vous n'avez pas renseigné votre email\n";};
	if(document.getElementById('connexion_mdp2').value==""){lib_retour+="vous n'avez pas renseigné votre mot de passe\n";};
	if(lib_retour!=""){
		alert(lib_retour);
		return;
	}else{
		document.connexion2.submit();
	}
}
function charge_frame(wframe,wsrc){
	document.getElementById(wframe).src=wsrc;
}
function choix_billet_electonique(choix){
	if(document.getElementById('eticket').checked && document.getElementById('contremarque').checked){
		if(choix=='eticket'){
			document.getElementById('contremarque').checked=false;
		}else{
			document.getElementById('eticket').checked=false;
		}
	}
}
function adresse_b(){
	if(document.getElementById('memeadr').checked){
		document.getElementById('adresse_b').style.display="none";
	}else{
		document.getElementById('adresse_b').style.display="block";		
	}
}
function valide_newsletter(){
    var re_email = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/ ;
    t_email=re_email.test(document.getElementById("email").value);
	var lib_retour="";
	if(document.getElementById('email').value==""){lib_retour+=" - Email \n";};
	if(document.getElementById('alerte_sms').checked && document.getElementById('portable').value==""){lib_retour+=" - Téléphone portable \n";};
	if(document.getElementById('nom').value!="" && !document.getElementById('civilite1').checked && !document.getElementById('civilite2').checked && !document.getElementById('civilite3').checked){lib_retour+=" - Civilité \n";};
	if(document.getElementById('cdp').value==""){lib_retour+=" - Code postal \n";};
	//if(document.getElementById('nom').value==""){lib_retour+=" - Nom \n";};
	//if(document.getElementById('prenom').value==""){lib_retour+=" - Prénom \n";};
	if(lib_retour!=""){lib_retour="Vous n'avez pas renseigné les champs suivants :\n"+lib_retour;};
	if(!t_email && document.getElementById('email').value==""){lib_retour+="\nL'email n'est pas valide\n";};
	if(lib_retour!=""){
		alert(lib_retour);
		return;
	}else{
		document.newsletter.submit();
	};
}

