//Declare global variables    
  var geocoder = null;
  var router = null;
  var routePoints = [];
  var routeID = null;
   
  
  function goMap24() {
   //alert(start, destination);
   Map24.loadApi( ["core_api", "wrapper_api"] , map24ApiLoaded );
  }
  
  function map24ApiLoaded(){
    var ort1 = document.getElementsByName('ort')[0].value;
    var plz1 = document.getElementsByName('plz')[0].value;
    var ort2 = document.getElementsByName('ort_e')[0].value;
    var plz2 = document.getElementsByName('plz_e')[0].value;
    
    if(ort2 != ""){ destination = ort2+','+plz2+', DE';}
    else{           destination = ort1+','+plz1+', DE';}
    
    Map24.MapApplication.init( { NodeName: "maparea" } );
    startRouting('Neukirchen - Vluyn', destination);
  }
 
  function startRouting(start, destination){
    //Retrieve start and destination of the route from the input fields
    //var start = Map24.trim( $v('start') );
    //var destination = Map24.trim( $v('destination') );
    //var start = "Wuppertal";
    //var destination = "Berlin";
    
    //Check if the start and the destination form fields are empty
    if( start == "" ) { alert("Please enter start address!"); return; }
    if( destination == "" ) { alert("Please enter destination address!"); return; }
	  
    //Disable the button for starting a route calculation.
    //document.getElementById("button_calculate_route").disabled = true;
    
    //Create a geocoder stub
    var geocoder = new Map24.GeocoderServiceStub();
    
    //Geocode the start point of the route
    geocoder.geocode({ 
      SearchText: start, 
      //Define the name of the callback function that is called when the result is available on the client.
      CallbackFunction: setRouteEndPoint, 
      //Set a parameter that is passed to the callback function. The parameter defines that this is the start point.
      CallbackParameters: {position: "start"}
    });
    
    //Geocode the destination point of the route
    geocoder.geocode({
      SearchText: destination,  
      CallbackFunction: setRouteEndPoint,
      CallbackParameters: {position: "destination"}
    });
  }
  
  //Callback function that is called when the geocoding result is available.
  //The locations parameter contains an array with multiple alternative geocoding results.
  //The params parameter passes the value of CallbackParameters that specifies which route 
  //end point is returned (start or destination point).
  function setRouteEndPoint(locations, params){
 
    //Access the geocoded address and add it to the routePoints array.
    //The geocoded address is stored at the first position in the locations array.
    routePoints[ params.position ] = locations[0];
    
    //After both the start and the destination addresses are geocoded, this function calls the calculateRoute() function.
    if( typeof routePoints["start"] != "undefined" && typeof routePoints["destination"] != "undefined")
      calculateRoute(); 
  }
  
  //Calculate the route.
  function calculateRoute() {
    router = new Map24.RoutingServiceStub();
    router.calculateRoute({
      Start: routePoints["start"],
      Destination: routePoints["destination"],
      CallbackFunction: displayDistance,
      DescriptionLanguage: "DE",
      CalculationMode: "Shortest",
      VehicleType: "Car",
      MinQualityOfGeocodeResults: "ZIP",      
      ShowRoute: false
    });
    //document.getElementById("print").disabled = true;
    routePoints = [];
  }
  
    function displayDistance( route ){
      // Rueckgabe an Hidden-Field
      document.getElementsByName("entfernung")[0].value = (route.TotalLength/1000);
      // Form submitten
      submit_form(4);
    
    }

