////////////////////////
//
//Get and List stations
//
////////////////////////
function getStations(){
        var maxLat = map.getBounds().getNorthEast().lat();
        var maxLng = map.getBounds().getNorthEast().lng();
        var minLat = map.getBounds().getSouthWest().lat();
        var minLng = map.getBounds().getSouthWest().lng();

        var request = GXmlHttp.create();
        var random_nocache = Math.random()*4;
        var url="api/getStationsAPI.php?&minLat=" + minLat + "&maxLat=" + maxLat + "&minLng=" + minLng + "&maxLng=" + maxLng + "&noCache="+random_nocache;
	
	request.open("Get", url, true);
	request.onreadystatechange = function()
		{

		  	station = null;
		  	station = new Array();
		  	
		  	if (request.readyState == 4 && request.status == 200)
		  	{
		  	
		  	 	var xmlDoc = request.responseXML;
				var markers = xmlDoc.documentElement.getElementsByTagName("station");
				if(markers.length >0){
					cheapestPrice = 0;
					for (var i = 0; i < markers.length; i++)
					{
						station[i] = new Array();
						station[i]['point']=new GLatLng(parseFloat(markers[i].getAttribute("Lat")),parseFloat(markers[i].getAttribute("Lng")));
						station[i]['ID'] = markers[i].getAttribute("ID");
						//station[i]['Updater'] = markers[i].getAttribute("Updater");
						station[i]['name'] = markers[i].getAttribute("name");
						station[i]['brand'] = markers[i].getAttribute("brand");
						station[i]['addr1'] = markers[i].getAttribute("addr1");
						station[i]['addr2'] = markers[i].getAttribute("addr2");
						station[i]['markerColour'] = "green";
						station[i]['marker'] = createMarker(station[i]['point'],i,"green");
						station[i]['Zone'] = markers[i].getAttribute("Zone");
						station[i]['County'] = markers[i].getAttribute("County");
					 }
				}

				 tabulateStations();
				 mapStations();
			}
		}

	request.send(null);
	


}

function tabulateStations(){
	//This method writes 15 top valid stations to the stations table
	
	var oldtbody = document.getElementById("stationsTable").getElementsByTagName("tbody")[0];
	var tbody = document.createElement("tBody");
	
	var maxRows = station.length;
	var rowsWritten=0;
	
	//create outside loop to avoid memory leaks.
	var pmarkerColour;
	var pBrand;
	var pAddr1;
	var pAddr2;
	var pDateupdated;
	var fancyHTML;
	
	if(maxRows>0){
		for (var i = 0; i < station.length && rowsWritten < maxRows; i++)
		{
		//alert("RowsWritten"+rowsWritten+" station.length"+station.length);
			if(station[i]['stale']!="true")	
			{
				row = document.createElement("tr");

				pBrand = document.createElement("td");
				pBrand.setAttribute("width","80");
				pBrand.setAttribute("valign","top");	
				fancyHTML = "<a href=javascript:map.setZoom(14);map.setCenter(station['" + i + "']['point']);station['" + i + "']['marker'].openInfoWindowHtml(getMiniHTML('"+i+"'));>" + station[i]['brand'] + "</a>";
				pBrand.innerHTML = fancyHTML;
				row.appendChild(pBrand);

				pAddr1 = document.createElement("td");
				pAddr1.setAttribute("width","110");
				pAddr1.setAttribute("valign","top");	
				pAddr1.innerHTML = station[i]['addr1'];
				row.appendChild(pAddr1);		

				pAddr2 = document.createElement("td");
				pAddr2.setAttribute("width","110");
				pAddr2.setAttribute("valign","top");	
				pAddr2.innerHTML = station[i]['addr2'];
				row.appendChild(pAddr2);				

				tbody.appendChild(row);	
				rowsWritten++;
			}

		}
	
	}else{
		//nothing to display
		row = document.createElement("tr");
		var pNoStations = document.createElement("td");
		pNoStations.setAttribute("colspan","3");
		pNoStations.innerHTML = "There are no stations to display for this area";
		row.appendChild(pNoStations);		
		tbody.appendChild(row);	
	
	}	
	document.getElementById("stationsTable").replaceChild(tbody,oldtbody);
}
