var map;
var mapIcons = [];
var markerFlags = [true, true, true, true, true, true];
var geoLat;
var geoLng;
var currZoom = 5;
var currLat = -28.2399;
var currLng = 24.7288;
var dirflag = false;
var dirStart = null;
var dirEnd = null;
var dirPoly = null;
var markers = [[], [], [], [], [], []];
var currentmarkers = [];
var newMarker = null;

var dirIcon = new GIcon();
dirIcon.iconSize = new GSize(24, 24);
dirIcon.iconAnchor = new GPoint(3, 24);
var startIcon = new GIcon(dirIcon);
startIcon.image = "/img/start.png";

var endIcon = new GIcon(dirIcon);
endIcon.image = "/img/end.png";

var markerIcon = new GIcon();
markerIcon.iconSize = new GSize(24, 24);
markerIcon.iconAnchor = new GPoint(12, 12);
var crossIcon = new GIcon(markerIcon);
crossIcon.image = "/img/cross.png";

var baseIcon = new GIcon();
baseIcon.iconSize = new GSize(24, 24);
baseIcon.iconAnchor = new GPoint(12, 24);
baseIcon.infoWindowAnchor = new GPoint(9, 2);

function load() {
	if (GBrowserIsCompatible()) {

		map = new GMap2(document.getElementById("geo_map"));

		var ca = document.cookie.split(';');
		for(var i=0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0)==' ')
				c = c.substring(1, c.length);
			if (c.indexOf("GeoProp") == 0) {
				var geocookie = c.substring(8, c.length);
				var geoarray = geocookie.split(":");
				var geoZoom = geoarray[0];
				currZoom = parseInt(geoZoom);
				var geoCurrLat = geoarray[1];
				if (geoCurrLat != "NaN")
					currLat = parseFloat(geoCurrLat);
				var geoCurrLng = geoarray[2];
				if (geoCurrLng != "NaN")
					currLng = parseFloat(geoCurrLng);
				if (geoarray[3] == "false")
					markerFlags[1] = false;
				if (geoarray[4] == "false")
					markerFlags[2] = false;
				if (geoarray[5] == "false")
					markerFlags[3] = false;
				if (geoarray[6] == "false")
					markerFlags[4] = false;
				if (geoarray[7] == "false")
					markerFlags[5] = false;
				if (geoarray[8] == "true")
					dirflag = true;
				if (geoarray[9])
					map.setMapType(map.getMapTypes()[parseInt(geoarray[9])]);
				if (geoarray[10]) {
					if (geoarray[10] == "none") {
						document.getElementById("toggle_img").src="/img/rightbutton.gif";
						document.getElementById("toggle_img").setAttribute("title", "Maximize Sidebar");
						document.getElementById("sb_info").style.display = "none";
						document.getElementById("geo_map_border").style.width = "752px";
						document.getElementById("geo_map_directions").style.width = "752px";
						map.checkResize();
					}
				}
				if (geoarray[11]) {
					document.getElementById(geoarray[11]).checked = true;
				} else {
					document.getElementById("r1").checked = true;
				}
			}
		}

		for (var n = 1; n < 7; n++) {
			document.getElementById("check" + n).checked = false;
			document.getElementById("check" + n).disabled = true;
		}

		map.setCenter(new GLatLng(currLat, currLng), currZoom);

		var div = document.createElement("div");
		div.id = "pleasewait";
		div.style.position = "relative";
		div.style.whiteSpace = "nowrap";
		div.style.border = "1px solid silver";
		div.style.backgroundColor = "#F2F8FB";
		div.style.padding = "5px";
		div.innerHTML = "<h1>Loading. Please wait.</h1>";
		div.style.top = (map.fromLatLngToDivPixel(map.getCenter()).y - 50) + 'px';
		div.style.left = (map.fromLatLngToDivPixel(map.getCenter()).x - 142) + 'px';
		map.getPane(G_MAP_FLOAT_PANE).appendChild(div);

		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		//map.enableContinuousZoom();
		map.enableScrollWheelZoom();

		var publisherID = 'ca-pub-4002245024903759';
		var adsManagerOptions = {
			maxAdsOnMap : 1,
			style: 'adunit',
			channel: '0639344749'
		};
		var adsManager = new GAdsManager(map, publisherID, adsManagerOptions);
		adsManager.enable();

		mapIcons.push(null);

		var mapIcon = new GIcon(baseIcon);
		mapIcon.image = "/img/ress.png";
		mapIcons.push(mapIcon);

		mapIcon = new GIcon(baseIcon);
		mapIcon.image = "/img/resr.png";
		mapIcons.push(mapIcon);

		mapIcon = new GIcon(baseIcon);
		mapIcon.image = "/img/comms.png";
		mapIcons.push(mapIcon);

		mapIcon = new GIcon(baseIcon);
		mapIcon.image = "/img/commr.png";
		mapIcons.push(mapIcon);

		mapIcon = new GIcon(baseIcon);
		mapIcon.image = "/img/farm.png";
		mapIcons.push(mapIcon);

		GEvent.addListener(map, "moveend", function() {
			if (map.getInfoWindow().isHidden()) {
				displayMarkers();
			}
		});

		GEvent.addListener(map, "singlerightclick", function(point,  src,  overlay) {
			if (document.getElementById("check6").checked) {
				var latlng = map.fromContainerPixelToLatLng(point);
 				geoLat = latlng.lat();
				geoLng = latlng.lng();
				if (geoLat < -22.1111 && geoLat > -34.8422 && geoLng > 16.4516 && geoLng < 32.8966) {
					if (dirStart == null) {
						var markerOptions = { icon:startIcon };
						dirStart = new GMarker(latlng, markerOptions);
						map.addOverlay(dirStart);
						if (dirEnd != null) {
							getDirections();
						}
					} else if (dirEnd == null) {
						var markerOptions = { icon:endIcon };
						dirEnd = new GMarker(latlng, markerOptions);
						map.addOverlay(dirEnd);
						if (dirStart != null) {
							getDirections();
						}
					} else {
						map.removeOverlay(dirStart);
						map.removeOverlay(dirEnd);
						map.removeOverlay(dirPoly);
						dirStart = null;
						dirEnd = null;
						dirPoly = null;
					}
				}
			} else {
				if (!loggedin) {
					alert("You must be logged in before you can add a property.");
				} else {
					if (map.getZoom() < 14) {
						alert ("Please zoom in before trying to add a property.");
					} else {
						var latlng = map.fromContainerPixelToLatLng(point);
 						geoLat = latlng.lat();
						geoLng = latlng.lng();
						if (geoLat < -22.1111 && geoLat > -34.8422 && geoLng > 16.4516 && geoLng < 32.8966) {
							document.getElementById("geo_lat").value = geoLat;
							document.getElementById("geo_lng").value = geoLng;
							document.getElementById("geo_type").selectedIndex = 0;
							document.getElementById("geo_title").value = "";
							document.getElementById("geo_desc").value = "";
							document.getElementById("geo_subtype").options.length = 0;
							document.getElementById("geo_unit").value = "";
							document.getElementById("geo_street").value = "";
							document.getElementById("geo_suburb").value = "";
							document.getElementById("geo_city").value = "";
							document.getElementById("geo_province").selectedIndex = 0;
							document.getElementById("geo_site").value = "";
							document.getElementById("geo_period").selectedIndex = 0;
							document.getElementById("geo_price").value = "0.00";
							document.getElementById("geo_area").value = "0.00";
							document.getElementById("geo_prop_area").value = "0.00";
							document.getElementById("geo_beds").value = "0";
							document.getElementById("geo_bath").value = "0.00";
							document.getElementById("geo_garage").value = "0";
							document.getElementById("geo_carport").value = "0";
							document.getElementById("geo_image").value = "";
							hideAll();
							document.getElementById("geo_add").style.display = "block";
						}
					}
				}
			}
		});

		AJAXCall("getMarkers", encodeURI("type=1"), "gotMarkers(rep)");

	}
}

