function showHide(layer) {
	if (layer.style.display == "none") 	layer.style.display = "block";
	else layer.style.display = "none";
}

function importClient(alias) {
	var xhr; 
	try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
	catch (e) 
	{
		try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
		catch (e2) 
		{
		  try {  xhr = new XMLHttpRequest();     }
		  catch (e3) {  xhr = false;   }
		}
	 }
	 xhr.onreadystatechange  = function()
	{ 
		 if(xhr.readyState  == 4)
		 {
			  if(xhr.status  == 200)
			  {
				var reg=new RegExp("[|]+", "g");
				var tableau=xhr.responseText.split(reg);
				document.getElementById('latitudeI').value = tableau[0]; 
				document.getElementById('longitudeI').value = tableau[1]; 
				document.getElementById('titre').value = tableau[2]; 
			  }
			  else 
				 document.getElementById('content').innerHTML = "Error code : " + xhr.status;
		 }
	};
	
	xhr.open('POST', 'Google/coordonneesClient.php',  true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.send('alias=' + alias);	
}

function directionFrom(lat,lng)
{
	document.getElementById('direction').innerHTML = '<input type="hidden" name="from" id="from" value="' + lat + ',' + lng + '"/><input type="text" name="to" id="to" value=""/><input type="button" name="OK" value="OK" onclick="get_directions(document.getElementById(\'from\').value, document.getElementById(\'to\').value);" />';
};

function get_directions(from,to) 
{
	map.closeInfoWindow();
	dirn.load("from: "+from+" to: "+to, {getSteps:true});
	window.location.href="#name";
	return false;
}

function get_directionsInv(arr) 
{
	map.closeInfoWindow();
	dirInv.loadFromWaypoints(arr, {getSteps:true});
	window.location.href="#name";
	return false;
}

function GO(from,to) {
	map.closeInfoWindow();
	directionsPanel = document.getElementById("route");
    directions = new GDirections(map, directionsPanel);
	GEvent.addListener(directions, "error", handleErrors);
	directions.load('from: '+from+' to: '+to,{ "locale": "fr_fr" });
	window.location.href="#name";
};

function handleErrors(){
   if (dirn.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     alert("Aucune correspondance géographique n'a été trouvée pour l'adresse donnée. Peut-être cette adresse est-elle trop récente ou erronée.");
   else if (dirn.getStatus().code == G_GEO_SERVER_ERROR)
     alert("Une requête de géocodage ou d'itinéraire ne peut aboutir pour une raison inconnue.");
  
   else if (dirn.getStatus().code == G_GEO_MISSING_QUERY)
     alert("La paramètre q est manquant ou nul. Pour géocoder un itinéraire il faut saisir une adresse destination ou de provenance.");
 
   else if (dirn.getStatus().code == G_GEO_BAD_KEY)
     alert("Le clé donnée est invalide.");

   else if (dirn.getStatus().code == G_GEO_BAD_REQUEST)
     alert("L'itinéraire demandé ne peut être traité correctement.");
   
   else alert("Erreur inconnue.");
}

function handleErrorsInv(){
   if (dirInv.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     alert("Aucune correspondance géographique n'a été trouvée pour l'adresse donnée. Peut-être cette adresse est-elle trop récente ou erronée.");
   else if (dirInv.getStatus().code == G_GEO_SERVER_ERROR)
     alert("Une requête de géocodage ou d'itinéraire ne peut aboutir pour une raison inconnue.");
  
   else if (dirInv.getStatus().code == G_GEO_MISSING_QUERY)
     alert("La paramètre q est manquant ou nul. Pour géocoder un itinéraire il faut saisir une adresse destination ou de provenance.");
 
   else if (dirInv.getStatus().code == G_GEO_BAD_KEY)
     alert("Le clé donnée est invalide.");

   else if (dirInv.getStatus().code == G_GEO_BAD_REQUEST)
     alert("L'itinéraire demandé ne peut être traité correctement.");
   
   else alert("Erreur inconnue.");
}

function directionTo(lat,lng)
{
	document.getElementById('direction').innerHTML = '<input type="hidden" name="to" id="to" value="' + lat + ',' + lng + '" /><input type="text" name="from" id="from" value="" /><input type="button" name="OK" value="OK" onclick="get_directions(document.getElementById(\'from\').value,document.getElementById(\'to\').value);" />';
};

function centerMap(id) {
	map.closeInfoWindow() ;
	GEvent.trigger(marker[id],'click');
}

function addSite(alias,module,categorie) {
	map.clearOverlays();
	var xhr; 
	try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
	catch (e) 
	{
		try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
		catch (e2) 
		{
		  try {  xhr = new XMLHttpRequest();     }
		  catch (e3) {  xhr = false;   }
		}
	 }
	 xhr.onreadystatechange  = function()
	{ 
		if(xhr.readyState  == 4)
		{
			if(xhr.status  == 200)
			{
				GEvent.clearListeners(map,"moveend");
				document.getElementById('content').innerHTML = xhr.responseText;
				var redIcon1 = new GIcon(G_DEFAULT_ICON);
				redIcon1.iconAnchor = new GPoint(0, 0);
				redIcon1.infoWindowAnchor = new GPoint(7, 0 );

				var marqueur = new GMarker(map.getCenter(),{icon:redIcon1, draggable: true});
				document.getElementById("latitudeI").value = map.getCenter().lat().toString();
				document.getElementById("longitudeI").value = map.getCenter().lng().toString();

				GEvent.addListener(marqueur, 'click', function()
				{
					if (FCKeditorAPI.GetInstance('developpement').GetXHTML() == null || FCKeditorAPI.GetInstance('developpement').GetXHTML() == '' || FCKeditorAPI.GetInstance('developpement').GetXHTML() == '<br>' || FCKeditorAPI.GetInstance('developpement').GetXHTML() == '<br/>' || FCKeditorAPI.GetInstance('developpement').GetXHTML() == '<br />' )
					{
						var html = "<div style='background-color:#cccccc; font-style:bold; margin-bottom:3px'>" + FCKeditorAPI.GetInstance('titre').GetXHTML() + "</div>" + FCKeditorAPI.GetInstance('detail').GetXHTML();
						html += '<br /><br /><a href="javascript:void(editSite(\''+escape(id)+'\',\''+alias+'\',\''+module+'\')); map.closeInfoWindow();">Editer</a> - <a href="javascript:void(deleteSite(\''+escape(id)+'\',\''+alias+'\',\''+module+'\'));">Supprimer</a>';
    					marker.openInfoWindowHtml(html);
					} else {
						// DEV
						var maxContentDiv = document.createElement("div");
						maxContentDiv.innerHTML = "Chargement...";
						var html = '<div style="background-color:#cccccc; font-style:bold; margin-bottom:3px">' + FCKeditorAPI.GetInstance('titre').GetXHTML() + '</div>' + FCKeditorAPI.GetInstance('detail').GetXHTML();
						html += '<br /><br /><a href="javascript:void(editSite(\''+escape(id)+'\',\''+alias+'\',\''+module+'\')); map.closeInfoWindow();">Editer</a> - <a href="javascript:void(deleteSite(\''+escape(id)+'\',\''+alias+'\',\''+module+'\'));">Supprimer</a>';
						marker.openInfoWindowHtml(html, {maxContent: maxContentDiv, maxTitle: "Plus d\'infos"} );
						var iw = map.getInfoWindow();
      					GEvent.addListener(iw, "maximizeclick", function() { maxContentDiv.innerHTML = FCKeditorAPI.GetInstance('developpement').GetXHTML(); } );
					}
				}); 
				GEvent.addListener(marqueur, 'dragend', function() 
				{
					var point = marqueur.getPoint();
			    	document.getElementById("latitudeI").value = point.lat();
					document.getElementById("longitudeI").value = point.lng();
				});
				map.addOverlay(marqueur);
				marqueur.enableDragging();
			  }
			  else 
				 document.getElementById('content').innerHTML = "Error code : " + xhr.status;
		 }
	};
	
	xhr.open('POST', 'Google/sites.php',  true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.send('action=Add&alias=' + alias + '&module=' + module+'&categorie='+categorie);
}
function deleteSite(id,alias,module,nom){
	if (confirm('Etes-vous sûr(e) de vouloir ce lieu ?'))
	{
		var xhr; 
		try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
		catch (e) 
		{
			try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
			catch (e2) 
			{
			  try {  xhr = new XMLHttpRequest();     }
			  catch (e3) {  xhr = false;   }
			}
		 }
		 xhr.onreadystatechange  = function()
		{ 
			 if(xhr.readyState  == 4)
			 {
				  if(xhr.status  == 200)
					window.location.href = window.location.href; 
				  else 
					 document.getElementById('content').innerHTML = "Error code : " + xhr.status;
			 }
		};
		
		xhr.open('POST', 'Google/sites.php',  true);
		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xhr.send('action=Delete&id='+id+'&alias=' + alias + '&module=' + module);
	}
}
function deleteSIcone () {
	document.getElementById('sIcone').src='';
	document.getElementById('Icone').value='';
}

function editSite(id,alias,module) {
	for (var i in polyline)
	{
		map.removeOverlay(polyline[i]);
	}
	for (var i in marker)
	{
		if (i.toString() != id)
		{	map.removeOverlay(marker[i]);
		}
		else
		{
			marker[i].enableDragging();
			GEvent.clearListeners(marker[i],"click");
			GEvent.clearListeners(marker[i],"mouseover");
			GEvent.addListener(marker[i], 'click', function()
			{
				if (FCKeditorAPI.GetInstance('developpement').GetXHTML() == null || FCKeditorAPI.GetInstance('developpement').GetXHTML() == '' || FCKeditorAPI.GetInstance('developpement').GetXHTML() == '<br>' || FCKeditorAPI.GetInstance('developpement').GetXHTML() == '<br/>' || FCKeditorAPI.GetInstance('developpement').GetXHTML() == '<br />' )
				{
					var html = "<div style='background-color:#cccccc; font-style:bold; margin-bottom:3px'>" + document.getElementById('titre').value + "</div>" + FCKeditorAPI.GetInstance('detail').GetXHTML();
    				marker[i].openInfoWindowHtml(html);
				} else {
					// DEV
					var maxContentDiv = document.createElement("div");
					maxContentDiv.innerHTML = "Chargement...";
					var html = '<div style="background-color:#cccccc; font-style:bold; margin-bottom:3px">' + document.getElementById('titre').value + '</div>' + FCKeditorAPI.GetInstance('detail').GetXHTML();
					marker[i].openInfoWindowHtml(html, {maxContent: maxContentDiv, maxTitle: "Plus d\'infos"} );
					var iw = map.getInfoWindow();
      				GEvent.addListener(iw, "maximizeclick", function() { maxContentDiv.innerHTML = FCKeditorAPI.GetInstance('developpement').GetXHTML(); } );
				}
			});
		}
	}
	GEvent.clearListeners(map,"moveend");
	var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
     }
	 xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200)
                document.getElementById('content').innerHTML = xhr.responseText; 
              else 
                 document.getElementById('content').innerHTML = "Error code : " + xhr.status;
         }
    };
	
	xhr.open('POST', 'Google/sites.php',  true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.send('action=Edit&id='+id+'&alias=' + alias + '&module=' + module);
}

function readSites(alias,module) {
	var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
     }
	 xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200)
                document.getElementById('content').innerHTML = xhr.responseText; 
              else 
                 document.getElementById('content').innerHTML = "Error code : " + xhr.status;
         }
    };
	
	xhr.open('POST', 'Google/sites.php',  true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.send('action=Read&alias=' + alias + '&module=' + module);
}

