// MAIN DE L'APPLICATION
new Event.observe(window, "load", function (evt) {
	
	// Gestion du menu à gauche
	petitMenu();
	
	// Gestion des liens à droite sous les derniers articles
	menuSupplementaire();
		
	// Gestion JS du formulaire
	if ($("ouiDif")) {
		initOuiDif();
	}
	
	if ($("ouiAutrePayeur")) {
		initAutrePayeur();
	}
	
	if ($("nomParticipant")) {
		ecouteurClavier("nomParticipant");
		ecouteurClavier("prenomParticipant");
	}
	
	if ($("entrepriseParticipant")) {
		ecouteurClavier2("entrepriseParticipant");
	}
	
	if ($("choixResponsable")) {
		initChoixResponsable();
	}
	
	if ($("choixEtablissementPayeur")) {
		initChoixEtablissementPayeur();
	}
	
	if ($("formationForm")) {
		initFormationForm("formationForm");
	}
	
	if ($("formulairePaiement")) {
		initFormationForm("formulairePaiement");
	}
	
});

function petitMenu() {
	$$(".petitMenuLien").each(function(el) {
		new Event.observe(el, 'mouseover', function(evt) {
	
			new Effect.Morph(el, {style:'padding-left: 4px;', duration:0.5});

			$$(".petitMenuLien").each(function(elAutre) {
				if (elAutre != el) {
					new Effect.Morph(elAutre, {style:'padding-left: 0px;', duration:0.5});
				}
			});
		});	
		
		new Event.observe(el, 'mouseout', function(evt) {
			$$(".petitMenuLien").each(function(elBis) {
				new Effect.Morph(elBis, {style:'padding-left: 0px;', duration:0.5} );
			});
		});
	});
}

function menuSupplementaire() {
	$$(".menuSupplementaireLien").each(function(el) {
		new Event.observe(el, 'mouseover', function(evt) {
	
			new Effect.Morph(el, {style:'padding-left: 14px;', duration:0.5});

			$$(".menuSupplementaireLien").each(function(elAutre) {
				if (elAutre != el) {
					new Effect.Morph(elAutre, {style:'padding-left: 10px;', duration:0.5});
				}
			});
		});	
		
		new Event.observe(el, 'mouseout', function(evt) {
			$$(".menuSupplementaireLien").each(function(elBis) {
				new Effect.Morph(elBis, {style:'padding-left: 10px;', duration:0.5} );
			});
		});
	});
}

function initSelectChange(nomSelect) {
	new Event.observe(nomSelect, 'change', function(evt) {
		var loader = nomSelect+"Loader";
		$(loader).show();
		if ($(nomSelect).value != "-1") {
			var num = ($(nomSelect).value).split("_");
			num = num[1];
			new Ajax.Request("xhr/xhr_formations.php", {
				parameters: "num="+num+"&nomSelect="+nomSelect,
				onSuccess: function(transport) {
					var fonction = nomSelect+"Fonction";
					$(fonction).replace(transport.responseText);
					$(loader).hide();
				},
				evalScripts : true
			});
		} else {
			var fonction = nomSelect+"Fonction";
			$(fonction).replace('<select class="txtchamps" id="activiteClientFonction" name="activiteClientFonction"><option value="-1" selected="selected">Choisir</option></select>');
			$(loader).hide();
		}
	});
}

function initChoixResponsable() {
	new Event.observe("choixResponsable", 'change', function(evt) {
		var num = ($("choixResponsable").value);
		if (num == "-1") {
			$("responsable").replace('<div id="responsable"></div>');
		} else {
			new Ajax.Request("xhr/xhr_formations_responsable.php", {
				onSuccess: function(transport) {
					$("responsable").replace(transport.responseText);
				},
				evalScripts : true
			});
		}
	});	
}

function initChoixEtablissementPayeur() {
	new Event.observe("choixEtablissementPayeur", 'change', function(evt) {
		var num = ($("choixEtablissementPayeur").value);
		if ((num == "-1")) {
			$("entreprisePayeur").replace('<div id="entreprisePayeur"></div>');
		} else if (num == "1")  {
			new Ajax.Request("xhr/xhr_formations_entreprisePayeur.php", {
				onSuccess: function(transport) {
					$("entreprisePayeur").replace(transport.responseText);
				},
				evalScripts : true
			});
		} else if (num == "opca") {
			new Ajax.Request("xhr/xhr_formations_opca.php", {
				onSuccess: function(transport) {
					$("entreprisePayeur").replace(transport.responseText);
				},
				evalScripts : true
			});
		}
	});	
}

function initOuiDif() {
	new Event.observe("ouiDif", 'click', function(evt) {
		$("heureDif").replace('<p id="heureDif">Si oui, combien d\'heures souhaitez-vous imputer sur le compte DIF ? <input class="text" type="text" name="heureDifParticipant" style="width:30px;" value="0" /></p>');
	});	
	
	new Event.observe("nonDif", 'click', function(evt) {
		$("heureDif").replace('<p id="heureDif"></p>');
	});	
	
	new Event.observe("neSaitPasDif", 'click', function(evt) {
		$("heureDif").replace('<p id="heureDif"></p>');
	});	
}

function initAutrePayeur() {
	new Event.observe("ouiAutrePayeur", 'click', function(evt) {
		$("payeur").replace('<p id="payeur" style="margin-bottom: 10px;">Si oui, lequel ? &nbsp;&nbsp;<input class="radio" type="radio" name="choixPayeur" value="opca" /> OPCA&nbsp;&nbsp;<input class="radio" type="radio" name="choixPayeur" value="autreGroupe" checked="checked" /> Autre établissement de mon groupe</p>');
	});	
	
	new Event.observe("nonAutrePayeur", 'click', function(evt) {
		$("payeur").replace('<p id="payeur"></p>');
	});	
}

function ecouteurClavier(idInput) {
	new Form.Element.Observer(idInput, 0.2, function(el) {
		var nomParticipant = $("nomParticipant").value;
		var prenomParticipant = " "+$("prenomParticipant").value;
		$("choixResponsable").replace('<select class="txtchamps" id="choixResponsable" name="choixResponsable"><option id="optionParticipant" value="-1" selected="selected">'+nomParticipant+prenomParticipant+'</option><option value="1">Nouveau contact</option></select>');
		initChoixResponsable();
	});
}

function ecouteurClavier2(idInput) {
	new Form.Element.Observer(idInput, 0.2, function(el) {
		var entrepriseParticipant = $("entrepriseParticipant").value;
		$("choixEtablissementPayeur").replace('<select class="txtchamps" id="choixEtablissementPayeur" name="choixEtablissementPayeur"><option id="optionEntrepriseParticipant" value="-1" selected="selected">'+entrepriseParticipant+'</option><option value="opca">OPCA</option><option value="1">Nouvelle entreprise</option></select>');
		initChoixEtablissementPayeur();
	});
}

function initFormationForm(idFormation) {
	new Event.observe(idFormation, 'submit', function(evt) {
		new Event.stop(evt);
		var verifFormation = true;
		var erreursFormation = "";
		
		if ($("conditionsGenerales").checked != true ) {
			erreursFormation = erreursFormation + "Veuillez accepter les conditions générales de ventes.\n"; 
			verifFormation = false;
		} 
		
		if (idFormation == "formationForm") {
		
			if ($("emailParticipant").value != $("email2Participant").value) {
				erreursFormation = erreursFormation + "La confirmation de l'e-mail ne correspond pas à l'e-mail que vous avez spécifié.";
				verifFormation = false;
			}
		}
				
		if (!verifFormation) alert(erreursFormation); else $(idFormation).submit();
	});
}






