// JavaScript Document
function checkAll(vclass) {
	
	if (document.getElementById('coche'+vclass).checked) {
		$('.'+vclass).attr('checked',true);	
	}
	else {
		$('.'+vclass).attr('checked',false);	
	}
}

function derouler(div) {
	
	if ($("#deroule"+div).is(':hidden')) {
		$("#lien-"+div).addClass('deroules_on').removeClass('deroules_off');
		$("#deroule"+div).slideDown("slow");
	} else {
		$("#lien-"+div).removeClass('deroules_on').addClass('deroules_off');
		$("#deroule"+div).slideUp("slow");
	}
	
	
}


function MAJSelect(id_supprime, type, typeElement, idElement, idReception, idIdReception, idValeurReception) 
{
	/*
	id_supprime -> id de l'element a supprime
	type -> type de l'action (ajout ou  suppression)
	typeElement -> type de l'element ou on recupere la valeur (text, select)
	idElement -> id ou on recupere la valeur => selectdiplome
	idReception -> id du champ qui affiche les resultats => diplomeSelect
	idIdReception -> id du champ hidden qui contient les id => diplome_select
	idValeurReception -> id champ hidden qui contient les valeurs => nom_diplome_select
	*/
	var mots_select_test = '';
	var mots_test = '';

	mots_select_test = document.getElementById(idIdReception).value;
	mots_test = document.getElementById(idValeurReception).value;
	if(type=='ajout')
	{
		if(typeElement == 'select')
		{
			if(document.getElementById(idElement).selectedIndex != 0){
				redondant="n";
				id_mot = document.getElementById(idElement).options[document.getElementById(idElement).selectedIndex].value;
				ids = mots_select_test.split(';');
				nb = ids.length;
				for( var i=0 ; i < nb - 1 ; i++)
				{
					if(ids[i]==id_mot)
					{
						redondant="o";
					}
				}
				if(redondant=="n")
				{
					nom_mot = document.getElementById(idElement).options[document.getElementById(idElement).selectedIndex].title;
					mots_test += nom_mot+';';
					mots_select_test += id_mot+';';
					document.getElementById(idIdReception).value = mots_select_test;
					document.getElementById(idValeurReception).value = mots_test;
					codehtml = $("#"+idReception).html();
					codehtml += '<tr style="height:20px;" valign="middle"><td style="width:70%; padding-left:15px" align="left">&bull;&nbsp;'+nom_mot+'</td><td width="30" align="center"><a href="javascript:MAJSelect(\''+id_mot+'\',\'supprime\',\''+typeElement+'\',\''+idElement+'\',\''+idReception+'\',\''+idIdReception+'\',\''+idValeurReception+'\');"><img src="/images/interface/delete.png" alt="supprimer" title="supprimer" border="0" /></a></td></tr>';
					$("#"+idReception).html(codehtml);
					
					AddSpecialite(id_mot);
				}
			}
			document.getElementById(idElement).selectedIndex = 0;
		} 
		else if(typeElement == 'text')
		{
			if(document.getElementById(idElement).value != '' && document.getElementById(idElement).value.length != 0 && document.getElementById(idElement).value != null){
				redondant="n";
				id_mot = document.getElementById(idElement).value;
				ids = mots_select_test.split(';');
				nb = ids.length;
				for( var i=0 ; i < nb - 1 ; i++)
				{
					if(ids[i]==id_mot)
					{
						redondant="o";
					}
				}
				if(redondant=="n")
				{
					nom_mot = document.getElementById(idElement).value;
					mots_test += nom_mot+';';
					mots_select_test += id_mot+';';
					document.getElementById(idIdReception).value = mots_select_test;
					document.getElementById(idValeurReception).value = mots_test;
					codehtml = $("#"+idReception).html();
					codehtml += '<tr style="height:20px;" valign="middle"><td style="width:70%; padding-left:15px" align="left">&bull;&nbsp;'+nom_mot+'</td><td width="30" align="center"><a href="javascript:MAJSelect(\''+id_mot+'\',\'supprime\',\''+typeElement+'\',\''+idElement+'\',\''+idReception+'\',\''+idIdReception+'\',\''+idValeurReception+'\');"><img src="/images/interface/delete.png" alt="supprimer" title="supprimer" border="0" /></a></td></tr>';
					$("#"+idReception).html(codehtml);
					
					AddDomaine(nom_mot);
					
				}
			}
			document.getElementById(idElement).value = '';
		}
		else if(typeElement == 'doubletext')
		{
			if(document.getElementById(idElement+'1').value != '' && document.getElementById(idElement+'1').value.length != 0 && document.getElementById(idElement+'1').value != null && document.getElementById(idElement+'2').value != '' && document.getElementById(idElement+'2').value.length != 0 && document.getElementById(idElement+'2').value != null){
				redondant="n";
				id_mot = document.getElementById(idElement+'2').value;
				ids = mots_select_test.split(';');
				nb = ids.length;
				for( var i=0 ; i < nb - 1 ; i++)
				{
					if(ids[i]==id_mot)
					{
						redondant="o";
					}
				}
				if(redondant=="n")
				{
					nom_mot = document.getElementById(idElement+'1').value;
					mots_test += nom_mot+';';
					mots_select_test += id_mot+';';
					document.getElementById(idIdReception).value = mots_select_test;
					document.getElementById(idValeurReception).value = mots_test;
					codehtml = $("#"+idReception).html();
					codehtml += '<tr style="height:20px;" valign="middle"><td style="width:50%" align="left">'+nom_mot+'</td><td width="20%" align="center"><a href="javascript:MAJSelect(\''+id_mot+'\',\'supprime\',\''+typeElement+'\',\''+idElement+'\',\''+idReception+'\',\''+idIdReception+'\',\''+idValeurReception+'\');"><img src="/_images/interface/delete.png" alt="supprimer" title="supprimer" border="0" /></a></td></tr>';
					$("#"+idReception).html(codehtml);
				}
			}
			document.getElementById(idElement+'1').value = '';
			document.getElementById(idElement+'2').value = '';
		}
	}
	else if(type=='supprime')
	{
		codehtml = '';
		ids = mots_select_test.split(';');
		mot = mots_test.split(';');
		mots_select_test = '';
		mots_test = '';
		nb = ids.length;

		for( var i=0 ; i < nb - 1 ; i++)
		{
			//alert(ids[i]);
			if(ids[i]!=id_supprime)
			{
				codehtml += '<tr style="height:20px;" valign="middle"><td style="width:70%; padding-left:15px;" align="left">&bull;&nbsp;'+mot[i]+'</td><td width="30" align="center"><a href="javascript:MAJSelect(\''+ids[i]+'\',\'supprime\',\''+typeElement+'\',\''+idElement+'\',\''+idReception+'\',\''+idIdReception+'\',\''+idValeurReception+'\');"><img src="/images/interface/delete.png" alt="supprimer" title="supprimer" border="0" /></a></td></tr>';
				mots_test += mot[i]+';';
				mots_select_test += ids[i]+';';
			}
		}
		$("#"+idReception).html(codehtml);
		//document.getElementById(idReception).innerHTML = codehtml;
		document.getElementById(idIdReception).value = mots_select_test;
		document.getElementById(idValeurReception).value = mots_test;
		
	}
}


