	//Algemene variabelen
	var mapWidth; // Lat
	var mapHeight; //Lng
	var lastCenterLat; // Laatste lat van opvraag huizen
	var lastCenterLng; // Laatste lng van opvraag huizen
	
	var map;
	var icon;
	var direction;
	
	function load() { // Bij het laden van de pagina de googlemaps laden
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("map"));
			
			// Controls toevoegen
			map.addControl(new GLargeMapControl());			
			
			// Midden en zoom bepalen
			map.setCenter(new GLatLng(52.045974, 5.634251), 7);
			map.setZoom(6);
						
		}
	}
	
	function drawRoute(query1, query2){ // De route tekenen
		
		if(direction){
			direction.clear();
		}
		direction = new GDirections(map);		
		direction.load("from:" + query1 + " to: " + query2);	
		
		GEvent.addListener(direction, "load", onGDirectionsLoad)
		GEvent.addListener(direction, "error", handleErrors);
	}
	
	function onGDirectionsLoad(){ // Na het laden van het directions opject
		
		var sdepartureProvince = document.getElementById("departure_province");
		var sdepartureCity = document.getElementById("departure_city");
		var sdeparturePostalcode = document.getElementById("departure_postalCode");
		var sdestinationProvince = document.getElementById("destination_province");
		var sdestinationCity = document.getElementById("destination_city");
		var sdestinationPostalcode = document.getElementById("destination_postalCode");
		
		var departureProvince = sdepartureProvince.options[sdepartureProvince.selectedIndex].value;
		var departureCity = sdepartureCity.options[sdepartureCity.selectedIndex].value;
		var departurePostalcode = sdeparturePostalcode.options[sdeparturePostalcode.selectedIndex].value;
		var destinationProvince = sdestinationProvince.options[sdestinationProvince.selectedIndex].value;
		var destinationCity = sdestinationCity.options[sdestinationCity.selectedIndex].value;
		var destinationPostalcode = sdestinationPostalcode.options[sdestinationPostalcode.selectedIndex].value;

		var calculation;
		var distance;
		var duration;
		
		calculation = document.getElementById('calculation'); // Een table object
		
		distance = direction.getDistance().meters / 1000;
		duration = direction.getDuration().seconds / 60;		
		
		document.getElementById('departure').value = departurePostalcode + " " + departureCity + " (" + departureProvince + ")";
		document.getElementById('destination').value = destinationPostalcode + " " + destinationCity + " (" + destinationProvince +  ")";
		document.getElementById('distance').value = Math.round(distance * 10) / 10 + " KM";
		document.getElementById('duration').value = Math.round(duration) + " minuten";
		document.getElementById('price').value = "€" + calculatePrice(distance);		
		
	}
	
    function handleErrors(){
		if(direction.getStatus().code == G_GEO_UNKNOWN_ADDRESS){
			alert('De opgegeven locatie bestaat niet.');
		}else{
			alert('Er is een onbekende fout opgetreden tijdens het berekenen van de route. Foutcode: ' + direction.getStatus().code);	
		}
	   
	}
	
	function calculatePrice(distance){  // De prijs berekenen a.d.h.v. het aantal kilometers
		
		var ret;		
		ret=Math.round((7.50 + ((distance - 2) * 2.20)) * 100) / 100;
		if(ret < 7.50) ret = 7.50;
		
		return ret;
		
	}
	
