////////////////////////
//
//Get and List stations
//
////////////////////////
function getStationsByBrand(brand){
        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/getStationsByLocationAndBrandAPI.php?minLat=" + minLat + "&maxLat=" + maxLat + "&minLng=" + minLng + "&maxLng=" + maxLng + "&brand=" + brand + "&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]['brand'] = markers[i].getAttribute("brand");
                                                station[i]['addr1'] = markers[i].getAttribute("addr1");
                                                station[i]['addr2'] = markers[i].getAttribute("addr2");
                                                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 headerRow = document.createElement("tr");
	headerRow.setAttribute("class","stationListHeader");
	
	var pStation = document.createElement("td");
	pStation.innerHTML = "<b>Station</b>";
	headerRow.appendChild(pStation);
	
	var pRoad = document.createElement("td");
	pRoad.innerHTML = "<b>Road</b>";
	headerRow.appendChild(pRoad);

	var pArea = document.createElement("td");
	pArea.innerHTML = "<b>Area</b>";
	headerRow.appendChild(pArea);	

	var pDetails = document.createElement("td");
	pDetails.innerHTML = "<b>Details</b>";
	headerRow.appendChild(pDetails);	

	tbody.appendChild(headerRow);

	
	
	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")	
			{
				var rowshade=i % 2;
				row = document.createElement("tr");

				pBrand = document.createElement("td");
				pBrand.setAttribute("width","80");
				pBrand.setAttribute("valign","top");
				pBrand.setAttribute("class","row" + rowshade);				
				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","135");
				pAddr1.setAttribute("valign","top");	
				pAddr1.setAttribute("class","row" + rowshade);					
				pAddr1.innerHTML = station[i]['addr1'];
				row.appendChild(pAddr1);		

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

				pDetails = document.createElement("td");
				pDetails.setAttribute("width","30");
				pDetails.setAttribute("valign","top");
				pDetails.setAttribute("class","row" + rowshade);				
				fancyHTML = "<a href='viewStation.php?stationID="+station[i]['ID']+"'>Details</a>";
				pDetails.innerHTML = fancyHTML;
				row.appendChild(pDetails);

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

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