var GoogleMapsKey;
var map;
var markers = new Array();
markers[0] = new Array();
markers[1] = new Array();
var bounds;

var request = null;
function createRequest() { // ajax
	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
		request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
			request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = null;
			}
		}
	}
	if (request == null)
		alert ("Error!");
}

function load(gmapkey, mode) {
	if (GBrowserIsCompatible()) {
		GoogleMapsKey = gmapkey;
		//if (document.getElementById("gmap")) { alert('ok') } else { alert('niet ok') };

		if (mode != 0) {
			map = new GMap2(document.getElementById("gmap"), {size:new GSize(500, 500)} ); // create map
			map.setCenter(new GLatLng(0, 0), 8); // latitude/longitude, zoom
		} else {
			map = new GMap2(document.getElementById("gmap"), {size:new GSize(100, 66)} ); // create map
			map.setCenter(new GLatLng(0, 0), 5); // latitude/longitude, zoom
		}
		bounds = new GLatLngBounds();
		if (mode != 0) {
			map.addControl(new GLargeMapControl()); // map type
		}
		// --------------
		// GMapTypeControl: Toggler for switching between the Google map and satellite views.
		// GLargeMapControl: Complete Map Control.
		// GSmallMapControl: A smaller version of GlargeMapControl, eliminating the zooming scalebar but including the zoom controls.
		// GSmallZoomControl: This control only includes the zooming buttons, eliminating everything else found in the GLargeMapControl.

		map.disableDragging(); // disable dragging
	}
}

function addaddress(adres, mode) {
	createRequest(); // create ajax request
	request.open("GET", "../js/ajax/getcoords.asp?adres="+adres+"&key="+GoogleMapsKey, true); // this is the adres we need lat and lng of
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var adressen = request.responseText.split("||");
			for (var i = 0; i < adressen.length; i++) {
				var data = adressen[i].split("|");
				var regioid = data[0];
				var adres = data[1];
				var lat = data[2];
				var lng = data[3];
//				var lat = adressen[i].substr(adressen[i].indexOf("|")+1).substr(0, adressen[i].substr(adressen[i].indexOf("|")+1).indexOf("|"));
//				var lng = adressen[i].substr(adressen[i].indexOf("|")+1).substr(adressen[i].substr(adressen[i].indexOf("|")+1).indexOf("|")+1);
//				var adres = adressen[i].substr(0, adressen[i].indexOf("|"));

				// ajax om op te slaan in db
//				var bedrijf = adres.substring(32, adres.indexOf("</span><br />"));
				createRequest(); // create ajax request
				request.open("GET", "../js/ajax/savecoords.asp?regioid="+regioid+"&lat="+lat+"&lng="+lng, true); // this is the adres we need lat and lng of
				request.onreadystatechange = function() {
					if (request.readyState == 4) {
						if (request.responseText != "") {
							alert(request.responseText);
						}
					}
				}
				request.send(null);

				plotpoints(adres, lat, lng, mode);
			}
		}
	}; // call function to process ajax return
	request.send(null);
}

function plotpoints(adres, lat, lng, mode) {
	var point = new GLatLng(lat, lng);
	map.setCenter(point); // set the center in the middle of the markers
	var marker = new GMarker(point); // create a new marker on this point
	if (mode != 0) {
		map.addOverlay(marker); // add the marker on the map
		marker.bindInfoWindowHtml("<p style='font-family:Verdana;padding:0;margin:0;'>" + adres + "</p>"); // show this when clicked on
	}
//	bounds.extend(point); // contain new point in curent visible field
//	map.setZoom(map.getBoundsZoomLevel(bounds)); // set the zoom so we can see all markers
//	map.setCenter(bounds.getCenter()); // set the center in the middle of the markers
}