
//<![CDATA[
	 var select_html = '<select class="long" onChange="handleSelected(this)" STYLE="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; border: #000000; border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; width:400px;">' +
					   '<option selected style="background-color:#FFD22A">Select a Citizens Advice Bureau or an Outreach Office<\/option>';
	
	 var close_html = '';
	
	 var gmarkers = [];
	
	 var htmls = [];
	 var postcodes = [];
	 var labels = [];
	 var bureaus = [];
	 
	
	 var to_htmls = [];
	 var from_htmls = [];
	 var i = 0;
	
	 var allmarkers = 0;
	 
	 var localSearch = new GlocalSearch();
	 
	 var icon = new GIcon();
	 var iconb = new GIcon();
	 
	 var homeicon = new GIcon();
	 
	 var map=null;
	 
	 var home = new GMarker(new GLatLng(56.861,-1.934),{icon: homeicon, title: "Home"});
	
	 icon.image = "images/icons/mm_20_blue.png";
	 icon.shadow = "images/icons/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); 
	 
	 
	// iconb.image = "images/icons/mm_20_yellow.png";
	iconb.image = "images/icons/mm_20_blue.png";
	 iconb.shadow = "images/icons/mm_20_shadow.png";
	 iconb.iconSize = new GSize(12, 20);
	 iconb.shadowSize = new GSize(22, 20);
	 iconb.iconAnchor = new GPoint(6, 20);
	 iconb.infoWindowAnchor = new GPoint(5, 1); 
	 
	
	 homeicon.image = "images/icons/mm_home.png";
	// homeicon.shadow = "http://maps.google.com/mapfiles/arrowshadow.png";
	 homeicon.iconSize = new GSize(39, 34);
	 homeicon.shadowSize = new GSize(39, 34);
	 homeicon.iconAnchor = new GPoint(10, 34);
	
	 
