 // byte2hex - Takes number n from 0-255 and converts to hexadecimal string e.g. 'AA'
   // Courtesy Jim Bumgardner of krazydad.com
   function byte2Hex(n)
   {
    var nybHexString = "0123456789ABCDEF";
    return String(nybHexString.substr((n >> 4) & 0x0F,1)) + nybHexString.substr(n & 0x0F,1);
   }

   // RGB2Color - Takes 3 hexadecimal string color components and concatenates into standard HTML format
   // Courtesy Jim Bumgardner of krazydad.com
   function RGB2Color(r,g,b)
   {
    return '#' + byte2Hex(r) + byte2Hex(g) + byte2Hex(b);
   }

   
    /** Utility Functions **/
   // rand - Generates random number from 1 to n, inclusive
    function rand ( n )
    {
      return ( Math.floor ( Math.random ( ) * n + 1 ) );
    }
	
	// supprime tout ce qui a été ajouté sur une carte (marqueurs, cercles ...)
	function clearShapes(){
	map.clearOverlays();
    }
	
	
	// 
	  function draw(pos,radius) {
   mapClick(0,pos,radius);
   }
   // mapClick - Handles the event of a user clicking anywhere on the map
   // Draws either stars or polygons with random variation in arguments
   // with clicked point as center.

   function mapClick(overlay, clickedPoint,monRadius) {
	center=clickedPoint;
	//function drawCircle(center, radius, nodes)
	
	// Esa 2006
	//calculating km/degree
	var latConv = center.distanceFrom(new GLatLng(center.lat()+0.1, center.lng()))/100;
	var lngConv = center.distanceFrom(new GLatLng(center.lat(), center.lng()+0.1))/100;
		
	var polyColor = RGB2Color(rand(255),rand(255),rand(255));
	var nodes=30;
	var radius=monRadius;
	bounds = new GLatLngBounds();
	//Loop 
	var points = [];
	var step = parseInt(360/nodes)||10;
	for(var i=0; i<=360; i+=step)
	{
	var pint = new GLatLng(center.lat() + (radius/latConv * Math.cos(i * Math.PI/180)), center.lng() + 
	(radius/lngConv * Math.sin(i * Math.PI/180)));
	points.push(pint);
	bounds.extend(pint); //this is for fit function
	}
	
	var polygon = new GPolygon(points,"#000000",2,.5,polyColor,.5);
	map.addOverlay(polygon);
	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)-1);// le -1 permets d'	voir un zoom arrière pour bien voir tous les marqueurs
	
     }
	 
	 
	 function afficherAdresse(address,bouton) {

	   if (!address) {
	   if (document.forms["coord"].elements["list2"].selectedIndex==-1) {
	   ville=""
	   } else {
	    ville=document.forms["coord"].elements["list2"].options[document.forms["coord"].elements["list2"].selectedIndex].text
	   }
	   
	   address=document.forms["coord"].elements["adresse"].value+" , "+document.forms["coord"].elements["list1"].value +" , "+ville
//alert(address)		 	  
 }
         if (geocoder) {
           geocoder.getLatLng(
             address,
             function(point) {
               if (!point) {
                 alert("Google Maps ne parvient pas à localiser cette adresse. Merci de bien vouloir la compléter ou la modifier. Il vous sera possible, par la suite, d’affiner votre positionnement sur la carte.");
               } else {
			   if (bouton) {EnableButton()}
			  	// enléve les markers présents
               	map.clearOverlays()
                //centre la carte sur le point donné, avec une valeur de zoom 
                map.setCenter(point, 16);
 			   //Création du marqueur au centre de la carte. 
				var point = map.getCenter();
				
				var marqueur = new GMarker(point, {draggable: true});
				// Ajoutons le marqueur à la carte.
				 map.addOverlay(marqueur);
				document.forms["coord"].elements["slct_points"].value=point
				GEvent.addListener(marqueur, "dragend", function() 
				 {
				document.forms["coord"].elements["slct_points"].value=this.getPoint()
				 }
				);
               }
             }
           );
         }
       }
