Merge branch 'master' of github.com:maxious/ACTBus-ui
Merge branch 'master' of github.com:maxious/ACTBus-ui

file:a/about.php -> file:b/about.php
--- a/about.php
+++ b/about.php
@@ -34,8 +34,9 @@
     <br />
     Some icons by Joseph Wain / glyphish.com<br />
     Native clients also available for iPhone(<a href="http://itunes.apple.com/au/app/cbrtimetable/id444287349?mt=8">cbrTimetable by Sandor Kolotenko</a>
-    , <a href="http://itunes.apple.com/au/app/act-buses/id376634797?mt=8">ACT Buses by David Sullivan</a>) 
-    , Android (<a href="https://market.android.com/details?id=com.action">MyBus 2.0 by Imagine Team</a>) 
+    , <a href="http://itunes.apple.com/au/app/act-buses/id376634797?mt=8">ACT Buses by David Sullivan</a>, <a href="http://itunes.apple.com/app/bus-trips-act/id489146525?mt=8">Bus Trips ACT by Molson Chengalath</a>) 
+    , Android (<a href="https://market.android.com/details?id=com.action">MyBus 2.0 by Imagine Team</a>, <A href="https://market.android.com/details?id=GetMe2CanberraFree.source">GetMe2 Canberra by
+Colin Thompson </a>) 
     and Windows Phone 7 (<a href="http://www.windowsphone.com/en-AU/apps/d840375e-429c-4aa4-a358-80eec6ea9e66">TransHub Canberra by Soul Solutions</a>) Other web clients include <a href="http://canberra.itranzit.com/option.html">iTranzit</a>.
     <br />
     GTFS-realtime API:

--- a/aws/busuidb.sh
+++ b/aws/busuidb.sh
@@ -7,7 +7,8 @@
 psql -d transitdata -f /var/www/transitdata.cbrfeed.sql
 #createuser transitdata -SDRP
 #password transitdata
-#psql -d transitdata -c "GRANT SELECT ON TABLE agency,calendar,calendar_dates,routes,stop_times,stops,trips TO transitdata;"
+#psql -d transitdata -c "GRANT SELECT ON TABLE agency,calendar,calendar_dates,routes,stop_times,stops,trips\
+#,servicealerts_alerts,servicealerts_informed TO transitdata;"
 #psql -d transitdata -c "GRANT SELECT,INSERT ON	TABLE myway_observations,myway_routes,myway_stops,myway_timingdeltas TO transitdata;"
 #psql -d transitdata -c	"GRANT SELECT,INSERT,UPDATE ON TABLE myway_routes,myway_stops TO transitdata;"
 ##psql -d transitdata -c "GRANT SELECT ON ALL TABLES IN SCHEMA public TO transitdata;"
@@ -15,3 +16,4 @@
 ##SELECT '', 'public', 'shapes', 'shape_pt', ST_CoordDim(shape_pt), ST_SRID(shape_pt), GeometryType(shape_pt)
 ##FROM shapes LIMIT 1;
 php /var/www/updatedb.php
+

--- a/geo/route.kml.php
+++ b/geo/route.kml.php
@@ -1,4 +1,5 @@
 <?php
+
 header('Content-Type: application/vnd.google-earth.kml+xml');
 include ('../include/common.inc.php');
 echo '<?xml version="1.0" encoding="UTF-8"?>
@@ -14,15 +15,15 @@
       </PolyStyle>
 	</Style>';
 $route = getRoute($routeid);
- echo "\n<Placemark>\n";
- $link = curPageURL()."/../trip.php?routeid=".htmlspecialchars ($route["route_id"]);
- echo "<name>".$route['route_short_name']."</name>";
-  echo '<atom:link href="'.$link.'"/>';
- echo '<description><![CDATA[ <a href="'.$link.'">'.$route['route_short_name']." ".$route['route_long_name']."</a>]]> </description>";
+echo "\n<Placemark>\n";
+
+$link = curPageURL() . "/../trip.php?routeid=" . htmlspecialchars($route["route_id"]);
+echo "<name>" . $route['route_short_name'] . "</name>";
+echo '<atom:link href="' . $link . '"/>';
+echo '<description><![CDATA[ <a href="' . $link . '">' . $route['route_short_name'] . " " . $route['route_long_name'] . "</a>]]> </description>";
 echo "<styleUrl>#yellowLineGreenPoly</styleUrl>";