function readCat(alias, module) {
	var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
     }

    xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200) {
				 if (document.getElementById('categories')) document.getElementById('categories').innerHTML = xhr.responseText; 
			  }
              else 
                 alert("Error code : " + xhr.status);
         }
    };
	
	xhr.open('POST', 'Google/categories.php',  true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.send('action=Read&alias=' + alias + '&module=' + module);
}

function modifieCat(id, alias, module) {
	var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
     }
 
    xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200)
				{
					if (xhr.responseText.indexOf('RELOAD') > 0)
						window.location.href = window.location.href;
					else
						document.getElementById('categories').innerHTML = xhr.responseText;
				}
              else 
                 document.getElementById('categories').innerHTML = "Error code : " + xhr.status;
         }
    }; 

	xhr.open("POST", "Google/categories.php",  true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	if (document.getElementById("newCat").value == '' || document.getElementById("icone").src == 'http://www.applications-services.com/data/icones/default.gif' )
	   	alert('Vous devez saisir un nom de catégorie et choisir un icone par défaut !');
	   else 
	xhr.send("action=Modify&id="+id+"&alias="+alias+"&module="+module+'&newCat='+document.getElementById("newCat").value+"&icone="+document.getElementById("icone").src);
}

function editCat(id, alias, module) {
	var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
     }
 
    xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200)
				 document.getElementById('categories').innerHTML = xhr.responseText;
              else 
                 document.getElementById('categories').innerHTML = "Error code : " + xhr.status;
         }
    }; 

	xhr.open("POST", "Google/categories.php",  true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send("action=Edit&id="+id+"&alias="+alias+"&module="+module);
}

