function createRequest() {
    var myRequest = null;
    if (myRequest) { return myRequest; }
    else {
        req = false;
        if (window.XMLHttpRequest) {
            try { myRequest = new XMLHttpRequest(); return myRequest; }
            catch (e) { myRequest = false; return false; }
            return false;
        } else if (window.ActiveXObject) {
            try { myRequest = new ActiveXObject('Msxml2.XMLHTTP'); return myRequest; }
            catch (e) {
                try { myRequest = new ActiveXObject('Microsoft.XMLHTTP'); return myRequest; } catch (e) { myRequest = false; return false; }
            }
            return false;
        } else { myRequest = false; return false; }
    }

}



var ajaxRequest = createRequest();
var geocoder;
var map;
var baseIcon = new GIcon(G_DEFAULT_ICON);
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);

///////////////////////////////////////////////////////////////////////////////////////
// Laurie's Generic AJAX content requestor
// Gets content from file at URL
// Using variables VARS
// And Posts into ElementId TARGET
function getContent(url, vars, target) {
    targetElement = target;
    //alert(url + vars + " - " + target);
    showContent("status_loading");
    ajaxRequest.open("POST", url, true);
    ajaxRequest.onreadystatechange = refreshContent;
    ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajaxRequest.send(vars + "&submit=true");
}

function refreshContent() {
    if (ajaxRequest.readyState == 4) {
        if (ajaxRequest.status == 200) {
            setContent(targetElement, ajaxRequest.responseText);
            showContent(targetElement);

            hideContent("status_loading");
            ajaxRequest = createRequest();          
        }
    }
}

function showContent(target) {
    document.getElementById(target).style.visibility = 'visible';
}
function hideContent(target) {
    document.getElementById(target).style.visibility = 'hidden';
}
function clearContent(target) {
    document.getElementById(target).innerHTML = '';
}
function setContent(target, value) {
    document.getElementById(target).innerHTML = value;
}
function setHeight(target, height) {
    el = document.getElementById(target)
    if (el.style.height == height)
        el.style.height = 'auto';
    else if(el.style.height == 'auto')
        el.style.height = height;
}
function minimizeContent(target) {
    document.getElementById(target).style.height = '1px;';
}
function ScrollToTarget(target){
	
	var selectedPosX = 0;
  	var selectedPosY = 0;
  	var theElement = document.getElementById(target)            

	while(theElement != null){
		selectedPosX += theElement.offsetLeft;
		selectedPosY += theElement.offsetTop;
		theElement = theElement.offsetParent;
	}         		      
 	window.scrollTo(selectedPosX,selectedPosY);
}

function sleep(delay)
{
    var start = new Date().getTime();
    while (new Date().getTime() < start + delay);
}

function getSearchLocation()
{
	//town = document.getElementById("TownSrc").value;
	//address = document.getElementById("Address1Src").value + document.getElementById("Address2Src").value;
	if (document.getElementById("Postcode").value == "")
	{
		document.getElementById("Postcode").value = "W1 1EA"
		address = "London, GB"; 
	}
	else
	{
		address = document.getElementById("Postcode").value + " , GB";
	}
	
	
	

	map = new GMap2(document.getElementById("map_canvas_search"));
 //	map.setMapType(G_HYBRID_MAP);
	map.setMapType(G_PHYSICAL_MAP);
	geocoder = new GClientGeocoder();
	
	GEvent.addListener(map, "moveend", function() {
      var center = map.getCenter();
      document.getElementById("SearchLat").value = center.lat();
	  document.getElementById("SearchLon").value = center.lng();
	  document.forms['SearchForm'].submit()});
    //map.addControl(new GSmallMapControl());
	//map.setCenter(new GLatLng(56.72259433299854, -4.075927734375), 7);
	geocoder.getLocations(address, addAddressToMapSearch);
}

function getSrcLocation()
{
	town = document.getElementById("TownSrc").value;
	address = document.getElementById("Address1Src").value + document.getElementById("Address2Src").value;
	postcode = document.getElementById("PostcodeSrc").value;
	address = address + ", " + town + ", " + postcode + ", GB";
	
	map = new GMap2(document.getElementById("map_canvas_src"));
 //	map.setMapType(G_HYBRID_MAP);
	map.setMapType(G_PHYSICAL_MAP);
	geocoder = new GClientGeocoder();
	
	GEvent.addListener(map, "moveend", function() {
      var center = map.getCenter();
      document.getElementById("LatSrc").value = center.lat();
	  document.getElementById("LonSrc").value = center.lng(); });
	
    map.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng(56.72259433299854, -4.075927734375), 7);
	geocoder.getLocations(address, addAddressToMapSrc);
}

