	var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0; 
    var map;
    var dirObj;
    var geocoder = null;
    var addressMarker;
    var bGoogleEarthPlugin = false;
    var bTrapicOverlay = false;
    var bGoogleStreetView = false;

    function initMaps() {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map_canvas"), {draggableCursor:"crosshair"});
		/*Google Earth Plugin addon*/
		//alert(bGoogleEarthPlugin);
		if(bGoogleEarthPlugin){
			map.addMapType(G_SATELLITE_3D_MAP);
			var mapControl = new GMapTypeControl();
			map.addControl(mapControl);
			map.setMapType(G_SATELLITE_3D_MAP);
		}
		/*Traphic overlay*/
		//alert(bTrapicOverlay);
		if(bTrapicOverlay){
			var trafficOptions = {incidents:true};
    		trafficInfo = new GTrafficOverlay(trafficOptions);
   			map.addOverlay(trafficInfo);
		}
		
		/*StreetView */
		if(bGoogleStreetView){
		
		}
		          
        dirObj = new GDirections(map, document.getElementById("map_route"));
        GEvent.addListener(dirObj, "error", handleErrors);
		GEvent.addListener(dirObj, 'load', function(){ // Listnener for directions on load 

		}); 
        geocoder = new GClientGeocoder();
        geocoder.setBaseCountryCode('NL');
        showAddress(defaultAddress);
      }
    }
    
    function showAddress(address) {
    if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
		      map.addControl(new GSmallMapControl());
        	  map.addControl(new GMapTypeControl());
        	  map.setCenter(point, 13);
            }
          }
        );
      }
    }
    
    function handleErrors(){
		if (dirObj.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
			alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + dirObj.getStatus().code);
		else if (dirObj.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: " + dirObj.getStatus().code);
		else if (dirObj.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. or directions requests, this means that no query was specified in the input.\n Error code: " + dirObj.getStatus().code);
		else if (dirObj.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: " + dirObj.getStatus().code);
		else if (dirObj.getStatus().code == G_GEO_BAD_REQUEST)
			alert("A directions request could not be successfully parsed.\n Error code: " + dirObj.getStatus().code);
		else 
			alert("An unknown error occurred.");
	}
	
	function setDirections() {
    sFrom = document.getElementById('from').value;
    if(sFrom.toLowerCase().indexOf(',Netherlands')==-1){
    	sFrom += ' , netherlands'
    }
    sTo = defaultAddress;
    dirObj.load("from: " + sFrom + " to: " + sTo,{ "locale": 'nl,getPolyline:true,getSteps:true' });
	document.getElementById('map_route').style.display = 'block';
	document.getElementById('Printroute').style.display = 'block';		
    }
    
    function showRoute(){
    	setDirections()
    }
    function printRoute(){
		var f = replaceSubstring(document.getElementById( 'from' ).value, " ", "+");
		if(f.toLowerCase().indexOf(',Netherlands')==-1){
    	f += ' , netherlands'
    	}
		var t = replaceSubstring(defaultAddress, " ", "+");
		var url = 'http://maps.google.nl/maps?saddr=';
		url += f+'&daddr='+t+'&ie=UTF8&z=8&om=1&pw=2';
		
		window.open(url,'route');
	}
	
	function loadMaps() {
  		google.load("maps", "2", {"callback" : initMaps});
	}
	
	function start(){
		var script = document.createElement("script");
  		script.src = googleMapsURL+'key='+googleMapsAPIkey+'&hl=de&callback=loadMaps';
  		script.type = "text/javascript";
  		document.getElementsByTagName("head")[0].appendChild(script);
  		//loadMaps()
	}
	
	function openMapsFieldBlock(oLink){
	oImg = document.getElementById('img'+oLink.id)
	oImg.src = '/'+dbPath+'/minusButton.gif'
	oLink.onclick = function() { closeMapsFieldBlock(this); };
	oLink.title = 'close panel';
	document.getElementById('mapinputBody').style.display = 'block';
	//restoreBlock('mapinputBody')
	}

	function closeMapsFieldBlock(oLink){
	oImg = document.getElementById('img'+oLink.id)
	oImg.src = '/'+dbPath+'/plusButton.gif'
	oLink.onclick = function() { openMapsFieldBlock(this); };
	oLink.title = 'open panel';
	document.getElementById('mapinputBody').style.display = 'none';
	//collapseBlock('mapinputBody')
	}
	
	function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

