function locatorTraitementForm() // Fonction de traitement du formulaire du locator
{
	//Définition des variables
	chaineAPasser = "";
	nbproduit = 0;
	
	if(document.getElementById('departement'))
	{
		var departement = document.getElementById('departement').value;
		var localite = document.getElementById('localite').value;
	}
	else
	{
		var departement = document.getElementById('trouver-departement').value;
		var localite = document.getElementById('trouver-localite').value;
	}
	
	chaineAPasser = 'localite=' + localite + '&departement=' + departement;
	
	localite = localite + ' ' + departement;
	
	var selectedList = new Array();
	//var selectBox = document.forms[0].monSelect;
	if(document.getElementById('listeProduits'))
	{
		var selectBox = document.getElementById('listeProduits');
	}
	else
	{
		var selectBox = document.getElementById('trouver-listeProduits');
	}
	for (var i=0; i<selectBox.options.length; i++) {
		if (selectBox.options[i].selected) {
			nbproduit++;
			chaineAPasser += '&produit' + nbproduit + "=" + selectBox.options[i].value;
	  	}
	}
	chaineAPasser += '&nbProduit=' + nbproduit;
	var xhrFrom = new xhr();
	xhrFrom.open('post', 'scripts/php/locatorTraitementForm.php', false);
	xhrFrom.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	xhrFrom.send(chaineAPasser);
	
	if(xhrFrom.responseText == 'ok')
	{
		// On appelle la page 2 du locator
		if(document.getElementById('fenetrepopin'))
		{
			clientSideInclude('fenetrepopin', 'popin-product-locator2.php');
		}
		else
		{
			popin('fenetrepopin', 969, 615, 'popin-product-locator2.php');
		}
		
		// On affiche les éléments jQuery
		$('.media').media();
		$("#popin .product-locator .commune .jquery-selectbox").css("width", "200px");
		$("#popin .product-locator .commune .jquery-selectbox-list").css("width", "186px");
		changeSelectMultiple();
		$('.scroll-pane').jScrollPane({showArrows:true, scrollbarWidth: 19});
		
		//On récupère chacun des distributeurs correspondant à la demande
		var xhrDistrib = new xhr();
		xhrDistrib.open("get", "scripts/php/retourneDistributeur.php", false);
		xhrDistrib.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		xhrDistrib.send(null);	
		
		// Récupération du noeud root
		var docXML= xhrDistrib.responseXML;
		
		if(docXML.getElementsByTagName('distributeur').length > 0)
		{
			for(var i = 0; i < docXML.getElementsByTagName('distributeur').length; i++)
			{
				// On récupère le distributeur courant
				var distribCourant = docXML.getElementsByTagName('distributeur')[i];
				
				// Puis les données du distributeur
				if(distribCourant.getElementsByTagName("IdDistributeur")[0].firstChild != null)
				{
					var IdDistributeur = distribCourant.getElementsByTagName("IdDistributeur")[0].firstChild.nodeValue;
				}
				else
				{
					var IdDistributeur = '';
				}
				
				if(distribCourant.getElementsByTagName("NomDistributeur")[0].firstChild != null)
				{
					var NomDistributeur = distribCourant.getElementsByTagName("NomDistributeur")[0].firstChild.nodeValue;
				}
				else
				{
					var NomDistributeur = '';
				}
				
				if(distribCourant.getElementsByTagName("NumeroRue")[0].firstChild != null)
				{
					var NumeroRue = distribCourant.getElementsByTagName("NumeroRue")[0].firstChild.nodeValue;
				}
				else
				{
					var NumeroRue = '';
				}
				
				if(distribCourant.getElementsByTagName("NomRue")[0].firstChild != null)
				{
					var NomRue = distribCourant.getElementsByTagName("NomRue")[0].firstChild.nodeValue;
				}
				else
				{
					var NomRue = '';
				}
				
				if(distribCourant.getElementsByTagName("ComplementRue")[0].firstChild != null)
				{
					var ComplementRue = distribCourant.getElementsByTagName("ComplementRue")[0].firstChild.nodeValue;
				}
				else
				{
					var ComplementRue = '';
				}
				
				if(distribCourant.getElementsByTagName("CP")[0].firstChild != null)
				{
					var CP = distribCourant.getElementsByTagName("CP")[0].firstChild.nodeValue;
				}
				else
				{
					var CP = '';
				}
				
				if(distribCourant.getElementsByTagName("Ville")[0].firstChild != null)
				{
					var Ville = distribCourant.getElementsByTagName("Ville")[0].firstChild.nodeValue;
				}
				else
				{
					var Ville = '';
				}
				
				if(distribCourant.getElementsByTagName("Telephone")[0].firstChild != null)
				{
					var Telephone = distribCourant.getElementsByTagName("Telephone")[0].firstChild.nodeValue;
				}
				else
				{
					var Telephone = '';
				}
				
				if(distribCourant.getElementsByTagName("Fax")[0].firstChild != null)
				{
					var Fax = distribCourant.getElementsByTagName("Fax")[0].firstChild.nodeValue;
				}
				else
				{
					var Fax = '';
				}
				
				// On stocke la distance
				stockeDistance(localite, IdDistributeur, NomDistributeur, NumeroRue, NomRue, ComplementRue, CP, Ville, Telephone, Fax, i, (docXML.getElementsByTagName('distributeur').length-1));
			}
		}
		else
		{
			document.getElementById('listeDistributeurs').innerHTML = '<li>Aucun r&eacute;sultat.</li>';
		}
	}
	else
	{
		alert('Une erreur est survenue. Veuillez re-valider le formulaire s\'il vous plait');	
	}
}

