	// see http://conversationswithmyself.com/googleMapDemo.html for code
	// and http://www.google.com/apis/maps/documentation/
	// and http://www.tomanthony.co.uk/blog/geocoding-uk-postcodes-with-google-map-api/ (ajax way)
    //<![CDATA[
// Reference to googles local search (used to obtain uk postcodes)		   
var localSearch = new GlocalSearch();	   
// Reference to googles local search (used to obtain uk postcodes)
var map = null;
var geocoder = null;
var circleRadius;
var point;
function loadmap() 
{
	if (GBrowserIsCompatible()) 
	{
		map = new GMap2(document.getElementById("map"));
		
		if (document.getElementById("postcode").value !='')
		{
			getPointFromPostcode(document.getElementById("postcode").value);
		}
		else
		{
			/////////////////////////////////////////// Geocoder info
			map.setCenter(new GLatLng(54.007769, -2.329102), 4);
			// Add geocoder for postcodes
			geocoder = new GClientGeocoder();
			// Add geocoder for postcodes
			/////////////////////////////////////////// Geocoder info
			//map.centerAndZoom(new GPoint(-2.329102, 54.007769), 10); // setCenter function is slightly different for geocoding
			
			map.addControl(new GSmallMapControl()); // Add controls
		}
		
		//map.recenterOrPanToLatLng is not a function
		GEvent.addListener(map, 'click', function(overlay, point) 
		{
		
			if (overlay) 
			{
				map.removeOverlay(overlay);
			} 
			else if (point) 
			{
				map.setCenter(point);
				var marker = new GMarker(point);
				map.clearOverlays(); // Clear markers before adding a new one
				map.addOverlay(marker);
				////////////////// EXTRA MAP RADIUS
				//var oRadius = document.getElementById('radiusInput');
				/*var oRadius = 6;
				//oRadius.value = oRadius.value ? oRadius.value : 500;
				oRadius = oRadius ? oRadius : 500;
				circleUnits = 'MI';
			
				circleRadius = oRadius;
				
				doDrawCircle();*/
				////////////////// EXTRA MAP RADIUS
			}
		}
		);
		// Recenter Map and add Coords by clicking the map
		GEvent.addListener(map, "click", function(overlay, point) 
		{
			if (!overlay) 
			{
				document.getElementById("latbox").value=point.y;
				document.getElementById("lonbox").value=point.x;
			}
		}
		);
		// Recenter Map and add Coords by clicking the map
//		GEvent.addListener(point, "dragend", function() 
//		{
//			alert(point);
///*			if (!overlay) 
//			{
//				document.getElementById("latbox").value=point.y;
//				document.getElementById("lonbox").value=point.x;
//			}
//*/		}
//		);
	}
}

// Add radius circle to map
function addRadiusOverlay(form_name)
{
	if (form_name.local.checked)
	{
		var resultLat = form_name.lat.value;
		var resultLng = form_name.lon.value;
		var markerpoint = new GLatLng( resultLat , resultLng );
		//map.setCenter(markerpoint);		
		
		////////////////// EXTRA MAP RADIUS
		//var oRadius = document.getElementById('radiusInput');
		var oRadius = form_name.radius.value;
		//oRadius.value = oRadius.value ? oRadius.value : 500;
		oRadius = oRadius ? oRadius : 500;
		circleUnits = 'MI';
	
		circleRadius = oRadius;
		//doDrawCircle();
		////////////////// EXTRA MAP RADIUS
	}
	else
	{	
		map.removeOverlay(circle);
	}
}

