/*

*/
//---------------------------------------------------------------------------------

function test(num)
{
	contenu('produits');
	marquer(document.getElementById('td_produ'));
	recherche_categ_produits(num);
	//document.getElementById("td_produ").style.background = "#084c26" ;
}

function plant()
{
	document.getElementById("plant").checked = !document.getElementById("plant").checked;
}

function fruits()
{
	document.getElementById("fruits").checked = !document.getElementById("fruits").checked;
}
function animaux()
{
	document.getElementById("animaux").checked = !document.getElementById("animaux").checked;
}
function miel()
{
	document.getElementById("miel").checked = !document.getElementById("miel").checked;
}
function legumes()
{
	document.getElementById("legumes").checked = !document.getElementById("legumes").checked;
}
function epices()
{
	document.getElementById("epices").checked = !document.getElementById("epices").checked;
}
function produits_transformes()
{
	document.getElementById("produits_transformes").checked = !document.getElementById("produits_transformes").checked;
}

//---------------------------------------------------------------------------------



// fonction de récupération de données : ici on va chercher toutes les villes de la réunion dans la base  sous format xml grace à ajax
function get_villes()
{
	var docXML = ajax("get_villes.php") ;
	
	var items = docXML.getElementsByTagName("ville") ;	

	var selectVilles = document.createElement("select");
	selectVilles.id = "ville" ;
	selectVilles.name = "ville" ;
	
	var option = document.createElement("OPTION");
	
	option.appendChild(document.createTextNode("Selectionnez une ville")) ;
					
	for (i=0;i<items.length;i++)
	{
		option = document.createElement('option') ;
		option.value = items.item(i).firstChild.data ;
		option.appendChild(document.createTextNode(items.item(i).firstChild.data)) ;
		selectVilles.appendChild(option) ;
	}				
	
	document.getElementById("formulaireExploitant").appendChild(selectVilles) ;
	document.getElementById("ville").appendChild(selectVilles) ;
	
}

//fonction d'interaction avec l'exterieur grâce à l'objet XMLHttpRequest ou  ActiveXObject
function ajax(url)
{
	if(window.XMLHttpRequest) // FIREFOX
	xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
	xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
	return(false);
	xhr_object.open("GET", url, false);
	xhr_object.send(null);
	if(xhr_object.readyState == 4) return(xhr_object.responseXML);
	else return(false); 
}

function ajaxTXT(url)
{
	if(window.XMLHttpRequest) // FIREFOX
	xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
	xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
	return(false);
	xhr_object.open("GET", url, false);
	xhr_object.send(null);
	if(xhr_object.readyState == 4) return(xhr_object.responseText);
	else return(false); 
}

//fonction qui remplace ce qu'il y a dans un div par ce qu'on lui passe en paramètres !!
function writediv(div,texte)
{
	document.getElementById(div).innerHTML = texte;
}



//fonction qui affiche le formulaire de recherche des producteurs
function rechercheProducteurs()
{
	var nom = document.getElementById("nom_exploitant").value ;
	var prenom = document.getElementById("prenom_exploitant").value ;
	var texte = "" ;
	
	document.getElementById('info_recherche').className = "info_recherche" ;
	//si le nom est rempli
	if(nom != "")
	{
		//si le nom et le prenom sont remplis
		if(prenom != "")
		{
			texte = ajaxTXT("resultat_recherche/TEXTget_exploitations.php?critere=nom_prenom&nom_exploitant=" + nom + "&prenom_exploitant=" + prenom) ;
		}
		//si il n'y a que le nom
		else
		{
			texte = ajaxTXT("resultat_recherche/TEXTget_exploitations.php?critere=nom&nom_exploitant=" + nom) ;
		}
		
	}
	//si seulement le prenom est rempli
	else if(prenom != "")
	{	
		texte = ajaxTXT("resultat_recherche/TEXTget_exploitations.php?critere=prenom&prenom_exploitant=" + prenom) ;
	}
	//si ni l'un ni l'autre n'est rempli
	else
	{
		texte = ajaxTXT("resultat_recherche/TEXTget_exploitations.php") ;
	}
	
	if(nom != "")
	{
		if(prenom != "")
		{
			writediv("info_recherche","R&eacute;sultats de la recherche pour \"" + nom + " + " + prenom + "\"") ;
		}
		else
		{
			writediv("info_recherche","R&eacute;sultats de la recherche pour \"" + nom + "\"") ;
		}
	}
	else
	{
		if(prenom != "")
		{
			writediv("info_recherche","R&eacute;sultats de la recherche pour \"" + prenom + "\"") ;
		}
		else
		{
			writediv("info_recherche","Tous les exploitants : ") ;
		}
	}
	
	//on écrit le résultat qui a été retoourné pour cette recherche dans le div correspondant
	writediv("resultat_recherche",texte);

	document.getElementById("resultat_recherche").innerHTML += "<div id='exploitation' class='exploitation'></div>" ;
}