-
-	$trips = getRouteTrips($routeid);
-	echo getTripShape($trips[0]['trip_id']);
+$trip = getRouteNextTrip($routeid);
+echo getTripShape($trip['trip_id']);
 
 echo "</Placemark>\n</Document></kml>\n";
 ?>

--- a/geo/stops.kml.php
+++ b/geo/stops.kml.php
@@ -1,11 +1,11 @@
 <?php

+header('Content-type: application/vnd.google-earth.kml+xml');

 include ('../include/common.inc.php');

-header('Content-type: application/vnd.google-earth.kml+xml');

 //http://wiki.openstreetmap.org/wiki/OpenLayers_Dynamic_KML

 // Creates the KML/XML Document.

 $dom = new DOMDocument('1.0', 'UTF-8');

 // Creates the root KML element and appends it to the root document.

-$node = $dom->createElementNS('http://earth.google.com/kml/2.1', 'kml');

+$node = $dom->createElementNS('http://www.opengis.net/kml/2.2', 'kml');

 $parNode = $dom->appendChild($node);

 // Creates a KML Document element and append it to the KML element.

 $dnode = $dom->createElement('Document');

@@ -13,7 +13,7 @@
 if ($suburb != "") $result_stops = getStopsBySuburb($suburb);

 else $result_stops = getStops();

 foreach ($result_stops as $stop) {

-	$description = 'http://bus.lambdacomplex.org/' . 'stop.php?stopid=' . $stop['stop_id'] . " <br>";

+	$description = baseURL() . 'stop.php?stopid=' . $stop['stop_id'] . " <br>";

 	// Creates a Placemark and append it to the Document.

 	$node = $dom->createElement('Placemark');

 	$placeNode = $docNode->appendChild($node);


file:b/geo/trip.kml.php (new)
--- /dev/null
+++ b/geo/trip.kml.php
@@ -1,1 +1,31 @@
+<?php
 
+header('Content-Type: application/vnd.google-earth.kml+xml');
+include ('../include/common.inc.php');
+echo '<?xml version="1.0" encoding="UTF-8"?>
+<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom"><Document>';
+echo '
+    <Style id="yellowLineGreenPoly">
+      <LineStyle>
+        <color>7f00ff00</color>
+        <width>4</width>
+      </LineStyle>
+      <PolyStyle>
+        <color>7f00ffff</color>
+      </PolyStyle>
+	</Style>';
+$trip = getTrip($tripid);
+echo "\n<Placemark>\n";
+$link = curPageURL() . "/../trip.php?tripid=" . htmlspecialchars($$tripid);
+echo "<name>" . $tripid . "</name>";
+echo '<atom:link href="' . $link . '"/>';
+echo '<description><![CDATA[ <a href="' . $link . '">' . $tripid . "</a>]]> </description>";
+echo "<styleUrl>#yellowLineGreenPoly</styleUrl>";
+
+
+echo getTripShape($tripid);
+
+echo "</Placemark>\n</Document></kml>\n";
+?>
+
+

--- a/include/common-db.inc.php
+++ b/include/common-db.inc.php
@@ -27,7 +27,9 @@
 }
 
 function databaseError($errMsg) {
-    die($errMsg);
+    if ($errMsg[1] != "") {
+    die(print_r($errMsg,true));
+    }
 }
 
 include ('db/route-dao.inc.php');

--- a/include/common-net.inc.php
+++ b/include/common-net.inc.php
@@ -35,7 +35,11 @@
     debug(print_r($page, true), "json");
     return $page;
 }