function getDirections() {
	document.getElementById("directions").src = "img/Throbber20.gif";
	var sp = dirStart.getLatLng().lng() + "," + dirStart.getLatLng().lat();
	var ep = dirEnd.getLatLng().lng() + "," + dirEnd.getLatLng().lat();
	AGCall(sp, ep, "directionloadhandler(rep)", 1);
}

function getTextDirections() {
	document.getElementById("directions").src = "img/Throbber20.gif";
	var sp = dirStart.getLatLng().lat() + "|" + dirStart.getLatLng().lng();
	var ep = dirEnd.getLatLng().lat() + "|" + dirEnd.getLatLng().lng();
	AGCall(sp, ep, "textdirloadhandler(rep)", 2);
}

function textdirloadhandler(obj) {
	document.getElementById("geo_map_border").style.display = "none";
	var doc = document.getElementById("dir_text").contentDocument || document.getElementById("dir_text").contentWindow.document;

	doc.open();
        doc.write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head><meta http-equiv='content-type' content='text/html; charset=UTF-8'/><link rel='stylesheet' type='text/css' href='Global.css'/></head><body style='background-color: white;'>" + obj + "</body></html>");
        doc.close();    

	document.getElementById("geo_map_directions").style.display = "block";
	document.getElementById("directions").src = "img/car.png";
}