function delCat(categorie,alias,module) {
	var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
     }
 
    xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200)
				readCat(alias,module);
              else 
                 document.getElementById('categories').innerHTML = "Error code : " + xhr.status;
         }
    }; 

	xhr.open("POST", "Google/categories.php",  true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send("action=Delete&delCat="+categorie+"&alias="+alias+"&module="+module);
}

function iconSFix(icone, MarkerID, alias, module) {
	document.getElementById('sIcone').src = icone;
	document.getElementById('Icone').value = icone;
	document.getElementById('sIcones').innerHTML = "";
	marker[MarkerID].setImage(icone);
}

function iconFix(icone,alias,module) {
	document.getElementById('icone').src = icone;
	readSites(alias,module);
}

function deleteFile(fichier,marker,alias,module,mode)
{
	var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
     }
 
    xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200)
			  {
				  if(document.getElementById('sIcones')) document.getElementById('sIcones').innerHTML = xhr.responseText;
				 else document.getElementById('content').innerHTML = xhr.responseText;
				 
			  }
              else 
			  {
				 if(document.getElementById('sIcones'))  document.getElementById('sIcones').innerHTML = "Error code : " + xhr.status;
				 else document.getElementById('content').innerHTML = "Error code : " + xhr.status;
			  }
         }
    }; 

	xhr.open("POST", "Google/listIcons.php",  true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send("alias="+alias+"&marker="+marker+"&module="+module+'&delete='+fichier+'&mode='+mode);
}

