var localSearch = new GlocalSearch();
var resultLat;
var resultLng;

function usePointFromPostCode(postcode,miles) {
	localSearch.setSearchCompleteCallback(null, 
	function() {
		if (localSearch.results[0])
		{		
			resultLat = localSearch.results[0].lat;
			resultLng = localSearch.results[0].lng;
			getAllInRange(resultLat,resultLng,miles);
		}else{
			alert("Postcode or city not found!");
		}
	});	
	localSearch.execute(postcode + ", UK");
}

function getAllInRange(lat,lon,miles){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		//alert ("Browser does not support HTTP Request")
	return
	} 
	var url="include/inspire-me-query.asp"
	url=url+"?Lat="+lat+"&Lon="+lon+"&miles="+miles
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=stateChangedI
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}
	
function stateChangedI(){ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")	{ 
		if (GBrowserIsCompatible()) {
			var nearby = new GMap2(document.getElementById("inspire"));
			nearby.setCenter(new GLatLng(resultLat, resultLng), 8);
			var mapControl = new GMapTypeControl();
			nearby.addControl(mapControl);
			nearby.addControl(new GLargeMapControl());
			var point = new GLatLng(resultLat,resultLng);
			nearby.addOverlay(new GMarker(point));
			var responseArray=xmlHttp.responseText.split("|");
			for (i=0;i<responseArray.length;i++) {
				var responseRow=responseArray[i].split("X");
				addMarker(responseRow[0],responseRow[1],"'"+responseRow[2]+"'",responseRow[3],responseRow[4],nearby);
			}
		 }
	} 
} 

function addMarker(x,y,text,mileText,urls,map){	
	var marker2 = new GMarker(new GLatLng(x,y), new GIcon(G_DEFAULT_ICON, "images/markers/blue-marker.png"));
	GEvent.addListener(marker2,"click",function(){
	marker2.openInfoWindowHtml(text+'<br />'+urls+mileText+' Miles')})
	map.addOverlay(marker2);
}

function GetXmlHttpObject(handler)
{
    var objXmlHttp=null
 
    if (navigator.userAgent.indexOf("Opera")>=0)
    {
        //alert("Opera not supported...")
        return;
    }
    if (navigator.userAgent.indexOf("MSIE")>=0)
    {
        if (navigator.appVersion.indexOf("MSIE 7.0")>=1)
		{
			try
        	{
            	objXmlHttp=new XMLHttpRequest()
            	//objXmlHttp.onreadystatechange=handler
            	return objXmlHttp
        	}
        	catch(e)
        	{
            	//alert("Error. Scripting for XML might be disabled")
            	return
        	}
		}
		else
		{
			var strName="Msxml2.XMLHTTP"
			if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
			{
				strName="Microsoft.XMLHTTP"
			}
			try
			{
				objXmlHttp=new ActiveXObject(strName)
				//objXmlHttp.onreadystatechange=handler
				return objXmlHttp
			}
			catch(e)
			{
				//alert("Error. Scripting for ActiveX might be disabled")
				return
			}
		}
    }
	
    if (navigator.userAgent.indexOf("Mozilla")>=0)
    {
        objXmlHttp=new XMLHttpRequest()
        objXmlHttp.onload=handler
        objXmlHttp.onerror=handler
        return objXmlHttp
    }
} 