function HideDir() {
	document.getElementById("geo_map_directions").style.display = "none";
	document.getElementById("geo_map_border").style.display = "block";
}

function directionloadhandler(obj) {
	var coords = obj.RoutingPoints.split("|");
	var polyCoords = [];
	for (var n = 0; n < coords.length; n++) {
		var ll = coords[n].split(",");
		polyCoords.push(new GLatLng(parseFloat(ll[1]), parseFloat(ll[0])));
	}
	dirPoly = new GPolyline(polyCoords);
	map.addOverlay(dirPoly);
	document.getElementById("directions").src = "img/car.png";
	var ans = confirm("Would you like text directions for the displayed route?");
	if (ans) {
		getTextDirections();
	}
}

function displayMarkers() {
	var ne = map.getBounds().getNorthEast();
	var sw = map.getBounds().getSouthWest();
	var top = ne.lat();
	var right = ne.lng();
	var bot = sw.lat();
	var left = sw.lng();
	var zoom = map.getZoom();
	var maxmarkers = 100;
	var max = 0;
	
	if (markerFlags[1] && markers[1].length > max) max = markers[1].length;
	if (markerFlags[2] && markers[2].length > max) max = markers[2].length;
	if (markerFlags[3] && markers[3].length > max) max = markers[3].length;
	if (markerFlags[4] && markers[4].length > max) max = markers[4].length;
	if (markerFlags[5] && markers[5].length > max) max = markers[5].length;

	clearAll();
	var latlng;
	var breakflag = false;
	for (var n = 0; n < max; n++) {

		for (var x = 1; x < 8; x++) {
			if (markerFlags[x] && n < markers[x].length) {
				latlng = markers[x][n].getLatLng();
				if (latlng.lat() > bot && latlng.lat() < top && latlng.lng() > left && latlng.lng() < right) {
					map.addOverlay(markers[x][n]);
					currentmarkers.push(markers[x][n]);
				}
			}
			if (zoom < 12 && currentmarkers.length == maxmarkers) {
				breakflag = true;
				break;
			}
		}
		if (breakflag) break;
	}
	if (breakflag) {
		document.getElementById("msg").innerHTML = "<strong>Zoom in to see more properties.</strong>";
	} else {
		document.getElementById("msg").innerHTML = "<strong>Click map pins to view more information.</strong>";
	}
}