function chooseSIcon(icone, marker, alias, module) {
	var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
     }
 
    xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200)
				 document.getElementById('sIcones').innerHTML = xhr.responseText;
              else 
                 document.getElementById('sIcones').innerHTML = "Error code : " + xhr.status;
         }
    }; 

	xhr.open("POST", "Google/listIcons.php",  true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send("alias="+alias+"&marker="+marker+"&module="+module+"&currentIcon="+icone+"&mode=marker");
}

function chooseIcon(icone, alias, module) {
	var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
     }
 
    xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200)
				 document.getElementById('content').innerHTML = xhr.responseText;
              else 
                 document.getElementById('content').innerHTML = "Error code : " + xhr.status;
         }
    }; 

	xhr.open("POST", "Google/listIcons.php",  true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send("alias="+alias+"&module="+module+"&currentIcon="+icone+"&mode=categorie");
}

function showBox(alias,module) {
	if (!document.getElementById('icone'))
	document.getElementById('categories').innerHTML = '<p align="center"><a href="javascript:chooseIcon(document.getElementById(\'icone\').src,\''+alias+'\',\''+module+'\');"><img src="/data/icones/default.gif" id="icone" border="0" hspace="5" /></a><br/><input type="textfield" id="newCat" name="newCat" /> <input type="button" value="Ajouter" onclick="ajouteCat(\''+alias+'\',\''+module+'\');"/></p>' + document.getElementById('categories').innerHTML
}

function ajouteCat(alias,module) {
	var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
     }
 
    xhr.onreadystatechange  = function()
    { 
         if(xhr.readyState  == 4)
         {
              if(xhr.status  == 200)
			  	{
					if (xhr.responseText.indexOf('RELOAD') > 0)
						readCat(alias,module);
					else
						document.getElementById('categories').innerHTML = xhr.responseText;
				}
              else 
                 document.getElementById('categories').innerHTML = "Error code : " + xhr.status;
         }
    }; 

   xhr.open("POST", "Google/categories.php",  true);
   xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   
   if (document.getElementById("newCat"))
   {
	   if (document.getElementById("newCat").value == '' || document.getElementById("icone").src == 'http://www.applications-services.com/data/icones/default.gif')
	   	alert('Vous devez saisir un nom de catégorie et choisir un icone par défaut !');
	   else 
		xhr.send("action=Add&newCat="+document.getElementById("newCat").value+"&icone="+document.getElementById("icone").src+"&alias="+alias+"&module="+module);
   };
}

