var resultLat;
var resultLng;
var localSearch = new GlocalSearch();
// Global variables
    var map,icon1,marker,listCounter;
	listCounter = 1;
var icons = new Array();
var resultLatList = new Array();
var resultLngList = new Array();
function usePointFromPostcode(postcode, callbackFunction) {
  
  localSearch.setSearchCompleteCallback(null, 
    function() {
      
      if (localSearch.results[0]) {    
        resultLat = localSearch.results[0].lat;
        
		resultLng = localSearch.results[0].lng;
        var point = new GLatLng(resultLat,resultLng);
        callbackFunction(point);
      }else{
        // alert("Postcode not found!");
      }
    });  
    
  localSearch.execute(postcode + ", UK");
}

function usePointFromPostcodeExt(postcode, callbackFunction, exParam) {
  
  localSearch.setSearchCompleteCallback(null, 
    function() {
      
      if (localSearch.results[0]) {    
        resultLatList[exParam] = localSearch.results[0].lat;
        
		resultLngList[exParam] = localSearch.results[0].lng;
        var point = new GLatLng(resultLat,resultLng);
        callbackFunction(point,exParam);
      }else{
        // alert("Postcode not found!");
      }
    });  
    
  localSearch.execute(postcode + ", UK");
}

function autofill(xxx, exParam) {
	document.getElementById("lat" + exParam).value = resultLatList[exParam];
	document.getElementById("lng" + exParam).value = resultLngList[exParam];
	doAutoFill(exParam + 1);
}

function getAddressFromPoint() {
	var url="revco2.asp?lat=" + resultLat + "&lng=" + resultLng + "&acc=20"
	alert(url)
	 if (window.XMLHttpRequest) {
		        req = new XMLHttpRequest();
		        req.onreadystatechange = populateAdd;
		        req.open("get", url, true);
		        req.send(null);
		    // branch for IE/Windows ActiveX version
		    } else if (window.ActiveXObject) {
		        req = new ActiveXObject("Microsoft.XMLHTTP");
				//alert(searchTerm)
		        if (req) {
		            req.onreadystatechange = populateAdd;
		            req.open("get", url, true);
		            req.send();
		        }
		    }
		}




function populateAdd() 
		{
		    if (req.readyState == 4) {
		        // only if "OK"
		        if (req.status == 200) {
		            // ...processing statements go here...
					var response  = req.responseText;
					responseBits = new Array();
					responseBits = response.split("|");
					alert(response)
					
					
		        } else {
					// Failure - degrade gracefully and alert user with useful message
		            alert("We were not able to check the user database - please try again later.");
		        }
				
		    }
			
		} 

function doStuff() {
	// alert("Lat: " + resultLat + ", Lng: " + resultLng);
	document.getElementById("latspan").innerHTML = resultLat;
	document.getElementById("lngspan").innerHTML = resultLng;
	document.getElementById("lat").value = resultLat;
	document.getElementById("lng").value = resultLng;
}
function doStuffQuick() {
	// alert("Lat: " + resultLat + ", Lng: " + resultLng);
	document.getElementById("quicklat").value = resultLat;
	document.getElementById("quicklng").value = resultLng;
}

function load() {
      if (GBrowserIsCompatible()) {
      	map = new GMap2(document.getElementById("map"));
		// alert("currLat="+currLat);
       	map.addControl(new GSmallMapControl());
		//map.addControl(new GScaleControl());
        map.setCenter(new GLatLng(currLat, currLng),zoom);
        initialiseIcons();
        
       
        
      }
 }
 
 function initialiseIcons() {

	  for (var f=1; f<=99; f++) {
	  	icons[f] = new GIcon(G_DEFAULT_ICON,"images/mapicons/marker" + f + ".png");
		
		//icons[f].image = "images/mapicons/marker" + f + ".png";
	  }
	  
	  
	}   
	
	
function makePin(pLat,pLng,pName,pTown,pPostCode,pPhone, number, moreInfoLink) {
		// Strip the actual conditions out
		
		var thisIcon;
		thisIcon = icons[number];
		// document.getElementById("list").innerHTML += "<br>" + pName;
				
		point = new GLatLng(parseFloat(pLat),parseFloat(pLng))
		var thisMarker = new GMarker(point,thisIcon)
		GEvent.addListener(thisMarker,"click",function() {
			var html = "<div class='bubble'><b>" + pName + "</b><br>" + pTown + "<br>" + pPostCode + "<br>" + pPhone + "<br>" + "<a href=\"" + moreInfoLink + "\">Details</a>";
			
			currentLat = pLat;
			currentLng = pLng;
			thisMarker.openInfoWindowHtml(html);
			
		});
		//alert(thisMarker)
		//alert(map)
		map.addOverlay(thisMarker)
		
	}
	
	function makeSimplePin(pLat,pLng,pName,pTown,pPostCode,pPhone, number, moreInfoLink) {
		// Strip the actual conditions out
		
		var thisIcon;
		thisIcon = icons[number];
		// document.getElementById("list").innerHTML += "<br>" + pName;
				
		point = new GLatLng(parseFloat(pLat),parseFloat(pLng));
		var thisMarker = new GMarker(point,thisIcon);
		GEvent.addListener(thisMarker,"click",function() {
			var html = "<div class='bubble'><b>" + pName + "</b><br>" + pTown + "<br>" + pPostCode + "<br>" + pPhone + "<br>";
			
			currentLat = pLat;
			currentLng = pLng;
			thisMarker.openInfoWindowHtml(html);
			
		});
		//alert(thisMarker)
		//alert(map)
		map.addOverlay(thisMarker);
	}
	
function loadDetail() {
	load();
	makeSimplePin(locationLat,locationLng,bubbleName,bubbleTown,bubblePostcode,bubbleTel, "1", "");
	var center = new GLatLng(locationLat,locationLng); 
	
	map.setCenter(center);
}


function getDefaultAddressValues(franchiseID) {
	var url="getfranchiseaddress.asp?franchiseid=" + franchiseID
	 if (window.XMLHttpRequest) {
		        req = new XMLHttpRequest();
		        req.onreadystatechange = populateSessionAddressFromFranchise;
		        req.open("get", url, true);
		        req.send(null);
		    // branch for IE/Windows ActiveX version
		    } else if (window.ActiveXObject) {
		        req = new ActiveXObject("Microsoft.XMLHTTP");
		        if (req) {
		            req.onreadystatechange = populateSessionAddressFromFranchise;
		            req.open("get", url, true);
		            req.send();
		        }
		    }
		}
		
function populateSessionAddressFromFranchise() 
		{
		    if (req.readyState == 4) {
		        // only if "OK"
		        if (req.status == 200) {
		            // ...processing statements go here...
					var response  = req.responseText;
					responseBits = new Array();
					responseBits = response.split("|");
					if (responseBits.length > 2) {
						document.getElementById("contactname").value = responseBits[0];
						document.getElementById("address1").value = responseBits[1];
						document.getElementById("address2").value = responseBits[2];
						document.getElementById("town").value = responseBits[3];
						document.getElementById("county").value = responseBits[4];
						document.getElementById("postcode").value = responseBits[6];
						document.getElementById("tel").value = responseBits[7];
						document.getElementById("email").value = responseBits[8];
						// try and autofill Lat & Lng
						usePointFromPostcode(responseBits[6],doStuff)
						
					}
					
		        } else {
					// Failure - degrade gracefully and alert user with useful message
		            alert("We were not able to check the user database - please try again later.");
		        }
				
		    }
			
		} 	
