var zIndexCourant=10000;
var prixProduits=new Array();
var prixReduitsProduits=new Array();
var seuilReductionProduits=new Array();
var nbAccredsProduits=new Array();
var nbProgsProjsProduits=new Array();
var nbProjsCommProduits=new Array();
var nbAccredsDispos=0;
var nbProgsProjsDispos=0;
var nbProjsCommDispos=0;
var identifiantsModulesParZone=new Array();
var zonesParId=new Array();
var flagSauvegarde=false;
var flagPaiementCB=false;
var nbProgs=null;


function YAP_isEmail(who) {
	var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	return(email.test(who));
}

$(document).ready(function(){
	$("#etapes li:not(:last)").click(function(){
		aMontrer=$(this).attr("rel");
		var numeroEtape=$("fieldset:visible").attr("rel");
		var reg=new RegExp("etape","g");
		var ok=false;
		numeroAMontrer=aMontrer.replace(reg,"");
		if(numeroAMontrer<=numeroEtape){
			oteInfosBulles();
			ok=true;
		}else{
			if(numeroAMontrer==(numeroEtape*1.0+1)){
				if(!flagSauvegarde){
					if(valideEtape(numeroEtape)){
						ok=true;
					}
				}else{
					ok=true;
				}
			}else{
				if(flagSauvegarde){
					ok=true;
				}
			}
		}
		if(ok){
			$("#" + aMontrer).show();
			$("fieldset[id!='" + aMontrer + "']").hide();
			$(this).siblings().removeClass("actif");
			$("#etapes li:gt(" + (numeroAMontrer*1.0-1) + ")").removeClass("fait").addClass("nonActif");
			$("#etapes li:lt(" + (numeroAMontrer*1.0-1) + ")").removeClass("nonActif").addClass("fait");
			$(this).removeClass("fait").addClass("actif");
		}

	});
	$("#modesReglement > ul").tabs();

	// On affiche l'étape 1

	$("#etape1").show();
	$("#etapes li[rel='etape1']").addClass("actif");

	$("#formInscription").submit(function(){
		if(flagSauvegarde){
			flagSauvegarde=false;
			retour=true;
		}else{
			if(flagPaiementCB){
				flagPaiementCB=false;
			}
			var nbEtapes=$("#etapes li").size();
			var retour=true;
			var valide_Etape=true;

			for(var i=1;i<=(nbEtapes-1);i++){
				valide_Etape=valideEtape(i);
				if(!valide_Etape){
					tb_remove();
					affEtape(i);
					valideEtape(i);
					return false;
				}
				retour=retour && valide_Etape;
				if(retour){
					affEtape((i+1.0));
				}else{
					tb_remove();
					return false;
				}
			}
		}
		if(retour){
			$("#formInscription").append('<input type="hidden" name="aslrfc" value="1"/>');
			return true;
		}else{
			tb_remove();
			return false;
		}
	});
});
function affEtape(idEtape){
	$("li[rel='etape" + idEtape + "']").click();
}
function ajouteInfobulle(x,y,texte,id,positionnement){
	if(!positionnement){
		var positionnement="SE";
	}
	switch(positionnement){
		case "O":
			offsetX=-10;
			offsetY=10
			break;
		default:
			offsetX=10;
			offsetY=10;
			break;
	}
	zIndexCourant=zIndexCourant-1;
	var idInfobulle="infob" + encodeID(id);
	if(!$("#" + idInfobulle).size()){
		//$("#grosConteneur").append('<p class="infoBulle" rel="' + id + '" id="' + idInfobulle + '" style="z-index:' + (zIndexCourant-1) + ';top:' + (y+10) + ';left:' + (x+10) + ';"><a name="hash' + idInfobulle + '"></a>' + texte + '<span class="fermer"><img src="medias/bouton_fermer.gif"></span></p>');
		$("body").append('<p class="infoBulle" rel="' + id + '" id="' + idInfobulle + '" style="z-index:' + (zIndexCourant-1) + ';"><a name="hash' + idInfobulle + '"></a>' + texte + '<span class="fermer"><img src="medias/bouton_fermer.gif"></span></p>');
		$("#" + idInfobulle).css("top",(y+offsetY));
		$("#" + idInfobulle).css("left",(x+offsetX));
		$("#" + idInfobulle).fadeIn("slow");
		$("#" + idInfobulle).click(function(){
			$(this).fadeOut("fast",function(){
				$(this).remove();
			});
		});
	}else{
		$("#" + idInfobulle).fadeOut("fast",function(){
			$(this).fadeIn("slow");
		});

	}
	if(!scrollToInfoBulle){
		$('html,body').animate({ scrollTop: ($("#" + idInfobulle).offset().top-25) }, 1000);
		scrollToInfoBulle=true;
	}
}

