function loadMap()
{
	if (GBrowserIsCompatible()) 
	{
		//Set variables (to store markers and use them)
		var arrMarkers = [];
		var NbMarkers = 0;
		
		//Set Map
		var map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(17.309,-3.516), 1); //center the map to greenwitch (before any other map setting)
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		map.addControl(new GSmallMapControl()); //allow users to zoom
		map.addControl(new GMapTypeControl()); //allow users to switch map types from Satellite, Hybrid, Map
		
		//Clear markers
		map.clearOverlays();
		
		//Set up Icon for marker
		var oIcon = new GIcon();
		oIcon.image = "http://www.unep.org/billiontreecampaign/map/btc_map_icon.gif";
		oIcon.iconSize = new GSize(20,19);
		oIcon.iconAnchor = new GPoint(6, 20);
		oIcon.infoWindowAnchor = new GPoint(5, 1);
		// Set up our GMarkerOptions object literal
		markerOptions = { icon:oIcon };

		//Show Loading DIV / Hide map
		document.getElementById("loading").style.visibility = "visible";
		document.getElementById("map").style.visibility = "hidden";
		
		//Get KML from server (generated from DB)
		var request = GXmlHttp.create();
		var loc = "kml_generator.asp?noCache="+new Date().getTime(); // prevent caching
		document.getElementById("kml").setAttribute("href", loc); // Set text link to KMl file
		request.open("GET", loc, true);
		
		// Parse the KML, once downloaded, and set the markers
		request.onreadystatechange = function() 
		{
			if (request.readyState == 4) 
			{
	        	//Hide Loading DIV / Show map
				document.getElementById("loading").style.visibility = "hidden";
				document.getElementById("map").style.visibility = "visible";
	        	
	        	// Get markers from Placemark tag, and go through them
	        	var xmlDoc = GXml.parse(request.responseText)
				var placemarks = xmlDoc.documentElement.getElementsByTagName("Placemark");
				if(placemarks.length == 0)
				{	
					alert("No information found about planted trees.");
				}
				else 
				{
					//Parse each Placemark
					for (var i = 0; i < placemarks.length; i++) 
          			{
						//Go through all the children of the current placemark
						var children = placemarks[i].childNodes;
						for (var j = 0; j < children.length; j++)
						{
							// Add text from name tag
							if(children[j].nodeName == "name"){ var label = children[j].firstChild.nodeValue;}
							// Add text from description tag
							else if(children[j].nodeName == "description")	{var html = children[j].firstChild.nodeValue;}
                   			// Add the lat, long pair from the Point tag
							else if(children[j].nodeName == "Point") 
							{
								//We need to split the Point to pass lat and long individually to GLatLng
								var coords = children[j].firstChild.nodeValue.split(", ");
								var longitude = parseFloat(coords[0]);
								var latitude = parseFloat(coords[1]);
							}
                      	}
                      	//Create a marker with placemark data (used of a function for code disclosure) and add it to the map
						map.addOverlay(CreateMarker(map, latitude, longitude, markerOptions, html));
                	}
				}
			}
		}
		request.send(null); //Stop request on the KML
		
	}
}

function CreateMarker(oMap, oLatitude, oLongitude, oMarkersOptions, oHtml)
{
	//Use this function for code disclosure (remember which marker / specific info to display in balloon)
	var marker = new GMarker(new GLatLng(oLatitude, oLongitude), oMarkersOptions);
	GEvent.addListener(marker, "click", function(overlay,point)
	{
		//Get zoom level and add 5 (default is 1; 6 is like region level)
		var ZoomLevel = oMap.getZoom();
		if (ZoomLevel <= 6){ZoomLevel=6;}
		//Zoom and pan the view to the selected marker
		oMap.setZoom(ZoomLevel);
		//Open balloon
		marker.openInfoWindowHtml(oHtml);
	});
	//GEvent.addListener(map,"click", function(overlay,point)
	//{
	//	var myHtml = "The GPoint value is: " + map.fromLatLngToDivPixel(point) + " at zoom level " + map.getZoom();
	//	alert(myHtml);
	//	map.openInfoWindow(point, myHtml);
	//});
	return marker;
}