//nouveau prestataire ?
function checkAutrePrestataire(id_presta) {
	if (String(id_presta) == 'autre') {
		$('.nouveaupresta').show();
	}
	else $('.nouveaupresta').hide();
}

function loadSpecialites(id_activite, div, loading) {
	if (!div) div = '#divspecialite';
	
	if (loading) $('#loading').show();
	
	$(div).load('../_librairies/t_ajax.php',{id_ajax:16, id_activite:id_activite}, function(loading,autocomplete) { if (loading) $('#loading').hide();});
}

function loadSpecialitesAuto(id_activite, div, loading) {
	if (!div) div = '#divspecialite';
	
	if (loading) $('#loading').show();
	$(div).load('../_librairies/t_ajax.php',{id_ajax:16, id_activite:id_activite}, function(loading,autocomplete) { if (loading) $('#loading').hide(); 
	
	$('#id_specialite').change(function() {		  					  
		MAJSelect('0','ajout','select', 'id_specialite', 'specialiteselect' , 'specialites_id', 'nom_specialite_select')
	})
			  ;});
}


function loadPrestataire(id_prestataire, div) {
	
	if (!div) div = 'infos';
	$('#'+div).load('../_librairies/t_ajax.php',{id_ajax:25, id_prestataire:id_prestataire});
}


