/////////////////////////////
//  REMPLIR UN SELECT      //
/////////////////////////////
function remplir_select(input,table,champ,valeur_champ,libelle,id_selected,texte,langue_absent){
	var pars = '?input='+input+'&table='+table+'&champ='+champ+'&valeur_champ='+valeur_champ+'&libelle='+libelle+'&id_selected='+id_selected+'&texte='+texte+'&langue_absent='+langue_absent;
	executeAjax('http://www.annuairegreen.com/page/ajax_rubrique.php',pars);
}


/////////////////////////////////////////
//  REMPLIR LE SELECT DES VILLES       //
/////////////////////////////////////////
function remplir_select_ville(blockId,pays_niveaux_detail,id_selected,texte_defaut){
var pars = '?blockId='+blockId+'&pays_niveaux_detail='+pays_niveaux_detail+'&id_selected='+id_selected+'&texte_defaut='+texte_defaut;
executeAjax('http://www.annuairegreen.com//page/ajax_ville.php',pars);
}


//////////////////////////
//   SELECT PERSO       //
//////////////////////////
function remplir_select_perso(input,inputSuiv,table,champ,valeur_champ,libelle,id_selected,texte,langue_absent){
var pars = '?input='+input+'&inputSuiv='+inputSuiv+'&table='+table+'&champ='+champ+'&valeur_champ='+valeur_champ+'&libelle='+libelle+'&id_selected='+id_selected+'&texte='+texte+'&langue_absent='+langue_absent;
executeAjax('http://www.annuairegreen.com/page/ajax_select_perso.php',pars);
}

function SelectPersoInitial(liste){
	
	for(i=0; i<liste.length; i++){
		//eval(liste[i]+'_auto = 0');
		document.getElementById(liste[i]+'_auto').value=0;
	}
	listeSelect = liste;
	Event.observe(document, 'click', SelectPersoCache, false);
}

function SelectPersoAffiche(block){
	if( document.getElementById(block+'_auto').value == '0' ){
		Effect.BlindDown(block+'_auto',{duration:'0.4'});
		//eval(block+'_auto = 1');
		document.getElementById(block+'_auto').value=1;
	}
}
function SelectPersoRempli(id,text,block,id_form){
	var form = 'form';
	if( id_form!=null ){ form=id_form;}
	document.getElementById(form).elements[block+'_select'].value = text;
	document.getElementById(form).elements[block].value = id;
}
function SelectPersoCache(){
	tab = listeSelect;
	
	for(i=0; i<tab.length; i++){
		if( document.getElementById(tab[i]+'_auto').style.display=='' && document.getElementById(tab[i]+'_auto').value == 1 ){
			document.getElementById(tab[i]+'_auto').value = 0;
			Effect.BlindUp(tab[i]+'_auto',{duration:'0.2'});
		}
	}
}
function SelectPersoVide(tab){
	for(z=0; z<tab.length; z++){
		SelectPersoRempli('0','...',tab[z]);
		document.getElementById(tab[z]+'_auto').innerHTML = '...';
	}
}


////////////////////////////////
//  VIDER CONTENU SELECT      //
////////////////////////////////
function vider_select(champ,texte){
	// 
	src = document.getElementById(champ);
	
	// 
	src.options.length = 0;
	src.options[0] = new Option(texte,'0');
}


//////////////////////////////////////
//            ENVOYER AMI          ///
//////////////////////////////////////

function AfficherVoile(){
	var Cacher=false;
	// div transparent servant de voile
	mondiv = document.createElement('div');
	mondiv.setAttribute('id','div_voile');
	mondiv.setAttribute('onclick','CacherDiv();');
	mondiv.style.position = 'fixed';
	mondiv.style.zIndex = '99';
	mondiv.style.top = '0';
	mondiv.style.left = '0';
	mondiv.style.width = document.documentElement.scrollWidth+'px';
	mondiv.style.height = document.documentElement.scrollHeight+'px';
	mondiv.style.background = '#444444';
	mondiv.style.opacity = '0.7';
	mondiv.style.filter = 'alpha(opacity=70)';
	document.getElementsByTagName('body')[0].appendChild(mondiv);
	
}

function fermerVoile(){
	document.getElementsByTagName('body')[0].removeChild(mondiv);
}

function fermerEnvoiAmi()
{
	//enlever select civilite de la liste des select
	liste_select_init = liste_select_init.replace(",'civilite'","");
	liste_select_init = liste_select_init.replace("'civilite'","");
	if( liste_select_init!='' ){ eval("SelectPersoInitial(new Array("+liste_select_init+"))");}
	document.getElementsByTagName('body')[0].removeChild(mondiv2);
	fermerVoile();
}

function CacherDiv(){
	if( Cacher )
	{
		fermerEnvoiAmi();
	}
}