function deletePoly(id,alias,module) {
	if (confirm('Etes-vous sûr(e) de vouloir ce lieu ?'))
	{
		var xhr; 
		try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
		catch (e) 
		{
			try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
			catch (e2) 
			{
			  try {  xhr = new XMLHttpRequest();     }
			  catch (e3) {  xhr = false;   }
			}
		 }
		 xhr.onreadystatechange  = function()
		{ 
			 if(xhr.readyState  == 4)
			 {
				  if(xhr.status  == 200)
					window.location.href = window.location.href; 
				  else 
					 document.getElementById('content').innerHTML = "Error code : " + xhr.status;
			 }
		};
		
		xhr.open('POST', 'Google/sites.php',  true);
		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xhr.send('action=DeletePoly&id='+id+'&alias=' + alias + '&module=' + module);
	}
}

function addPoly (alias,module,categorie) {
	for (var i in marker)
	{
		map.removeOverlay(marker[i]);
	}
	// map.clearOverlays();
	var poly;
	var count = 0;
	var points = new Array();
	var markers = new Array();
	var icon_url ="http://labs.google.com/ridefinder/images/";
	var tooltip;
	var lineColor = "#0000af";
	var fillColor = "#335599";
	var lineWeight = 3;
	var lineOpacity = .8;
	var fillOpacity = .2;
	tooltip = document.createElement("div");
	tooltip.className="tooltip";
	map.getPane(G_MAP_MARKER_PANE).appendChild(tooltip);
	
	addIcon = function(icon)
	{ // Add icon attributes
		icon.shadow= icon_url + "mm_20_shadow.png";
		icon.iconSize = new GSize(12, 20);
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);
	}
	showTooltip = function (marker)
	{
		tooltip.innerHTML = marker.tooltip;
		tooltip.style.display = "block";

 		// Tooltip transparency specially for IE
		if(typeof(tooltip.style.filter) == "string")
		{
 			tooltip.style.filter = "alpha(opacity:70)";
 		}
		var currtype = map.getCurrentMapType().getProjection();
 		var point= currtype.fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
 		var offset= currtype.fromLatLngToPixel(marker.getLatLng(),map.getZoom());
 		var anchor = marker.getIcon().iconAnchor;
 		var width = marker.getIcon().iconSize.width + 6;
		// var height = tooltip.clientHeight +18;
		 var height = 10;
		 var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y - height)); 
		 pos.apply(tooltip);
	}
	leftClick = function(overlay, point)
	{
		if(point)
		{
			count++;
			if(count%2 != 0)
			{
				// Light blue marker icons
				var icon = new GIcon();
				icon.image = icon_url + "mm_20_blue.png";
				addIcon(icon);
			}
			else
			{
				// Purple marker icons
				var icon = new GIcon();
				icon.image = icon_url +"mm_20_purple.png";
				addIcon(icon);
			}
			// Make markers draggable
			var marker = new GMarker(point, {icon:icon, draggable:true, bouncy:false, dragCrossMove:true});
			map.addOverlay(marker);
			marker.content = count;
			markers.push(marker);
			marker.tooltip = "Point "+ count;

			GEvent.addListener(marker, "mouseover", function() {
				showTooltip(marker);
			});

			GEvent.addListener(marker, "mouseout", function() {
				tooltip.style.display = "none";
			});

			// Drag listener
			GEvent.addListener(marker, "drag", function() {
				tooltip.style.display= "none";
				drawOverlay();
			});

			// Second click listener
			GEvent.addListener(marker, "click", function() {
				tooltip.style.display = "none";

				// Find out which marker to remove
				for(var n = 0; n < markers.length; n++) {
					if(markers[n] == marker) {
						map.removeOverlay(markers[n]);
						break;
					}
				}
				// Shorten array of markers and adjust counter
				markers.splice(n, 1);
				if(markers.length == 0) {
					count = 0;
					document.getElementById("coord").value = '';
				}
				else
				{
					count = markers[markers.length-1].content;
					drawOverlay();
				}
			});
			drawOverlay();
		}
	}

	toggleMode = function ()
	{
		if(markers.length > 1) drawOverlay();
		else
		document.getElementById("coord").value = '';
	}

	drawOverlay = function ()
	{
		document.getElementById("coord").value = '';
		for (var i=0; i<markers.length; i++)
		{
			document.getElementById("coord").value += markers[i].getPoint()+';';
		}
		
		// Check radio button
		var lineMode = document.getElementById("mode0").checked;

		if(poly) { map.removeOverlay(poly); }
		points.length = 0;

		for(i = 0; i < markers.length; i++) {
			points.push(markers[i].getLatLng());
		}
		if(lineMode)
		{
			// Polyline mode
			poly = new GPolyline(points, document.getElementById("color").value, lineWeight, document.getElementById('alpha').options[document.getElementById('alpha').selectedIndex].value);
 			var length = poly.getLength()/1000;
			var unit = " km";
		}
		else
		{
			// Polygon mode
			points.push(markers[0].getLatLng());
			poly = new GPolygon(points, document.getElementById("color").value, lineWeight, document.getElementById('alpha').options[document.getElementById('alpha').selectedIndex].value, document.getElementById("color").value, document.getElementById('alpha').options[document.getElementById('alpha').selectedIndex].value);
			var area = poly.getArea()/(1000*1000);
			var unit = " km&sup2;";
		}
		map.addOverlay(poly);
	}
	
	clearMap = function ()
	{
		// Clear current map and reset arrays
		map.clearOverlays();
		points.length = 0;
		markers.length = 0;
		count = 0;
		document.getElementById("coord").value = '';
	}


	GEvent.addListener(map, "click", leftClick);
	for (var i in marker)
	{
		map.removeOverlay(marker[i]);
	}
	var xhr; 
	try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
	catch (e) 
	{
		try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
		catch (e2) 
		{
		  try {  xhr = new XMLHttpRequest();     }
		  catch (e3) {  xhr = false;   }
		}
	 }
	 xhr.onreadystatechange  = function()
	{ 
		if(xhr.readyState  == 4)
		{
			if(xhr.status  == 200)
			{
				GEvent.clearListeners(map,"moveend");
				document.getElementById('content').innerHTML = xhr.responseText;
				$('#picker').farbtastic('#color');
			  }
			  else 
				 document.getElementById('content').innerHTML = "Error code : " + xhr.status;
		 }
	};
	
	xhr.open('POST', 'Google/sites.php',  true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.send('action=AddPoly&alias=' + alias + '&module=' + module+'&categorie='+categorie);
}

