// JavaScript Document
		



    //<![CDATA[
	// this page will have some url parameters passed into it - postcode etc
	// need to get these, pass over to ggogle to get coordinates then display
	
	
	
	// var map = new GMap2(document.getElementById("map"));
	// map.setCenter(new GLatLng(37.4419, -122.1419), 13);

var map;
var geocoder;
var usersaddress;



    function load()
		{
			
			if (GBrowserIsCompatible())
				{
					// create the map
					map = new GMap2(document.getElementById("map"));
					map.addControl(new GLargeMapControl());
					map.addControl(new GMapTypeControl());
					geocoder = new GClientGeocoder();
					// reset this
					geocoder.setCache(null);

					// display icons for all the addresses first - taken from the returned xml	
					// get the selected radio button
					for (counter = 0; counter < document.frmFindLocal.radService.length; counter++)
						{
							if (document.frmFindLocal.radService[counter].checked)
								var iCount = counter;
						}
					//alert("1");
					//alert(getSelectedRadioValue(document.frmFindLocal.radService));
					// create the url including the postcode and service id to get back a list of services
					var strURL = "../googlemaps/createmap.asp?postcode=" + document.frmFindLocal.txtPostcode.value + "&service=" + getSelectedRadioValue(document.frmFindLocal.radService);
					//alert(strURL);
					GDownloadUrl(strURL ,
							function(data, responseCode)
							{
								var xml = GXml.parse(data);
								var addresses = xml.documentElement.getElementsByTagName("address");
								// alert(addresses.length);
								// loop through all the returned addresses
								for (var i = 0; i < addresses.length; i++)
									{
										// the users details come up first, so...
										if (i == 0)
										{
											var userLat = 	addresses[i].getAttribute("latitude");
											var userLong = 	addresses[i].getAttribute("longitude");
										}
										// alert(addresses[i].getAttribute("latitude") + " " + addresses[i].getAttribute("longitude") + " " + addresses[i].getAttribute("type"));
										showAddress(addresses[i].getAttribute("latitude"), addresses[i].getAttribute("longitude"), addresses[i].getAttribute("type"), GXml.value(addresses[i]) );

									}
								map.setfocus(userLat, userLong);
							}
						);
					
				
		
				}
		}




// shifts the map and views the info box for the clicked address
function showAddress(latitude, longitude, thetype, addressdetails)
	{
		if (thetype == 'user')
		{
			// add in a different icon for the persons own house
			map.setCenter(new GLatLng(latitude, longitude), 15);
			var blueIcon = new GIcon();
			blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
			blueIcon.iconAnchor = new GPoint(20, 20);
			blueIcon.infoWindowAnchor = new GPoint(5, 1);
			// Set up our GMarkerOptions object
			markerOptions = { icon:blueIcon };
			var marker = new GMarker(new GLatLng(latitude, longitude), markerOptions);
			map.addOverlay(marker);
			var userlatlng = new GLatLng(latitude, longitude);
			usersaddress = addressdetails;
		}
		else
		{
			// this is a service address, add in the address and name etc
			var marker = new GMarker(new GLatLng(latitude, longitude));
			map.addOverlay(marker);
			GEvent.addListener(marker, "click", function() {
			  	marker.openInfoWindowHtml(addressdetails);
			});
			//marker.openInfoWindowHtml(addressdetails);
		}
		
	}

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function showServiceAddress(address, addressdetails, lat, lng)
	{


	
	// this is a service address, add in the address and name etc
	//var map = new GMap2(document.getElementById("map"));
	//map.setCenter(new GLatLng(lat, lng), 13);


	var latlng = new GLatLng(lat, lng);
	var marker = new GMarker(latlng);
	map.panTo(latlng);
	map.addOverlay(marker);
	marker.openInfoWindowHtml(addressdetails);
	
	/*
	geocoder.getLatLng
		(
			address,
			function(latlng)
				{
					
	
					if (!latlng)
						{
							alert(address + " not found");
						}
						else
						{
							alert(latlng);
							// this is a service address, add in the address and name etc
							var marker = new GMarker(latlng);
							map.addOverlay(marker);
							marker.openInfoWindowHtml(addressdetails);
						}
				}
		);
	*/
}




// if the form has been submitted run the scripts for the map? 

	//if (!document.getElementById("txtPostcode").value == "")
	{
		load();
	}


    //]]>

