	var map;
	var gdir;
	var from = "";
	var to = "";
	var geocoder = null;
	var addressMarker;
	var dist = "0"; 
	var totalmiles = 1000;
	
	var verbrauch;
	var strecke;
	var kpreis;
	var kraftstoff;
	var mitfahrer;
	var zusatzkosten;
    var oldfuel;

    function gmload() {
      if (GBrowserIsCompatible()) {   
		map = new GMap2(document.getElementById("map")); 
        map.setCenter(new GLatLng(50, 10), 3);
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
      }
    }
    
    function setDirections(fromAddress, toAddress) {
      $("#strecke").addClass("ac_loading");
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": + "de" });
    }

	function onGDirectionsLoad(){ 
	   //The working version
	   $("#strecke").removeClass("ac_loading");
	   document.getElementById("strecke").value = parseInt(gdir.getDistance().meters/1000);
	   calc();
	}

    function handleErrors(){
     $("#strecke").removeClass("ac_loading");
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("keine Adresse\nError code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}
    
    function initializeVars()
    {
	  	if (document.formCalculator.from.value!="" && document.formCalculator.to.value!="")
    	{
	  		if (document.formCalculator.from.value!=from || document.formCalculator.to.value!=to)
    		{
    			from=document.formCalculator.from.value;
    			to=document.formCalculator.to.value;
    			setDirections(from, to);
    		}
    	}
    	
		if(isNaN(document.formCalculator.strecke.value)) document.formCalculator.strecke.value = 0;
		strecke = eval(Komma(document.formCalculator.strecke.value));
		if(isNaN(document.formCalculator.verbrauch.value)) document.formCalculator.verbrauch.value = 0;
		verbrauch = eval(Komma(document.formCalculator.verbrauch.value));
		if(isNaN(document.formCalculator.kpreis.value)) document.formCalculator.kpreis.value = 0;
		kpreis = eval(Komma(document.formCalculator.kpreis.value));
		if(isNaN(document.formCalculator.zusatzkosten.value) || document.formCalculator.zusatzkosten.value == "") document.formCalculator.zusatzkosten.value = 0;
		zusatzkosten = eval(Komma(document.formCalculator.zusatzkosten.value));
        kraftstoff = parseInt(document.formCalculator.kraftstoff.selectedIndex+1);
		mitfahrer = eval(document.formCalculator.mitfahrer.options[document.formCalculator.mitfahrer.selectedIndex].value);
		if (mitfahrer < 0) mitfahrer=0;  
    }
    
    function calcCO2()
    { 
		if (kraftstoff == 4) co2faktor=27;
		else co2faktor=24;   
        document.formCalculator.co2.value = Math.round(verbrauch*co2faktor*strecke*mitfahrer/1000*100)/100; 
    }
    
    function calcPrice()
    {   
       
		if (kraftstoff == 1) 
		{
			if (document.formCalculator.kpreis.value=="" || oldfuel != kraftstoff) document.formCalculator.kpreis.value=fuelprice[2];
			document.formCalculator.bundesdurchschnitt.value=fuelprice[2];
		}
		if (kraftstoff == 2)
		{
			if (document.formCalculator.kpreis.value=="" || oldfuel != kraftstoff) document.formCalculator.kpreis.value=fuelprice[3];
			document.formCalculator.bundesdurchschnitt.value=fuelprice[3];
		}
		if (kraftstoff == 3)
		{
			if (document.formCalculator.kpreis.value=="" || oldfuel != kraftstoff) document.formCalculator.kpreis.value=fuelprice[4];
			document.formCalculator.bundesdurchschnitt.value=fuelprice[4];
		}
		if (kraftstoff == 4)
		{
			if (document.formCalculator.kpreis.value==""  || oldfuel != kraftstoff) document.formCalculator.kpreis.value=fuelprice[1];
			document.formCalculator.bundesdurchschnitt.value=fuelprice[1];
		}
        oldfuel = kraftstoff;
 		kpreis = eval(Komma(document.formCalculator.kpreis.value))
		document.formCalculator.erg.value = Math.round((((strecke/100)*verbrauch*kpreis+zusatzkosten)/(mitfahrer+1))*100)/100;
    }
    
    function Komma(text){
        text=text.replace(/,/g,".");
        return text;
    }
    
    function calc()
    {
    	initializeVars();
    	calcPrice();
    	calcCO2();
    }
    
    function updateDirections() {
      if(document.formCalculator.from.value!='' && document.formCalculator.to.value!='') {
        setDirections(document.formCalculator.from.value, document.formCalculator.to.value);
      }
    }