function getDestLocation()
{
	town = document.getElementById("TownDest").value;
	address = document.getElementById("Address1Dest").value + document.getElementById("Address2Dest").value;
	postcode = document.getElementById("PostcodeDest").value;
	address = address + ", " + town + ", " + postcode + ", GB";

	map = new GMap2(document.getElementById("map_canvas_dest"));
 //	map.setMapType(G_HYBRID_MAP);
	map.setMapType(G_PHYSICAL_MAP);
	geocoder = new GClientGeocoder();
	
	GEvent.addListener(map, "moveend", function() {
      var center = map.getCenter();
      document.getElementById("LatDest").value = center.lat();
	  document.getElementById("LonDest").value = center.lng(); });
	
    map.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng(56.72259433299854, -4.075927734375), 7);
	geocoder.getLocations(address, addAddressToMapDest);
	
}

function drawSearchMap(lat, lon) 
{
	//town = document.getElementById("Town").value;
	//address = document.getElementById("Address1").value + document.getElementById("Address2").value;
	//postcode = document.getElementById("Postcode").value;
	//address = address + ", " + town + ", " + postcode + ", GB";
	//var lon = document.getElementById("CemeteryLon").value;
 	//var lat = document.getElementById("CemeteryLat").value;
	map = new GMap2(document.getElementById('map_canvas2'));
 //	map.setMapType(G_HYBRID_MAP);
	map.setMapType(G_PHYSICAL_MAP);
	geocoder = new GClientGeocoder();
 	//var mapTypeControl = new GMapTypeControl();
    //var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
   // var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
    
	//map.addControl(mapTypeControl, topRight);
    /*GEvent.addListener(map, "dblclick", function() {
      map.removeControl(mapTypeControl);
      map.addControl(new GMapTypeControl(), bottomRight);});*/
	
	//GEvent.addListener(map, "moveend", function() {
      //var center = map.getCenter(); });
     // document.getElementById("Lat").value = center.lat();
	  //document.getElementById("Lon").value = center.lng(); });
	
    map.addControl(new GSmallMapControl());
	//map.setCenter(new GLatLng(lat, lon), 10);
	map.clearOverlays();
	showContent('map_canvas2');
	//alert(map + "woof" + lat + ',' + lon);
	//getLocation(address); 
	
}


function createMarker(point, index) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(index); });
  return marker;
}
function createPremiumMarker(point, index) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(index); });
  return marker;
}

function drawSearchMarker(num, description, lat, lon)
{
	   point = new GLatLng(lat, lon);
	   var myIcon = new GIcon(G_DEFAULT_ICON);
              myIcon.image = "/images/truck_small.png"; // URL of your big yellow marker
			  myIcon.shadow = "/images/truck_small_sdw.png"; // URL of your big yellow marker
			  myIcon.iconSize = new GSize(24,24);
			   myIcon.shadowSize = new GSize(36,20);
			   myIcon.iconAnchor = new GPoint(24, 24);
			   myIcon.infoWindowAnchor = new GPoint(12, 12);

       var marker = new GMarker(point,{icon:myIcon});
	  //var marker = new GMarker(point,description);
	   GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(description); });
	   //description =  '<br>' + num + ':&nbsp;<b>' + name + '</b>'
	   map.addOverlay(marker);
		//marker.openInfoWindowHtml(description);
	

}
function drawPremiumSearchMarker(num, description, lat, lon)
{
	//alert( name); // + ' ' + lat + ',' + lon);
	 //place = response.Placemark[0];
	   point = new GLatLng(lat, lon);
	   var myIcon = new GIcon(G_DEFAULT_ICON);
              myIcon.image = "/images/truck.png"; // URL of your big yellow marker
			  myIcon.shadow = "/images/truck_sdw.png"; // URL of your big yellow marker
			  myIcon.iconSize = new GSize(36,36);
			  myIcon.shadowSize = new GSize(48,34);
			   myIcon.iconAnchor = new GPoint(36, 36);
			   
			    myIcon.infoWindowAnchor = new GPoint(18, 18);
       var marker = new GMarker(point,{icon:myIcon});
	  //	  var marker = new GMarker(point,description);
	   GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(description); });
	   //description =  '<br>' + num + ':&nbsp;<b>' + name + '</b>'
	   map.addOverlay(marker);
		//marker.openInfoWindowHtml(description);
		//map.addOverlay(createMarker(point,description));
	

}
function drawMapLL()
{
	// Draw Map from Lat, Lon stored in Elements on Page
	map = new GMap2(document.getElementById("map_canvas"));
	map.setMapType(G_PHYSICAL_MAP);
	geocoder = new GClientGeocoder();
    GEvent.addListener(map, "dblclick", function() {
      map.removeControl(mapTypeControl);
      map.addControl(new GMapTypeControl(), bottomRight);});
    map.addControl(new GSmallMapControl());
	map.clearOverlays();
 	if(document.getElementById("Lat").value != '' && document.getElementById("Lon").value != '')
	{
	  // place = response.Placemark[0];
	   point = new GLatLng(document.getElementById("Lat").value,
						  document.getElementById("Lon").value);
	   marker = new GMarker(point);
	   map.addOverlay(marker);
	   map.setCenter(point, 6);
	   showContent("map_canvas");
	}
}