function oteInfobulle(id){
	var idInfobulle="infob" + encodeID(id);
	$("#" + idInfobulle).remove();
}
function encodeID(id){
	return(hex_md5(id));
}

function oteInfosBulles(){
	$("[id^='infob']").remove();
}


function etapeSuivante(numeroEtape){
	if(valideEtape(numeroEtape)){
		oteInfosBulles();
		affEtape(numeroEtape+1);
	}
}
function valideEtape(numeroEtape){
	messAlert="";
	oteInfosBulles();
	//return true;
	scrollToInfoBulle=false;
	passeEtapeSuivante=true;
	aAlerter=false;
	emailPasValide=false;
	numeroEtapeCourant=numeroEtape;
	$("#etape" + numeroEtape + " .required").each(function(){
		// Si le champ est disabled, pas de pb :
		if($(this).attr("disabled")){
			$(this).removeClass("enAttente");
			return;
		}
		// En fonction du type, on agit pas forc&eacute;ment pareil :
		aModifier=false;
		switch($(this)[0].nodeName){
			case "INPUT":
				switch($(this).attr("type")){
					case "text":
					case "textarea":
						if(!$(this).val()){
							aModifier=true;
							passeEtapeSuivante=false;
							/*
							$(this).addClass("enAttente");
							*/
						}else{
							if($(this).hasClass("isEmail")){
								if(!YAP_isEmail($(this).val())){
									aModifier=true;
									passeEtapeSuivante=false;
									emailPasValide=true;
									/*
									$(this).addClass("enAttente");
									*/
								}
							}
						}
						break;
					case "checkbox":
						var leNom=$(this).attr("name");
						// Si il s'agit d'une série, c'est qu'il faut en
						// cocher au moins UN
						if(!$("#etape" + numeroEtapeCourant + " input:checkbox[name='" + leNom + "']:checked").size()>=1){
							// On v&eacute;rifie quand m&ecirc;me qu'il n'existe pas
							// un champ texte rempli avec le m&ecirc;me name
							aModifier=true;
							passeEtapeSuivante=false;
							if(!$("#etape" + numeroEtapeCourant + " input:text[name='" + leNom + "'][value='']").size()==0){
								aModifier=true;
								passeEtapeSuivante=false;
							}
						}
						break;
				}
				break;
			case "SELECT":
				if(!$(this).val()){
					aModifier=true;
					passeEtapeSuivante=false;
					/*
					$(this).addClass("enAttente");
					*/
				}
				break;
			default:
				// On est pas dans un &eacute;l&eacute;ment de formulaire
				// Cela veut dire qu'il faut qu'il y ait au moins un &eacute;l&eacute;ment
				// de formulaire non nul dans les enfants
				aModifier=true;
				$(this).find("input:text,input:hidden,select,textarea").each(function(){
					var laValeur_=$(this).val();
					laValeur_= (laValeur_!="0" && laValeur_!="" && laValeur_!=null && laValeur_!=false);
					if(laValeur_){
						aModifier=false;
					}
				});
				$(this).find(":checkbox:checked,:radio:checked").each(function(){
					var laValeur_=$(this).val();
					laValeur_= (laValeur_!="0" && laValeur_!="" && laValeur_!=null && laValeur_!=false);
					if(laValeur_){
						aModifier=false;
					}
				});
					break;
		}
		if(aModifier){
			passeEtapeSuivante=false;
			if(!$(this).val() || ($(this).attr("type")=="checkbox" && $(this).val())){
				if($(this).attr("alt")){
					messAlert=messAlert + "\n" + $(this).attr("alt");
				}
				/*
				if($(this).attr("alt")){
					var offset = $(this).offset();
					if(!$(this).attr("id")){
						var laDate=new Date();
						$(this).attr("id",laDate.getTime() + Math.random());
					}
					ajouteInfobulle(offset.left,offset.top,$(this).attr("alt"),$(this).attr("id"));
					$(this).click(function(){
						oteInfobulle($(this).attr("id"));
						$(this).unbind('click');
						$(this).removeClass("enAttente");
					});
					$(this).focus(function(){
						oteInfobulle($(this).attr("id"));
						$(this).unbind('click');
						$(this).removeClass("enAttente");
					});
				}
				*/
			}else{
				if($(this).attr("alt2")){
					messAlert=messAlert + "\n" + $(this).attr("alt2");
				}
				/*
				if($(this).attr("alt2")){
					var offset = $(this).offset();
					if(!$(this).attr("id")){
						var laDate=new Date();
						$(this).attr("id",laDate.getTime() + Math.random());
					}
					ajouteInfobulle(offset.left,offset.top,$(this).attr("alt2"),$(this).attr("id"));
					$(this).click(function(){
						oteInfobulle($(this).attr("id"));
						$(this).unbind('click');
						$(this).removeClass("enAttente");
					});
					$(this).focus(function(){
						oteInfobulle($(this).attr("id"));
						$(this).unbind('click');
						$(this).removeClass("enAttente");
					});
				}
				*/
			}
		}else{
			$(this).removeClass("enAttente");
		}
		aAlerter=aModifier || aAlerter;
	});
	if(aAlerter){
		//var messAlert=messageChampsObligatoire;
		/*
		if(emailPasValide){
			messAlert+="\n"+messageEmailPasValide;
		}
		*/
		alert(messAlert);
	}
	return passeEtapeSuivante;

}