function editPoly (id,alias,module,categorie) {
	for (var i in marker)
	{
		map.removeOverlay(marker[i]);
	}
	//map.clearOverlays();
	var poly;
	var count = 0;
	var points = new Array();
	var markers = new Array();
	var icon_url ="http://labs.google.com/ridefinder/images/";
	var tooltip;
	var lineColor = "#0000af";
	var fillColor = "#335599";
	var lineWeight = 3;
	var lineOpacity = .8;
	var fillOpacity = .2;
	tooltip = document.createElement("div");
	tooltip.className="tooltip";
	map.getPane(G_MAP_MARKER_PANE).appendChild(tooltip);
	
	addIcon = function(icon)
	{ // Add icon attributes
		icon.shadow= icon_url + "mm_20_shadow.png";
		icon.iconSize = new GSize(12, 20);
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);
	}
	showTooltip = function (marker)
	{
		tooltip.innerHTML = marker.tooltip;
		tooltip.style.display = "block";

 		// Tooltip transparency specially for IE
		if(typeof(tooltip.style.filter) == "string")
		{
 			tooltip.style.filter = "alpha(opacity:70)";
 		}
		var currtype = map.getCurrentMapType().getProjection();
 		var point= currtype.fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
 		var offset= currtype.fromLatLngToPixel(marker.getLatLng(),map.getZoom());
 		var anchor = marker.getIcon().iconAnchor;
 		var width = marker.getIcon().iconSize.width + 6;
		// var height = tooltip.clientHeight +18;
		 var height = 10;
		 var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y - height)); 
		 pos.apply(tooltip);
	}
	leftClick = function(overlay, point)
	{
		if(point)
		{
			count++;
			if(count%2 != 0)
			{
				// Light blue marker icons
				var icon = new GIcon();
				icon.image = icon_url + "mm_20_blue.png";
				addIcon(icon);
			}
			else
			{
				// Purple marker icons
				var icon = new GIcon();
				icon.image = icon_url +"mm_20_purple.png";
				addIcon(icon);
			}
			// Make markers draggable
			var marker = new GMarker(point, {icon:icon, draggable:true, bouncy:false, dragCrossMove:true});
			map.addOverlay(marker);
			marker.content = count;
			markers.push(marker);
			marker.tooltip = "Point "+ count;

			GEvent.addListener(marker, "mouseover", function() {
				showTooltip(marker);
			});

			GEvent.addListener(marker, "mouseout", function() {
				tooltip.style.display = "none";
			});

			// Drag listener
			GEvent.addListener(marker, "drag", function() {
				tooltip.style.display= "none";
				drawOverlay();
			});

			// Second click listener
			GEvent.addListener(marker, "click", function() {
				tooltip.style.display = "none";

				// Find out which marker to remove
				for(var n = 0; n < markers.length; n++) {
					if(markers[n] == marker) {
						map.removeOverlay(markers[n]);
						break;
					}
				}
				// Shorten array of markers and adjust counter
				markers.splice(n, 1);
				if(markers.length == 0) {
					count = 0;
					document.getElementById("coord").value = '';
				}
				else
				{
					count = markers[markers.length-1].content;
					drawOverlay();
				}
			});
			drawOverlay();
		}
	}

	toggleMode = function ()
	{
		if(markers.length > 1) drawOverlay();
		else
		document.getElementById("coord").value = '';
	}

	drawOverlay = function ()
	{
		// Check radio button
		var lineMode = document.getElementById("mode0").checked;

		if(poly) { map.removeOverlay(poly); }
		points.length = 0;
		document.getElementById("coord").value = '';

		for(i = 0; i < markers.length; i++)
		{
			points.push(markers[i].getLatLng());
			document.getElementById("coord").value += markers[i].getPoint()+';';
		}
		if(lineMode)
		{
			// Polyline mode
			poly = new GPolyline(points, document.getElementById("color").value, lineWeight, document.getElementById('alpha').options[document.getElementById('alpha').selectedIndex].value);
 			var length = poly.getLength()/1000;
			var unit = " km";
		}
		else
		{
			// Polygon mode
			points.push(markers[0].getLatLng());
			poly = new GPolygon(points, document.getElementById("color").value, lineWeight, document.getElementById('alpha').options[document.getElementById('alpha').selectedIndex].value, document.getElementById("color").value, document.getElementById('alpha').options[document.getElementById('alpha').selectedIndex].value);
			var area = poly.getArea()/(1000*1000);
			var unit = " km&sup2;";
		}
		map.addOverlay(poly);
	}
	
	clearMap = function ()
	{
		// Clear current map and reset arrays
		map.clearOverlays();
		points.length = 0;
		markers.length = 0;
		count = 0;
		document.getElementById("coord").value = '';
	}
	GEvent.addListener(map, "click", leftClick);
	
	var xhr; 
	try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
	catch (e) 
	{
		try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
		catch (e2) 
		{
		  try {  xhr = new XMLHttpRequest();     }
		  catch (e3) {  xhr = false;   }
		}
	 }
	 xhr.onreadystatechange  = function()
	{ 
		if(xhr.readyState  == 4)
		{
			if(xhr.status  == 200)
			{
				GEvent.clearListeners(map,"moveend");
				document.getElementById('content').innerHTML = xhr.responseText;				
				$('#picker').farbtastic('#color');
				map.removeOverlay(polyline[id]);
				for (var l=0; l<coords[id].length; l++)
				{
					leftClick(map,eval('new GLatLng('+coords[id][l]+')'));
				}
				/*
				setAndExecute = function (divId, innerHTML)
				{  
					var div = document.getElementById(divId);  
					div.innerHTML = innerHTML;  
					var x = div.getElementsByTagName("script");   
					for( var i=0; i < x.length; i++)
					{  
						eval(x[i].text);  
					}  
				}  
       */
     
				//var theInnerHTML = xhr.responseText;  
				//var theID = "content";  
				//setAndExecute(theID,document.getElementById('content').innerHTML);
				
			}
			else 
				document.getElementById('content').innerHTML = "Error code : " + xhr.status;
		}
	}
	
	xhr.open('POST', 'Google/sites.php',  true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.send('action=EditPoly&id=' + id + '&alias=' + alias + '&module=' + module + '&categorie=' + categorie);
}



