var geocoder = null;
var marker = null;

function showAddress(address,form_name) {
	//var mf=document.getElementById(form_name);
	if (geocoder) {
	geocoder.getLatLng(
		address,
			function(point) {
				if (!point) {
					alert(address + " not found");
				} else {
		          map.setCenter(point, 13);
		          //mf.latitude.value=point.lat();
		          //mf.longitude.value=point.lng();
		          //mf.search_string.value=address;	    
		          if (marker!=null) map.removeOverlay(marker);
		          marker = new GMarker(point);
		          map.addOverlay(marker);
		          marker.openInfoWindowHtml(address);
				}
			}
		);
	}
}

function add_info(marker,html) {
	GEvent.addListener(marker, "click", function() {
        this.openInfoWindowHtml(html);
    });		
}

function ajaxLoader(url,id,qs,fs) {
	
	if (document.getElementById) {
		var xml_request = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	}
		
	if (xml_request) {
		xml_request.onreadystatechange = function() {
			if (xml_request.readyState == 4 && xml_request.status == 200) {
				
				el = document.getElementById(id);
				var response = xml_request.responseXML.documentElement;
				x=response.getElementsByTagName('place');
				var html='<table id="place_viewer_list"><thead><tr><td>Name</td><td>Distance</td></tr><thead><tbody>';
				for (i=0;i<x.length;i++) {
							html=html+'<tr><td>'+x[i].childNodes[0].childNodes[0].nodeValue+'</td><td></td></tr>';
							if (x[i].childNodes[3].childNodes[0].nodeValue=='new') {
 							var markerOptions;
						var m_pos = new GLatLng(x[i].childNodes[1].childNodes[0].nodeValue, x[i].childNodes[2].childNodes[0].nodeValue);
 							if (x[i].childNodes[4].childNodes[0].nodeValue!=null) {
							var blueIcon = new GIcon(G_DEFAULT_ICON);
							blueIcon.image = fs+'images/icons/maps/'+x[i].childNodes[4].childNodes[0].nodeValue+'.png';
							blueIcon.shadow = fs+'images/icons/maps/'+x[i].childNodes[4].childNodes[0].nodeValue+'_shadow.png';
							blueIcon.iconSize=new GSize(25,25);
							blueIcon.shadowSize=new GSize(34,25);
							blueIcon.iconAnchor=new GPoint(12,12);
							markerOptions = { icon:blueIcon };
						}
						
						markers[markers.length]=new GMarker(m_pos,markerOptions);

						if (x[i].childNodes[5].childNodes[0]) {
							infohtml=x[i].childNodes[5].childNodes[0].nodeValue;
							add_info(markers[(markers.length-1)],x[i].childNodes[5].childNodes[0].nodeValue);
						}
						    								
						map.addOverlay(markers[(markers.length-1)]);
						
					}
						}
						html=html+'</tbody></table>';
					el.innerHTML=html; 						
			}
		}
		xml_request.open("GET", url+'?'+qs, true);
		xml_request.send(null);
	}
	
}

var localSearch = new GlocalSearch();

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("Postcode not found!");
      }
    });  
    
  localSearch.execute(postcode + ", UK");
  
}
