function URLEncode(str) {
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";
	var encoded = "";
	for (var i = 0; i < str.length; i++ ) 
	{
		var ch = str.charAt(i);
		if (ch == " ") {
			encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
			encoded += ch;
		} else {
			var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				alert( "Unicode Character '" 
						+ ch 
						+ "' cannot be encoded using standard URL encoding.\n" +
						  "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}

var map = null;
var geocoder = null;
var daddr;
var html;

function initialize(mapname, daddr, html) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById(mapname));
		geocoder = new GClientGeocoder();
		daddr = daddr;
		html = html;
		showAddress(daddr);
	}
}

function showAddress(address) {
	address = URLEncode(address);
	if (geocoder) {
		geocoder.getLatLng(
			address,
			function (point) {
				map.setCenter(point, 13);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				
				GEvent.addListener(marker, "click", function() {
				  marker.openInfoWindowHtml(html);
				});
				map.addOverlay(marker);
				marker.openInfoWindowHtml(html);
			}
		);
	}
}

function getRoute() {
	var saddr = URLEncode(document.getElementById('start').value);
	window.open('http://maps.google.com/maps?saddr='+saddr+'&daddr='+daddr, '_blank');
}

