// Find buses between start and destination stop // Parameters: s - startstopnameid to d - deststopnameid // var dBugLvl = 3; var WALKDIST = 1000; // Find stops within 1 km radius var NEARLATLON = 0.01; var CHECKSTOPS = 40; var SHOWMAXSTOPS = 5; var MAXBUSESTOSHOW = 4; var MAXRECENTBUSESFOUND = 7; var stopLines; var stopBusLines; var busMaster; var oStartStop, oDestStop; // The stopbus info for start and dest var startStopArea, destStopArea; var arBusesFound = []; var sAr, dAr; // Array of stops near start and destination /* $(document).ready(function() { // var XMLFileSupport = CheckXMLHttpFile(); busesfoundinit(); return; }); */ function busesfoundinit() { scrollTo(0, 0); s = gup('s'); d = gup('d'); if(s === "" && d === "") { s = getCookie('cStartStopNameId'); d = getCookie('cDestStopNameId'); } if(d === "") { alert('No Buses Found as yet. Select from the Find Bus tab'); window.location.href = "index.html"; return; } startStopNameId = s; destStopNameId = d; dBug('', ' s = ' + s + ', d = ' + d, 5); stopBusLines = getStopBusList(); busMaster = getbusmaster(); var sStopSs = findStopId(startStopNameId).stopsrchstr; var dStopSs = findStopId(destStopNameId).stopsrchstr; var htmlTitle = sStopSs.stopsrchstr + '
' + dStopSs; var dirHdr = ""; var dirHdrClose = '
Stop nameBuses (frequency)
'; var htmlBody = ''; var dHTML = '', nHTML = '', cHTML = ''; if(startStopNameId !== '') { // start not empty, find near stops, needed for all rAr = getStopsNear(startStopNameId); if(rAr !== null) { sAr = rAr.startStops; oStartStop = rAr.stopInfo; } document.getElementById("foundBusFrom").innertHTML = sStopSs; } if(destStopNameId !== '') { // dest not empty, find near stops rAr = getStopsNear(destStopNameId); dAr = rAr.startStops; oDestStop = rAr.stopInfo; var el = "To: " + dStopSs; document.getElementById("foundBusTo").innertHTML = el; } if(startStopNameId === '' && destStopNameId !== '') { // start is blank so stops at destination htmlTitle = 'Buses at and near destination: ' + dStopSs; // document.getElementById("foundBusTitle").innerHTML = dStopSs; htmlStart = stopsNear(destStopNameId); htmlBody = htmlStart; } else if(startStopNameId !== '' && destStopNameId === '') { htmlTitle = 'Buses at and near start: ' + sStopSs; htmlDest = stopsNear(startStopNameId); htmlBody = dirHdr + htmlDest + dirHdrClose; } else { var rtDb = [0, '']; var rtNb = [0, '']; rtDb = getDirectBuses(oStartStop, oDestStop); dHTML = ''; if(rtDb != undefined) { if(rtDb[0]) dHTML = rtDb[1]; else dHTML = '
No Direct buses found
'; if(rtDb[0] < 3) { rtNb = getNearBuses(startStopNameId, destStopNameId); if(rtNb[0]) nHTML = rtNb[1]; else if(!rtDb[0]) // no message if we found direct buses nHTML = '
No buses from nearby stops
'; } } cHTML = ''; if(arBusesFound.length < 1) { // if(rtDb[0]< 3 && rtNb[0] < 3) { rtCb = conxions(startStopNameId, destStopNameId); if(rtCb !== null && rtCb[0]) cHTML = rtCb[1]; if(rtCb === null || !rtCb[0]) // no message if we found direct buses cHTML = '
No bus connections
'; /* if(cHTML != "") fndBus = fndBus + "
" + cHTML else fndBus = fndBus + "
" + "No Connections found" + '
'; */ } // var dirHdr = '', dirHdrClose = ''; if((rtDb == undefined || rtDb[0] === 0) && rtNb[0] === 0) { dirHdr = ""; dirHdrClose = ''; } htmlBody = dirHdr + dHTML + nHTML + dirHdrClose + cHTML; } // document.getElementById("foundBusTitle").innerHTML = htmlTitle; if (startStopNameId != "") if ( destStopNameId != "" ) document.getElementById("foundBusFrom").innerHTML = "From: " + sStopSs; else document.getElementById("foundBusFrom").innerHTML = "Buses at and near: " + sStopSs; if(destStopNameId != "") document.getElementById("foundBusTo").innerHTML = "To: " + dStopSs; else document.getElementById("foundBusTo").innerHTML = ""; // No dest provided, blank it if(htmlBody == "") htmlBody = "
No buses found
"; document.getElementById("directBuses").innerHTML = htmlBody; addRecentBusesFound({'busesFrom':sStopSs, 'busesTo':dStopSs, 'busHtml':htmlBody}); showRecentBusesFound(); } // Buses from exact start to dest function getDirectBuses(oStartStop, oDestStop) { dBug('getDirectBuses', 'start', 5); // directBuses = ""; var nDirectBuses = 0; rAr = directBus(oStartStop, oDestStop); if(rAr === null) return null; nDirectBuses = rAr[0]; sDirectBuses = rAr[1]; dBug('getDirectBuses', nDirectBuses + ', Start: ' + oStartStop.searchstr + ', Dest: ' + oDestStop, 5); // dirHdr = "Stop nameBuses"; // sDirectBuses = "" + dirHdr + ""; sDirectBuses = ""; if(nDirectBuses) fndBusDirect = sDirectBuses; else fndBusDirect = "
No direct buses found
"; fndBus = fndBusDirect; for(var x=0; x 0 ? distStartStop : 0; d = distDestStop > 0 ? distDestStop : 0; if(nNearBuses) { startSearchStr = sAr[x][1].searchstr; destSearchStr = dAr[y][1].searchstr; if(startStop == sAr[x][STOPIDX]) arNearBuses[arNearBuses.length] = [d, '', destSearchStr, sBuses, nNearBuses, rAr[2]]; // sNearBuses = sNearBuses + ''; else if(destStop == dAr[y][STOPIDX]) arNearBuses[arNearBuses.length] = [s, startSearchStr, '', sBuses, nNearBuses, rAr[2]]; // sNearBuses = sNearBuses + ''; else { var combDist = ''; if( distStartStop > 0 || distDestStop > 0) { s = distStartStop > 0 ? distStartStop : 0; d = distDestStop > 0 ? distDestStop : 0; // combDist = '(' + distFormat(s + d) + ')'; arNearBuses[arNearBuses.length] = [s+d, startSearchStr, destSearchStr, sBuses, nNearBuses, rAr[2]]; } } } } } arNearBuses.sort(sortDiStop); sNearBuses = ''; // busesDone = []; sComma = ''; sBus = ''; nNearBuses = 0; for(x=0; x ' + nearBusList[a][0]; sBus = sBus + sComma + ' ' + nearBusList[a][0] + "" ; arBusesFound[arBusesFound.length] = nearBusList[a][0]; } sComma = ', '; } if(busesFoundInRow < nearBusList.length) { nNearBuses++; sNearBuses = sNearBuses + ''; } if(arBusesFound.length >= MAXBUSESTOSHOW) break; } /* if(!nDirectBuses) sNearBuses = "
Direct Buses (" + // nDirectBuses + ") " + sDirectBuses + "
Direct Buses (" + nDirectBuses + ") " + sDirectBuses + "
To ' + destSearchStr + // ' (' + nNearBuses + ') ' + sBuses + '
From : ' + startSearchStr + // ' (' + nNearBuses + ') ' + sBuses + '
' + (arNearBuses[x][1] !== "" ? 'From: ' + arNearBuses[x][1] : ""); sNearBuses = sNearBuses + (arNearBuses[x][2] !== "" ? ' To: ' + arNearBuses[x][2] : "") + ' (' + arNearBuses[x][4] + ') - ' + distFormat(arNearBuses[x][0]) + '' + arNearBuses[x][3] + '
" + "" + sNearBuses; */ if(nNearBuses) { fndBus = fndBus + ""; fndBus = fndBus + sNearBuses; } else fndBus = fndBus + "
No near buses found
"; // No direct or near buses found, look for connections return [nNearBuses, sNearBuses]; } function directBus(oSStop, oDStop) { if(oSStop === undefined || oDStop === undefined) return null; dBug("directBus", "start", 5); var sDirectBus = ''; var nDirectBus = 0; var startBuses = ''; var destBuses = ''; var x = 0; // Get the buses at the start and destination stops arStartBus = oSStop.busList.split(", "); arDestBus = oDStop.busList.split(", "); sComma = ""; var retArray = []; var dBus = []; for(x=0; x < arStartBus.length; x++) { sb = arStartBus[x].split("|"); for(var y=0; y < arDestBus.length; y++) { db = arDestBus[y].split("|"); if(sb[0] == db[0]) { busInfo = getBusInfo(sb[0]); busfreq = ""; if(busInfo != undefined && busInfo.frequency !="") busfreq = " ( " + busInfo.frequency + " mins)"; url = 'journey.html?b=' + sb[0] + '&s=' + sb[1] + '&d=' + db[1]; url = '#'; sDirectBus = sDirectBus + sComma + buildUrl(sb[0], sb[1], db[1]); nDirectBus++; sComma = ",  "; dBus[dBus.length] = [sb[0], sb[1], db[1]]; } } } retArray = [nDirectBus, sDirectBus, dBus]; return retArray; } // Find connections - change bus at one point between start and destination function conxions(startStop, destStop) { if(startStop == undefined || destStop == undefined) return null; var AREA = 0; var STOPNAMEID = 1; var SEARCHSTR = 2; var LAT = 3; var LON = 4; var BUSLIST = 5; var MAXROUTES = 5; var commonBus = new Array(); // var sCB = new Array(); // var dCB = new Array(); var arBestDist = []; var areas = getAreas(); var startBuses = ''; var destBuses = ''; var htmCommonStops = ""; // first get the stop info if(oStartStop == undefined || oDestStop == undefined) return null; startBuses = oStartStop.busList; destBuses = oDestStop.busList; var startAreaLat = 0; var startAreaLon = 0; var destAreaLat = 0; var destAreaLon = 0; for(var i=0; i destAreaLon) { nwLon = destAreaLon; seLon = startAreaLon; } arStartBuses = startBuses.split(", "); arDestBuses = destBuses.split(", "); var commonStops = []; bStartFound = false; bDestFound = false; br = "
"; var n = 1; curStop = ""; curSb = ""; curDb = ""; for(x=0; x < stopBusLines.length; x++) { // dBug('conxion: ', 'checking common stops - ' + x, 5); if(stopBusLines[x] !== "") { ar = stopBusLines[x]; // ["138048", "10throad", "[Chembur W] 10Th Road", "8 L|34, 19 L|29, 92 L|48, 355 L|14, 362|2, 364|17"] // Check whether the area of this stop is within the square start and dest if(ar[LAT] > nwLat || ar[LON] > seLon || ar[LAT] < seLat || ar[LON] < nwLon) continue; sCb = ""; dCb = "";// Source and dest common buses buses = ar[BUSLIST].split(", "); // ["8 L|34", "19 L|29", "92 L|48" ....] // check if the stop has a bus in start and a bus in destination bStartFound = false; bDestFound = false; sCb = ''; sComma = ""; dCb = ''; dComma = ""; for(var c=0; c ' + sb[0] + busfreq + ""; sComma = ", "; } } for(var z=0; z ' + db[0] + busfreq + ""; // dCb = dCb + dComma + db; dComma = ", "; } } } if(bStartFound && bDestFound) { dBug('conxions: ', 'Found stop', 5); var csLat = 0; var csLon = 0; var diStart = -1, diDest = -1; for(i=0; i 0 && diDest > 0) { combDist = diStart + diDest; arBestDist[combDist] = [ar[STOPNAMEID], ar[SEARCHSTR], sCb, dCb, combDist]; dBug('conxions: ', 'Comb. dist - ' + combDist, 5); } // Check if this bus is in the direct or near buses, if so skip commonStops[commonStops.length] = [ar[STOPNAMEID], ar[SEARCHSTR], sCb, dCb, combDist]; n++; } } } dBug('conxions: ', 'found - ' + arBestDist.length, 5); htmCommonStops = ""; var nStops = 0; for(y=0; y
"; nStops++; dBug('conxions', arBestDist[y][1] + " (" + distFormat(arBestDist[y][4]), 5); if(nStops > MAXROUTES) break; } } if (!nStops) { for(x=0; x"; } } htmHdr = ""; htmCommonStops = '
Stop nameBuses