function load(){
	if (GBrowserIsCompatible()) {
	// create the map
	 map = new GMap2(document.getElementById("map"));
	  
	 map.enableDoubleClickZoom();
	 map.addControl(new GLargeMapControl());
	 map.addControl(new GMapTypeControl()); 
	 map.setCenter(new GLatLng(52.7, -2.0), 6);
	
	 // create the marker manager
	
	 mgr = new GMarkerManager(map);

	
	GDownloadUrl("cab.xml", function (doc) {
	  var xmlDoc = GXml.parse(doc);
	  var markers = xmlDoc.documentElement.getElementsByTagName("marker");
	  
	  allmarkers = markers.length - 1;
	
	  for (var i = 0; i < markers.length; i++) {
	   
	   // obtain the attributes of each marker
	   var lat = parseFloat(markers[i].getAttribute("lat"));
	   var lng = parseFloat(markers[i].getAttribute("lng"));
	   var point = new GLatLng(lat,lng);
	   htmls[i] = markers[i].getAttribute("html");
	   labels[i] = markers[i].getAttribute("label");
	   postcodes[i] = markers[i].getAttribute("postcode");
	   bureaus[i] = markers[i].getAttribute("bureau");
	   
	   
	   
	   // create the marker
	   
	   
	   createMarker(point);
	 }
	
	  mgr.addMarkers(gmarkers,0,17);
	  mgr.refresh();
	
	  document.getElementById("pcresults").innerHTML = close_html;
	 
	  select_html += '<\/select>';
	  document.getElementById("cablist").innerHTML = select_html;
	
	 });
	
	}
	else {
	 alert("Sorry, the Google Maps API is not compatible with this browser");
	}
}
//]]>


 //<![CDATA[

 function checkEnter() {
	 var home = new GMarker(new GLatLng(56.861,-1.934),{icon: homeicon, title: "Home"});
	 homeicon.image = "images/icons/mm_home.png";
	 //homeicon.shadow = "http://maps.google.com/mapfiles/arrowshadow.png";
	 homeicon.iconSize = new GSize(39, 34);
	 homeicon.shadowSize = new GSize(39, 34);
	 homeicon.iconAnchor = new GPoint(10, 34);
   var postcode = document.getElementById('postcode').value;

   if (postcode == "") {
   }
   else if (validatePostCode(postcode) == false) {
    highlightValidationError('postcode');
   }
   else {
    clearError('postcode');
    usePointFromPostcode(postcode, placeHomeMarkerAtPoint);
   }

   return false;
 }

 // A function to create the marker and set up the event window

 function createMarker(point) {

	var marker = new GMarker(point,icon);
	
	var markerb = new GMarker(point,iconb);
	


  var label = labels[i];
  var postcode = postcodes[i];
  var bureau = bureaus[i];
  var html = htmls[i];
  
//  alert (bureau)
if (bureau == 'True')
	{
	  gmarkers[i] = marker;
	}
else
	{
 	gmarkers[i] = markerb;
	 }

  htmls[i] = html;

  // The info window version with the "to here" form open
  to_htmls[i] = html + '<br \/><div class="directions">Directions: <b>To here<\/b> - <a href="javascript:fromhere(' + i + ')">From here<\/a>' +
   '<br \/>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
   '<input type="text" class="short" name="saddr" id="saddr" value="" />' +
   ' <input value="Get Directions" class="submit" type="submit" />' +
  // '<input type="hidden" name="sll" value="' + point.lat() + ',' + point.lng() + '"/>' +
   '<input type="hidden" name="daddr" value="' + postcode + '"/>';
  
  // The info window version with the "from here" form open
  from_htmls[i] = html + '<br\/><div class="directions">Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <b>From here<\/b>' +
   '<br \/>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
   '<input type="text" class="short" name="daddr" id="daddr" value="" />' +
   ' <input value="Get Directions" class="submit" type="submit" />' +
  // '<input type="hidden" name="sspn" value="' + point.lat() + ',' + point.lng() + '"/>';
   '<input type="hidden" name="saddr" value="' + postcode + '"/>';
  
  // The inactive version of the direction info
  //html = html + '<br \/>Directions: <a href="javascript:tohere('+i+')">To here<\/a> - <a href="javascript:fromhere('+i+')">From here<\/a>';

 
 if (bureau == 'True')
	{
	   GEvent.addListener(marker, "click", function() {
  	 marker.openInfoWindowHtml('<div style="background-color:#FFFFFF">'+html+'<\/div>');
  	});
	}
else
	{
 	  GEvent.addListener(markerb, "click", function() {
  	 markerb.openInfoWindowHtml('<div style="background-color:#FFFFFF">'+html+'<\/div>');
 	 });

	 }
 

  // Add the entry to the select box
  select_html += '<option>' + label + '<\/option>';

  if (i == allmarkers) {
   select_html += '<\/select>';
  }

  i++;
 }

 // This function picks up the click and opens the corresponding info window
 function myclick(i) {
  gmarkers[i].openInfoWindowHtml(htmls[i]);
 }

 // functions that open the directions forms
 function tohere(i) {
  gmarkers[i].openInfoWindowHtml(to_htmls[i]);
 }

 function fromhere(i) {
  gmarkers[i].openInfoWindowHtml(from_htmls[i]);
 }

 function handleSelected(opt) {
  var i = opt.selectedIndex - 1;
  if (i > -1) {
   GEvent.trigger(gmarkers[i],"click");
  }
  else {
   map.closeInfoWindow();
  }
 }

 function handleClicked(i) { 
  GEvent.trigger(gmarkers[i],"click");
 } 

 function usePointFromPostcode(postcode,callbackFunction) {
  localSearch.setSearchCompleteCallback(null,
   function() {
    if (localSearch.results[0]) {
     var resultLat = localSearch.results[0].lat;
     var resultLng = localSearch.results[0].lng;
     var point = new GLatLng(resultLat,resultLng);
     callbackFunction(point);
    }
    else {
     alert("Post code not found.");
    }
   }
  );
  localSearch.execute(postcode + ", UK");
 }
 

 function placeHomeMarkerAtPoint(point) {
  
  map.removeOverlay(home);
  home.setPoint(point);
  map.addOverlay(home);
  map.setCenter(new GLatLng(point.lat(),point.lng()),10);
  getDistancesFromHome();
 }

 function getDistancesFromHome() {

  var locations = new Array();
 
  close_html = '<strong>Nearest Citizen Advice Bureau<\/strong><p>';

  for (var i = 0; i < gmarkers.length; i++) {
   var distance = (gmarkers[i].getPoint().distanceFrom(home.getPoint()) / 1609.344) * 1.25;
   distance = distance.toFixed(0);
   locations[i] = { marker: gmarkers[i], distance: distance, label: labels[i], position: i }; 
  }

  locations.sort(sortByDistance);

  for (var j = 0; j < gmarkers.length; j++) {
   close_html += locations[j].distance + ' miles from <a href="javascript:handleClicked(' + locations[j].position + ');">'+ locations[j].label + '<\/a><br \/>';
  }

  close_html += '<\/p>';

  document.getElementById("pcresults").innerHTML = close_html;

 }

 function sortByDistance(a, b) {
  var x = a.distance;
  var y = b.distance;
  return x-y;
 }

 //]]>