	function render_map(lat, long, info, delivery) {
		var map = getmap(lat, long);
		var marker = createMarker(map, info);
		map.addOverlay(marker);
		var key;
		var iKey = 0;
		if (info.length > 0) {
			marker.openInfoWindowHtml(info);
		}
		delivery.keys().each(function(key, number) {
			buildBorder(map, delivery[key]['points'], delivery[key]['levels'], delivery[key]['color']);
		});
		return map;
	}

	function getmap(lat, long) {
		if (GBrowserIsCompatible()) {
			var map = new GMap2($("map"), {draggableCursor: 'crosshair'});
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(lat, long), 13);
		}
		return map;
	}

	function startDeliveryArea(map) {
		points = []
		var color = 'red';
		var title = 'Starting Point';
		GEvent.addListener(map, "click", function(marker, point) {
			if (marker) {
				if (marker.getPoint().x == points[0].Longitude && marker.getPoint().y == points[0].Latitude) {
					$('delivery_area_name').value = "";
					$('delivery_area_description').value = "";
					$('delivery_fields').show();
					$('delivery_tips').innerHTML = "Now fill in the fields above and click ok.  The description will appear on the Location page under Delivery Area.";
					new Effect.Highlight('delivery_tips', {duration: 5});
				  var newPoint = {
						    Latitude: marker.getPoint().y,
						    Longitude: marker.getPoint().x,
						    Level: 4
						  };					
					points.push(newPoint);
					map.addOverlay(marker);
					createEncodings();
				}
			}
			else {
			  var newPoint = {
					    Latitude: point.y,
					    Longitude: point.x,
					    Level: 4
					  };
				if (points.length > 0) {
					color = 'blue';
					title = 'Delivery Point';
				}
				points.push(newPoint);
				var marker = new GMarker(new GLatLng(point.y, point.x), { title: title, icon: getIcon(color) });
				map.addOverlay(marker);
			}
		});
	}

	function buildBorder(map, points, levels, color) {
		var polyline = new GPolyline.fromEncoded({
		    color: color,
		    weight: 5,
		    points: points,
		    levels: levels,
		    zoomFactor: 32,
		    numLevels: 5
		});
		map.addOverlay(polyline);
	}

	function buildUnEncodedBorder(map) {		
		var polyline = new GPolyline(points, "#C30;", 5);
		map.addOverlay(polyline);
	}

	// Encode a signed number in the encode format.
	function encodeSignedNumber(num) {
	  var sgn_num = num << 1;

	  if (num < 0) {
	    sgn_num = ~(sgn_num);
	  }

	  return(encodeNumber(sgn_num));
	}

	// Encode an unsigned number in the encode format.
	function encodeNumber(num) {
	  var encodeString = "";

	  while (num >= 0x20) {
	    encodeString += (String.fromCharCode((0x20 | (num & 0x1f)) + 63));
	    num >>= 5;
	  }

	  encodeString += (String.fromCharCode(num + 63));
	  return encodeString;
	}

	// Create the encoded polyline and level strings. If moveMap is true
	// move the map to the location of the first point in the polyline.
	function createEncodings() {
	  var i = 0;

	  var plat = 0;
	  var plng = 0;

	  var encoded_points = "";
	  var encoded_levels = "";

	  for(i = 0; i < points.length; ++i) {
	    var point = points[i];
	    var lat = point.Latitude;
	    var lng = point.Longitude;
	    var level = point.Level;

	    var late5 = Math.floor(lat * 1e5);
	    var lnge5 = Math.floor(lng * 1e5);

	    dlat = late5 - plat;
	    dlng = lnge5 - plng;

	    plat = late5;
	    plng = lnge5;
	    
			encoded_points += encodeSignedNumber(dlat) + encodeSignedNumber(dlng);
	    encoded_levels += encodeNumber(level);
	  }

	  $('encodedLevels').value = encoded_levels;
	  $('encodedPolyline').value = encoded_points;

	  if (document.overlay) {
	    document.map.removeOverlay(document.overlay);
	  }

	  if (points.length > 1) {
	    document.overlay = GPolyline.fromEncoded({color: "#0000FF",
	                                              weight: 10,
	                                              points: encoded_points,
	                                              zoomFactor: 32,
	                                              levels: encoded_levels,
	                                              numLevels: 4
	                                             });

	    document.map.addOverlay(document.overlay);
	  }
	}


	function clear(map) {
		points = new Array();
		map.clearOverlays();		
	}
		
	function buildPoints(map, done) {		
		var marker;
		var point;
		var ipoint = 0;
		while (ipoint < points.length){
			point = points[ipoint];
			if (!done) {
				if (ipoint == points.length - 1) {
					if (ipoint == 0) {
						marker = new GMarker(new GLatLng(point.Latitude, point.Longitude), { title: 'Draggable', icon: getIcon('red') });
					}
					else {
						marker = new GMarker(new GLatLng(point.Latitude, point.Longitude), { title: 'Draggable', icon: getIcon('blue') });
					}
					GEvent.addListener(marker, "dragend", function() {
						points.pop();
					  var newPoint = {
							    Latitude: marker.getPoint().y,
							    Longitude: marker.getPoint().x,
							    Level: 0
							  };					
						points.push(newPoint);
					});
				}
				else {
					if (ipoint == 0) {
						marker = new GMarker(new GLatLng(point.Latitude, point.Longitude), { title: 'Starting Point', icon: getIcon('red'), draggable: true });
					}
					else {
						marker = new GMarker(new GLatLng(point.Latitude, point.Longitude), { icon: getIcon('blue') });
					}
					map.addOverlay(marker);
				}
			}
			ipoint = ipoint + 1;
		}
	}

	function getIcon(color, draggable) {
		var icon = new GIcon();
		icon.image = "http://labs.google.com/ridefinder/images/mm_20_" + color + ".png";
		icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		icon.iconSize = new GSize(12, 20);
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);
		return icon;
	}
	function createMarker(map, info) {		
		var marker = new GMarker(map.getCenter());
		if (info.length > 0) {
			GEvent.addListener(marker, "mouseover", function() {			
				marker.openInfoWindowHtml(info);			
			});
			map.openInfoWindow(map.getCenter(), info);
			map.addOverlay(marker);
		}
		return marker;
	}
		
	function formatAddress(name, address, city, state, zip, other) {
		result = "<u>" + name + "</u>" + "<br>" + address + "<br>" + city + ", " + state + " " + zip + "<br>" + other;
		return "<h3>" + result + "</h3>";
	}