From stops near you:

" + arBestDist[y][1] + " (" + distFormat(arBestDist[y][4]) + ")" + arBestDist[y][2] + "" + arBestDist[y][3] + "
" + commonStops[x][1] + ")" + commonStops[x][2] + "" + commonStops[x][3] + "
Connecting StopBuses from startBuses to Destination
' + htmHdr + htmCommonStops + "
"; return [nStops, htmCommonStops]; } // Check if bus already found function checkFoundB4(busno) { if(busno == undefined) return false; var bFound = false; for(b=0; b ' + stopbus[0] + "" ; sComma = ",  "; } if(nNewBus) { rHTML = rHTML + '' + stopInfo.searchstr + ' (' + distFormat(sAr[x][0]) + ')'; rHTML = rHTML + sBus + ''; } } return rHTML; } // Sort the distance, stops array by the shortest distance function sortDiStop(a, b) { return ((a[0] < b[0]) ? -1 : ((a[0] > b[0]) ? 1 : 0)); } function getStopInfo(stopnameid) { var oBus = null; for(x=0; x < stopBusLines.length; x++) { if(stopBusLines[x] !== "") { ar = stopBusLines[x]; stopname = ar[1]; if(stopname == stopnameid) { oBus = {"area":ar[0], "stopnameid":ar[1], "searchstr":ar[2], "lat":ar[3], "lon":ar[4], "busList":ar[5]}; break; } } } return oBus; } function getStopsNear(stopNameId) { var STOPNAMEID = 0; var STOPLAT = 1; var STOPLON = 2; var startStopIdx = 0; var destStopIdx = 0; var bStopFound = false; oStop = getStopInfo(stopNameId); if(oStop === null) return null; var sAr = []; for(x=0; x < stopBusLines.length && oStop != null; x++) { var ar = stopBusLines[x]; if(ar[3] != undefined && ar[4] != undefined) { if(oStop.lat != undefined && Math.abs(oStop.lat - ar[3]) < NEARLATLON && Math.abs(oStop.lon - ar[4]) < NEARLATLON) { di = distanceLatLon(ar[3], ar[4], oStop.lat, oStop.lon); if( di < WALKDIST) sAr[sAr.length] = [di, {"area":ar[0], "stopnameid":ar[1], "searchstr":ar[2], "lat":ar[3], "lon":ar[4], "busList":ar[5]}]; } } } sAr.sort(sortDiStop); // Check if source and destination stops are there // If not prolly because lat lon not there add them var SHOWMAXSTOPS = 5; var stopFound = false; for(x=0; x' + recentBusesFound[i].busesTo + '
' + recentBusesFound[i].busHtml + '
'; recentBusesFoundStr = recentBusesFoundStr + elm; } } recentBusesFoundStr = recentBusesFoundStr + ''; document.getElementById("recentBusesFound").innerHTML = recentBusesFoundStr; } }