function drawMap() 
{
	town = document.getElementById("Town").value;
	address = document.getElementById("Address1").value + document.getElementById("Address2").value;
	postcode = document.getElementById("Postcode").value;
	address = address + ", " + town + ", " + postcode + ", GB";
	//var lon = document.getElementById("CemeteryLon").value;
 	//var lat = document.getElementById("CemeteryLat").value;
	map = new GMap2(document.getElementById("map_canvas"));
 //	map.setMapType(G_HYBRID_MAP);
	map.setMapType(G_PHYSICAL_MAP);
	geocoder = new GClientGeocoder();
 	//var mapTypeControl = new GMapTypeControl();
    //var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
   // var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
    
	//map.addControl(mapTypeControl, topRight);
    GEvent.addListener(map, "dblclick", function() {
      map.removeControl(mapTypeControl);
      map.addControl(new GMapTypeControl(), bottomRight);});
	
	GEvent.addListener(map, "moveend", function() {
      var center = map.getCenter();
      document.getElementById("Lat").value = center.lat();
	  document.getElementById("Lon").value = center.lng(); });
	
    map.addControl(new GSmallMapControl());
	//map.setCenter(new GLatLng(56.72259433299854, -4.075927734375), 7);
	getLocation(address); 
	
}
function getLocation(address)
{
	
	geocoder.getLocations(address, addAddressToMap);
}


function addAddressToMap(response) 
{
	map.clearOverlays();
 	if (!response || response.Status.code != 200) 
	{
   		alert("Sorry, we were unable to geocode that address");
 	} 
	else 
	{
	   place = response.Placemark[0];
	   point = new GLatLng(place.Point.coordinates[1],
						   place.Point.coordinates[0]);
	   marker = new GMarker(point);
	   map.addOverlay(marker);
	  // marker.openInfoWindowHtml(place.address + '<br>' + '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
	   map.setCenter(new GLatLng(place.Point.coordinates[1],  place.Point.coordinates[0]), 6);
	   showContent("map_canvas");
	}
}
function addAddressToMapSrc(response) 
{
	map.clearOverlays();
 	if (!response || response.Status.code != 200) 
	{
   		alert("Sorry, we were unable to geocode that address");
 	} 
	else 
	{
	   place = response.Placemark[0];
	   point = new GLatLng(place.Point.coordinates[1],
						   place.Point.coordinates[0]);
	   marker = new GMarker(point);
	   map.addOverlay(marker);
	  // marker.openInfoWindowHtml(place.address + '<br>' + '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
	   map.setCenter(new GLatLng(place.Point.coordinates[1],  place.Point.coordinates[0]), 6);
	   showContent("map_canvas_src");
	}
}
function addAddressToMapDest(response) 
{
	map.clearOverlays();
 	if (!response || response.Status.code != 200) 
	{
   		alert("Sorry, we were unable to geocode that address");
 	} 
	else 
	{
	   place = response.Placemark[0];
	   point = new GLatLng(place.Point.coordinates[1],
						   place.Point.coordinates[0]);
	   marker = new GMarker(point);
	   map.addOverlay(marker);
	  // marker.openInfoWindowHtml(place.address + '<br>' + '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
	   map.setCenter(new GLatLng(place.Point.coordinates[1],  place.Point.coordinates[0]), 6);
	   showContent("map_canvas_dest");
	}
}
function addAddressToMapSearch(response) 
{
	map.clearOverlays();
 	if (!response || response.Status.code != 200) 
	{
   		alert("Sorry, we were unable to geocode that address");
 	} 
	else 
	{
	   place = response.Placemark[0];
	   point = new GLatLng(place.Point.coordinates[1],
						   place.Point.coordinates[0]);
	   marker = new GMarker(point);
	   map.addOverlay(marker);
	  // marker.openInfoWindowHtml(place.address + '<br>' + '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
	  map.setCenter(new GLatLng(place.Point.coordinates[1],  place.Point.coordinates[0]), 6);
	  //showContent("map_canvas_search");
	 
	}
}
function showLocation() 
{
	var address = document.getElementById('map_address').value;
	geocoder.getLocations(address, addAddressToMap);
}