////////////////////////
//   PAYS NIVEAUX     //
////////////////////////
function appelPaysNiveaux(pays,contact,validation){
	//
	var pars = '?pays='+pays+'&amp;ville=1'+'&amp;contact='+contact+'&amp;validation='+validation;
	executeAjax('http://www.annuairegreen.com/page/ajax_pays_niveaux.php',pars);
}
function appelPaysNiveauxSelect(input,inputSuiv,table,champ,valeur_champ,id_selected,texte,validation){
	var pars = '?input='+input+'&amp;inputSuiv='+inputSuiv+'&amp;table='+table+'&amp;champ='+champ+'&amp;valeur_champ='+valeur_champ+'&amp;id_selected='+id_selected+'&amp;texte='+texte+'&amp;validation='+validation;
	executeAjax('http://www.annuairegreen.com/page/ajax_pays_niveaux_select.php',pars);
}


//////////////////////
//   RUBRIQUE       //
//////////////////////
function rubriqueAffiche(id){
	if( document.getElementById('rubriqueFils_'+id).style.display == 'block' || document.getElementById('rubriqueFils_'+id).style.display=='' ){
		document.getElementById('rubrique_'+id).className = 'rubrique-inscription';
		Effect.BlindUp('rubriqueFils_'+id,{duration:'0.5'});
	}else{
		document.getElementById('rubrique_'+id).className = 'rubrique-inscription-deroule';
		Effect.BlindDown('rubriqueFils_'+id,{duration:'0.4'});
	}
}


//////////////////////
//   MOT CLE        //
//////////////////////
var listeAjoutMotcle = '';
var listeSupprimerMotcle = '';
var nbMotcle = 0;
var nbRubrique = 0;

function ajouterBkMotCle(form,blocId,rubrique,contact,nb_motcle_max){
	nb_max_motcle = nb_motcle_max;
	//alert(nb_max_motcle);
	if( document.getElementById(form).elements['rubrique_'+rubrique].checked==true ){sens = 1;nbRubrique = Number(nbRubrique)+1;}
	if( document.getElementById(form).elements['rubrique_'+rubrique].checked==false ){
		sens = 0;
		
		var motcle = eval("rubrique_detail_"+rubrique+".split(';');");

		for(var i=0; i<motcle.length; i++)
		{
			if( motcle[i]!='' ){ nbMotcle = Number(nbMotcle)-1;}
		}

		Number(nbRubrique)+1;
	}
	
	var pars = '?blocId='+blocId+'&amp;rubrique='+rubrique+'&amp;contact='+contact+'&amp;sens='+sens+'&amp;nb_max_motcle='+nb_max_motcle;
	executeAjax('http://www.annuairegreen.com/page/ajax_motcle.php',pars);

	eval("rubrique_detail_"+rubrique+" = ';';");
	eval("listeAjoutMotcle_"+rubrique+" = '';");
	eval("listeSupprimerMotcle_"+rubrique+" = '';");
}
function selectionMotcle(id,prefixe,liste){
	champ = document.getElementById(prefixe+id);

	if( champ.style.backgroundColor=='white' || champ.style.backgroundColor=='' ){
		if( (nbMotcle<nb_max_motcle && prefixe=='l_') || prefixe=='s_' ){
			// changement de la couleur de fond
			champ.style.backgroundColor = '#E9E9E9';

			// ajout du mot cle dans la liste des mot cles a ajouter
			if( eval(liste+"==''") ){ eval(liste+" += ';';")}
			eval(liste+" += id+';';");

			if( prefixe=='l_' ){ nbMotcle = Number(nbMotcle)+1;}
		}else{
			alert('Vous ne pouvez pas avoir plus de '+nb_max_motcle+' mot clés');
		}
	}else{
		// changement de la couleur de fond
		champ.style.backgroundColor = 'white';

		// ajout du mot cle dans la liste des mot cles a ajouter
		eval(liste+" = "+liste+".replace(';'+id+';',';');");
		if( eval(liste+"==';'") ){ eval(liste+" = '';")}

		nbMotcle = Number(nbMotcle)-1;
	}
	//alert('Ajouter: '+listeAjoutMotcle+'\nSupprimer: '+listeSupprimerMotcle+'\nNombre: '+nbMotcle);
}
function ajouterMotcle(sens,liste,rubrique,formValid){
	
	var motcle = eval(liste+".split(';');");
	for(var i=0; i<motcle.length; i++)
	{
		if( motcle[i]!='' ){
			champL = document.getElementById('l_'+motcle[i]);
			champS = document.getElementById('s_'+motcle[i]);

			if( sens==1 ){
				champL.style.backgroundColor = 'white';
				champL.style.display = 'none';
				champS.style.display = 'block';

				if( document.getElementById('form').elements['motcle'].value=='' ){ document.getElementById('form').elements['motcle'].value = ';';}
				document.getElementById('form').elements['motcle'].value += motcle[i]+';';

				eval("rubrique_detail_"+rubrique+" += '"+motcle[i]+";';");
				
			}else{
				champS.style.backgroundColor = 'white';
				champS.style.display = 'none';
				champL.style.display = 'block';
				
				document.getElementById('form').elements['motcle'].value = document.getElementById('form').elements['motcle'].value.replace(';'+motcle[i]+';',';');
				if( document.getElementById('form').elements['motcle'].value==';' ){ document.getElementById('form').elements['motcle'].value = '';}

				eval("rubrique_detail_"+rubrique+" = rubrique_detail_"+rubrique+".replace(';"+motcle[i]+";',';');");

				nbMotcle = Number(nbMotcle)-1;
			}
		}
	}
	eval(liste+" = '';");
	if( formValid!='0' ){ formulaire_validation('ok');}
}


