	
	
	function evenclick(element) {
		element.backuptext = $(element).text();
		nom = element.className;
		num = parseInt(nom.substring(nom.length, nom.lastIndexOf("_")+1 ));
		id = "backup_"+nom;
		backupObjs[id] = $(element).siblings("select").clone();
		$(element).siblings("select").remove();
		$(element).before('<input type="text" value="" name="'+nom+'"/>');
		$(element).siblings("input").focus();
		$(element).text('Annuler');
		return false;	
	}
	function oddclick(element) {
		nom = element.className;
		num = parseInt(nom.substring(nom.length, nom.lastIndexOf("_")+1 ));
		id = "backup_"+nom ;
		$(element).siblings("input").remove();
		$(element).text(element.backuptext)
		$(element).before(backupObjs[id]);
		return false;	
	}
	
	function togglesearchopt(obj) {
		$(obj).parent().siblings("span").toggle();
		txt = $(obj).text();
		reg = /\(\+\)/
		if (reg.test(txt)) {
			r = "open";
			txt = txt.replace(/\(\+\)/,"(-)");
		} else {
			r = "closed";
			txt = txt.replace(/\(\-\)/,"(+)");
		}
		$(obj).text(txt);
		return r;
	}
	
	
	$(function(){
// -------------------------------------------------------------------------------------
// Search
// -------------------------------------------------------------------------------------
		
	
		
		$("#r_instrument a").bind("click",function(){
		//togglesearchopt(this)
			if (togglesearchopt(this) == "closed") {
				$("#opt_instrument option:first").each(function(){ this.selected = true });
			}
			return false;
		});
		$("#r_sport a").bind("click",function(){
			if (togglesearchopt(this) == "closed") {
				$("#opt_sport option:first").each(function(){ this.selected = true });
			}
			return false;
		});
		$("#r_sexe a").bind("click",function(){
			if (togglesearchopt(this) == "closed") {
				$("#opt_sexe option:first").each(function(){ this.selected = true });
			}
			return false;
		});
		$("#r_age a").bind("click",function(){
			if (togglesearchopt(this) == "closed") {
				$("#opt_agebegin option:first").each(function(){ this.selected = true });
				$("#opt_ageend option:first").each(function(){ this.selected = true });
			}
			return false;
		});
		
		
		// ferme le popin
		$("a[@class^=close_]").bind("click",function(){
			classe = this.className;
			cible = classe.split("close_")[1];
			$("#"+cible).toggle();
			return false;
		});

		// ouvre popin
		$("#pasdecompte").bind("click",function(){
			$("#inscription").toggle();	
			return false;
		});
		
		/////Pour <=ie 6
		$("div.singleresultat").bind("mouseover",function(){
			$(this).addClass("searchover");
		});
		$("div.singleresultat").bind("mouseout",function(){
			$(this).removeClass("searchover");
		});
		
		// rendre block resultat cliquable
		$(".singleresultat").bind("click", function(){
			leid = this.id;
			leid = leid.split("id_")[1];
			document.location = "/viewfiche.php?fromsrch=1&id="+leid;
		});	
		
// -------------------------------------------------------------------------------------
// Image delete
// -------------------------------------------------------------------------------------
		$("a[@id^=imgid_]").bind("click",function(){
			strid = this.id;
			num = strid.split("imgid_")[1];
			
			$.ajax({ 
				type: "POST", 
				url: "db_image_delete.php", 
				data: "img_id="+num,
				success: function(msg){ 
					if ( msg == "true" )
						$("#imgid_"+num).parent().remove();
				},
				error: function(xhttp,msg,e) {
					alert(msg +": "+ e.name + ", "+e.message);
				} 
			});
			
			return false;
		});
// -------------------------------------------------------------------------------------
// Image swap
// -------------------------------------------------------------------------------------	
		$("#imagesdisplay .zoom").bind("click",function(){
			src_big = $("#imagesdisplay img:first").attr("src");

			part1 = src_big.substring(0,src_big.lastIndexOf("/"));
			part2 = src_big.substring(src_big.lastIndexOf("/"),src_big.length);
			newsrc_small = part1 + "/mini" + part2;
			
			source = $("img", this).attr("src");
			source = source.split("/mini/");//  upload/5/mini/0769.jpg
			id = source[0].substring(source[0].lastIndexOf("/")+1,source[0].length);
			newsource_big = source[0]+"/"+source[1];
			newhref_originale = "upload/originaux/"+id+"/"+source[1];
			$("#imagesdisplay img:first").attr("src",newsource_big)
			$("img", this).attr("src",newsrc_small);
			$("#imagesdisplay .originale").attr("href",newhref_originale);
			return false;
		})
		
		$("#imagesdisplay .originale").attr("target","_blank");
		
		
		
		
		
		
		
// -------------------------------------------------------------------------------------
// Formulaire login
// -------------------------------------------------------------------------------------		
	$("#frm_login").submit(function(){	
		error = 0;
		if ( $("#password").val() == "") { $("#password").siblings("label").addClass("error"); error++; };
		if ( $("#username").val() == "") { $("#username").siblings("label").addClass("error"); error++; };	
				
		if ( error > 0 ) {
			return false;
		} else {
			/*$("#frm_login").unbind("submit");
			$("#frm_login").submit();	*/
			return true;
		}
	});
	
// -------------------------------------------------------------------------------------
// oubli de mot de passe
// -------------------------------------------------------------------------------------		
	
	$("#oubliemdp").bind("click",function(){
		$("#oublimotdepasse").toggle();									 
	});


// -------------------------------------------------------------------------------------
// change password
// -------------------------------------------------------------------------------------	
	
	$("#changepass").submit(function(){
		$("p.error").empty()
		$("p.error").hide();
		$("#changepass label").removeClass("error");
		erreur = 0;
		erreurMSG = '';
		
		$("label:contains('*')").siblings("input").each(function(){
			if (this.value == "") {
				$(this).siblings("label").addClass("error");
				erreurMSG = "Le ou les champs marqu&eacute;s en rouge ne peuvent &ecirc;tre vide.";
				erreur++;
			}
		});
		
		if ( $("#txt_newpass").val() != $("#txt_newpass_confirm").val() ) {
			$("#txt_newpass").siblings("label").addClass('error');
			$("#txt_newpass_confirm").siblings("label").addClass('error');
			erreurMSG = "Les champs ne concordent pas.";
			erreur++;
		}
		
		if (erreur > 0 ) {
			$("p.error").append(erreurMSG);
			$("p.error").show();
			return false;
		} else {
			return true;
			//document.getElementById("changepass").submit();	
		}
	

	});
// -------------------------------------------------------------------------------------
// Formulaire d'inscription
// -------------------------------------------------------------------------------------		
		function checkempty(id) {
			if ( $("#"+id).val() == "" ) { $("#"+id).siblings("label").addClass("error"); return false; } 
			else { return true; }	
		}
		
		$("#frm_inscription").submit(function(){
			$("label",this).removeClass("error");
			$("#msg").empty();
			// Le premier call AJAX valide le captcha
			$.ajax({ 
				type: "POST", 
				url: "db_captcha.php", 
				data: "code="+$("#code").val(),
				success: function(msg){ 
					// si le captcha est bon, on valide le reste du form
					if (msg =="true") {
						erreur=0;
						
						// Code postal ou Zipcode
						codepostalreg = /^[a-zA-Z][0-9][a-zA-Z] *?[0-9][a-zA-Z][0-9]$/;
						zipcodereg = /^[0-9]*$/;
						phonereg = /^([0-9]{1})?[ -\.]*?([0-9]{3})[ -\.]*?([0-9]{3})[ -\.]*?([0-9]{4})([ #0-9]*)?$/;
						emailreg = /^[\w-_\.]+@[a-zA-Z\-_0-9]+\.[a-zA-Z0]{2,6}$/;
						
						
						$("label:contains('*')").siblings("input").each(function(){
							if (this.value == "") {
								$(this).siblings("label").addClass("error");
								erreur++;
							}
						});

						
						cp = $("#code_postal").val();
						if ( cp != "" ) {
							if ( !codepostalreg.test(cp) ) {
								if ( !zipcodereg.test(cp) ) {
									$("#code_postal").siblings("label").addClass("error");
									erreur++; 
								}
							}
						}
						
						phoneval = $("#telephone").val();						
						if ( !phonereg.test(phoneval) ) {
							$("#telephone").siblings("label").addClass("error");
							 erreur++; 
						}
						
						
						phone_autre_val = $("#telephone_autre").val();	
						if (phone_autre_val != "") {
							if ( !phonereg.test(phone_autre_val) ) {
								$("#telephone_autre").siblings("label").addClass("error");
								 erreur++; 
							}
						}
						

						email = $("#courriel").val();
						if (email != '') {
							if (!emailreg.test(email)) {
								$("#courriel").siblings("label").addClass("error");
								erreur++;
							}
						}
						if (erreur == 0) {		
							document.getElementById('frm_inscription').submit();
						//Sinon on affiche un message d'errreur
						} else {
							$("#msg").append("<span style='color:#F90000;font-weight:bold;'>Veuillez v&eacute;rifier le ou les champs marqu&eacute;(s) en rouge</span>")
							return false;
						}
						
					} else {
						//Sinon on affiche un message d'errreur
						$("label[@for=code]").addClass("error");
						$("#msg").append("<span style='color:#F90000;font-weight:bold;'>Veuillez v&eacute;rifier le ou les champs marqu&eacute;(s) en rouge</span>")
						return false;
					}
				},
				error: function(xhttp,msg,e) {
					alert(msg +": "+ e.name + ", "+e.message);
				} 
			});
			return false
		});
// -------------------------------------------------------------------------------------
// Formulaire d'ajout/modif d'une fiche
// -------------------------------------------------------------------------------------
		clones = new Array();
		clones["experience_artistique"] = $("table.experience_artistique tr:last").clone();
		clones["formation_artistique"] = $("table.formation_artistique tr:last").clone();
		clones["activies_sportives"] = $("table.activies_sportives tr:last").clone();
		clones["musique"] = $("table.musique tr:last").clone();
		clones["aptitudes_particulieres"] = $("table.aptitudes_particulieres tr:last").clone();
		clones["notes"] = $("table.notes tr:last").clone();
		
		$("input:text",clones["experience_artistique"]).val('');
		$("input:text",clones["formation_artistique"]).val('');
		$("input:text",clones["activies_sportives"]).val('');
		$("input:text",clones["musique"]).val('');
		$("input:text",clones["aptitudes_particulieres"]).val('');
		$("input:text",clones["notes"]).val('');
		
		$("option[@selected]",clones["formation_artistique"]).removeAttr("selected");
		$("option",clones["activies_sportives"]).removeAttr("selected");
		$("option",clones["musique"]).removeAttr("selected");
			
			
			
		$("p.ajoutligne a").bind("click",function() {
			var classe = $(this).attr("class").toString();
			var objCloned = $(clones[classe]).clone();

			namelast = $("table."+classe+" tr:last :input").attr("name");
			//console.log("table."+classe+" tr:last :input");
			num = parseInt(namelast.substring(namelast.length, namelast.lastIndexOf("_")+1 ));				
			num++;
			
			$(":input",objCloned).each(function(){
				nom = this.name;
				newName = nom.substr(0,nom.lastIndexOf("_")+1) + num;
				this.name = newName;
			});
			$("a[@class*=other_]",objCloned).each(function(){
				nom = this.className;	
				namelast = $("table."+classe+" tr:last :input").attr("name");
				newName = nom.substr(0,nom.lastIndexOf("_")+1) + num;
				this.className = newName;
				$(this).toggle(	function(){ evenclick(this) }, function(){ oddclick(this) });
			});
			
			$("table."+classe).append(objCloned);
			return false;
		});
		
		backupObjs = [];	
		$("a[@class*=other_]").toggle( 	function(){ evenclick(this) }, function(){ oddclick(this) });		
		
		$("a#plusphoto").bind("click", function(){
			clonedObj = $("p.photo:last").clone();	
			nom = $("label",clonedObj).attr("for");
			num = parseInt(nom.substring(nom.length, nom.lastIndexOf("_")+1 ));
			newNum = num+1;
			$("label",clonedObj).attr("for",$("label",clonedObj).attr("for").replace(num,newNum));
			$("label",clonedObj).text($("label",clonedObj).text().replace(num,newNum));
			$("input",clonedObj).attr("id",$("input",clonedObj).attr("id").replace(num,newNum));
			$("input",clonedObj).attr("name",$("input",clonedObj).attr("name").replace(num,newNum));
			$("input",clonedObj).val("");
			$(this).before(clonedObj);
			return false;
		});
		
// -------------------------------------------------------------------------------------
// User manager
// -------------------------------------------------------------------------------------
		$("a[@class^=activer_]").bind("click",function(){
			classe = this.className;
			id = classe.split("activer_")[1];
			action = $("#frm_hidden_post").attr("action") + "?accepter=" + id;
			
			$("#frm_hidden_post").attr("action",action);
			$("input[@name=u_login]").val($("#login_"+id).val());
			$("input[@name=u_password]").val($("#password_"+id).val());
			document.getElementById("frm_hidden_post").submit();
			return false;
		});
		
		
		$("a[@class^=modifier_]").toggle(
			function(){
				classe = this.className;
				id = classe.split("modifier_")[1];
				$(this).text('enregistrer les modifications');
				$("#p_"+id+" span[@class^=u_]").each(function(){
					name = this.className;
					val = $(this).text();
					if (name == "u_courriel" && val == "pas de courriel") {
						val ='';	
					}							
					$(this).after("<input type='text' value='"+val+"' name='"+name+"_"+id+"' id='"+name+"_"+id+"'/>");
					$(this).remove();
				});
				return false;
			},
			function(){
				classe = this.className;
				id = classe.split("modifier_")[1];
				action = $("#frm_hidden_post").attr("action") + "?modifier=" + id;
				$("#frm_hidden_post").attr("action",action);

				$("#frm_hidden_post input[@name=u_login]").val($("#u_login_"+id).val());
				$("#frm_hidden_post input[@name=u_password]").val($("#u_password_"+id).val());
				$("#frm_hidden_post input[@name=u_prenom]").val($("#u_prenom_"+id).val());
				$("#frm_hidden_post input[@name=u_nom]").val($("#u_nom_"+id).val());
				$("#frm_hidden_post input[@name=u_courriel]").val($("#u_courriel_"+id).val());
				$("#frm_hidden_post input[@name=u_telephone]").val($("#u_telephone_"+id).val());
				$("#frm_hidden_post input[@name=u_telephone_autre]").val($("#u_telephone_autre_"+id).val());
				$("#frm_hidden_post input[@name=u_adresse]").val($("#u_adresse_"+id).val());
				$("#frm_hidden_post input[@name=u_ville]").val($("#u_ville_"+id).val());
				$("#frm_hidden_post input[@name=u_code_postal]").val($("#u_code_postal_"+id).val());
				
				document.getElementById("frm_hidden_post").submit();	
				return false;
			}
		);
	}); // fin de DOM Ready
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	