function echappeSelecteurJquery(chaine){
	var reg=new RegExp("(\\[)", "g");
	chaine=chaine.replace(reg,"\\\\[");
	var reg=new RegExp("(\\])", "g");
	chaine=chaine.replace(reg,"\\\\]");
	var reg=new RegExp("(\\.)", "g");
	chaine=chaine.replace(reg,"\\\\.");
	return chaine;
}



function sauvegarde(messageOblig){
	// Il faut au moins que le champ email du responsable
	// de l'inscription soit rempli
	tb_remove();
	var leInput=$("#" + encodeID("responsable[email]"));
	if(leInput.val()){
		flagSauvegarde=true;
		// On ajoute un champ caché pour dire qu'il s'agit d'une sauvegarde :
		$("#formInscription").append('<input type="hidden" name="flagSauvegarde" id="flagSauvegarde" value="1" />');
		$("#formInscription").submit();
		$("#flagSauvegarde").remove();
	}else{
		// On affiche l'étape "responsable de l'inscription" :
		flagSauvegarde=true;
		affEtape(leInput.parents("fieldset").attr("rel"));
		flagSauvegarde=false;
		var offset = leInput.offset();
		scrollToInfoBulle=false;
		ajouteInfobulle(offset.left,offset.top,messageOblig,leInput.attr("id"));
		leInput.click(function(){
			oteInfobulle($(this).attr("id"));
			$(this).unbind('click');
			$(this).removeClass("enAttente");
		});
	}
}


function moulineSauve(){
	oteInfosBulles();
	var email=$("#" + encodeID("responsable[email]")).val();
	if(email){
		$(".emailDestinataire").text(" (" + $("#" + encodeID("responsable[email]")).val() + ")");
	}else{
		$(".emailDestinataire").text("");
	}
}

function valideCB(){
	flagPaiementCB=true;
	// On ajoute un champ caché pour dire qu'il s'agit d'un paiement CB :
	$("#formInscription").append('<input type="hidden" name="flagPaiementCB" id="flagPaiementCB" value="1" />');
	$("#formInscription").submit();
	$("#flagPaiementCB").remove();
}


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

ACCREDITATIONS

************************************/
function updateAccredsQtes(){
	var nbAccredsRestantes=nbAccredsDispos*1.0 - $("#detailsAccreds tbody.detailAccred").size()*1.0;
	$("#nbAccredsRestantes").text(Math.max(0,nbAccredsRestantes));
	/*	S'il n'y a qu'un bloc d'accred, on masque son bouton de suppression ... */
	if($("#detailsAccreds tbody.detailAccred").size()==1){
		$(".boutonSuppAccreditation").hide();
	}else{
		$(".boutonSuppAccreditation").show();
	}
	/* Si il n'y a plus d'accreds restantes, on masque le bouton d'ajout... */
	if(nbAccredsRestantes<=0){
		$("#boutonAjoutAccreditation").hide();
	}else{
		$("#boutonAjoutAccreditation").show();
	}
	/* Si on a moins d'accreds dispos que de blocs, il faut "disabler" les blocs
	surnuméraires... */
	$("#detailsAccreds tbody.detailAccred:gt(" + (nbAccredsDispos-1) + ") :input").each(function(){
		$(this).attr("disabled","disabled");
	});
	$("#detailsAccreds tbody.detailAccred:lt(" + (nbAccredsDispos) + ") :input").each(function(){
		$(this).removeAttr("disabled");
	});

}

