

////////////////////////
//
//Manage Location Information
//
////////////////////////

function showWholeCountry(){
        map.setCenter(new GLatLng(53.5,-7.8), 7);
        currentCountyName ='all';
        currentZoneName = 'all';
        getStations();
}



function getCounties(){

	var request = GXmlHttp.create();
	request.open("Get", "api/getCountiesAPI.php", true);
	request.onreadystatechange = function()
		{
		  	// Clear previous overlays so they don't build up with every map move!
		  	counties = new Array();
		  	if (request.readyState == 4)
		  	{
		  	 	var xmlDoc = request.responseXML;
				var countyResults = xmlDoc.documentElement.getElementsByTagName("county");
				var countyName = null;
				
				for (var i = 0; i < countyResults.length; i++)
				{
					countyName = countyResults[i].getAttribute("Name");
				     	counties[countyName] = new Array();
					counties[countyName]['Point'] = new GLatLng(parseFloat(countyResults[i].getAttribute("Lat")),parseFloat(countyResults[i].getAttribute("Lng")));						
				        counties[countyName]['Lat'] = countyResults[i].getAttribute("Lat");
				        counties[countyName]['Lng'] = countyResults[i].getAttribute("Lng");
				        counties[countyName]['Name'] = countyResults[i].getAttribute("Name");
				        counties[countyName]['ZoomLevel'] = countyResults[i].getAttribute("ZoomLevel");
				 }

 			 	 listCounties();

			}
		}

	request.send(null);
}


function listCounties(){
	
	
	var optionsHTML = "";
	var startSelectHTML = "<select id='countyChoices' name='countyChosen' onChange='changeCounty(this.form.countyChoices.value);'><option SELECTED>Select county</option>";
	var endSelectHTML = "</select>";
	var countyChoiceHTML;
	


	
	for (var countyName in counties)
	{
		countyChoiceHTML = "<option value='" + countyName + "'>" + countyName + "</option>";
		optionsHTML += countyChoiceHTML;
	}

	
	document.getElementById("countyList").innerHTML = startSelectHTML + optionsHTML + endSelectHTML;

}


function getZones(countyName){
	var request = GXmlHttp.create();
	request.open("Get", "api/getZonesAPI.php?county="+countyName, true);
	request.onreadystatechange = function()
		{
		  	// Clear previous overlays so they don't build up with every map move!
		  	zones = null;
		  	zones = new Array();
		  	if (request.readyState == 4)
		  	{

		  	 	var xmlDoc = request.responseXML;
				var zoneResults = xmlDoc.documentElement.getElementsByTagName("zone");
				var zoneName = null;
				
				for (var i = 0; i < zoneResults.length; i++)
				{
					zoneName = zoneResults[i].getAttribute("Name");
				     	zones[zoneName] = new Array();
				        zones[zoneName]['ID'] = zoneResults[i].getAttribute("ID");
						zones[zoneName]['Point']= new GLatLng(parseFloat(zoneResults[i].getAttribute("Lat")),parseFloat(zoneResults[i].getAttribute("Lng")));							
				        zones[zoneName]['Lat'] = zoneResults[i].getAttribute("Lat");
				        zones[zoneName]['Lng'] = zoneResults[i].getAttribute("Lng");
				        zones[zoneName]['Name'] = zoneResults[i].getAttribute("Name");
				        zones[zoneName]['ZoomLevel'] = zoneResults[i].getAttribute("ZoomLevel");
				        zones[zoneName]['County'] = zoneResults[i].getAttribute("County");

				 }

				 listZones();
			}	
		}

	request.send(null);	
	

}

function listZones(){
	var oldSpan = document.getElementById("zoneList").getElementsByTagName("span")[0];
	var optionsHTML = "";
	var startSelectHTML = "<select id='zoneChoices' name='zoneChosen' onChange='viewZone(this.form.zoneChoices.value);'><option SELECTED>Select an area</option>";
	var endSelectHTML = "</select>";
	var zoneChoiceHTML;
	
	pZoneList = document.createElement("span");
	
	for (var zoneName in zones)
	{
	
		zoneChoiceHTML = "<option value='" + zoneName + "'>" + zoneName + "</option>";
		optionsHTML += zoneChoiceHTML;
	}
	pZoneList.innerHTML = startSelectHTML + optionsHTML + endSelectHTML;
	document.getElementById("zoneList").replaceChild(pZoneList,oldSpan);

	//document.getElementById("zoneList").innerHTML = startSelectHTML + optionsHTML + endSelectHTML;


}

function viewZone(zoneName){
	currentZoneName=zoneName;
	map.setZoom(17 - zones[zoneName]['ZoomLevel']);	
	map.panTo(zones[zoneName ]['Point']);
	setTimeout("getStations()",1000);
	
}

function changeCounty(countyName){
	currentCountyName=countyName;
	getZones(currentCountyName);
	map.setZoom(17 - counties[currentCountyName]['ZoomLevel']);
	map.panTo(counties[currentCountyName]['Point']);
	setTimeout("getStations()",1000);
}

function changeFuel(fuelType){
        fuel=fuelType;
	if(fuelType=="petrol"){
		showCell("dieselTab");
		hideCell("petrolTab");
	} else {
		showCell("petrolTab");
		hideCell("dieselTab");
	}
        getStations();
}

function debugWriter(debugText){
	document.getElementById("debugText").innerHTML+=debugText;
}

function mapStations(){
	// Clear previous overlays so they don't build up with every map move!
	map.clearOverlays();
	
	for (var i = 0; i < station.length ; i++)
	{
		map.addOverlay(station[i]['marker']);
	}

}
