function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}

function createXMLHttpRequest2() {
	if (window.ActiveXObject) {
		xmlHttp2 = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		xmlHttp2 = new XMLHttpRequest();
	}
}

function createXMLHttpRequest3() {
	if (window.ActiveXObject) {
		xmlHttp3 = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		xmlHttp3 = new XMLHttpRequest();
	}
}

function gMapLoad() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		//var mapCentre = new GLatLng(0, 0);
		//map.setCenter(mapCentre, 1);
		//map.disableDragging();
	  	var url = location.href;
        url = url.split('/');
		if (url[url.length-1] == 'grassroots-organisation' || (url[url.length-2] == 'grassroots-organisation' && url[url.length-1] == '')) {
			var ppoint = 0;
		} else {
			var i = 1;
			while (url[url.length-i] == '') i++;
			var ppoint = url[url.length-i];
		}
		createXMLHttpRequest();
		var address = '/includes/getppoint.php?ppoint='+ppoint;
		xmlHttp.open('GET', address, true);
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				if (xmlHttp.status == 200) {
					var results = xmlHttp.responseText;
					if (results == '') {
						var mapCentre = new GLatLng(0, 0);
						map.setCenter(mapCentre, 1);
					} else {
						var results = results.split("[break2]");
						var mapCentre = new GLatLng(parseFloat(results[3]), parseFloat(results[4]));
						map.setCenter(mapCentre, parseFloat(results[5]));
					}
				}
			}
		};
		xmlHttp.send(null);
		createXMLHttpRequest2();
		var address = '/includes/getmappoints.php?ppoint='+ppoint;
		xmlHttp2.open('GET', address, true);
		xmlHttp2.onreadystatechange = function() {
			if (xmlHttp2.readyState == 4) {
				if (xmlHttp2.status == 200) {
					var results = xmlHttp2.responseText;
					if (results != '') {
						var results = results.split("[break1]");
						for (var i = 0; i < results.length; i++) {
							var results2 = results[i].split("[break2]");
							var point = new GLatLng(parseFloat(results2[3]), parseFloat(results2[4]));
							map.addOverlay(createMarker(point, results2[2]));
						}
					}
				}
			}
		};
		xmlHttp2.send(null);
		createXMLHttpRequest3();
		var address = '/includes/getcentrepoints.php?ppoint='+ppoint;
		xmlHttp3.open('GET', address, true);
		xmlHttp3.onreadystatechange = function() {
			if (xmlHttp3.readyState == 4) {
				if (xmlHttp3.status == 200) {
					var results = xmlHttp3.responseText;
					if (results != '') {
						var results = results.split("[break1]");
						for (var i = 0; i < results.length; i++) {
							var results2 = results[i].split("[break2]");
							var point = new GLatLng(parseFloat(results2[4]), parseFloat(results2[5]));
							map.addOverlay(createCentreMarker(point, results2[1], results2[2]));
						}
					}
				}
			}
		};
		xmlHttp3.send(null);
	}
}

function createMarker(point, title) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() {
    var url = location.href;
	var newurl;
	urlsplit = url.split('/');
	if (urlsplit[urlsplit.length-1] == '') newurl = url + title;
	else newurl = url + '/' + title;
	location.href = newurl;
  });
  return marker;
}

function createCentreMarker(point, name, info) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml("<strong>"+name+"</strong><br />"+info);
  });
  return marker;
}

addLoadEvent(gMapLoad);