/////////////////////////////////////////// Geocoder info (for UK post codes)
function getPointFromPostcode(postcode) 
{
	localSearch.setSearchCompleteCallback(null,
	function() 
	{
		if (localSearch.results[0]) 
		{    
			var resultLat = localSearch.results[0].lat;
			var resultLng = localSearch.results[0].lng;
			var point = new GLatLng( resultLat , resultLng );
			map.setCenter(point, 13);
			var marker = new GMarker(point ,{icon:G_DEFAULT_ICON, draggable: true});
			map.addOverlay(marker);
			//marker.openInfoWindowHtml("Location by Postcode: " + postcode.toUpperCase() + "<BR/>If you would like to place the pointer at a new,<BR/> more accurate location - please click on the map.");
			marker.openInfoWindowHtml("<BR/>If this location is incorrect,<BR/>you can click and drag the marker<BR/>to your desired location.");
			document.getElementById("latbox").value=point.y;
			document.getElementById("lonbox").value=point.x;
			
			GEvent.addListener(marker, "dragstart", function() {
  				map.closeInfoWindow();
  			});
			GEvent.addListener(marker, "dragend", function() {
  				marker.openInfoWindowHtml("If this location is incorrect,<BR/>you can click and drag the marker<BR/>to your desired location.");
				// Insert new coordinates into form field
				//var new_point = marker.getCenter();
				//alert(marker.getPoint().y);
				document.getElementById("latbox").value=marker.getPoint().y;
				document.getElementById("lonbox").value=marker.getPoint().x;
			});
		}
		else
		{
			alert("Address not found!");
		}
	});


	
	if (document.getElementById("country_id").value =='Please select')
	{
		$country =', United Kingdom' ;
	}
	else
	{
		$country = ", " + document.getElementById("country_id").value;
	}
	if (document.getElementById("address1"))
	{
//		if (document.getElementById("address1").value !='')
//		{
//			$address1 = ", " + document.getElementById("address1").value;
//		}
//		else
//		{
			// $address1 = "";
			$address1 = document.getElementById("address1").value; // For registration (address1 is the street)
//		}
	}
	else
	{
		$address1 = "";	
	}
	if (document.getElementById("address2"))
	{
//		if (document.getElementById("address2").value !='')
//		{
//			$address2 = ", " + document.getElementById("address2").value;
//		}
//		else
//		{
			$address2 = "";
//		}
	}
	else
	{
		$address2 = "";	
	}
	if (document.getElementById("city"))
	{
		if (document.getElementById("city").value !='')
		{
			$city = ", " + document.getElementById("city").value;
		}
		else
		{
			$city = "";
		}
	}
	else
	{
		if (document.getElementById("conference-location").value !='')
		{
			$city = ", " + document.getElementById("conference-location").value;
		}
		else
		{
			$city = "";
		}
	}
	if (document.getElementById("conference-venue"))
	{
//		if (document.getElementById("conference-venue").value !='')
//		{
//			$venue = ", " + document.getElementById("conference-venue").value;
//		}
//		else
//		{
			$venue = "";
//		}
	}
	else
	{
		$venue = "";	
	}
	if (document.getElementById("conference-street"))
	{
		if (document.getElementById("conference-street").value !='')
		{
			//$street = ", " + document.getElementById("conference-street").value;
			$street = document.getElementById("conference-street").value;
		}
		else
		{
			$street = "";
		}
	}
	else
	{
		$street = "";	
	}

	//$address2 = ", " + document.getElementById("address2").value;
	//$city = ", " + document.getElementById("city").value;
	//localSearch.execute(postcode + ", UK");
	//localSearch.execute(postcode + $venue + $street + $address1 + $address2 + $city +$country);	
	//localSearch.execute($venue + $street + $address1 + $address2 + $city +$country);	
	
	if (postcode!="" && $country == ", United Kingdom")
	{
		localSearch.execute(postcode + $country);
		//alert("test");
	}
/*	else if (postcode && $country != ", United Kingdom")
	{
		localSearch.execute(postcode + $venue + $street + $address1 + $address2 + $city + $country);
	}
*/	else
	{
		localSearch.execute($venue + $street + $address1 + $address2 + $city + $country);
		//alert($venue + $street + $address1 + $address2 + $city + $country);
	}
/*	if (postcode!="" && $country == ", United Kingdom")
	{
		alert(postcode + $country);
		//alert("test");
	}
	else if (postcode && $country != ", United Kingdom")
	{
		alert($venue + $street + $address1 + $address2 + $city + $country);
	}
	else
	{
		alert($venue + $street + $address1 + $address2 + $city + $country);
	}
*/	
}

//var container;
//var opacity = 0.4;
var circle;
//var centerMarker;
function doDrawCircle()
{
	var center = map.getCenter();
	var circlePoints = Array();
	var bounds = new GLatLngBounds();
	with (Math) 
	{
		if (circleUnits == 'KM') {
			var rLat = (circleRadius/6378.8) * (180/PI);
		}
		else { //miles
			var rLat = (circleRadius/3963.189) * (180/PI);
		}
		var rLng = rLat/cos(center.lat() * (PI/180));

		for (var a = 0 ; a < 361 ; a+=1 ) {
			var aRad = a*(PI/180);
			var x = center.lng() + (rLng * cos(aRad));
			var y = center.lat() + (rLat * sin(aRad));
			var point = new GLatLng(parseFloat(y),parseFloat(x));
			bounds.extend(point);
			circlePoints.push(point);
		}
	}
	circle = new GPolygon(circlePoints, true, '#ff0000', 0.25, true);	
	map.addOverlay(circle); 
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  }
	}
}