+function baseURL() {
+     $protocol = $_SERVER['HTTPS'] ? "https" : "http";
+  return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
 
+}
 function curPageURL() {
     $isHTTPS = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on");
     $port = (isset($_SERVER["SERVER_PORT"]) && ((!$isHTTPS && $_SERVER["SERVER_PORT"] != "80") || ($isHTTPS && $_SERVER["SERVER_PORT"] != "443")));

--- a/include/common-request.inc.php
+++ b/include/common-request.inc.php
@@ -60,11 +60,14 @@
 if (isset($_REQUEST['tripid'])) {
     $tripid = filter_var($_REQUEST['tripid'], FILTER_SANITIZE_STRING);
 }
+if (isset($_REQUEST['routeid'])) {
+    $routeid = filter_var($_REQUEST['routeid'], FILTER_SANITIZE_STRING);
+}
+if (isset($_REQUEST['directionid'])) {
+    $directionid = filter_var($_REQUEST['directionid'], FILTER_SANITIZE_STRING);
+}
 if (isset($_REQUEST['stopid'])) {
     $stopid = filter_var($_REQUEST['stopid'], FILTER_SANITIZE_NUMBER_INT);
-}
-if (isset($_REQUEST['routeid'])) {
-    $routeid = filter_var($_REQUEST['routeid'], FILTER_SANITIZE_NUMBER_INT);
 }
 if (isset($_REQUEST['geolocate'])) {
     $geolocate = filter_var($_REQUEST['geolocate'], FILTER_SANITIZE_URL);

--- a/include/common-session.inc.php
+++ b/include/common-session.inc.php
@@ -17,14 +17,6 @@
  */
 // you have to open the session to be able to modify or remove it
 session_start();
-if (isset($_REQUEST['service_period'])) {
-    $_SESSION['service_period'] = filter_var($_REQUEST['service_period'], FILTER_SANITIZE_STRING);
-    sessionUpdated();
-}
-if (isset($_REQUEST['time'])) {
-    $_SESSION['time'] = filter_var($_REQUEST['time'], FILTER_SANITIZE_STRING);
-    sessionUpdated();
-}
 if (isset($_REQUEST['geolocate']) && $_REQUEST['geolocate'] != "Enter co-ordinates or address here") {
     $geocoded = false;
     if (isset($_REQUEST['lat']) && isset($_REQUEST['lon'])) {
@@ -69,8 +61,10 @@
 }
 
 //debug(print_r($_SESSION, true) , "session");
-function current_time() {
-    return ($_SESSION['time'] ? $_SESSION['time'] : date("H:i:s"));
+function current_time($time = "") {
+    if (isset($_REQUEST['time'])) return $_REQUEST['time'];
+    else if ($time != "") date("H:i:s",$time);
+    else return date("H:i:s");
 }
 
 ?>

--- a/include/common-template.inc.php
+++ b/include/common-template.inc.php
@@ -54,7 +54,7 @@
 <link rel="dns-prefetch" href="//code.jquery.com">
 <link rel="dns-prefetch" href="//ajax.googleapis.com">
 	<link rel="stylesheet"  href="' . $basePath . 'css/jquery-ui-1.8.12.custom.css" />';
-    $jqmVersion = "1.0";
+    $jqmVersion = "1.0.1";
     if (isDebugServer()) {
         $jqmcss = $basePath . "css/jquery.mobile-$jqmVersion.css";
         $jqjs = $basePath . "js/jquery-1.6.4.min.js";
@@ -175,7 +175,7 @@
         <div data-role="content"> ';
         if ($GTFSREnabled) {
         $overrides = getServiceOverride();
-        if ($overrides['service_id']) {
+        if (isset($overrides['service_id'])) {
             if ($overrides['service_id'] == "noservice") {
                 echo '<div id="servicewarning">Buses are <strong>not running today</strong> due to industrial action/public holiday. See <a 
 href="http://www.action.act.gov.au">http://www.action.act.gov.au</a> for details.</div>';
@@ -212,9 +212,37 @@
     }
     echo "\n</div></div></body></html>";
 }
-
+function timeSettings() {
+    global $service_periods;
+echo '<div id="settings" data-role="collapsible" data-collapsed="true">
+<h3>Change Time (' . (isset($_REQUEST['time']) ? $_REQUEST['time'] : "Current Time,") . ' ' . ucwords(service_period()) . ')...</h3>
+        <form action="' . basename($_SERVER['PHP_SELF']) . '" method="GET">
+               <input type="hidden" name="suburb" id="suburb" value="' . (isset($_REQUEST['suburb']) ? $_REQUEST['suburb'] : "") . '"/>
+       
+            <input type="hidden" name="stopid" id="stopid" value="' . (isset($_REQUEST['stopid']) ? $_REQUEST['stopid'] : "") . '"/>
+                 <input type="hidden" name="stopcode" id="stopcode" value="' . (isset($_REQUEST['stopcode']) ? $_REQUEST['stopcode'] : "") . '"/>
+        <div class="ui-body"> 
+    		<div data-role="fieldcontain">
+		        <label for="time"> Time: </label>
+		    	<input type="time" name="time" id="time" value="' . (isset($_REQUEST['time']) ? $_REQUEST['time'] : date("H:i")) . '"/>
+			<a href="#" name="currentTime" id="currentTime" onClick="var d = new Date();' . "$('#time').val(d.getHours() +':'+ (d.getMinutes().toString().length == 1 ? '0'+ d.getMinutes():  d.getMinutes()));" . '">Current Time?</a>
+	        </div>
+		<div data-role="fieldcontain">
+		    <label for="service_period"> Service Period:  </label>
+			<select name="service_period" id="service_period">';
+foreach ($service_periods as $service_period) {
+    echo "<option value=\"$service_period\"" . (service_period() === $service_period ? " SELECTED" : "") . '>' . ucwords($service_period) . '</option>';
+}
+echo '</select>
+			<a href="#" style="display:none" name="currentPeriod" id="currentPeriod">Current Period?</a>
+		</div>
+		
+		<input type="submit" value="Update"/>
+                </div></form>
+            </div>';
+}
 function placeSettings() {
-    global $service_periods;
+    
     $geoerror = false;
     $geoerror = !isset($_SESSION['lat']) || !isset($_SESSION['lat']) || $_SESSION['lat'] == "" || $_SESSION['lon'] == "";
 

--- a/include/common-transit.inc.php
+++ b/include/common-transit.inc.php
@@ -23,15 +23,18 @@
 
 function service_period($date = "") {
 
-    if (isset($_SESSION['service_period']))
-        return $_SESSION['service_period'];
+    if (isset($_REQUEST['service_period'])) {
+        return $_REQUEST['service_period'];
+    }
+
     $override = getServiceOverride($date);
-    if ($override['service_id']) {
-        $idParts = explode("-",$override['service_id']);
-        return strtolower($idParts[2]);
-    }
-
-    switch (date('w', ($date != "" ? $date : time()))) {
+    if (isset($override['service_id'])) {
+        return strtolower($override['service_id']);
+    }
+    $date = ($date != "" ? $date : time());
+    $dow = date('w', $date);
+
+    switch ($dow) {
         case 0:
             return 'sunday';
         case 6:
@@ -40,16 +43,28 @@
             return 'weekday';
     }
 }
-function service_ids($service_period) {
+
+function service_ids($service_period, $date = "") {
     switch ($service_period) {
         case 'sunday':
-            return Array("2010-TUGGSUN-Sunday-20","2010-BELCSUN-Sunday-19");
+            return Array("Sunday", "Sunday");
         case 'saturday':
-            return Array("2010-BELCSAT-Saturday-19","2010-TUGGSAT-Saturday-19");
+            return Array("Saturday", "Saturday");
         default:
-            //return 'weekday';
-            return Array("2010-BELCMAST-Weekday-15","2010-TUGGMAST-Weekday-14"); 
-    }
+            $date = ($date != "" ? $date : time());
+// school holidays
+            $ymd = date('Ymd', $date);
+            $dow = date('w', $date);
+            if (intval($ymd) < "20120203" && $dow != 0 && $dow != 6) {
+                return Array("Weekday-SchoolVacation", "Weekday-SchoolVacation");
+            } else {
+                return Array("Weekday", "Weekday");
+            }
+    }
+}
+
+function valid_service_ids() {
+    return array_merge(service_ids(""), service_ids('saturday'), service_ids('sunday'));
 }
 
 function midnight_seconds($time = "") {

--- a/include/db/route-dao.inc.php
+++ b/include/db/route-dao.inc.php
@@ -29,6 +29,7 @@
     }
     return $query->fetch(PDO :: FETCH_ASSOC);
 }
+
 function getRoutesByShortName($routeShortName) {
     global $conn;
     $query = "Select distinct route_id, route_short_name from routes where route_short_name = :routeShortName";
@@ -45,8 +46,11 @@
 
 function getRouteHeadsigns($routeID) {
     global $conn;
-    $query = "select distinct stops.stop_name, trip_headsign,direction_id from routes join trips on trips.route_id = routes.route_id
-join stop_times on stop_times.trip_id = trips.trip_id join stops on stop_times.stop_id = stops.stop_id where trips.route_id = :routeID and stop_times.stop_sequence = 1";
+    $query = "select stops.stop_name, trip_headsign, direction_id,max(service_id) as service_id, count(*)
+        from routes join trips on trips.route_id = routes.route_id
+join stop_times on stop_times.trip_id = trips.trip_id join stops on 
+stop_times.stop_id = stops.stop_id where trips.route_id = :routeID 
+and stop_times.stop_sequence = 1 group by stops.stop_name, trip_headsign, direction_id having count(*) > 2";
     debug($query, "database");
     $query = $conn->prepare($query);
     $query->bindParam(":routeID", $routeID);
@@ -55,9 +59,14 @@
         databaseError($conn->errorInfo());
         return Array();
     }
-return $query->fetchAll();
-}
-
+    return $query->fetchAll();
+}
+function getRouteDescription($routeID, $directionID) {
+    $trip = getRouteNextTrip($routeID, $directionID);
+    $start = getTripStartingPoint($trip['trip_id']); 
+    $end = getTripDestination($trip['trip_id']);
+    return "From ".$start['stop_name']." to ".$end['stop_name'];
+}
 function getRouteByFullName($routeFullName) {
     global $conn;
     $query = "Select * from routes where route_short_name||route_long_name = :routeFullName LIMIT 1";
@@ -77,30 +86,6 @@
     $query = "Select * from routes order by route_short_name;";
     debug($query, "database");
     $query = $conn->prepare($query);
-    $query->execute();
-    if (!$query) {
-        databaseError($conn->errorInfo());
-        return Array();
-    }
-    return $query->fetchAll();
-}
-
-function getRoutesByNumber($routeNumber = "") {
-    global $conn;
-    if ($routeNumber != "") {
-        $query = "Select distinct routes.route_id,routes.route_short_name,routes.route_long_name,service_id from routes  join trips on trips.route_id =
-routes.route_id join stop_times on stop_times.trip_id = trips.trip_id
-where route_short_name = :routeNumber OR route_short_name LIKE :routeNumber2 order by route_short_name;";
-    } else {
-        $query = "SELECT DISTINCT route_short_name from routes order by route_short_name";
-    }
-    debug($query, "database");
-    $query = $conn->prepare($query);
-    if ($routeNumber != "") {
-        $query->bindParam(":routeNumber", $routeNumber);
-        $routeNumber2 = "% " . $routeNumber;
-        $query->bindParam(":routeNumber2", $routeNumber2);
-    }
     $query->execute();
     if (!$query) {
         databaseError($conn->errorInfo());
@@ -132,31 +117,43 @@
     return $query->fetchAll();
 }
 
-function getRouteNextTrip($routeID) {
-    global $conn;
-    $query = "select * from routes join trips on trips.route_id = routes.route_id
-join stop_times on stop_times.trip_id = trips.trip_id where
-arrival_time > :currentTime and routes.route_id = :routeID order by
+function getRouteNextTrip($routeID, $directionID) {
+    global $conn;
+   
+    $query = "select routes.route_id,direction_id,trips.trip_id,trip_headsign,departure_time from routes join trips on trips.route_id = routes.route_id
+join stop_times on stop_times.trip_id = trips.trip_id where  arrival_time between :currentTime and :futureTime 
+and routes.route_id = :routeID and trips.direction_id = :directionID order by
 arrival_time limit 1";
     debug($query, "database");
     $query = $conn->prepare($query);
     $query->bindParam(":currentTime", current_time());
+    $futureTime = current_time(strtotime(current_time() ." +2h"));
+    if (date("h",strtotime(current_time()) > 22)) $futureTime = "23:59:59";
+        $query->bindParam(":futureTime", $futureTime);
     $query->bindParam(":routeID", $routeID);
-    $query->execute();
+    $query->bindParam(":directionID", $directionID);
+    $query->execute();
+    databaseError($conn->errorInfo());
     if (!$query) {
         databaseError($conn->errorInfo());
         return Array();
     }
     $r = $query->fetch(PDO :: FETCH_ASSOC);
-
-    // past last trip of the day special case
-    if (sizeof($r) < 16) {
-        $query = "select * from routes join trips on trips.route_id = routes.route_id
-join stop_times on stop_times.trip_id = trips.trip_id where routes.route_id = :routeID order by
+    return $r;
+}
+
+function getRouteFirstTrip($routeID,$directionID) {
+       global $conn;
+       
+       $query = "select * from routes join trips on trips.route_id = routes.route_id
+join stop_times on stop_times.trip_id = trips.trip_id where routes.route_id = :routeID 
+and trips.direction_id = :directionID order by
 arrival_time DESC limit 1";
         debug($query, "database");
         $query = $conn->prepare($query);
         $query->bindParam(":routeID", $routeID);
+        
+    $query->bindParam(":directionID", $directionID);
         $query->execute();
         if (!$query) {
             databaseError($conn->errorInfo());
@@ -164,12 +161,11 @@
         }
 
         $r = $query->fetch(PDO :: FETCH_ASSOC);
-    }
-    return $r;
-}
-
-function getRouteAtStop($routeID, $stop_id) {
-    $nextTrip = getRouteNextTrip($routeID);
+        return $r;
+}
+
+function getRouteAtStop($routeID, $directionID, $stop_id) {
+    $nextTrip = getRouteNextTrip($routeID, $directionID);
     if ($nextTrip['trip_id']) {
         foreach (getTripStopTimes($nextTrip['trip_id']) as $tripStop) {
             if ($tripStop['stop_id'] == $stop_id)
@@ -179,40 +175,59 @@
     return Array();
 }
 
-function getRouteTrips($routeID) {
-    global $conn;
-    $query = "select routes.route_id,trips.trip_id,service_id,arrival_time, stop_id, stop_sequence from routes join trips on trips.route_id = routes.route_id
-join stop_times on stop_times.trip_id = trips.trip_id where routes.route_id = :routeID and stop_sequence = '1' order by
-arrival_time ";
-    debug($query, "database");
-    $query = $conn->prepare($query);
-    $query->bindParam(":routeID", $routeID);
-    $query->execute();
-    if (!$query) {
-        databaseError($conn->errorInfo());
-        return Array();
-    }
-    return $query->fetchAll();
-}
-
-function getRoutesByDestination($destination = "", $service_period = "") {
+function getRouteTrips($routeID, $directionID = "", $service_period = "") {
     global $conn;
     if ($service_period == "")
         $service_period = service_period();
+    $service_ids = service_ids($service_period);
+    $sidA = $service_ids[0];
+    $sidB = $service_ids[1];
+    $directionSQL = "";
+    if ($directionID != "")
+        $directionSQL = " and direction_id = :directionID ";
+    $query = "select routes.route_id,trips.trip_id,service_id,arrival_time, stop_id, stop_sequence from routes join trips on trips.route_id = routes.route_id
+join stop_times on stop_times.trip_id = trips.trip_id where (service_id=:service_periodA OR service_id=:service_periodB)
+AND (routes.route_id = :routeID) " . $directionSQL . " and stop_sequence = '1' order by
+arrival_time ";
+    debug($query, "database");
+    $query = $conn->prepare($query);
+    $query->bindParam(":routeID", $routeID);
+    $query->bindParam(":service_periodA", $sidA);
+    $query->bindParam(":service_periodB", $sidB);
+    if ($directionSQL != "")
+        $query->bindParam(":directionID", $directionID);
+    $query->execute();
+    if (!$query) {
+        databaseError($conn->errorInfo());
+        return Array();
+    }
+    return $query->fetchAll();
+}
+
+function getRoutesByDestination($destination = "", $service_period = "") {
+    global $conn;
+    if ($service_period == "")
+        $service_period = service_period();
+    $service_ids = service_ids($service_period);
+    $sidA = $service_ids[0];
+    $sidB = $service_ids[1];
     if ($destination != "") {
-        $query = "SELECT DISTINCT trips.route_id,route_short_name,route_long_name, service_id
+       /* $query = "SELECT DISTINCT trips.route_id,route_short_name,route_long_name, service_id
 FROM stop_times join trips on trips.trip_id =
 stop_times.trip_id join routes on trips.route_id = routes.route_id
-WHERE route_long_name = :destination AND  service_id=:service_period order by route_short_name";
+WHERE route_long_name = :destination AND (service_id=:service_periodA OR service_id=:service_periodB)
+ order by route_short_name";*/
+        $query = "select route_id, direction_id, stop_name, b.trip_id, b.stop_sequence from (select route_id, direction_id, max(stop_sequence) as stop_sequence, max(a.trip_id) as trip_id  from stop_times inner join (SELECT route_id, direction_id, max(trip_id) as trip_id
+        from trips group by route_id,direction_id) as a on stop_times.trip_id = a.trip_id group by route_id, direction_id) as b inner join stop_times on b.trip_id = stop_times.trip_id inner join stops on stop_times.stop_id = stops.stop_id where stop_times.stop_sequence = b.stop_sequence and stop_name = :destination order by route_id;";
     } else {
-        $query = "SELECT DISTINCT route_long_name
-FROM stop_times join trips on trips.trip_id =
-stop_times.trip_id join routes on trips.route_id = routes.route_id
-WHERE service_id= :service_period order by route_long_name";
-    }
-    debug($query, "database");
-    $query = $conn->prepare($query);
-    $query->bindParam(":service_period", $service_period);
+        $query = "select stop_name from (select route_id, direction_id, max(stop_sequence) as stop_sequence, max(a.trip_id) as trip_id  from stop_times inner join (SELECT route_id, direction_id, max(trip_id) as trip_id
+        from trips group by route_id,direction_id) as a on stop_times.trip_id = a.trip_id group by route_id, direction_id) as b inner join stop_times on b.trip_id = stop_times.trip_id inner join stops on stop_times.stop_id = stops.stop_id where stop_times.stop_sequence = b.stop_sequence group by stop_name order by stop_name;";
+    }
+    debug($query, "database");
+    $query = $conn->prepare($query);
+
+    //$query->bindParam(":service_periodA", $sidA);
+    //$query->bindParam(":service_periodB", $sidB);
     if ($destination != "")
         $query->bindParam(":destination", $destination);
     $query->execute();
@@ -226,35 +241,40 @@
 function getRoutesBySuburb($suburb, $service_period = "") {
     if ($service_period == "")
         $service_period = service_period();
+    $service_ids = service_ids($service_period);
+    $sidA = $service_ids[0];
+    $sidB = $service_ids[1];
+   
     global $conn;
     $query = "SELECT DISTINCT service_id,trips.route_id,route_short_name,route_long_name
 FROM stop_times join trips on trips.trip_id = stop_times.trip_id
 join routes on trips.route_id = routes.route_id
 join stops on stops.stop_id = stop_times.stop_id
-WHERE zone_id LIKE ':suburb AND service_id=:service_period ORDER BY route_short_name";
-    debug($query, "database");
-    $query = $conn->prepare($query);
-    $query->bindParam(":service_period", $service_period);
-    $suburb = "%" . $suburb . ";%";
+WHERE stop_desc LIKE :suburb AND (service_id=:service_periodA OR service_id=:service_periodB)
+ ORDER BY route_short_name";
+    debug($query, "database");
+    $query = $conn->prepare($query);
+    $query->bindParam(":service_periodA", $sidA);
+    $query->bindParam(":service_periodB", $sidB);
+    $suburb = "%Suburb: %" . $suburb . "%";
     $query->bindParam(":suburb", $suburb);
     $query->execute();
-    if (!$query) {
-        databaseError($conn->errorInfo());
-        return Array();
-    }
+    
+        databaseError($conn->errorInfo());
+  
     return $query->fetchAll();
 }
 
 function getRoutesNearby($lat, $lng, $limit = "", $distance = 500) {
     if ($service_period == "")
         $service_period = service_period();
-        $service_ids = service_ids($service_period);
+    $service_ids = service_ids($service_period);
     $sidA = $service_ids[0];
     $sidB = $service_ids[1];
     if ($limit != "")
         $limitSQL = " LIMIT :limit ";
     global $conn;
-    $query = "SELECT service_id,trips.route_id,route_short_name,route_long_name,min(stops.stop_id) as stop_id,
+    $query = "SELECT service_id,trips.route_id,trips.direction_id,route_short_name,route_long_name,min(stops.stop_id) as stop_id,
         min(ST_Distance(position, ST_GeographyFromText('SRID=4326;POINT($lng $lat)'), FALSE)) as distance
 FROM stop_times
 join trips on trips.trip_id = stop_times.trip_id
@@ -262,7 +282,7 @@
 join stops on stops.stop_id = stop_times.stop_id
 WHERE (service_id=:service_periodA OR service_id=:service_periodB)
 AND ST_DWithin(position, ST_GeographyFromText('SRID=4326;POINT($lng $lat)'), :distance, FALSE)
-        group by service_id,trips.route_id,route_short_name,route_long_name
+        group by service_id,trips.route_id,trips.direction_id,route_short_name,route_long_name
         order by distance $limitSQL";
     debug($query, "database");
     $query = $conn->prepare($query);

--- a/include/db/stop-dao.inc.php
+++ b/include/db/stop-dao.inc.php
@@ -46,7 +46,7 @@
         }
     }
     $query .= " order by stop_name;";
-    debug($query,"database");
+    debug($query, "database");
     $query = $conn->prepare($query);
     if ($firstLetter != "")
         $query->bindParam(":firstLetter", $firstLetter);
@@ -167,16 +167,16 @@
     if ($service_period == "") {
         $service_period = service_period();
     }
-        $service_ids = service_ids($service_period);
+    $service_ids = service_ids($service_period);
     $sidA = $service_ids[0];
     $sidB = $service_ids[1];
     $limitSQL = "";
     if ($limit != "")
         $limitSQL .= " LIMIT :limit ";
-        
+
     global $conn;
     if ($afterTime != "") {
-        $query = " SELECT stop_times.trip_id,stop_times.arrival_time,stop_times.stop_id,stop_sequence,service_id,trips.route_id,route_short_name,route_long_name,end_times.arrival_time as end_time
+        $query = " SELECT stop_times.trip_id,stop_times.arrival_time,stop_times.stop_id,stop_sequence,service_id,trips.route_id,trips.direction_id,trips.trip_headsign,route_short_name,route_long_name,end_times.arrival_time as end_time
 FROM stop_times
 join trips on trips.trip_id =
 stop_times.trip_id
@@ -184,7 +184,7 @@
 	WHERE stop_times.arrival_time IS NOT NULL group by trip_id) as end_times 
 WHERE stop_times.stop_id = :stopID
 AND stop_times.trip_id = end_times.trip_id
-AND (service_id=:service_periodA OR service_id=:service_periodB) ".($route_short_name != "" ? " AND route_short_name = :route_short_name ":"")." 
+AND (service_id=:service_periodA OR service_id=:service_periodB) " . ($route_short_name != "" ? " AND route_short_name = :route_short_name " : "") . " 
 AND end_times.arrival_time > :afterTime
 ORDER BY end_time $limitSQL";
     } else {
@@ -194,7 +194,7 @@
 stop_times.trip_id
 join routes on trips.route_id = routes.route_id
 WHERE stop_times.stop_id = :stopID
-AND (service_id=:service_periodA OR service_id=:service_periodB) ".($route_short_name != "" ? " AND route_short_name = :route_short_name ":"")." 
+AND (service_id=:service_periodA OR service_id=:service_periodB) " . ($route_short_name != "" ? " AND route_short_name = :route_short_name " : "") . " 
 ORDER BY arrival_time $limitSQL";
     }
     debug($query, "database");

--- a/include/db/trip-dao.inc.php