var geocoder, location1, location2, map, directions;
var address1 = "Sophiastraat, 4811 Breda";
geocoder = new GClientGeocoder();

var kmdistance;
var totalcost;


function calculateDistance()
{
	try
	{
		var glatlng1 = new GLatLng(location1.lat, location1.lon);
		var glatlng2 = new GLatLng(location2.lat, location2.lon);
		var miledistance = glatlng1.distanceFrom(glatlng2, 3959).toFixed(1);
		var kmdistance = (miledistance * 1.609344).toFixed(1);
 		var totalcost  = Math.round((kmdistance * 0.35)*100)/100;
		document.getElementById('results').innerHTML = 'Uw Adres: </strong>' + location2.address + '<br /><strong>Afstand af te leggen: </strong>' + kmdistance + ' km <br <strong>Voorrijkosten:</strong> &euro; '+totalcost+' (excl btw)';
			
	}
	catch (error)
	{
		alert(error);
	}
}


function calculatePrice(postal){
	var address2 = postal;
	
	geocoder.getLocations(address1, function (response) {
		if (!response || response.Status.code != 200){
			alert("Sorry, we were unable to geocode the first address");
		}	else{
			location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
			
			geocoder.getLocations(address2, function (response) {
				if (!response || response.Status.code != 200){
					alert("Sorry, we were unable to geocode the second address");
				}else{
					location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
					var glatlng1 = new GLatLng(location1.lat, location1.lon);
					var glatlng2 = new GLatLng(location2.lat, location2.lon);
					var miledistance = glatlng1.distanceFrom(glatlng2, 3959).toFixed(1);
					kmdistance = (miledistance * 1.609344).toFixed(1);
					totalcost  = Math.round(((kmdistance * 0.35)*1.19)*100)/100;		
							
					return totalcost;
				}
			});
		}			
	});
} 
		
function getValue(value){
	calculatePrice(value);
	alert(totalcost);
	
}

		