function recherche_categ_produits(produit)
{
	var texte = ajaxTXT("resultat_recherche/TEXTget_exploitations.php?critere=code_categ&code_categ=" + produit) ;
	var categ = "" ;
	
	switch(produit)
	{
		case 1 :	categ = "Produits transform&eacute;s" ;
					break;
		case 2 :	categ = "Animaux" ;
					break;
		case 3 :	categ = "Legumes" ;
					break;
		case 4 :	categ = "Fruits" ;
					break;
		case 5 :	categ = "Epices" ;
					break;
		case 6 :	categ = "Miel" ;
					break;
		case 7 :	categ = "Plantes" ;
					break;
	}
	document.getElementById('info_recherche').className = "info_recherche" ;
	writediv("info_recherche","R&eacute;sultats de la recherche pour la cat&eacute;gorie \"" + categ + "\"") ;
	
	writediv("resultat_recherche",texte);
	document.getElementById("resultat_recherche").innerHTML += "<br><div id='exploitation' class='exploitation'></div>" ;
}


function recherche_produit()
{	
	var produit = document.getElementById("nom_produit").value ;
	var texte = ajaxTXT("resultat_recherche/TEXTget_exploitations.php?critere=produit&produit=" + produit) ;
	
	document.getElementById('info_recherche').className = "info_recherche" ;
	if(produit != "")writediv("info_recherche","R&eacute;sultats de la recherche pour le produit \"" + produit + "\"") ;
	else writediv("info_recherche","Tous les exploitants") ;
	writediv("resultat_recherche",texte);
	//on ajoute ce div dans resultat recherche pour pouvoir afficher la liste des exploiants la dedans
	document.getElementById("resultat_recherche").innerHTML += "<br><div id='exploitation' class='exploitation'></div>"
}

//si la touche entree est pressée on lance la recherche
function key(formulaire,event)
{
	if(event.keyCode == 13)
	{
		if(formulaire == "producteurs") rechercheProducteurs() ;
		if(formulaire == "produit") recherche_produit() ;
	}
}
 
function codeTouche(evenement)
{
	for(prop in evenement)
	{
		if(prop=='which') return(evenement.wich) ;
	}
	return(evenement.keyCode) ;
}


//fonction qui affiche le formulaire de recherche des producteurs
function contenu(TheContenu)
{
	//RAZ de contenu
	writediv("accueil","") ;
	writediv("formulaire_recherche","");
	writediv("resultat_recherche","");
	writediv("info_recherche","");
	document.getElementById('formulaire_recherche').style.visibility = "visible" ;
	document.getElementById('info_recherche').className = "" ;
	
	if(TheContenu == "producteurs")
	{
		var texte = ajaxTXT("formulaires.php?formulaire=" + TheContenu) ;
		writediv("formulaire_recherche",texte) ;
	}
	if(TheContenu == "produits")
	{
		var texte = ajaxTXT("formulaires.php?formulaire=" + TheContenu) ;
		writediv("formulaire_recherche",texte) ;
	}
	if(TheContenu == "regions")
	{
		
		var texte = ajaxTXT("carte_region.php") ;
		writediv("formulaire_recherche",texte) ;
	}
}


