////////////////////////
//
//Get and List stations
//
////////////////////////
function getStationsByRoute(route, order, fuel){
	myRoute=route;
	myOrder=order;	
	myFuel=fuel;
	var request = GXmlHttp.create();
	
	var random_nocache = Math.random()*4;
	request.open("Get", "api/getStationsByRouteAPI.php?route="+route+"&order=" + order + "&fuel=" + fuel + "&noCache="+random_nocache, 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]['price'] = markers[i].getAttribute("price");
						station[i]['type'] = markers[i].getAttribute("type");
						station[i]['trend'] = markers[i].getAttribute("trend");
						station[i]['addr1'] = markers[i].getAttribute("addr1");
						station[i]['addr2'] = markers[i].getAttribute("addr2");
						station[i]['dateupdated'] = markers[i].getAttribute("dateupdated");
						station[i]['stale'] = isStale(station[i]['dateupdated']);
						station[i]['Updater'] = markers[i].getAttribute("Updater");
						var markerColour = getColour(markers[i].getAttribute("price"),station[i]['stale']);
						station[i]['markerColour'] = markerColour;
						station[i]['marker'] = createMarker(station[i]['point'],i,markerColour);
						station[i]['Zone'] = markers[i].getAttribute("Zone");
						station[i]['County'] = markers[i].getAttribute("County");
					
						//Getting bounds of box to display all stations	
						if (i==0){
						    maxLat=parseFloat(markers[i].getAttribute("Lat"));
						    minLat=parseFloat(markers[i].getAttribute("Lat"));
						    maxLng=parseFloat(markers[i].getAttribute("Lng"));
						    minLng=parseFloat(markers[i].getAttribute("Lng"));
						}else{
						 
  						  //This wholse section is to find max and min lat andlng values 
                                                  var lat = parseFloat(markers[i].getAttribute("Lat"));
						  var lng = parseFloat(markers[i].getAttribute("Lng"));
						  if (lat>0){
                                                    if (lat>maxLat){
                                                        maxLat=parseFloat(markers[i].getAttribute("Lat"));
                                                    }
                                                    if (lat<minLat){
                                                        minLat=parseFloat(markers[i].getAttribute("Lat"));
                                                    }
						  }else{
                                                    if (lat<maxLat){
                                                        maxLat=parseFloat(markers[i].getAttribute("Lat"));
                                                    }
                                                    if (lat>minLat){
                                                        minLat=parseFloat(markers[i].getAttribute("Lat"));
                                                    }
						  }
                                                  if (lng>0){
                                                    if (lng>maxLng){
                                                        maxLng=lng;
                                                    }
                                                    if (lat<minLat){
                                                        minLng=lng;
                                                    }
                                                  }else{
                                                    if (lng<maxLng){
                                                        maxLng=lng;
                                                    }
                                                    if (lng>minLng){
                                                        minLng=lng;
                                                    }
                                                  }

	
						}

					 }
				}
			    setMapView();
		            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 pValue = document.createElement("td");
	pValue.innerHTML = "";
	headerRow.appendChild(pValue);

	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 pPrice = document.createElement("td");
	pPrice.innerHTML = "<b>Price</b>";
	headerRow.appendChild(pPrice);	

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

	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");
				pmarkerColour = document.createElement("td");
				pmarkerColour.setAttribute("width","12");
				pmarkerColour.setAttribute("valign","top");
				pmarkerColour.setAttribute("class","row" + rowshade);				
				pmarkerColour.innerHTML = "<img src=\"images/" + station[i]['markerColour'] +"mini.png\" border=0/>";
				row.appendChild(pmarkerColour);		

				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);				

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

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

				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);
}