var xhrMajListe = null;
function actualiserProduits(recherche) // Fonction permettant l'actualisation du select multiple suivant la recherche de l'internaute
{
	// On réinitialise l'objet XHR lors d'une saisie rapide
	if(xhrMajListe && xhrMajListe.readyState != 0)
	{
		xhrMajListe.abort();
	}
	
	// On remet à 0 la liste des produits
	var listeProduit = document.getElementById('listeProduits');
	listeProduit.options.length = 0;
	document.getElementById('listeProduitsJquery').innerHTML = '';
	
	// On va récuperer un XML contenant les produits recherchés
	xhrMajListe = xhr();
	xhrMajListe.open('post', 'scripts/php/creerListeProduits.php', true);
	xhrMajListe.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	xhrMajListe.onreadystatechange = function()
	{
		if(xhrMajListe.readyState == 4)
		{
			if(xhrMajListe.status == 200)
			{
				var root = xhrMajListe.responseXML;
		
				// On traite le XML retourné
				for(var i = 0; i < root.getElementsByTagName('produit').length; i++)
				{
					var produitCourant = root.getElementsByTagName('produit')[i];
					var titre = produitCourant.getElementsByTagName('titre')[0].firstChild.nodeValue;
					var id = produitCourant.getElementsByTagName('id')[0].firstChild.nodeValue;
					var table = produitCourant.getElementsByTagName('table')[0].firstChild.nodeValue;
					
					var option = document.createElement('OPTION');
					option.text = titre;
					option.value = table + '-' + id;
					
					try
					{
						listeProduit.add(option, null); // ne fonctionne pas sous IE
					}
					catch(ex)
					{
						listeProduit.add(option); // fonctionne sous IE seulement
					}
				}
				
				// Réinitialisation du div jQuery
				$(".multiple-select").each(function() {
					compt = 1;
					$(this).find("option").each( function() {
						compt++;
						if ( compt == 2 ) {
							paire = ' class="paire"';
							compt = 0;
						}
						else {
							paire = "";	
						}
						$(".multiple-select-scroll").find("ul").append("<li"+paire+" title="+$(this).attr("value")+">"+$(this).text()+"</li>");
						
					});
					$(".multiple-select-scroll ul li").click( function() {
						//$(".multiple-select-scroll ul li").removeClass("selected");
						//$(".multiple-select option").attr("selected", false);
						if($(this).attr('class').indexOf('selected') == -1)
						{
							$(this).addClass("selected");
							$(".multiple-select option[value="+$(this).attr("title")+"]").attr("selected", true);
						}
						else
						{
							$(this).removeClass("selected");
							$(".multiple-select option[value="+$(this).attr("title")+"]").attr("selected", false);
						}
					});						 
				});
			}
		}
	}
	
	xhrMajListe.send('chaine=' + recherche);
}