// DIRECTIONS LOCALE

function customPanel(map,mapname,dirn,div)
{
	var html = "";

	waypoint = function (point, type, address)
	{
		var target = '"' + mapname+".showMapBlowup(new GLatLng("+point.toUrlValue(6)+"))"  +'"';
		html += '<table style="border: 1px solid silver; margin: 10px 0px; background-color: rgb(238, 238, 238); border-collapse: collapse; color: rgb(0, 0, 0);">';
		html += '  <tr style="cursor: pointer;" onclick='+target+'>';
		html += '    <td style="padding: 4px 15px 0px 5px; vertical-align: middle; width: 20px;">';
		html += '      <img src="http://www.google.com/intl/en_ALL/mapfiles/icon-dd-' +type+ '-trans.png">'
		html += '    </td>';
		html += '    <td style="vertical-align: middle; width: 100%;">';
		html +=        address;
		html += '    </td>';
		html += '  </tr>';
		html += '</table>';
	}
 
         // === route distance ===
	routeDistance = function (dist) {
		html += '<div style="text-align: right; padding-bottom: 0.3em;">' + dist + '</div>';
	}      
 
	// === step detail ===
	detail = function (point, num, description, dist) {
		var target = '"' + mapname+".showMapBlowup(new GLatLng("+point.toUrlValue(6)+"))"  +'"';
		html += '<table style="margin: 0px; padding: 0px; border-collapse: collapse;">';
		html += '  <tr style="cursor: pointer;" onclick='+target+'>';
		html += '    <td style="border-top: 1px solid rgb(205, 205, 205); margin: 0px; padding: 0.3em 3px; vertical-align: top; text-align: right;">';
		html += '      <a href="javascript:void(0)"> '+num+'. </a>';
		html += '    </td>';
		html += '    <td style="border-top: 1px solid rgb(205, 205, 205); margin: 0px; padding: 0.3em 3px; vertical-align: top; width: 100%;">';
		html +=        description;
		html += '    </td>';
		html += '    <td style="border-top: 1px solid rgb(205, 205, 205); margin: 0px; padding: 0.3em 3px 0.3em 0.5em; vertical-align: top; text-align: right;">';
		html +=        dist;
		html += '    </td>';
		html += '  </tr>';
		html += '</table>';
	}

	 /* copyright = function (myString) {
		html = html + '<div style="font-size: 0.86em;">' + myString + "</div>";
	 }*/
     
	convert_dist = function (meters)
	{
         	// units = parseFloat(document.theForm.units[document.theForm.units.selectedIndex].value);
			units = 1000;
         	output = parseInt(meters,10); name = 'm';
         	if (units == 1) {
         	} else if (units == 1.7018) {
         		if (output > 1701) {
         			output = output / 17018;
         			name = 'KSm';
         		} else {
         			output = output / units;
         			name = 'Smoots';
         		}
         	} else if (units == 1000) {
         		if (output > 999) {
         			output = output / units;
         			name = 'Km'; 
         		}
         	} else if (units == 1609) {
         		if (output > 1608) {
         			output = output / units;
         			name = 'mi'; 
         		} else if (output > 100) {
         			output = output * (3.281/3);
         			name = 'yds'; 
         		} else {
         			output = output * 3.281;
         			name = 'ft'; 
         		}
         	} else {
         		output = output / units;
         		name = 'Nautical Miles';
         	}
         	bef = output; 
         	output = Math.round(output*10.0)/10.0;
         	return output+"&nbsp;"+name;
         }
 
	// === read through the GRoutes and GSteps ===
	for (var i=0; i<dirn.getNumRoutes(); i++)
	{
		if (i==0) {
			var type="play";
		} else {
			var type="pause";
		}
		var route = dirn.getRoute(i);
		var geocode = route.getStartGeocode();
		var point = route.getStep(0).getLatLng();
		// === Waypoint at the start of each GRoute
		waypoint(point, type, geocode.address);
		routeDistance(convert_dist(route.getDistance().meters)+" (&plusmn; "+route.getDuration().html+")");

		for (var j=0; j<route.getNumSteps(); j++)
		{
			var step = route.getStep(j);
			// === detail lines for each step ===
			detail(step.getLatLng(), j+1, step.getDescriptionHtml(), convert_dist(step.getDistance().meters));
		}
	}
         
	 // === the final destination waypoint ===   
	 var geocode = route.getEndGeocode();
	 var point = route.getEndLatLng();
	 waypoint(point, "stop", geocode.address);
			  
	 // === the copyright text ===
	//copyright(dirn.getCopyrightsHtml());

	// === drop the whole thing into the target div
	div.innerHTML = '<p align="right"><a href="javascript:Imprime();" target="_self">Imprimer l\'itin&eacute;raire</a></p><div id="routeDet" class="tplTextePage">'+html+'<div style="font-size: 0.86em;">' + dirn.getCopyrightsHtml() + "</div>"+"</div>";
}
function Imprime() {
	window.open('/DocImage/googlePrint.php','GooglePrint','width=600, height=450, scrollbars=1, menuBar=1, resizable=1');
}