//fonction qui va traiter le click sur chaque bouton du menu généré dynamiquement
function menuDynamique(code_theme)
{
	writediv("info_recherche","");
	//raz des contenus affichés dans la zone "contenu"
	writediv("formulaire_recherche","");
	writediv("resultat_recherche","");
	
	//recherche du contenu de la rubrique
	var texte = ajaxTXT("contenu.php?code_theme=" + code_theme) ;
	writediv("accueil",texte) ;
	
	//ici on cache ce div  car malgré qu'il soit vide on voit le cadre au chargement du site 
	document.getElementById('formulaire_recherche').style.visibility = "hidden" ;
}

var menu_SELECTED = 1 ;

function couleur(ide)
{
	var tableau = document.getElementById('tablex') ;
	
	for(var i=0;i<tableau.rows.length;i++)
	{
		tableau.rows[i].cells[0].style.background = "#9ac841" ;
		tableau.rows[i].cells[0].className = "bouton" ;		
		if(tableau.rows[i].cells[0] == document.getElementById(ide)) menu_SELECTED = i ;
		else tableau.rows[i].cells[0].style.fontWeight = "normal" ;
	}
	
	document.getElementById(ide).style.background = "#084c26" ; ;
}

function marquer(objectt)
{
	var tableau = document.getElementById('tablex') ;
	
	for(var i=0;i<tableau.rows.length;i++)
	{
		tableau.rows[i].cells[0].style.background = "#9ac841" ;
		tableau.rows[i].cells[0].className = "bouton" ;		
		if(tableau.rows[i].cells[0] == objectt) menu_SELECTED = i ;
		else tableau.rows[i].cells[0].style.fontWeight = "normal" ;
	}
	
	objectt.style.background = "#084c26" ;
}

function ville(hui)
{
	var txt = ajaxTXT("resultat_recherche/TEXTget_exploitations.php?critere=ville&ville=" + hui) ;
	writediv("liste_producteurs",txt) ;
	
	//on ajoute ce div dans resultat recherche pour pouvoir afficher la liste des exploiants la dedans
	document.getElementById("resultat_recherche").innerHTML = "<br><div id='exploitation' class='exploitation'></div>" ;
	//document.getElementById("resultat_recherche").innerHTML += "<br><div id='exploitation' class='exploitation'></div>" ;
	writediv("exploitation","") ;
	
	writediv("ville_selectionnee",hui) ;
}



/*
//recherche la liste des villes et l'écrit dans le contenant associé
function recherche_de_ville(cooo)
{
	var txt = ajaxTXT("resultat_recherche/TEXTget_exploitations.php?critere=ville&ville=" + cooo) ;
	writediv("liste_producteurs",txt) ;
	
	//on ajoute ce div dans resultat recherche pour pouvoir afficher la liste des exploiants la dedans
	document.getElementById("resultat_recherche").innerHTML += "<br><div id='exploitation' class='exploitation'></div>" ;
}
*/



// deux fonctions pour marquer le survol d'un lien
function survol(object)
{
	var tableau = document.getElementById('tablex') ;
	for(var i=0;i<tableau.rows.length;i++)
	{
		if(tableau.rows[i].cells[0] == object)
		{
			if(menu_SELECTED != i)
			{
				object.style.fontWeight = 'bold' ;
				object.style.background = "#c4e581" ;
			}
		}
	}
	
}

function finsurvol(object)
{
	
	var tableau = document.getElementById('tablex') ;
	//pour chaque td du tableau
	for(var i=0;i<tableau.rows.length;i++)
	{
		//si on a à faire au td en cours de survol
		if(tableau.rows[i].cells[0] == object)
		{
			//si l'index du td en cours de survol est différent de celui dont on ne doit pas changer la valeur
			if(menu_SELECTED != i)
			{
				object.style.fontWeight = 'normal' ;
				object.style.background = "#9ac841" ;
			}
		}
	}
	
}