function clearAll() {
	hideName();
	if (newMarker != null) {
		map.removeOverlay(newMarker);
		newMarker = null;
	}
	if (currentmarkers.length > 0) {
		for (var n = 0; n < currentmarkers.length; n++) {
			map.removeOverlay(currentmarkers[n]);
		}
		currentmarkers = [];
	}
}

function GUnload() {
	if (!document.getElementById("check1").disabled) {
		var value = map.getZoom() + ":";
		value += map.getCenter().lat() + ":" + map.getCenter().lng() + ":";
		value += document.getElementById("check1").checked;
		value += ":" + document.getElementById("check2").checked;
		value += ":" + document.getElementById("check3").checked;
		value += ":" + document.getElementById("check4").checked;
		value += ":" + document.getElementById("check5").checked;
		value += ":" + document.getElementById("check6").checked;
		for (var i = 0; i < map.getMapTypes().length; i++) {
			if (map.getCurrentMapType() == map.getMapTypes()[i]) {
				value += ":" + i;
			}
		}
		value += ":" + document.getElementById("sb_info").style.display;
		if (document.getElementById("r1").checked) {
			value += ":r1";
		} else {
			value += ":r2";
		}
		createCookie("GeoProp", value, 7);
	}
}

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function gotMarkers(object) {
	if (object[0][0] != "none") {

		document.getElementById("check1").checked = markerFlags[1];
		document.getElementById("check2").checked = markerFlags[2];
		document.getElementById("check3").checked = markerFlags[3];
		document.getElementById("check4").checked = markerFlags[4];
		document.getElementById("check5").checked = markerFlags[5];

		var marker;
		for (var n = 0; n < object.length; n++) {
			var LatLng = new GLatLng(object[n][1], object[n][2]);
			marker = new createMarker(LatLng, mapIcons[object[n][4]], object[n][3], object[n][0]);
			markers[object[n][4]].push(marker);
		}

		document.getElementById("check1").disabled = false;
		document.getElementById("check2").disabled = false;
		document.getElementById("check3").disabled = false;
		document.getElementById("check4").disabled = false;
		document.getElementById("check5").disabled = false;

		document.getElementById("check6").disabled = false;
		document.getElementById("check6").checked = dirflag;

		displayMarkers();
		var pleasewait = document.getElementById("pleasewait");
		if(pleasewait) {
			map.getPane(G_MAP_FLOAT_PANE).removeChild(pleasewait);
		}
	}
}

function dirFromTo(lat, lng) {
	map.getInfoWindow().hide();
	var latlng = new GLatLng(lat, lng);
	if (dirPoly != null) {
		map.removeOverlay(dirStart);
		map.removeOverlay(dirEnd);
		map.removeOverlay(dirPoly);
		dirStart = null;
		dirEnd = null;
		dirPoly = null;
	}

	if (dirStart == null) {
		var markerOptions = { icon:startIcon };
		dirStart = new GMarker(latlng, markerOptions);
		map.addOverlay(dirStart);
		if (dirEnd != null) {
			getDirections();
		}
	} else {
		var markerOptions = { icon:endIcon };
		dirEnd = new GMarker(latlng, markerOptions);
		map.addOverlay(dirEnd);
		if (dirStart != null) {
			getDirections();
		}
	}
}

function createMarker(point, ico, name, id) {
	var alttext = "";
	var markerOptions = { icon:ico };
	var marker = new GMarker(point, markerOptions);

	GEvent.addListener(marker, "click", function() {
		showInfo(this, name, id);
	});

	GEvent.addListener(marker, "mouseover", function(latlng) {
		showName(name, latlng);
	});

	GEvent.addListener(marker, "mouseout", function() {
		hideName();
	});

	return marker;
}

