			var color = null;
			var descriptionLanguage = null;
			var calculationMode = null;
			var routePoints = new Array();
			var type = null;
			var geocoder = null;
			var router = null;
			var routePointsCount = null;
			var geocodedPointsCount = null;
			var routeID = null;
			var viaLocs = new Array();
			var startLoc = null;
			var destLoc = null;
  
			function goMap24() {
			Map24.loadApi( ["core_api", "wrapper_api"] , map24ApiLoaded );
			}
  
			function map24ApiLoaded(){
			Map24.MapApplication.init( { NodeName: "maparea" } ); 
			}
			
			function startRouting(){
  	
			//Initialize the array for storing route points
			routePoints = {};
			//Initialize the counter for geocoded addresses
			geocodedPointsCount = 0;
			
			//Access the calculation mode from the radio buttons
			if ( document.getElementById('fast').checked )
			calculationMode = "Fastest";
			else
			calculationMode = "Shortest";
			
			//Access the type of route to be calculated (either for car or pedestrian)
			type = Map24.trim( $v('type') );
			
			//Access the description language setting
			descriptionLanguage = Map24.trim( $v('descriptionLanguage') );
			
			
			if(descriptionLanguage == "en"){
				descriptionLanguage = document.getElementById("measuringUnit").value;
			}
    
			
			var start = Map24.trim( $v('start') );
			var destination = Map24.trim( $v('destination') );
    
    //Check if the fields for setting the start and destination points are empty
    if( start == "" ) { alert("Entrer un lieu de depart"); return; }
    if( destination == "" ) { alert("Please enter destination address!"); return; }
    
    //Retrieve via points from the input fields. 
    //Ignore empty fields and fields that are filled with whitespaces only.
    var vias = [];
    for( var i=0; i < 4; i++ ){
      if( Map24.trim( $v( "via_"+i ) ) != "" )
        vias.push( $v("via_"+i) );
    }
    
    //Create a counter that contains the number of route points
    routePointsCount = 2 + vias.length;
   
    //Disable the button for starting a route calculation.
    document.getElementById("button_calculate_route").disabled = true;
 
    //Create a geocoder stub
    if( geocoder == null ) geocoder = new Map24.GeocoderServiceStub();
    
    //Geocode the start address of the route
    //Define the name of the callback function that is called when the result is available on the client.
    geocoder.geocode({
      SearchText: start,
      CallbackFunction: setRoutePoint,
      CallbackParameters: {position: "start"}
    });
    
    //Geocode the destination address of the route.
    geocoder.geocode({
      SearchText: destination,
      CallbackFunction: setRoutePoint,
      CallbackParameters: {position: "destination"}
    });
    
    //Geocode the via points.
    for( var i=0; i < vias.length; i++ ) {
      geocoder.geocode({
        SearchText: vias[i],
        CallbackFunction: setRoutePoint,
        CallbackParameters: { position: "via", index: i }
      });
    }
    
  }

  function setRoutePoint(locations, params){
  	
    if( params.position == "via") {
      
      if( typeof routePoints[ "via" ] == "undefined") 
        routePoints[ "via" ] = [];
    
      routePoints[ params.position ][ params.index ] = locations[0];
    }
    else 

      routePoints[ params.position ] = locations[0];

      geocodedPointsCount++;

    if( geocodedPointsCount == routePointsCount ) {
      calculateRoute(); 
	  }
  }

  function calculateRoute() {

    for (var i=0; i<(routePointsCount-2); i++){
      var via = new Map24.Location({
        Longitude: routePoints["via"][i].getLongitude(),
        Latitude: routePoints["via"][i].getLatitude()
      })

      via.setTransitRadius( parseInt(Map24.trim(document.getElementById('radius'+i).value * 1000)) );

      routePoints["via"][i] = via;
    }
    document.getElementById("print").disabled = true; 

    if( router == null ) router = new Map24.RoutingServiceStub();

    router.calculateRoute({
      Start: routePoints["start"],
      Destination: routePoints["destination"],
      CalculationMode: calculationMode,
      VehicleType: type,
      ViaPoints: routePoints["via"],
      DescriptionLanguage: descriptionLanguage,
      CallbackFunction: displayRoute,

      ShowRoute: false  
    });
  }

  function displayRoute( route ){

    routeID = route.RouteID;

    color = document.getElementById("colorBox").value;

    router.showRoute({
      RouteId: routeID,
      Color: [color, 150]
    });
    
    //Add a location at the position of the route start point.
    startLoc = new Map24.Location({
      Longitude: routePoints["start"].getLongitude(),
      Latitude: routePoints["start"].getLatitude(),
      Description: "Start Point",
      SymbolId: 20950
    });
    startLoc.commit();

    destLoc = new Map24.Location({
      Longitude: routePoints["destination"].getLongitude(),
      Latitude: routePoints["destination"].getLatitude(),
      Description: "Destination Point",
      SymbolId: 20958
    });
    destLoc.commit();    

    for(var i = 0; i < (routePointsCount-2); i++){
      var loc = new Map24.Location({
        Longitude: routePoints["via"][i].getLongitude(),
        Latitude: routePoints["via"][i].getLatitude(),
        Description: "Via Point "+(i+1),
        SymbolId: 20951
      });
      loc.commit();
      viaLocs[i] = loc;	
    }

    var totalTime = ((route.TotalTime)/(60*60) ).toPrecision(3) 

    var totalLength = (route.TotalLength/1000) 
	
	var div_content = "<div style=\"padding:4px; margin-bottom:10px; background-color:#990000; color:#ccff66; border:1px solid #ffcc33\">D&eacute;tail du trajet</div>";
    div_content+="<div style=\"margin-bottom:10px; padding:4px; border:1px solid #ffcc33;\">";
	div_content += "<strong>Temps de parcours</strong> : " + totalTime + " h<br>" ;     
    div_content += "<strong>Distance totale</strong> : "+ totalLength +" km<br>";
    div_content += "<br>";
 	
    for(var i = 0; i < route.Segments.length; i++){
      if( typeof route.Segments[i].Coordinates != "undefined" ) {
        coordinates = route.Segments[i].Coordinates;

        var longitudes = route.Segments[i].Coordinates.Longitudes.toString().split("|");
        var latitudes = route.Segments[i].Coordinates.Latitudes.toString().split("|");

        var centerLon = longitudes[parseInt(longitudes.length / 2)];
        var centerLat = latitudes[parseInt(latitudes.length / 2)];
      }
		
      for(var j = 0; j < route.Segments[i].Descriptions.length; j++){
        div_content += "<div style=\"margin-bottom:5px\"><strong>"+ (i+1) + "</strong>. " + route.Segments[i].Descriptions[j].Text.replace(/(\[|\[\/)[0-9A-Z_]+\]/g, '' ) 
        + "<img src=\"http://www.rando-valdeloire.fr/depts/zoom_button.png\" alt=\"Centrer\" align=\"absmiddle\" onclick=\"centerOnSegment("+centerLon+", "+centerLat+");\"/></div>"
      }
	  	
    }
	div_content+="</div>";
	//Show the route description
	document.getElementById('routeDescription').innerHTML = div_content;
	//Enable the buttons for hiding or removing the route, and for printing the route description
	document.getElementById("button_hide_route").disabled = false;
	document.getElementById("button_remove_route").disabled = false;
	document.getElementById("print").disabled = false;
	}

	function showRoute (routeID) {
	router.showRoute( {RouteId: routeID, Color: [color, 150]} );
	startLoc.show();
	destLoc.show();
	for(var i = 0; i < (routePointsCount-2); i++){
	viaLocs[i].show();
	}
	document.getElementById("button_show_route").disabled = true;
	document.getElementById("button_hide_route").disabled = false;
	document.getElementById("button_remove_route").disabled = false;
	}
  
	function hideRoute(routeID) {
	router.hideRoute( {RouteId: routeID} );
	startLoc.hide();
	destLoc.hide();
	for(var i = 0; i < (routePointsCount-2); i++){
	viaLocs[i].hide();
	}  
	document.getElementById("button_remove_route").disabled = true;
	document.getElementById("button_show_route").disabled = false;
	document.getElementById("button_hide_route").disabled = true;
	}
  
	function removeRoute(routeID) {
	router.removeRoute( {RouteId: routeID} );
	startLoc.remove();
	destLoc.remove();
	for(var i = 0; i < (routePointsCount-2); i++){
	viaLocs[i].remove();
	}

	document.getElementById("button_show_route").disabled = true;
	document.getElementById("button_hide_route").disabled = true;
	document.getElementById("button_remove_route").disabled = true;
	document.getElementById("routeDescription").innerHTML = "";
	document.getElementById("button_calculate_route").disabled = false;
	document.getElementById("print").disabled = true;   
	}
 
  	function centerOnSegment (centerLon, centerLat){
    	Map24.MapApplication.center( { Coordinate:new Map24.Coordinate(centerLon, centerLat), MinimumWidth: 3034 } );
  	}
  
  	function printRouteDescription(){
    	var printContent = document.getElementById("routeDescription");
    	var windowPrint = window.open('','','left=0,top=0,width=0,height=0,toolbar=0,scrollbars=0,status=0');
    	windowPrint.document.write(printContent.innerHTML);
    	windowPrint.document.close();
    	windowPrint.focus();
    	windowPrint.print();
    	windowPrint.close();
	}

  	function $v( id ) { 
    	return   (document.getElementById( id ).value != "undefined") ? document.getElementById( id ).value : ""; 
	}
	
	function centerOnCoordinate(longitude,latitude) {
   			 Map24.MapApplication.center( {Longitude:latitude, Latitude:longitude, MinimumWidth: 3500} );
  			}
	
	 function geocode( searchText ){
    if(Map24.trim( searchText ) == "") { alert("Please enter an address."); return; }
  
    var geocoder = new Map24.GeocoderServiceStub();
    //Geocodes the address. The address is passed in the Search field. The Alternatives field defines the number
    //of geocoded addresses that are returned in the response. You must pass the name of the callback function
    //that is called as soon as the client has received the response.
    geocoder.geocode( { SearchText: Map24.trim( searchText ), MaxNoOfAlternatives: 10, CallbackFunction: printResult } );
  }
    
  //Callback function that is called after the client has received the response.
  //This function accesses the array of geocoded addresses and shows them in a list.
  //The elements of this array are objects of the type Map24.Location.
  function printResult( locs ){
    //Center the map view on the first element in the array of geocoded addresses. A Map24.Location object has several
    //methods and properties which you can find in the API documentation for the corresponding class.
    Map24.MapApplication.center( { Longitude: locs[0].getLongitude(), Latitude: locs[0].getLatitude(), MinimumWidth: 2500 } );
    
    //Create a list that shows the results.
    var result = "<br /><center><b>Search results:</b></center><hr />";
    
    //Iterate through the array of locations.
    for( var i=0; i<locs.length; i++ ){
      
      //Output all relevant properties of all locations.
      result += "<b>Result Nr."+(i+1)+"</b><br />";  
      
      result += "Longitude "+[i+1]+": "+locs[i].getLongitude()+"<br />";
      result += "Latitude "+[i+1]+": "+locs[i].getLatitude()+"<br />";
 
      result += "City: "+locs[i].getCity()+"<br />";
      result += "Zip: "+locs[i].getZip()+"<br />";
      result += "County: "+locs[i].getCounty()+"<br />";
      result += "State: "+locs[i].getState()+"<br />";
      result += "Country: "+locs[i].getCountry()+"<br />";
      result += "<input type=\"button\" value=\"Center on Result\" onclick=\"Map24.MapApplication.center( {Longitude: "+locs[i].getLongitude()+", Latitude: "+locs[i].getLatitude()+"});\" />";            
      result += "<hr/>";
    }
    document.getElementById("geocodingresults").innerHTML = result;      
  }