function ajouteAccredidationDetail(){
	var nbAccredsRestantes=nbAccredsDispos*1.0 - $("#detailsAccreds tbody.detailAccred").size()*1.0;
	if(nbAccredsRestantes>0){
		numeroAccred++;
		var leClone=$("#detailsAccreds tbody.detailAccred:first").clone(true);
		leClone.find("input[id],select[id]").each(function(){
			var reg=new RegExp("(\\]\\[([0-9]*)\\])", "g");
			$(this).attr("name",$(this).attr("name").replace(reg,"][" + numeroAccred + "]"));
			$(this).attr("id",encodeID($(this).attr("id")));
			if($(this).attr("type")!="checkbox"){
				$(this).val("");
			}
		});
		$("#detailsAccreds").append(leClone);
		/*
		var leCode=$("#detailsAccreds tbody:first").eq(0).html();
		// On remplace les id par les name :
		var reg=new RegExp("((id=\")([^\"]*\)(\")(.*)(name=\")([^\"]*\)(\"))", "g");
		leCode=leCode.replace(reg,"name=\"$7\" id=\"$7\" $5 ");

		// On remplace le chiffre de l'instance :
		var reg=new RegExp("(\\]\\[([0-9]*)\\])", "g");
		leCode="<tbody>" + leCode.replace(reg,"][" + numeroAccred + "]") + "</tbody>";

		$("#detailsAccreds").append(leCode);

		// On remplace les id par leur hash et on vide les value :
	//	$("#detailsAccreds tbody:last [name$='\\\\]\\\\[" + numeroAccred + "\\\\]']").each(function(){
		$("#detailsAccreds tbody:last :input").each(function(){
			if($(this).attr("id")){
				$(this).attr("id",encodeID($(this).attr("id")));
				$(this).val("");
			}
		});
		*/
	}
	updateAccredsQtes();

}
function supprimeAccreditation(leInput){
	if($("#detailsAccreds tbody.detailAccred").size()>1){
		$(leInput).parents("tbody").remove();
	}
	oteInfosBulles();
	updateAccredsQtes();
}



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

PROGRAMMES ET PROJETS

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

function updateProgsProjsQtes(){
	$("#nbProgsProjsDispos").text((nbProgsProjsDispos + nbProjsCommDispos));
	$("#nbProjsCommDispos").text(nbProjsCommDispos);
	remplitSelectProgs(nbProgsProjsDispos,$("#selectNbProgrammes").val());
	remplitSelectProjs(nbProgsProjsDispos,$("#selectNbProjets").val());
}

function updateProjetsQtes(nbProjs){
	var qteProgs=$("#selectNbProgrammes").val();
	var qteProjsPossible=nbProgsProjsDispos-qteProgs;
	if(nbProjs==null){
		nbProjs=$("#selectNbProjets").val();
	}
	if(nbProgs==null){
		nbProgs=$("#selectNbProgrammes").val();
	}
	remplitSelectProjs(qteProjsPossible,nbProjs);
	$('#selectNbProjets').removeAttr("disabled");
}
function remplitSelectProjs(nb,nbSelect){
	$("#selectNbProjets").empty();
	for(var i=0;i<=nb;i++){
		$("#selectNbProjets").append('<option value="' + i + '">' + i + '</option>');
	}
	$("#selectNbProjets").val(nbSelect);
}
function remplitSelectProgs(nb,nbSelect){
	$("#selectNbProgrammes").empty();
	for(var i=0;i<=nb;i++){
		$("#selectNbProgrammes").append('<option value="' + i + '">' + i + '</option>');
	}
	$("#selectNbProgrammes").val(nbSelect);
}
function reinitialiseSelectsProgsProjs(){
	$('#selectNbProjets').val(0);
	$('#selectNbProgrammes').val(0);
}