function showInfo(marker, name, id) {
	var alttext = "";
	var point = marker.getPoint();
	var ico = marker.getIcon();
	marker.openInfoWindowHtml("<div class='info_div'><img src='" + ico.image + "' class='info_icon' alt='" + alttext + "'/><br/><div class='info_name'>" + name + "</div><br/><div style='text-align: center;'><input class='geo_button' type='button' value='More info...' onclick=\"document.location.href='/moreinfo.php?id=" + id + "'\"/><br/><br/><input class='geo_button' type='button' value='Directions' onclick=\"dirFromTo(" + point.lat() + ", " + point.lng() + ");\" /></div></div>");
}

function showName(name, latlng) {
	var div = document.createElement("div");
	div.id = "infopop";
	div.style.position = "relative";
	div.style.whiteSpace = "nowrap";
	div.innerHTML = name;
	div.style.top = (map.fromLatLngToDivPixel(latlng).y - 54) + 'px';
	div.style.left = (map.fromLatLngToDivPixel(latlng).x + 8) + 'px';
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
}

function hideName() {
	var infopop = document.getElementById("infopop");
	if(infopop) {
		map.getPane(G_MAP_FLOAT_PANE).removeChild(infopop);
	}
}

function checkValue(obj, type) {
	if (obj.value.length == 0) {
		if (type == 1) {
			obj.value = "0.00";
		} else {
			obj.value = "0";
		}
	}
}

function cityChanged(LatLng) {
	var values = LatLng.split(",");
	map.setCenter(new GLatLng(values[0], values[1]), parseInt(values[2]));
}

function geotypeChanged(index) {
	var elem = document.getElementById("geo_subtype");
	elem.options.length = 0;
	hideAll();
	if (index == 1) {
		elem.options[0] = new Option("Apartment/Flat", "1", true, false);
		elem.options[1] = new Option("Cottage", "2", false, false);
		elem.options[2] = new Option("Guest House", "3", false, false);
		elem.options[3] = new Option("Hotel", "4", false, false);
		elem.options[4] = new Option("House", "5", false, false);
		elem.options[5] = new Option("Timeshare", "6", false, false);
		elem.options[6] = new Option("Townhouse", "7", false, false);
		document.getElementById("l1").innerHTML = "Residence Type:";
		document.getElementById("l4").innerHTML = "Selling Price:";
		document.getElementById("l8").innerHTML = "Property Area (m&#178;):";
		showAll([true, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false]);
	} else if (index == 2) {
		elem.options[0] = new Option("Apartment/Flat", "1", true, false);
		elem.options[1] = new Option("Cottage", "2", false, false);
		elem.options[2] = new Option("Guest House", "3", false, false);
		elem.options[3] = new Option("House", "5", false, false);
		elem.options[4] = new Option("Room", "8", false, false);
		elem.options[5] = new Option("Timeshare", "6", false, false);
		elem.options[6] = new Option("Townhouse", "7", false, false);
		document.getElementById("l1").innerHTML = "Residence Type:";
		document.getElementById("l4").innerHTML = document.getElementById("geo_period").options[document.getElementById("geo_period").selectedIndex].text + " Rental:";
		document.getElementById("l8").innerHTML = "Property Area (m&#178;):";
		showAll([true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]);
	} else if (index == 3) {
		elem.options[0] = new Option("Business", "9", true, false);
		elem.options[1] = new Option("Entertainment", "10", false, false);
		elem.options[2] = new Option("Factory", "11", false, false);
		elem.options[3] = new Option("Fast Food", "12", false, false);
		elem.options[4] = new Option("Hospitality", "13", false, false);
		elem.options[5] = new Option("Office", "14", false, false);
		elem.options[6] = new Option("Restaurant", "15", false, false);
		elem.options[7] = new Option("Warehouse", "16", false, false);
		document.getElementById("l1").innerHTML = "Commercial Type:";
		document.getElementById("l4").innerHTML = "Selling Price:";
		document.getElementById("l8").innerHTML = "Property Area (m&#178;):";
		showAll([true, false, true, false, false, false, false, false, false, false, true, false, true, false, true, false, true]);
	} else if (index == 4) {
		elem.options[0] = new Option("Business", "9", true, false);
		elem.options[1] = new Option("Entertainment", "10", false, false);
		elem.options[2] = new Option("Factory", "11", false, false);
		elem.options[3] = new Option("Fast Food", "12", false, false);
		elem.options[4] = new Option("Hospitality", "13", false, false);
		elem.options[5] = new Option("Office", "14", false, false);
		elem.options[6] = new Option("Restaurant", "15", false, false);
		elem.options[7] = new Option("Warehouse", "16", false, false);
		document.getElementById("l1").innerHTML = "Commercial Type:";
		document.getElementById("l4").innerHTML = document.getElementById("geo_period").options[document.getElementById("geo_period").selectedIndex].text + " Rental:";
		document.getElementById("l8").innerHTML = "Property Area (m&#178;):";
		showAll([true, false, false, false, false, false, false, false, false, false, true, false, true, false, true, false, true]);
	} else if (index == 5) {
		elem.options[0] = new Option("Agricultural Farm", "17", true, false);
		elem.options[1] = new Option("Game Farm", "18", false, false);
		elem.options[2] = new Option("Development", "25", false, false);
		elem.options[3] = new Option("Livestock Farm", "19", false, false);
		elem.options[4] = new Option("Mixed Farming", "20", false, false);
		elem.options[5] = new Option("Smallholding", "21", false, false);
		elem.options[6] = new Option("Trout Farm", "22", false, false);
		elem.options[7] = new Option("Vacant Plot", "23", false, false);
		elem.options[8] = new Option("Wine Farm", "24", false, false);
		document.getElementById("l1").innerHTML = "Farm or Land Type:";
		document.getElementById("l4").innerHTML = "Selling Price:";
		document.getElementById("l8").innerHTML = "Property Area (ha):";
		showAll([true, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false]);
	}
}