function stockeDistance(depart, IdDistributeur, NomDistributeur, NumeroRue, NomRue, ComplementRue, CP, Ville, Telephone, Fax, icourant, imax) // Stocke la distance entre depart et arrivee (vol d'oiseau)
{
	var arrivee = NumeroRue + " " + NomRue + " " + CP + " " + Ville;
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(depart,
		function(point)
		{
			if (!point)
			{
				alert("- "+depart+" n'existe pas");
			}
			else
			{
			  var a =  point.lat();
			  var b =  point.lng();
			  
			  var geocoder2 = new GClientGeocoder();
			  geocoder2.getLatLng(arrivee,
				function(point2)
				{
					if (!point2)
					{
				 		alert("- "+arrivee+" n'existe pas");
					}
					else
					{
						var a2 =  point2.lat();
						var b2 =  point2.lng();
						var coord1 = new GLatLng(a, b);
						var coord2 = new GLatLng(a2, b2);
						var distance = Math.round(coord1.distanceFrom(coord2)/10, 0)/100;
						
						var xhrDistance = new xhr();
						xhrDistance.open('post', 'scripts/php/ajouteDistance.php', false);
						xhrDistance.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
						xhrDistance.send('IdDistributeur=' + IdDistributeur + '&NomDistributeur=' + NomDistributeur + '&NumeroRue=' + NumeroRue + '&NomRue=' + NomRue + '&ComplementRue=' + ComplementRue + '&CP=' + CP + '&Ville=' + Ville + '&Telephone=' + Telephone + '&Fax=' + Fax + '&distance=' + distance);
						
						if(xhrDistance.responseText == 'ok' && icourant == imax)
						{
							// On va ensuite créer le XML des magasins qui nous interessent avec les distances et les produits de chaque magasin
							var xhrMagasins = new xhr();
							xhrMagasins.open('get', 'scripts/php/listerMagasins.php', false);
							xhrMagasins.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
							xhrMagasins.send(null);
							
							var ulMagasins = document.getElementById('listeDistributeurs');
							
							var retourXML = xhrMagasins.responseXML;
							if(retourXML.getElementsByTagName('distributeur').length > 0)
							{
								for(var i = 0; i < retourXML.getElementsByTagName('distributeur').length; i++)
								{
									var distributeurCourant = retourXML.getElementsByTagName('distributeur')[i];
									var nom = distributeurCourant.getElementsByTagName('NomDistributeur')[0].firstChild.nodeValue;
									var adresse = distributeurCourant.getElementsByTagName('Adresse')[0].firstChild.nodeValue;
									
									if(distributeurCourant.getElementsByTagName('Telephone')[0].firstChild != null)
									{
										var tel = distributeurCourant.getElementsByTagName('Telephone')[0].firstChild.nodeValue;
									}
									else
									{
										var tel = '';
									}
									
									if(distributeurCourant.getElementsByTagName('Fax')[0].firstChild != null)
									{
										var fax = distributeurCourant.getElementsByTagName('Fax')[0].firstChild.nodeValue;
									}
									else
									{
										var fax = '';
									}
									
									var distance = distributeurCourant.getElementsByTagName('Distance')[0].firstChild.nodeValue;
									var produits = distributeurCourant.getElementsByTagName('Produits')[0].firstChild.nodeValue;
									
									// On crée l'item
									var li = document.createElement('li');
									
									// On crée le h2
									var h2 = document.createElement('h2');
									h2.innerHTML += '<h2>' + nom + '</h2>';
									li.appendChild(h2);
									
									// On complète l'adresse
									if(tel != '')
									{
										adresse += '<br/>T&eacute;l. : ' + tel;
									}
									if(fax != '')
									{
										adresse += '<br/>Fax : ' + fax;
									}
									
									// On crée l'élément address
									var address = document.createElement('address');
									address.innerHTML += adresse + '<br/>Distance approximative : ' + distance;
									li.appendChild(address);
									
									// On crée l'élément p
									var p = document.createElement('p');
									p.innerHTML = produits.substr(0, produits.length-1).replace('|', '<br/>');
									p.setAttribute('class', 'produit');
									p.setAttribute('className', 'produit');
									li.appendChild(p);
									
									// On ajoute l'item à la liste
									ulMagasins.appendChild(li);
								}
							}
							else
							{
								ulMagasins.innerHTML = '<li>Aucun r&eacute;sultat.</li>';
							}
						}
					}
				});
			}
		}
	);
}