addLoadEvent(loadmap);
addUnLoadEvent(GUnload);
/////////////////////////////////////////// Geocoder info (for UK post codes)
/////////////////////////////////////////////////////////////////////////////////////


/*function drawCircle(centerMarker, radiusMarker, borderColour, fillColour) 
{
	var normalProj = map.getCurrentMapType().getProjection();
	var zoom = map.getZoom();
	
	var centerPt = normalProj.fromLatLngToPixel(centerMarker, zoom);
	var radiusPt = normalProj.fromLatLngToPixel(radiusMarker, zoom);
	
	var circlePoints = Array();
	
	with (Math) 
	{
		var radius = floor(sqrt(pow((centerPt.x-radiusPt.x),2) +
		pow((centerPt.y-radiusPt.y),2)));
		
		for (var a = 0 ; a < 361 ; a+=5 ) 
		{
			var aRad = a*(PI/180);
			y = centerPt.y + radius * sin(aRad)
			x = centerPt.x + radius * cos(aRad)
			var p = new GPoint(x,y);
			circlePoints.push(normalProj.fromPixelToLatLng(p, zoom));
		}
		circleLine2 = new GPolygon(circlePoints,borderColour,0, 0, fillColour,0.5);
		map.addOverlay(circleLine2);
	}
}*/

/////////////////////////////////////////// Geocoder info (for US zip codes). You dont need to use this one - use the one below for the uk
function showAddress(address) 
{
	if (geocoder) 
	{
		geocoder.getLatLng(
		address,
		function(point) 
		{
			if (!point) 
		{
			alert(address + " not found");
		} 
		else 
		{
			map.setCenter(point, 13);
			var marker = new GMarker(point);
			map.addOverlay(marker);
			marker.openInfoWindowHtml(address);
			document.getElementById("lonbox").value=point.y;
			document.getElementById("lonbox").value=point.x;
		}
	});
	}
}
/////////////////////////////////////////// Geocoder info (for US zip codes). You dont need to use this one - use the one below for the uk

if (document.getElementById("mapadmin"))
{
	function loadmapbycoordinates() 
	{
		map = new GMap2(document.getElementById("mapadmin"));
		
		longitude = document.getElementById("postcode").value;
		latitude = document.getElementById("latbox").value;
		
		/////////////////////////////////////////// Geocoder info
		map.setCenter(new GLatLng(longitude, latitude), 4);
		// Add geocoder for postcodes
		geocoder = new GClientGeocoder();
		// Add geocoder for postcodes
		/////////////////////////////////////////// Geocoder info
		//map.centerAndZoom(new GPoint(-2.329102, 54.007769), 10); // setCenter function is slightly different for geocoding
		
		map.addControl(new GSmallMapControl()); // Add controls
		
		//map.recenterOrPanToLatLng is not a function
		GEvent.addListener(map, 'click', function(overlay, point) 
		{
		
			if (overlay) 
			{
				map.removeOverlay(overlay);
			} 
			else if (point) 
			{
				map.setCenter(point);
				var marker = new GMarker(point);
				map.clearOverlays(); // Clear markers before adding a new one
				map.addOverlay(marker);
				////////////////// EXTRA MAP RADIUS
				//var oRadius = document.getElementById('radiusInput');
				/*var oRadius = 6;
				//oRadius.value = oRadius.value ? oRadius.value : 500;
				oRadius = oRadius ? oRadius : 500;
				circleUnits = 'MI';
			
				circleRadius = oRadius;
				
				doDrawCircle();*/
				////////////////// EXTRA MAP RADIUS
			}
		}
		);
		// Recenter Map and add Coords by clicking the map
		GEvent.addListener(map, "click", function(overlay, point) 
		{
			if (!overlay) 
			{
				document.getElementById("latbox").value=point.y;
				document.getElementById("lonbox").value=point.x;
			}
		}
		);
	}
}

    //]]>