	var map = null;
	var home_shape = null;
	var RuachLoc = new VELatLong(50.94874, -1.46135);
	var pinPixel = null;

	function getdomitem(item_name, item_type)
	{
	  var obj = document.body.getElementsByTagName(item_type);
	  var retobj = null;
	  if (obj!=null)
	  {
	    if (obj.length>0)
	    {
	      for (i=0; i<obj.length; i++)
	      {
	        if (obj[i].id==item_name)
	        {
	          retobj = obj[i];
	          break;
	        }
	      }
	    } else {
	      if (obj.id==item_name)
	        retobj = obj;
	    }
	  }
	  return retobj;
	} // getdomitem

	function GetMap()
	{
		map = new VEMap('myMap');
		load_map();
	} // GetMap

	function clear_map()
	{
		var get_control;
//		var clear_control;
		var target_div;

		get_control = getdomitem('get_route', 'input');
//		clear_control = getdomitem('clear', 'input');
		get_control.disabled = false;
//		clear_control.disabled = true;
		target_div = getdomitem('direction_list', 'div');
		target_div.innerHTML = "";
		map.DeleteRoute();
		map.DeleteAllShapes();
		load_map();
	} // clear_map

	function load_map()
	{
		map.LoadMap(RuachLoc, 14, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
		home_shape = new VEShape(VEShapeType.Pushpin, RuachLoc);
		home_shape.SetCustomIcon("./images/flag2.gif");
		home_shape.SetPhotoURL("./images/menulogosmall.jpg");
		home_shape.SetTitle("Ruach Therapy");
		home_shape.SetDescription("18 Betteridge Drive<br />Rownhams<br />Southampton<br />SO16 8LE");
		add_pin();
	} // load_map

	function add_pin()
	{
		// Add a new pushpin to RuachLoc
		pinPixel = map.LatLongToPixel(RuachLoc);
		map.AddShape(home_shape);
	} // add_pin

	function do_directions()
	{
		// Read postcode from map and do directions...
		var locations;
		var myOptions;
		var start_control;
		var short_control;
		var measure_control;
		var start_point;
		var short_value;
		var measure_value;
		var get_control;
//		var clear_control;

		if (map==null)
		{
			alert("The map is not yet ready: please try again when it has finished loading.");
		} // if (map==null)
		else
		{
			start_control = getdomitem('postcode', 'input');
			short_control = getdomitem('shortest_time', 'input');
			measure_control = getdomitem('measure_km', 'input');
			start_point = start_control.value;
			short_value = short_control.checked;
			measure_value = measure_control.checked;
			find_start_point(start_point);
			locations = new Array(start_point, "SO16 8LE");
			myOptions = new VERouteOptions;
			myOptions.DrawRoute      = true;
			myOptions.SetBestMapView = true;
			if (short_value)
			{
				myOptions.RouteOptimize  = VERouteOptimize.MinimizeTime;
			} // if (short_value)
			else
			{
				myOptions.RouteOptimize  = VERouteOptimize.MinimizeDistance;
			} // else
			myOptions.RouteColor     = new VEColor(0,169,235,0.7);
			myOptions.RouteWeight    = 6;
			myOptions.RouteZIndex    = 20;
			if (measure_value)
			{
				myOptions.DistanceUnit  = VERouteDistanceUnit.Kilometer;
			} // if (measure_value)
			else
			{
				myOptions.DistanceUnit  = VERouteDistanceUnit.Mile;
			} // else
			myOptions.ShowDisambiguation = true;
			myOptions.SetBestMapView = false; 		// Don't change map view
			myOptions.RouteCallback  = route_handler;  	// Gets VERoute
			try
			{
				map.GetDirections(locations, myOptions);
				get_control = getdomitem('get_route', 'input');
//				clear_control = getdomitem('clear', 'input');
				get_control.disabled = true;
//				clear_control.disabled = false;
			} // try
			catch (e)
			{
				alert("Problem finding route: " + e.message);
			} //catch
		} // else
	} // do_directions

	function find_start_point(start_point)
	{
            try
            {
               results = map.Find("postcode",
                                  start_point,
                                  null,
                                  null,
                                  0,
                                  1,
                                  true,
                                  true,
                                  true,
                                  false,
                                  has_results);
            } // try
            catch(e)
            {
               alert("Exception finding start location: " + e.message);
            } // catch
	} // find_start_point

	function has_results(layer, resultsArray, places, hasMore, veErrorMessage)
	{
            if(places!=null && places.length>0)
            {
               map.IncludePointInView(places[0].LatLong);
            } // if(places!=null && places.length>0)
	} // has_results

	function route_handler(route)
	{
	   // Unroll route and populate page area
	   var legs          = route.RouteLegs;
	   var turns         = "<small><b>Turn-by-Turn Directions</b></small><br />";
	   var leg           = null;
	   var turnNum       = 0;  // The turn #
	   var totalDistance = 0;  // The sum of all leg distances
	   var target_div;
	   var measure_control;
	   var measure_value;
	   var measure_unit;

	   measure_control = getdomitem('measure_km', 'input');
	   measure_value = measure_control.checked;
	   if (measure_value)
	   {
	   	measure_unit = " km";
	   } // if (measure_value)
	   else
	   {
	   	measure_unit = " miles";
	   } // else
	   // Get intermediate legs
	   for (var i = 0; i < legs.length; i++)
	   {
	      // Get this leg so we don't have to dereference multiple times
	      leg = legs[i];  // Leg is a VERouteLeg object

	      // Unroll each intermediate leg
	      var turn        = null;  // The itinerary leg
	      var legDistance = null;  // The distance for this leg

	      for (var j = 0; j < leg.Itinerary.Items.length; j++)
	      {
	        turnNum++;
	        // turn is a VERouteItineraryItem object
	        turn = leg.Itinerary.Items[j];  
	        turns += "<small>" + turnNum + ":  " + turn.Text;
	        legDistance    = turn.Distance;
	        totalDistance += legDistance;

	        // Round distances to 1/10ths
	        // Note that miles is the default
		turns += " (" + legDistance.toFixed(1) + measure_unit + ")</small><br />";
	        
	      } // for j
	   } // for i
	   turns += "<small>Total distance:  " + totalDistance.toFixed(1) + measure_unit + "</small>";

	   // Show directions
	   target_div = getdomitem('direction_list', 'div');
	   target_div.innerHTML = turns;
	} // route_handler