function geo_add_close() {
	document.getElementById("geo_add").style.display = "none";
}

function isUrl(s) {
	var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}

function isImage(s) {
	var regexp = /(.jpg|.jpeg|.png|.gif)/i;
	return regexp.test(s);
}

function addProp() {
	var pricelabel = document.getElementById("l4").innerHTML;
	pricelabel = pricelabel.substring(0, pricelabel.length - 1).toLowerCase();
	if (document.getElementById("geo_type").value == -1) {
		alert("Please select a property type.");
	} else if (document.getElementById("geo_title").value.length == 0) {
		alert("Please supply a title for the property.");
	} else if (document.getElementById("geo_title").value == document.getElementById("geo_title").value.toUpperCase()) {
		alert("Property titles in uppercase are NOT allowed.");
	} else if (document.getElementById("geo_title").value.length > 40) {
		alert("Sorry, but the property title is limited to 40 characters.");
	} else if (document.getElementById("geo_desc").value.length == 0) {
		alert("Please supply a full description of the property, including features not otherwise accounted for.");
	} else if (document.getElementById("geo_desc").value.indexOf("!!") > -1) {
		alert("Please use only single exclamation marks!!!!!!!");
	} else if (document.getElementById("geo_unit").value.length > 40) {
		alert("Sorry, but the unit address is limited to 40 characters.");
	} else if (document.getElementById("geo_street").value.length > 40) {
		alert("Sorry, but the street address is limited to 40 characters.");
	} else if (document.getElementById("geo_suburb").value.length > 40) {
		alert("Sorry, but the suburb is limited to 40 characters.");
	} else if (document.getElementById("geo_city").value.length > 40) {
		alert("Sorry, but the city is limited to 40 characters.");

	} else if (isNaN(parseFloat(document.getElementById("geo_price").value))) {
		alert("The " + pricelabel + " must be a numeric value.");
	} else if (parseFloat(document.getElementById("geo_price").value) < 0.01) {
		alert("The " + pricelabel + " must be greater than zero.");

	} else if (isNaN(parseFloat(document.getElementById("geo_area").value))) {
		alert("The floor area must be a numeric value.");
	} else if (parseFloat(document.getElementById("geo_area").value) < 0.00) {
		alert("The floor area must be zero or greater.");

	} else if (isNaN(parseFloat(document.getElementById("geo_prop_area").value))) {
		alert("The property area must be a numeric value.");
	} else if (parseFloat(document.getElementById("geo_prop_area").value) < 0.00) {
		alert("The property area must be zero or greater.");

	} else if (isNaN(parseInt(document.getElementById("geo_beds").value))) {
		alert("The number of bedrooms must be a numeric value.");

	} else if (isNaN(parseFloat(document.getElementById("geo_bath").value))) {
		alert("The number of bathrooms must be a numeric value.");

	} else if (isNaN(parseInt(document.getElementById("geo_garage").value))) {
		alert("The number of garages must be a numeric value.");

	} else if (isNaN(parseInt(document.getElementById("geo_carport").value))) {
		alert("The number of carports must be a numeric value.");

	} else if (document.getElementById("geo_site").value.length > 0 && !isUrl(document.getElementById("geo_site").value)) {
		alert("Sorry, but the link to the web site seems to be invalid. It must start with http:// or https://");
	} else if (document.getElementById("geo_image").value.length > 0 && !isUrl(document.getElementById("geo_image").value)) {
		alert("Sorry, but the image URL seems to be invalid. It must start with http:// or https://");
	} else if (document.getElementById("geo_image").value.length > 0 && !isImage(document.getElementById("geo_image").value)) {
		alert("Sorry, but the image URL seems to be invalid. It must point at either a jpeg, a gif or a png image.");
	} else {
		document.getElementById("geo_price").value = parseFloat(document.getElementById("geo_price").value).toFixed(2);
		var icon = mapIcons[document.getElementById("geo_type").value];
		geo_add_close();
		var LatLng = new GLatLng(document.getElementById("geo_lat").value, document.getElementById("geo_lng").value);
		var markerOptions = { icon:icon };
		if (newMarker != null) {
			map.removeOverlay(newMarker);
		}
		newMarker = new GMarker(LatLng, markerOptions);
		map.addOverlay(newMarker);
		newMarker.openInfoWindowHtml("<br/><div class='info_name'>Are you sure that want to add that property there?<br/><span style='color: red;'>Inaccurately placed properties will be deleted!</span></div><br/><input class='geo_button' type='button' value='Yes' onclick=\"addNewGeo();\" /> <input class='geo_button' type='button' value='No' onclick=\"removeNewGeo();\" />");
	}
}