//////////////////////
//   LABEL          //
//////////////////////
function ajouterLabel(selected){
	nb_block=document.getElementById('form').elements['nb_label'].value;
	max=document.getElementById('form').elements['max_label'].value;
	
	if(nb_block < max){

		choisi = '';
		if( nb_block>0 ){
			for(cpt=1; cpt<=nb_block; cpt++){
				if( document.getElementById('form').elements['label_'+cpt] ){
					choisi += ';'+document.getElementById('form').elements['label_'+cpt].value;
				}
			}
		}

		document.getElementById('form').elements['nb_label'].value = Number(nb_block)+1;
		
		var pars = '?ajouter=1&amp;nb_block='+nb_block+'&amp;choisi='+choisi+'&amp;selected='+selected;
		executeAjax('http://www.annuairegreen.com/page/ajax_label.php',pars);
	}else{
		alert('Vous ne pouvez pas ajouter plus de label !');
	}
}
function supprimerLabel(){
	nb_block = document.getElementById('form').elements['nb_label'].value;
	
	document.getElementById('form').elements['nb_label'].value = Number(nb_block)-1;
	if( nb_block>1 ){
		var pars = '?sens=0&amp;nb_block='+nb_block;
		executeAjax('http://www.annuairegreen.com/page/ajax_label.php',pars);
	}
}


///////////////////////////////////////////////////
//BLOCK LA SAISIE DE CARACTERE NON NUMERIQUE     //
///////////////////////////////////////////////////
function nettoyerTel(form,champs,numero){
document.forms[form].elements[champs].value = numero.replace(/[^0-9]*/g,'');
}

//////////////////////////////////////////
// LIMITE LA SAISI D'UN CHAMP           //
//////////////////////////////////////////
function maxlength(text,length) {if(text.value.length>length) text.value=text.value.substr(0,length); } 

//////////////////////////
//POPUP                //
//////////////////////////
function popup(url,nom,parametre){
	window.open(url,nom,parametre);
}

//////////////////////////
//REDIRECTION          //
//////////////////////////
function redirection(url){
	document.location.href=url;
}

///////////////////////////////////////////////////////
//POPUP ET DEPLACEMENT D'UNE IMAGE          //
///////////////////////////////////////////////////////
var positionx='';
var positiony='';
var moinsx='';
var moinsy='';
var deplacable=false;
var iddeplacable='';

function MoveThisElement(){
	if(deplacable){
		document.getElementById(iddeplacable).style.top=positiony+'px';
		document.getElementById(iddeplacable).style.left=positionx+'px';
	}
}
function OnMoveDown(px,py,id){
	deplacable=true;
	moinsx=px;
	moinsy=py;
	iddeplacable=id;
}
function stopdragndrop(){
	deplacable=false;
}
/*Le code provenant de l'éditeur javascript et légèrement modifié montrant comment trouver la position du curseur*/
function position(e){
	x = (navigator.appName.substring(0,3) == "Net") ? e.pageX :
	event.x+document.body.scrollLeft;
	y = (navigator.appName.substring(0,3) == "Net") ? e.pageY :
	event.y+document.body.scrollTop;
	positionx =x-moinsx;
	positiony =y-moinsy;
}
if(navigator.appName.substring(0,3) == "Net"){
	document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = position;
}

function zoomimg(affiche,nomDiv,image,top,left,deplacer){
	if(affiche == 1 && !document.getElementById(nomDiv)){
		var maDiv = document.createElement('div');
		maDiv.id = nomDiv;
		maDiv.style.border = '1px solid #a90200';
		maDiv.style.position = 'absolute';
		maDiv.style.left = '75%';
		maDiv.style.top = top;
		maDiv.style.marginLeft = left;
		maDiv.style.textAlign = 'center';
		maDiv.style.backgroundColor = 'white';
		maDiv.style.zIndex = '999';
		maDiv.innerHTML = '';
		if(deplacer == 1){
			maDiv.innerHTML += "<div style=\"background:#a90200;width:100%;height:15px;color:white;font-size:9px;text-align:center;vertical-align:middle;cursor:move;\" onmousedown=\"OnMoveDown('0','0','"+nomDiv+"');\">Cliquer et garder la souris sur la barre pour déplacer l'image</div>"; //Peut contenir de l'html
		}
		maDiv.innerHTML += "<img src=\""+image+"\" name=\"image\" alt=\"\" onclick=\"zoomimg(2,'"+nomDiv+"',0,0,0);\" /><br /><span class=\"commentaires\">(cliquer sur l'image pour la fermer)</span>";
		
		document.getElementById('bod').appendChild(maDiv);
	}else{
		if(document.getElementById(nomDiv)){
			var obj = document.getElementById('bod');
			var old = document.getElementById(nomDiv);
			obj.removeChild(old);
		}
	}
}
