function setPointFromPostcode(postcode, callbackFunction) {
	//We don't have the latlng, so do a search from the postcode
	var localSearch = new GlocalSearch();
	localSearch.setSearchCompleteCallback(null,function() {

  	if (localSearch.results[0]) {   
	 
	     var resultLat = localSearch.results[0].lat;
	     var resultLng = localSearch.results[0].lng;
	
		 $.get('/pointupdater/'+google.courseid+'/'+resultLat+'/'+resultLng);
	     
	 	 callbackFunction(resultLat,resultLng);
	
	} else {
	     alert("Postcode not found!");
	}
 });  

localSearch.execute(postcode + ", UK");

}

function showGoogleMap(resultLat, resultLng) {

	var point = new GLatLng(resultLat,resultLng, 13);
	
	if (!point) {
		
		$("#gmap").hide();
		
	} else {		

		//Display the  map
		$("#gmap").show();
		div = document.getElementById("gmap_container");
		var map = new GMap2(div);
		map.addControl(new google.maps.LargeMapControl());
 		map.addControl(new google.maps.MapTypeControl());
	 	map.setCenter(point, 13);
		
		// Set up three markers with info windows 
		var marker = createMarker(point,'Location','<h1>' + google.courseName + '</h1>')
		map.addOverlay(marker);
		gmarkers[0].openInfoWindowHtml(htmls[0]);
		
	}	
}

function showCourseMapPostcode(address) {

	setPointFromPostcode(google.getPostcode(), showGoogleMap);	
}

function showCourseMapLatLng() {
	//We have the latlng, and have set it to the google var
	showGoogleMap(google.courseLat, google.courseLng);	
}

 // A function to create the marker and set up the event window
function createMarker(point,name,html) {
	var marker = new GMarker(point);

	htmls = [];
	to_htmls = [];
	from_htmls = [];
	var i=0;
    gmarkers = [];
	
	// The info window version with the "to here" form open
	to_htmls[i] = html + '<p>Get directions: <strong>To here<\/strong> or <a href="javascript:fromhere(' + i + ')">From here<\/a></p>' +
	   '<p><strong>Start address</strong><br /><form action="http://maps.google.com/maps" method="get" target="_blank">' +
	   '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" style="margin-bottom:10px" /><br>' +
	   '<INPUT value="Get Directions" TYPE="SUBMIT" style="width:100px">' +
	    ' (opens new window)</p>' +
	   '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
	          // "(" + name + ")" + 
	   '"/>';
	// The info window version with the "to here" form open
	from_htmls[i] = html + '<p>Get Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <strong>From here<\/strong></p>' +
	   '<p><strong>Destination address</strong><br /><form action="http://maps.google.com/maps" method="get"" target="_blank">' +
	   '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" style="margin-bottom:10px" /><br>' +
	   '<INPUT value="Get Directions" TYPE="SUBMIT" style="width:100px">' +
	   ' (opens new window)</p>' +
	   '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
	          // "(" + name + ")" + 
	   '"/>';
	// The inactive version of the direction info
	html = html + '<p>Get directions: <a href="javascript:tohere('+i+')">To here<\/a> - <a href="javascript:fromhere('+i+')">From here<\/a></p>';
	
	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml(html);
	});
	
	gmarkers[i] = marker;
	htmls[i] = html;
	i++;
	
	return marker;
}

// functions that open the directions forms
function tohere(i) {
	gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
function fromhere(i) {
	gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}