function addNewGeo() {
	var geo_title = document.getElementById("geo_title").value;
	geo_title = geo_title.replace(/&/g, "%26");
	var geo_desc = document.getElementById("geo_desc").value;
	geo_desc = geo_desc.replace(/&/g, "%26");
	var geo_unit = document.getElementById("geo_unit").value;
	geo_unit = geo_unit.replace(/&/g, "%26");
	var geo_street = document.getElementById("geo_street").value;
	geo_street = geo_street.replace(/&/g, "%26");
	var geo_suburb = document.getElementById("geo_suburb").value;
	geo_suburb = geo_suburb.replace(/&/g, "%26");
	var geo_city = document.getElementById("geo_city").value;
	geo_city = geo_city.replace(/&/g, "%26");
	var geo_province = document.getElementById("geo_province").options[document.getElementById("geo_province").selectedIndex].text;
	var geo_site = document.getElementById("geo_site").value;
	geo_site = geo_site.replace(/&/g, "%26");
	var geo_image = document.getElementById("geo_image").value;
	geo_image = geo_image.replace(/&/g, "%26");

	AJAXCall("add", encodeURI("title=" + geo_title + "&lat=" + document.getElementById("geo_lat").value + "&lng=" + document.getElementById("geo_lng").value + "&type=" + document.getElementById("geo_type").value + "&desc=" + geo_desc + "&subtype=" + document.getElementById("geo_subtype").value + "&unit=" + geo_unit + "&street=" + geo_street + "&suburb=" + geo_suburb + "&city=" + geo_city + "&province=" + geo_province + "&site=" + geo_site + "&image=" + geo_image + "&period=" + document.getElementById("geo_period").value + "&price=" + document.getElementById("geo_price").value + "&area=" + document.getElementById("geo_area").value + "&proparea=" + document.getElementById("geo_prop_area").value + "&beds=" + document.getElementById("geo_beds").value + "&bath=" + document.getElementById("geo_bath").value + "&garage=" + document.getElementById("geo_garage").value + "&carport=" + document.getElementById("geo_carport").value), "geoAdded(rep)");
	map.removeOverlay(newMarker);
	newMarker = null;
}