//fonction pour afficher l'exploitant dans le div exploitation
function affiche_exploitation(code_exploitation)
{
	var texte = ajaxTXT("resultat_recherche/get_exploitation.php?code_exploitation=" + code_exploitation) ;
	writediv("exploitation",texte) ;
	var posEX = get_full_position("page") ;
	
	//alert("page" + posEX.height) ;
	
	window.scrollBy(0,posEX.height-500) ;
}


function get_offset(elem, pos)
{
	var offset = 0 ;
	while(elem)
	{
		offset += elem[pos] ;
		elem = elem.offsetParent ;
	}
}

function get_full_position(id)
{
	var d = document.getElementById(id) ;
	var offsets = new Object ;
	offsets.top = get_offset(d, "offsetTop");
	offsets.left = get_offset(d, "offsetLeft");
	offsets.width = d.clientWidth ;
	offsets.height = d.clientHeight ;
	
	return offsets ;
}


//trois fonctions utilisées par carte.php
function show(object,e)
{
	writediv("info_ville",object) ;
	window.scrollBy(0,-10) ;
	
	document.getElementById("info_ville").style.position =  "absolute" ;
	document.getElementById("info_ville").style.left =  (e.clientX + 20) + "px" ;
	document.getElementById("info_ville").style.top =  (e.clientY - 30) + "px" ;
	document.getElementById("info_ville").style.width = "40px" ;
	document.getElementById("info_ville").className = "info_ville" ;
	document.getElementById("info_ville").style.visibility = "visible" ;
	
}
function hide()
{
	document.getElementById("info_ville").style.visibility = "hidden" ;
}



//partie admin suppression d'exploitation
function suppression_exploitation()
{	
	//omg  -_-
	var code_exploitation = document.getElementById('code_exploitation')[document.getElementById('code_exploitation').selectedIndex].value ;
	
	var texte = ajaxTXT("suppression_exploitation.php?code_exploitation=" + code_exploitation) ;
	//j'appelle cette focntion pour mettre à jour la liste de selection des exploitations
	gerer("del_exploitation") ;
}
function suppression_contenu(which)
{
	if(which == "theme")
	{
		var code_theme = document.getElementById('code_theme')[document.getElementById('code_theme').selectedIndex].value ;
		
		var texte = ajaxTXT("suppression_contenu.php?code_theme=" + code_theme) ;
		//j'appelle cette focntion pour mettre à jour la liste de selection des exploitations
	}
	if(which == "sstheme")
	{
		var code_theme = document.getElementById('code_sstheme')[document.getElementById('code_sstheme').selectedIndex].value ;
		
		var texte = ajaxTXT("suppression_contenu.php?code_sstheme=" + code_theme) ;
		//j'appelle cette focntion pour mettre à jour la liste de selection des exploitations
	}
	if(which == "article")
	{
		var code_theme = document.getElementById('code_article')[document.getElementById('code_article').selectedIndex].value ;
		
		var texte = ajaxTXT("suppression_contenu.php?code_article=" + code_theme) ;
		//j'appelle cette focntion pour mettre à jour la liste de selection des exploitations
	}
	gerer("del_contenu") ;
}



function get_ville_region(code_region)
{
	var txt = ajaxTXT("resultat_recherche/get_villes_region.php?code_region=" + code_region) ;
	writediv("liste_villes",txt) ;
	if(document.getElementById('exploitation'))writediv("exploitation","") ;
	if(document.getElementById('liste_producteurs')) writediv("liste_producteurs","") ;
	if(document.getElementById('ville_selectionnee')) writediv("ville_selectionnee","") ;
}

function la_region(region)
{
	if(region == "Est")writediv('region_selectionnee',"L'Est");	
	else if(region == "Ouest")writediv('region_selectionnee',"L'Ouest");	
	else writediv('region_selectionnee',region);	
}