//générer l'url d'un ejournal
function genereURLEjournaux(ejournal, recepteur, url_site, type) {
	if (!url_site) url_site = 'http://www.edimark.fr/';
	date_ejournal = new Date();
	$(recepteur).val(url_site+type+'/'+ejournal+'/'+date_ejournal.getFullYear()+'/');
}

//une case unique sur toutes les cases à cocher
function checkUnique(id, vclass) {
	$('.'+vclass).attr('checked',false);
	$('#'+id).attr('checked',true);
}

function AddCentreInteret(centre_id){

	$.post('../_librairies/t_ajax.php',{id_ajax:11, centre_id:centre_id});
	
}

function MAJCentresInterets(valeur, conteneur, id, enfant) {
	
	
	var nouvelle_valeur = '';
	var supprime = 'N';
	
	var valeurs_enfants = new Array();
	
	
	if (enfant) {
		$('.'+enfant).each(function(i){
		   valeurs_enfants.push(this.value);
		 });
	}	
	
	if (id && $("#"+id).is(':checked')) {
		
	}
	else {
		supprime = 'O';
	}
	
	//on récupère la valeur actuelle
	var ids = $("#"+conteneur).val().split(';');
	
	
	//réaffectation des valeurs	
	if (supprime =='N') {
		for (var i=0;i<=ids.length;i++) {
			if ((String(ids[i]) != 'undefined') && (String(ids[i]) != '')) nouvelle_valeur += ids[i]+';';	
			
		}
		for (var i=0;i<=valeurs_enfants.length;i++) {
				if ((String(valeurs_enfants[i]) != 'undefined') && (String(valeurs_enfants[i]) != '')) nouvelle_valeur += valeurs_enfants[i]+';';
		}
		nouvelle_valeur += valeur+';';	
	}
	else {
		for (var i=0;i<=ids.length;i++) {
			if ((String(ids[i]) != 'undefined') && (String(ids[i]) != String(valeur)) && (String(ids[i]) != '') && $.inArray(ids[i], valeurs_enfants)==-1) {
				
				nouvelle_valeur += ids[i]+';';	
			}
		}
		
		
	}
	
	
	
	//on réécrit la nouvelle valeur
	$("#"+conteneur).val(nouvelle_valeur);
}

function supprimerAlerte(id, div, type) {
	if (confirm("Voulez vous supprimer cette alerte ?")) {
		$('#'+div).load('../_librairies/t_ajax.php',{id_ajax:24,id:id, type:type});
	}
}

function retireaccent(chaine) {
	temp = chaine.replace(/[àâä]/gi,"a");
	temp = temp.replace(/[éèêë]/gi,"e");
	temp = temp.replace(/[îï]/gi,"i");
	temp = temp.replace(/[ôö]/gi,"o");
	temp = temp.replace(/[ùûü]/gi,"u");
	temp = temp.replace(/[ç]/gi,"c");
	temp = temp.replace(/['']/gi," ");
	temp = temp.replace("…","...");
	return temp;
}