function removeNewGeo() {
	map.removeOverlay(newMarker);
	newMarker = null;
}

function hideAll() {
	for (var n = 1; n < 17; n++) {
		document.getElementById("l" + n).style.display = "none";
		document.getElementById("i" + n).style.display = "none";
	}
}

function showAll(dontShow) {
	for (var n = 1; n < 17; n++) {
		if (!dontShow[n]) {
			document.getElementById("l" + n).style.display = "block";
			document.getElementById("i" + n).style.display = "block";
		}
	}
}

function changeRentalText() {
	document.getElementById("l4").innerHTML = document.getElementById("geo_period").options[document.getElementById("geo_period").selectedIndex].text + " Rental:";
}

function geoAdded(object) {
	var LatLng = new GLatLng(object[2], object[3]);
	var marker = new createMarker(LatLng, mapIcons[object[1]], object[4], object[0]);
	markers[object[1]].push(marker);
	map.addOverlay(marker);
}

function toggleOverlay(flag, index) {
	markerFlags[index] = flag;
	displayMarkers();
}

function toggleDirections(show) {
	if (!show) {
		if (dirStart != null) {
			map.removeOverlay(dirStart);
			dirStart = null;
		}
		if (dirEnd != null) {
			map.removeOverlay(dirEnd);
			dirEnd = null;
		}
		if (dirPoly != null) {
			map.removeOverlay(dirPoly);
			dirPoly = null;
		}
	}
}

function togglesidebar(image) {
	if (document.getElementById("sb_info").style.display == "block") {
		image.src="/img/rightbutton.gif";
		document.getElementById("toggle_img").setAttribute("title", "Maximize Sidebar");
		document.getElementById("sb_info").style.display = "none";
		document.getElementById("geo_map_border").style.width = "752px";
		document.getElementById("geo_map_directions").style.width = "752px";
	} else {
		image.src="/img/leftbutton.gif";
		document.getElementById("toggle_img").setAttribute("title", "Minimize Sidebar");
		document.getElementById("sb_info").style.display = "block";
		document.getElementById("geo_map_border").style.width = "608px";
		document.getElementById("geo_map_directions").style.width = "608px";
	}
	map.checkResize();
	displayMarkers();
}

function checkNum(e, obj) {
	var key = e.keyCode;
	if ((key > 47 && key < 58) || (key > 95 && key < 106) || key == 8 || key == 46 || key == 37 || key == 39) {
		return true;
	} else if ((key == 190 || key == 110) && obj.value.indexOf(".") == -1) {
		return true;
	} else {
		return false;
	}
}

function checkInt(e) {
	var key = e.keyCode;
	if ((key > 47 && key < 58) || (key > 95 && key < 106) || key == 8 || key == 46 || key == 37 || key == 39) {
		return true;
	} else {
		return false;
	}
}

function printDirections(){
	dir_text.focus();
	dir_text.print(); 
}