var BUS_NO;
var BUS_STOP;
var FROM_STOP;
var TO_STOP;
var TRANSITMETROIMAGE ="<img src = 'images/Train.png'  border='0' title ='This trip uses a Metro' />"; 
var TRANSITBUSIMAGE ="<img src = 'images/bus_stop.png'  border='0' title ='This trip uses a Bus' />"; 
function GetBusNoDetails(busno)
{
if ( busno == "Enter the Route No for its details")
    busno ="";
    
	if(busno.length == 0)
	{
		alert("Please enter the Route No  to find its details.");
		return;
	}
    BUS_NO = busno;
	var mapcount = getMapCount();
	if(mapcount == 0)
	{
		alert("Please select atleast one map for making this search.");
		return;
	}

    showProgressBox("Searching..");
    document.getElementById("btnBusNoSearch").disabled = true;
	Imap.WebServices.BusSvc.GetBusNoDetails(document.getElementById("hidLogId").value,busno,$find('AutoCompleteEx')._contextKey, SucceededBusDetailsResults, handleFailedBusNoDetails);
	
}

// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object as a parameter.

function SucceededBusDetailsResults(result, eventArgs)
{	
  	document.getElementById("btnBusNoSearch").disabled = false; 
    unloadProgressBox();
	DisplaySucceededBusDetailsResults(result);
	document.getElementById("AdText").innerHTML =  "<table><tr><td class='dlgad'>Local listings for " +BUS_STOP +"</td></tr><tr><td class='dlgad'>You can display your local listing free here</td></tr></table>";   

}

// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameter.

function handleFailedBusNoDetails(error)
{
	// Display the error.    
	unloadProgressBox();
	document.getElementById("btnBusNoSearch").disabled = false;
	alert("Route No Detail Service Connection Error: " + error.get_message() + '. Please submit your query again.');
}

function DisplaySucceededBusDetailsResults(places)
{

	var resHtml = "";
	var latitude;
	var longitude;
    var latlonString;
	var title;
    var firsttime = true;
    
    var ImageUrl ;
    var MarkerCount; 

	/**  if there are no results, display the error message and return */
	if(places == null)
	{
		alert("There were no results for the specified route No, please check your query.");
		return;
	}

	if(places.BusStops == null)
	{
		alert("There were no results for the specified route No, please check your query.");
		return;
	}
	if(places.BusStops.length == 0)
	{
		alert("There were no results for the specified route No, please check your query.");
		return;
	}



	
     latlonString="";
	/**  Create an entry for each VEPlace in the result set */
	for(var p = 0; p < places.BusStops.length; p++)
	{

			/**  Gather some info up front */
		var place = places.BusStops[p];
		BusStop = place.StopName;
		var Origin =  places.Origin;
		var Destination =  places.Destination;
		if (firsttime)
		{

            var JourneyTime = places.JourneyTime;
            var ServiceType = places.ServiceType;
           var str=places.Fare;
            if (str == null)
                str ="";
            var Fare =str.replace(new RegExp('<Stage',"g"), ''); ;
               Fare =Fare.replace(new RegExp('<stage',"g"), ''); ;
             Fare =Fare.replace(new RegExp('<br>',"g"), ''); ;
            Fare =Fare.replace(new RegExp('<fareInfo>',"g"), ''); ;
            Fare =Fare.replace(new RegExp('</ Stage>',"g"), ';'); ;
            
              Fare =Fare.replace(new RegExp('</ stage>',"g"), ';'); ;
             Fare =Fare.replace(new RegExp('>',"g"), ' for stage '); ;

             Fare =Fare.replace(new RegExp('>',"g"), ' for stage '); ;

             if (Fare=='0')
                Fare="NA";
             if (Fare=="")
                Fare="NA"; 
            var Distance = places.Distance ;
             if (Distance == null)
                Distance ="";
             if (Distance=='0')
                Distance="NA";
             if (Distance=="")
                Distance="NA"; 
            var DepartureFromO = places.DepartureFromO.replace(new RegExp(',',"g"), ', ');  ;
            var DepartureFromD = places.DepartureFromD.replace(new RegExp(',',"g"), ', ');  ;
            resHtml = resHtml + "<div class ='dlg' ><table>";
            resHtml = resHtml + "<tr class='dlg'><td class='dlgB' width= 50px;>";
            resHtml = resHtml + "Origin:";  
            resHtml = resHtml + "</td><td class='dlg' width= 250px;>";
            resHtml = resHtml + Origin;
            resHtml = resHtml + "</td></tr><tr class='dlg'><td class='dlgB' width= 50px;>";
            resHtml = resHtml + "Destination:";  
            resHtml = resHtml + "</td><td class='dlg' width= 250px;>";
            resHtml = resHtml + Destination;
            resHtml = resHtml + "</td></tr><tr class='dlg'><td class='dlgB' width= 50px;>";
            resHtml = resHtml + "Distance:";
            resHtml = resHtml + "</td><td class='dlg' width= 250px;>";
            resHtml = resHtml + Distance + " Kms";
            resHtml = resHtml + "</td></tr><tr class='dlg'><td class='dlgB' width= 50px;>";
            resHtml = resHtml + "JourneyTime:";  
            resHtml = resHtml + "</td><td class='dlg' width= 250px;>";
            resHtml = resHtml + JourneyTime;
            resHtml = resHtml + "</td></tr></table>";
            resHtml = resHtml + "<table>";
		    resHtml = resHtml + "<tr class='dlg'><th class='dlg' width= 50px;>";
		    resHtml = resHtml + "</th>";
		    resHtml = resHtml + "<th class='dlg' width= 250px;> ";
		    resHtml = resHtml + places.TotalStops + " Stops/Stations";
		    resHtml = resHtml + "</th>"  
        firsttime=false;
        }      
      

		latitude = place.Latitude;
		longitude = place.Longitude;
        title =BusStop;
        latlonString = latlonString + latitude + "|" + longitude + ","
		ImageUrl = getPushpinImage(p+1);
		/**  create the info box description
		var desc = "Latitude: " + latitude + "<br/>" + "Longitude: " + longitude;  */
        var desc =  "Route No:" + BUS_NO;
        var numberImage ="";
        var ttext ="";
        if ((latitude != "null") && (longitude != "null") && (latitude != "") && (longitude != ""))
        {
       
		    MarkerCount = CreateMarketForMaps(latitude, longitude,ImageUrl, title, desc);
		    numberImage ="<img style='cursor:pointer' src='" +  ImageUrl + "' onclick='MarkerClick(" + MarkerCount + ")'/>";
            ttext = "<a href='javascript:onclick=MarkerClick(" + MarkerCount + ");'>"  + title + "</a></td>";
        }
        else
        {
            numberImage ="<img src='" +  ImageUrl + "'/>";
            ttext =  title + "</td>";
        }
	
		/**  Add the information to the resultsDiv html, including a link */

		/**  that recenters the map over the pin */
		resHtml = resHtml + "<tr class='dlg'><td class='dlg' width= 50px;>";
		resHtml = resHtml + numberImage + "</td><td class='dlg' width= 250px;> ";
		resHtml = resHtml + ttext;
		if(trackLatitude)
		{
			resHtml = resHtml + "<td class='dlg'>Distance: " + place.Distance + "</td>";
		}

		resHtml = resHtml + "</tr>";

		
	}

	resHtml = resHtml + "</table>";
	
	
            	resHtml = resHtml + "<table>";
            resHtml = resHtml + "<tr class='dlg'><td class='dlgB' width= 50px;>";
            resHtml = resHtml + "Fare:";
            resHtml = resHtml + "</td><td class='dlg' width= 250px;>";
            resHtml = resHtml + Fare;
            resHtml = resHtml + "</td></tr><tr class='dlg'><td class='dlgB' width= 50px;>";
            resHtml = resHtml + "ServiceType:";  
            resHtml = resHtml + "</td><td class='dlg' width= 250px;>";
            resHtml = resHtml + ServiceType;
             resHtml = resHtml + "</td></tr><tr class='dlg'><td class='dlgB' width= 50px;>";
            resHtml = resHtml + "Transit Type:";  
            resHtml = resHtml + "</td><td class='dlg' width= 250px;>";
            resHtml = resHtml + places.TransitType;;
            resHtml = resHtml + "</td></tr><tr class='dlg'><td class='dlgB' width= 50px;>";
            resHtml = resHtml + "Departure From Origin:";  
            resHtml = resHtml + "</td><td class='dlg' width= 250px;>";
            resHtml = resHtml + DepartureFromO;
            resHtml = resHtml + "</td></tr><tr class='dlg'><td class='dlgB' width= 50px;>";
            resHtml = resHtml + "Departure From Destination:";  
            resHtml = resHtml + "</td><td class='dlg' width= 250px;>";
            resHtml = resHtml + DepartureFromD;
            resHtml = resHtml + "</td></tr></table></div>";
	
     drawPolyline(latlonString);
 
    var popupboxid1 = "rsltbx1" + (GetResultboxCount() + 1);
     ResultBox.push(popupboxid1);
         new popUp(50, 145, 300, 175, popupboxid1, resHtml, "white", "lightgrey", "9pt sans-serif", "Search results for Route No: " + BUS_NO, "#0084d8", "lightgrey", "gray", "#00468c", "black", true, true, true, true, false, false,'images/min.png','images/max.png','images/close.png','images/resize.jpg', true,'images/copy.png','images/print.png' );
    hidebox(popupboxid1);
    fadeboxin(popupboxid1);


	try
	{
		if(latitude != 0)
			GotoLocation(latitude, longitude, 13);
	}

	catch(err)
	{

		/**alert("Error! in init",err.message);  */
	}
}


function GetBusStopDetails(BusStopName)
{
if ( BusStopName == "Stop/Station Name [ex:K R Market]")
    BusStopName ="";
    
	if(BusStopName.length == 0)
	{
		alert("Please enter the Stop/Station Name to find its location.");
		return;
	}

	var mapcount = getMapCount();
	if(mapcount == 0)
	{
		alert("Please select atleast one map for making this search.");
		return;
	}
    BUS_STOP = BusStopName;
    showProgressBox("Getting Station/Stop Details");
    document.getElementById("btnBusStopSearch").disabled = true;
	Imap.WebServices.BusSvc.GetBusStopDetails(document.getElementById("hidLogId").value, BusStopName,$find('AutoCompleteEx')._contextKey, SucceededBusStopDetailsResults, handleFailedBusStopDetails);
	
}

// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object as a parameter.

function SucceededBusStopDetailsResults(result, eventArgs)
{
    unloadProgressBox();
	DisplaySucceededBusStopDetailsResults(result);
	document.getElementById("btnBusStopSearch").disabled = false;
}

// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameter.

function handleFailedBusStopDetails(error)
{
	// Display the error.    
    unloadProgressBox();
    alert("Stop/Station Detail Service Connection Error: " + error.get_message() + '. Please submit your query again.');
	document.getElementById("btnBusStopSearch").disabled = false;
}

function DisplaySucceededBusStopDetailsResults(BusStopDetails)
{


	var resHtml = "";
	var latitude;
	var longitude;
    var latlonString;
	var title;
	var title2;	
	var desc ="";
    var firsttime = true;
    var mcounter = -1;  /** marker is placed from index 0*/
    var numberImage;
    var ImageUrl;
  
	/**  if there are no results, display the error message and return */
	if(BusStopDetails == null)
	{
		alert("There were no results for the specified Station/Stop, please check your query.");
		return;
	}
     var  BusesDetails =BusStopDetails.BusesDetails;
	if(BusesDetails == null)
	{
		alert("There were no results for the specified Station/Stop, please check your query.");
		return;
	}
	if(BusesDetails.length == 0)
	{
		alert("There were no results for the specified Station/Stop, please check your query.");
		return;
	}	
	
	resHtml = resHtml + "<div class ='dlg' ><table>";
 
	/**  Create an entry for each VEPlace in the result set */
	for(var p = 0; p < BusesDetails.length; p++)
	{
               
		/**  Gather some info up front */
		var busdetails = BusesDetails[p];

		var BusNo = busdetails.BusNo;
		if (firsttime)
		{

		var currentStop = busdetails.CurrentStop.StopName;
		var currentStopLat = busdetails.CurrentStop.Latitude;
		var currentStopLon = busdetails.CurrentStop.Longitude;	
		title =currentStop;
		ImageUrl = getAlternate1PushpinImage(0);
		var ttext;

		  if ((currentStopLon != "null") && (currentStopLat != "null") && (currentStopLon != "") && (currentStopLat != ""))
		  {
		    
		   
	        mcounter = CreateMarketForMaps(currentStopLat, currentStopLon, ImageUrl, title, desc);
	        numberImage ="<img style='cursor:pointer' src='" +  ImageUrl + "' onclick='MarkerClick(" + mcounter+ ")'/>";	
	        ttext = "<a href='javascript:onclick=MarkerClick(" + mcounter + ");'>" + currentStop + " (" + (BusesDetails.length) + " Routes.)</a>";
	      }
	      else
	      {
	        numberImage ="<img src='" +  ImageUrl + "' />";	
	        ttext = currentStop + " (" + (BusesDetails.length) + " Routes.)";

	      }

		
		
		resHtml = resHtml + "<tr class='dlg'><td class='dlg' width= 50px;>";
		resHtml = resHtml + numberImage + "</td>";
		resHtml = resHtml + "<td class='dlg' width= 150px;> ";
		resHtml = resHtml + ttext;
		resHtml = resHtml + "</td>"
		resHtml = resHtml + "<td class='dlg' width= 150px;> ";
		resHtml = resHtml + "Search location";
		resHtml = resHtml + "</td>"
		resHtml = resHtml + "</tr>";
		resHtml = resHtml + "<tr class='dlg'>";
		resHtml = resHtml + "<th class='dlg' width= 140px;> ";
		resHtml = resHtml + "Route#";
		resHtml = resHtml + "</th>"
		resHtml = resHtml + "<th class='dlg' width= 150px;> ";
		resHtml = resHtml + "Start Stop";
		resHtml = resHtml + "</th>"
		resHtml = resHtml + "<th class='dlg' width= 150px;> ";
		resHtml = resHtml + "Last Stop";
		resHtml = resHtml + "</th>"		
		resHtml = resHtml + "</tr>";
		
		firsttime = false;	
		}
		
		var StartStop = busdetails.StartStop.StopName;
		var StartStopLat = busdetails.StartStop.Latitude;
		var StartStopLon = busdetails.StartStop.Longitude;	
		var LastStop = busdetails.LastStop.StopName;
		var LastStopLat = busdetails.LastStop.Latitude;
		var LastStopLon = busdetails.LastStop.Longitude;		
		title =StartStop;
		title2 =LastStop;	
		var ttext1 ="";
		
		ImageUrl = getAlternate1PushpinImage(p + 1);

		  if ((StartStopLat != "null") && (StartStopLon != "null") && (StartStopLat != "") && (StartStopLon != ""))
		  {
		       mcounter =  CreateMarketForMaps(StartStopLat, StartStopLon, ImageUrl, title, desc);
		        numberImage ="<img style='cursor:pointer' src='" +  ImageUrl + "' onclick='MarkerClick(" + mcounter + ")'/>";
		        ttext1 = "<a href='javascript:onclick=MarkerClick(" + mcounter + ");'>"  + StartStop + "</a>";
	      }
		else
		{
		        numberImage ="<img src='" +  ImageUrl + "' />";
		        ttext1 =  StartStop ;
		        
		}
	    	var temz1 = BusNo.replace(/ /gi, "%20"); 
		resHtml = resHtml + "<tr class='dlg'>";
		resHtml = resHtml + "<td class='dlg' width= 140px;> ";
		resHtml = resHtml + "<a href=javascript:GetBusNoDetails('" + temz1  + "');>";
		resHtml = resHtml + BusNo;
		resHtml = resHtml + "</a></td>";
		resHtml = resHtml + "<td class='dlg' width= 150px;> ";
		resHtml = resHtml + numberImage + ttext1;
		resHtml = resHtml + "</a></td>";		

	    ImageUrl = getAlternate2PushpinImage(p + 1);	
        var ttext3 ="";
        
        if ((LastStopLat != "null") && (LastStopLon != "null") && (LastStopLat != "") && (LastStopLon != ""))
        {
        	mcounter = CreateMarketForMaps(LastStopLat, LastStopLon, ImageUrl, title2, desc);		
        	 numberImage ="<img style='cursor:pointer' src='" +  ImageUrl + "' onclick='MarkerClick(" + mcounter + ")'/>";	
        	 ttext3 = "<a href='javascript:onclick=MarkerClick(" + mcounter + ");'>" + title2 + "</a>"
        }
        else
        {
            numberImage ="<img src='" +  ImageUrl + "' />";
            ttext3 =  title2 ;
        }
			


       

		resHtml = resHtml + "<td class='dlg' width= 150px;> ";
		resHtml = resHtml + numberImage + ttext3;
		resHtml = resHtml + "</td>";	
		
/**		resHtml = resHtml + "<a href='javascript:GotoLocation(" + latitude + "," + longitude + ", 13" + ");'>";
		resHtml = resHtml + LastStop;
		resHtml = resHtml + "</a></td>";	
		if(trackLatitude)
		{
			resHtml = resHtml + "<td class='dlg'>Distance: " + place.Distance + "</td>";
		}*/	

		resHtml = resHtml + "</tr>";

		
	}

	resHtml = resHtml + "</table></div>";

    var popupboxid1 = "rsltbx1" + (GetResultboxCount() + 1);
        ResultBox.push(popupboxid1);
    new popUp(50, 145, 300, 175, popupboxid1, resHtml, "white", "lightgrey", "9pt sans-serif","Station/Stop details for " + BUS_STOP, "#0084d8", "lightgrey", "gray", "#00468c", "black", true, true, true, true, false, false,'images/min.png','images/max.png','images/close.png','images/resize.jpg', true,'images/copy.png','images/print.png' );
    hidebox(popupboxid1);
    fadeboxin(popupboxid1);


	try
	{
		if(latitude != 0)
			GotoLocation(currentStopLat, currentStopLon, 13);
	}

	catch(err)
	{

		/**alert("Error! in init",err.message);  */
	}
}



function GetNearByBusStops(lat, lon)
{
	if(lat.length == 0)
	{
		alert("Please enter the latitude to find its location or click the map to choose location");
		return;
	}
	if(lon.length == 0)
	{
		alert("Please enter the longitude to find its location or click the map to choose location");
		return;
	}	

	var mapcount = getMapCount();
	if(mapcount == 0)
	{
		alert("Please select atleast one map for making this search.");
		return;
	}

    showProgressBox("Searching Route no");
	document.getElementById("btnBusStoplookup").disabled = true;
	Imap.WebServices.BusSvc.GetNearByBusStops(document.getElementById("hidLogId").value,lat, lon,$find('AutoCompleteEx')._contextKey, SucceededBusLookupResults, handleFailedBusLookup);
	
}

// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object as a parameter.

function SucceededBusLookupResults(result, eventArgs)
{
    unloadProgressBox();
	DisplaySucceededBusLookupResults(result);
	document.getElementById("btnBusStoplookup").disabled = false;
}

// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameter.

function handleFailedBusLookup(error)
{
	// Display the error.    
    unloadProgressBox();
	alert("Stop/Station lookup Detail Service Connection Error: " + error.get_message() + '. Please submit your query again.');
	document.getElementById("btnBusStoplookup").disabled = false;
}

function DisplaySucceededBusLookupResults(places)
{

	var resHtml = "";
	var latitude ="";
	var longitude ="";
	var title ="";
    var firsttime = true;
    var ImageUrl ="";
    
	/**  if there are no results, display the error message and return */
	if(places == null)
	{
		alert("There were no results for the specified Latitude and Longitude position, please check your query.");
		return;
	}

	if(places.BusStops == null)
	{
		alert("There were no results for the specified location, please check your query.");
		return;
	}

	resHtml = resHtml + "<div class ='dlg' ><table>";
	resHtml = resHtml + "<tr class='dlg'><td class='dlg' width= 50px;>";
    resHtml = resHtml + places.TotalStops + "</td><td class='dlg' width= 50px;> Station/Stops</td><td class='dlg' width= 50px;></td></tr>";
     var mmCounter;
	/**  Create an entry for each VEPlace in the result set */
	for(var p = 0; p < places.BusStops.length; p++)
	{

        
		/**  Gather some info up front */
		var place = places.BusStops[p];
		BusStop = place.StopName;
		latitude = place.Latitude;
		longitude = place.Longitude;
        title =BusStop;

		/**  create the info box description
		var desc = "Latitude: " + latitude + "<br/>" + "Longitude: " + longitude;  */
        var desc = "";
        ImageUrl =  getPushpinImage(MarkerCount + 1);
        var tem = title.replace(/ /gi, "%20"); 
        var numberImage = "";
        var ttext = "";
          if ((latitude != "null") && (longitude != "null") && (latitude != "") && (longitude != ""))
          {
            mmCounter =  CreateMarketForMaps(latitude, longitude, ImageUrl, title, desc);
            numberImage ="<img style='cursor:pointer' src='" + ImageUrl + "' onclick='MarkerClick(" + mmCounter + ")'/>";
            ttext = '<a href=javascript:GetBusStopDetails("' + tem + '");>'	 + title + "</a>";
            }
            else
            {
             numberImage ="<img src='" + ImageUrl + "' />";
             ttext =  title ;
            }
		
		/**resHtml = resHtml + "<a href='javascript:GMarkerClick(" + p +  ")'"; */
		/**  Add the information to the resultsDiv html, including a link */
        
		/**  that recenters the map over the pin */
		resHtml = resHtml + "<tr class='dlg'><td class='dlg' width= 50px;>";
		resHtml = resHtml + numberImage + "</td><td class='dlg' width= 150px;> ";
		resHtml = resHtml + ttext;
		resHtml = resHtml + "</td>";
		resHtml = resHtml + "<td class='dlg'>Distance: " + place.DistanceFromOrigin + "</td>";
		resHtml = resHtml + "</tr>";
		
	}

	resHtml = resHtml + "</table></div>";
	


    var popupboxid1 = "rsltbx1" + (GetResultboxCount() + 1);
        ResultBox.push(popupboxid1);
    new popUp(50, 145, 300, 175, popupboxid1, resHtml, "white", "lightgrey", "9pt sans-serif","Station/Stop lookup results for " + document.getElementById("txtLatitude").value.substring(0, 6) + ',' + document.getElementById("txtLongitude").value.substring(0, 6), "#0084d8", "lightgrey", "gray", "#00468c", "black", true, true, true, true, false, false,'images/min.png','images/max.png','images/close.png','images/resize.jpg', true,'images/copy.png','images/print.png' );
    hidebox(popupboxid1);
    fadeboxin(popupboxid1);


	try
	{
		if(latitude != 0)
			GotoLocation(latitude, longitude, 13);
	}

	catch(err)
	{

		/**alert("Error! in init",err.message);  */
	}
}


function GetBusRoute(from, to)
{
if ( from == "From [Place where you want to start.]")
    from ="";
    
if ( to == "To [Place where you want to go.]")
    to ="";

FROM_STOP = from;
TO_STOP = to ;
	if(from.length == 0)
	{
		alert("Please enter the from to find the directions");
		return;
	}
	if(to.length == 0)
	{
		alert("Please enter the to to find the directions");
		return;
	}	

	var mapcount = getMapCount();
	if(mapcount == 0)
	{
		alert("Please select atleast one map for making this search.");
		return;
	}


		document.getElementById("Submit").disabled = true;
	    showProgressBox("Searching routes for the destination");
		Imap.WebServices.BusSvc.GetRoute(document.getElementById("hidLogId").value,from, to,$find('AutoCompleteEx')._contextKey, SucceededRouteSearchResults, handleFailedRouteSearch);
	
}

// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object as a parameter.

function SucceededRouteSearchResults(result, eventArgs)
{
    unloadProgressBox();
	document.getElementById("Submit").disabled = false;
	DisplaySucceededRouteSearchResults(result);
	document.getElementById("AdText").innerHTML =  "<table><tr><td class='dlgad'>Local listings for " +FROM_STOP +"</td></tr><tr><td class='dlgad'>You can display your local information free here</td></tr></table>";   
	document.getElementById("AdText").innerHTML =  "<table><tr><td class='dlgad'>Local listings for " +TO_STOP +"</td></tr><tr><td class='dlgad'>You can display your local information free here</td></tr></table>";   

}

// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameter.

function handleFailedRouteSearch(error)
{
	document.getElementById("Submit").disabled = false;
	 unloadProgressBox();
	// Display the error.    
	alert("Route Search Service Connection Error: " + error.get_message() + '. Please submit your query again.');
}

function DisplaySucceededRouteSearchResults(busStopDetails)
{

	var resHtml = "";
	var latitude;
	var longitude;
    var latlonString;
	var title;
	var title2;	
	var desc ="";
    var firsttime = true;
    var mcounter = -1;  /** marker is placed from index 0*/
    var numberImage ="";
    var ImageUrl;
    var ChangeBusNo;
    var PChangeStop;
    var PChangeStopLat;
    var PChangeStopLon;
    var PChangeBusNo ="";
     var PBusNo ="";
     var BusNo;
     var mmCounter;
    var temp;
    BusStopDetails = busStopDetails;
	/**  if there are no results, display the error message and return */
	if(BusStopDetails == null)
	{
		alert("There were no results for the specified direction search, please check your query.");
		return;
	}

	if(BusStopDetails.BusesDetails == null)
	{
		alert("There were no results for the specified direction search, please check your query.");
		return;
	}
	if(BusStopDetails.TotalBusNoCount == 0)
	{
		alert("There were no results for the specified direction search, please check your query.");
		return;
	}
	resHtml = resHtml + "<div class ='dlg' >";

	/**  Create an entry for each VEPlace in the result set */
	for(var p = 0; p < BusStopDetails.BusesDetails.length; p++)
	{

               
		/**  Gather some info up front */
		var busdetails = BusStopDetails.BusesDetails[p];

		BusNo = busdetails.BusNo;
		if (firsttime)
		{
		    var FirstStop = busdetails.StartStop.StopName;
		    var FirstStopLat = busdetails.StartStop.Latitude;
		    var FirstStopLon = busdetails.StartStop.Longitude;	
		    LastStop = busdetails.LastStop.StopName;
		    LastStopLat = busdetails.LastStop.Latitude;
		    LastStopLon = busdetails.LastStop.Longitude;
		    if ((LastStopLat != "null") && (LastStopLat != "null") && (LastStopLat != "") && (LastStopLat != "") && (LastStopLon != "null") && (LastStopLon != "null") && (LastStopLon != "") && (LastStopLon != "") && (FirstStopLat != "null") && (FirstStopLat != "null") && (FirstStopLat != "") && (FirstStopLat != "") && (FirstStopLon != "null") && (FirstStopLon != "null") && (FirstStopLon != "") && (FirstStopLon != ""))
		    GetRouteByLatLon(FirstStopLat,FirstStopLon,LastStopLat,LastStopLon);
		    

		    title =FirstStop;
		    ImageUrl = getAlternate1PushpinImage(0);
			numberImage = numberImage + "<table>";
		    numberImage = numberImage + "<tr class='dlg'><td class='dlg' width= 40px;> ";    
		    if ((FirstStopLat != "null") && (FirstStopLon != "null") && (FirstStopLat != "") && (FirstStopLon != ""))
		    {
		        mmCounter = CreateMarketForMaps(FirstStopLat, FirstStopLon, ImageUrl, title, desc);
                numberImage = numberImage + "<img style='cursor:pointer' src='" +  ImageUrl + "' onclick='MarkerClick(" + (mmCounter)+ ")'/>";
		        numberImage = numberImage + "</td><td class='dlg' width= 110px;> ";
		        numberImage = numberImage + "<a href='javascript:onclick=MarkerClick(" + (mmCounter) + ");'>";
		        numberImage = numberImage +  "From: " + FirstStop + "</a>";		
            }
            else
            {
		        numberImage = numberImage + "<img  src='" +  ImageUrl + "' />";
		        numberImage = numberImage + "</td><td class='dlg' width= 110px;> ";
		        numberImage = numberImage +  "From: " + FirstStop;		

            }
  		    //resHtml = resHtml + "<tr class='dlg'><td class='dlg' width= 50px;></td><td class='dlg' width= 50px;>";

    		numberImage = numberImage + "</td><td class='dlg' width= 40px;> ";	
            mcounter = mcounter + 1;

            
		    title =LastStop;
		    var ImageUrl1 = getAlternate2PushpinImage(0);
            if ((LastStopLat != "null") && (LastStopLon != "null") && (LastStopLat != "") && (LastStopLon != ""))
		    {
	            mmCounter = CreateMarketForMaps(LastStopLat, LastStopLon, ImageUrl1, title, desc);
		        numberImage = numberImage + "<img style='cursor:pointer' src='" +   ImageUrl1 + "' onclick='MarkerClick(" + (mmCounter)+ ")'/>";	
		        numberImage = numberImage + "</td><td class='dlg' width= 110px;> ";
		        numberImage = numberImage + "<a href='javascript:onclick=MarkerClick(" + (mmCounter) + ");'>";
		        numberImage = numberImage +  " To: " + LastStop + "</a>";

	        }
	        else
            {
		        numberImage = numberImage + "<img  src='" +   ImageUrl1 + "' />";	
		        numberImage = numberImage + "</td><td class='dlg' width= 110px;> ";
		        numberImage = numberImage +  " To: " + LastStop ;

            }
            numberImage = numberImage + "</td><td class='dlg' width= 110px;>" + BusStopDetails.TotalBusNoCount +  " Routes</td></tr></table> ";
	        resHtml = resHtml + numberImage + "<table>";

		    mcounter = mcounter + 1;	
            PChangeStop = busdetails.ChangeStop.StopName;
            
            var RouteType = busdetails.RouteType;
             if (RouteType == 'I')
             {
 		        resHtml = resHtml + "<tr class='dlg'>";
		        resHtml = resHtml + "<th class='dlg' width= 50px;> ";
		        resHtml = resHtml + "Route#";
		        resHtml = resHtml + "</th>"
		        resHtml = resHtml + "<th colspan =2 class='dlg' width= 70px;> ";
		        resHtml = resHtml + "Change Stop/Station";
		        resHtml = resHtml + "</th>"
		        resHtml = resHtml + "<th class='dlg' width= 200px;> ";
		        resHtml = resHtml + "Change Route#";
		        resHtml = resHtml + "</th>"		
		        resHtml = resHtml + "</tr>";            
   
             
             }
             else
             {
             resHtml = resHtml + "<tr class='dlg'>";
		    resHtml = resHtml + "<th class='dlg' width= 300px;> ";
		    resHtml = resHtml + "Route#";
		    resHtml = resHtml + "</th>"
		    resHtml = resHtml + "</tr>";    
                var latlonString =  FirstStopLat + "|" + FirstStopLon + ","	+  LastStopLat + "|" + LastStopLon  + "," ;
		        drawPolyline(latlonString);   
             }
		    firsttime = false;	
		}
        var RouteType = busdetails.RouteType;
	    if (RouteType == 'I')
	    {
		    var ChangeStop = busdetails.ChangeStop.StopName;
		    ChangeStopLat = busdetails.ChangeStop.Latitude;
		    ChangeStopLon = busdetails.ChangeStop.Longitude;	
            ChangeBusNo = busdetails.ChangeBusNo;
	   	    BusNo = busdetails.BusNo;	    
    	   	
	         if ((ChangeStop != PChangeStop) || (BusStopDetails.BusesDetails.length -1 ==p))
	        {
                
    	        ImageUrl = getAlternate1PushpinImage(mcounter -1);
        		
		        title =PChangeStop;
			    desc = "Catch RouteNo: " + PBusNo + " Change RouteNo:" + PChangeBusNo;
			    if ((PChangeStopLon != "null") && (PChangeStopLat != "null") && (PChangeStopLon != "") && (PChangeStopLat != ""))
			    {
			        mmCounter = CreateMarketForMaps(PChangeStopLat, PChangeStopLon, ImageUrl, title, desc);
			        numberImage ="<img style='cursor:pointer' src='" +  ImageUrl + "' onclick='MarkerClick(" + mmCounter + ")'/>";	
			        ttext = "<a href='javascript:onclick=MarkerClick(" + mmCounter + ");'>" + title + "</a>";
        		}
        		else
        		{
        		    numberImage ="<img src='" +  ImageUrl + "' />";	
        		    ttext =  title ;
        		}
	            
	            if (ChangeStop == PChangeStop) 
	            {
	            		    PChangeStop = 	ChangeStop;
	                        PChangeStopLat = ChangeStopLat;
	                        PChangeStopLon = ChangeStopLon;
	                        PBusNo = BusNo ;
    	                       temz1 = ChangeBusNo.replace(/ /gi, "%20");      
		                    temp = "<a href=javascript:GetBusNoDetails('" + temz1  + "');>";
	                        temp = temp + ChangeBusNo;
	                        temp = temp + "</a>";
		                     temp = temp + " ";
		                    PChangeBusNo = PChangeBusNo + temp;
		                    resHtml = resHtml + "</tr>";
	            }
	            
    	        var temz1 = PBusNo.replace(/ /gi, "%20"); 
	            resHtml = resHtml + "<tr class='dlg'>";
		        resHtml = resHtml + "<td class='dlg' width= 50px;> ";
		        resHtml = resHtml + "<a href=javascript:GetBusNoDetails('" + temz1  + "');>";
		        resHtml = resHtml + PBusNo;
		        resHtml = resHtml + "</a></td>";
		        resHtml = resHtml + "<td class='dlg' width= 40px;> ";
		        resHtml =  resHtml + numberImage 
		        resHtml = resHtml + "</td><td class='dlg' width= 70px;> ";
		        resHtml = resHtml + ttext;
		        resHtml = resHtml + "</td>";
		        resHtml = resHtml + "<td class='dlg' width= 200px;> ";
		        resHtml = resHtml + PChangeBusNo;
		        resHtml = resHtml + "</td>";
		        mcounter = mcounter + 1;
		        PChangeBusNo ="";
		        var latlonString =  FirstStopLat + "|" + FirstStopLon + ","	+  PChangeStopLat + "|" + PChangeStopLon + "," +  LastStopLat + "|" + LastStopLon  + "," ;
		        drawPolyline(latlonString); 	 
            }	
    		
    		
		    PChangeStop = 	ChangeStop;
	        PChangeStopLat = ChangeStopLat;
	        PChangeStopLon = ChangeStopLon;
	        PBusNo = BusNo ;
    	       temz1 = ChangeBusNo.replace(/ /gi, "%20");      
		    temp = "<a href=javascript:GetBusNoDetails('" + temz1  + "');>";
	        temp = temp + ChangeBusNo;
	        temp = temp + "</a>";
		     temp = temp + " ";
		    PChangeBusNo = PChangeBusNo + temp;
		    resHtml = resHtml + "</tr>";
        }
        else
        {
        
              temp = "<a href=javascript:GetBusNoDetails('" + busdetails.BusNo  + "');>";
	        temp = temp +  busdetails.BusNo;
	        temp = temp + "</a>";
		     temp = temp + " ";
		     if (PBusNo =="")
		      PBusNo =  temp;   
		     else
		        PBusNo = PBusNo + ", " + temp;   
        }

	
	}
     if (RouteType != 'I')
     {
        resHtml = resHtml + "<tr class='dlg'>";
        resHtml = resHtml + "<td class='dlg' width= 300px;> ";
        resHtml = resHtml + PBusNo + "</td></tr>";
     
     }

	resHtml = resHtml + "</table></div>";
	
    var popupboxid1 = "rsltbx1" + (GetResultboxCount() + 1);
        ResultBox.push(popupboxid1);
    new popUp(50, 145, 300, 175, popupboxid1, resHtml, "white", "lightgrey", "9pt sans-serif","Route details from " + FROM_STOP + " to " + TO_STOP, "#0084d8", "lightgrey", "gray", "#00468c", "black", true, true, true, true, false, false,'images/min.png','images/max.png','images/close.png','images/resize.jpg', true,'images/copy.png','images/print.png' );
    hidebox(popupboxid1);
    fadeboxin(popupboxid1);



	try
	{
		if(latitude != 0)
			GotoLocation(FirstStopLat, FirstStopLon, 13);
	}

	catch(err)
	{

		/**alert("Error! in init",err.message);  */
	}
}




function button_click()
{
    if(window.event.keyCode==13)
    {
        var Button1=document.getElementById("Button1");
        Button1.click();
        window.event.cancel=true;
    }
}

function showHelp()
{
    var popupboxid1 = "helpbx1" + (GetResultboxCount() + 1);
  //  new popUp(50, 145, 300, 175, popupboxid1,"maphelp.htm", "white", "lightgrey", "9pt sans-serif", "Bangalore Journey Planner Help", "#0084d8", "lightgrey", "gray", "#00468c", "black", true, true, true, true, false, false,'images/min.png','images/max.png','images/close.png','images/resize.jpg', true,'images/copy.png','images/print.png' );
  new popUp(50, 145, 500, 300, popupboxid1, "maphelp.htm", "white", "#00385c", "16pt serif", "Bangalore Journey Planner Help", "#00385c", "white", "lightgrey", "#00568c", "black", true, true, true, true, true, false,'images/min.png','images/max.png','images/close.png','images/resize.jpg');

    hidebox(popupboxid1);
    fadeboxin(popupboxid1);


}
							
function initMenu()
{

}

//onclick call this function to show the secondSection information and hide the others 
function showRouteSearch()
{
trackLatitude =false;
var first = document.getElementById("SearchBusStop").style;
var second = document.getElementById("SrchRouteHd").style;
var third = document.getElementById("SrchBusNo").style;
var fourth = document.getElementById("BusStopLookup").style;
first.display="none";
third.display="none";
fourth.display="none";
    if (first.display == "block")
    {
        CollapseMenu('SrchBusStopTxt','collapseSearchBusStop');
     }
    if (third.display == "block")
    {
        CollapseMenu('SrchBusNotxt','collapseBusNo');
     }
    if (fourth.display == "block")
    {
        CollapseMenu('BusStopLookuptxt','collapseBusStopLookuptxt');
     }
        second.display = "block";
        ExpandMenu('srchRouteText','collapseBusRoute');
        reArrangeWindows('SrchRouteHd');
}

//onclick call this function to show the secondSection information and hide the others 
function showBusStopSearch()
{
trackLatitude =false;
var first = document.getElementById("SearchBusStop").style;
var second = document.getElementById("SrchRouteHd").style;
var third = document.getElementById("SrchBusNo").style;
var fourth = document.getElementById("BusStopLookup").style;

second.display="none";
third.display="none";
fourth.display="none";
    if (second.display == "block")
    {
        CollapseMenu('srchRouteText','collapseBusRoute');
     }

    if (third.display == "block")
    {
        CollapseMenu('SrchBusNotxt','collapseBusNo');
     }
    if (fourth.display == "block")
    {
        CollapseMenu('BusStopLookuptxt','collapseBusStopLookuptxt');
     }
   first.display = "block";
   ExpandMenu('SrchBusStopTxt','collapseSearchBusStop');
   reArrangeWindows('SearchBusStop');
}

function showBusNoSearch()
{

trackLatitude =false;
var first = document.getElementById("SearchBusStop").style;
var second = document.getElementById("SrchRouteHd").style;
var third = document.getElementById("SrchBusNo").style;
var fourth = document.getElementById("BusStopLookup").style;
first.display="none";
second.display="none";
fourth.display="none";

    
    if (second.display == "block")
    {
        CollapseMenu('srchRouteText','collapseBusRoute');
     }

    if (first.display == "block")
    {
        CollapseMenu('SrchBusStopTxt','collapseSearchBusStop');
     }
    if (fourth.display == "block")
    {
        CollapseMenu('BusStopLookuptxt','collapseBusStopLookuptxt');
     }
    third.display = "block";
    ExpandMenu('SrchBusNotxt','collapseBusNo');
    reArrangeWindows('SrchBusNo');
}

function showBusStopLookup()
{
trackLatitude =true;
var first = document.getElementById("SearchBusStop").style;
var second = document.getElementById("SrchRouteHd").style;
var third = document.getElementById("SrchBusNo").style;
var fourth = document.getElementById("BusStopLookup").style;
first.display="none";
second.display="none";
third.display="none";

    
    if (second.display == "block")
    {
        CollapseMenu('srchRouteText','collapseBusRoute');
     }

    if (first.display == "block")
    {
        CollapseMenu('SrchBusStopTxt','collapseSearchBusStop');
     }
    if (third.display == "block")
    {
        CollapseMenu('SrchBusNotxt','collapseBusNo');
     }
    fourth.display = "block";
    ExpandMenu('BusStopLookuptxt','collapseBusStopLookuptxt');
    reArrangeWindows('BusStopLookup');
}

function reArrangeWindows(div)
{
var first = document.getElementById("SearchBusStop");
var second = document.getElementById("SrchRouteHd");
var third = document.getElementById("SrchBusNo");
var fourth = document.getElementById("BusStopLookup");
var divmenuOne = document.getElementById('SrchBusStopTxt');
var divmenuTwo = document.getElementById('srchRouteText');
var divmenuthird = document.getElementById('SrchBusNotxt');
var divmenuFourth = document.getElementById('BusStopLookuptxt');

var prev;
 prev = 400;
    switch(div) 
   {  
    case 'SearchBusStop':   
            if (second.style.display == "block")
                {
                    second.style.top = prev + "px";
                    prev = prev + 30;
                }
            if (first.style.display == "block")
                {
                 first.style.top = prev + "px";
                if ((divmenuOne.style.visibility == "visible") || (divmenuOne.style.visibility == ""))
                    prev = prev + 120;
                else
                    prev = prev + 30;
                }
            if (third.style.display == "block")
                {
                third.style.top = prev + "px";
                 prev = prev + 30;
                }
            if (fourth.style.display == "block")
                {
                fourth.style.top = prev
                }
        break;

    case 'SrchRouteHd':
             if (second.style.display == "block")
                {
                    second.style.top = prev + "px";
                if ((divmenuTwo.style.visibility == "visible") || (divmenuTwo.style.visibility == ""))
                    prev = prev + 140;
                else
                    prev = prev + 40;
                }
            if (first.style.display == "block")
                {
                 first.style.top = prev + "px";
                prev = prev + 30;
                }
            if (third.style.display == "block")
                {
                third.style.top = prev + "px";
                 prev = prev + 30;
                }
            if (fourth.style.display == "block")
                {
                fourth.style.top = prev
                }   
            break; 
     case 'SrchBusNo':  
                if (second.style.display == "block")
                {
                    second.style.top = prev + "px";
                    prev = prev + 30;
                }
            if (first.style.display == "block")
                {
                 first.style.top = prev + "px";
                prev = prev + 30;
                }
            if (third.style.display == "block")
                {
                third.style.top = prev + "px";
                if ((divmenuthird.style.visibility == "visible") || (divmenuthird.style.visibility == ""))
                    prev = prev + 110;
                else
                    prev = prev + 30;
                }
            if (fourth.style.display == "block")
                {
                fourth.style.top = prev
                }  
         break;
     case 'BusStopLookup':   
            if (second.style.display == "block")
                {
                    second.style.top = prev + "px";
                    prev = prev + 30;
                }
            if (first.style.display == "block")
                {
                 first.style.top = prev + "px";
                prev = prev + 30;
                }
            if (third.style.display == "block")
                {
                third.style.top = prev + "px";
                 prev = prev + 30;
                }
            if (fourth.style.display == "block")
                {
                fourth.style.top = prev
                }  
     break;
    default:   
     break;
    }
}

function CollapseMenu(div,image)
{
var divmenu = document.getElementById(div);

    if (divmenu.style.visibility == "hidden")
        return;
    var imageEle = document.getElementById(image);
    divmenu.style.visibility = "hidden";
    imageEle.src="images/expand2.png" ;
    document.getElementById(div).style.display = "none"
    switch(div) 
   {  
    case 'srchRouteText':   
        reArrangeWindows('SrchRouteHd');
        break;
    case 'SrchBusStopTxt':   
         reArrangeWindows('SearchBusStop');
            break; 
     case 'SrchBusNotxt':   
         reArrangeWindows('SrchBusNo');
         break;
     case 'BusStopLookuptxt':   
        reArrangeWindows('BusStopLookup');   
     break;
    default:   
     break;
    }

}
function ExpandMenu(div,image)
{
var divmenu = document.getElementById(div);
var imageEle = document.getElementById(image);
if ((divmenu.style.visibility == "visible") || (divmenu.style.visibility == ""))
        return;
    divmenu.style.visibility = "visible";
    document.getElementById(div).style.display = "block"
    imageEle.src="images/collapse2.png" ;

    switch(div) 
   {  
    case 'srchRouteText':   
        CollapseMenu('SrchBusStopTxt','collapseSearchBusStop');
        CollapseMenu('SrchBusNotxt','collapseBusNo');
        CollapseMenu('BusStopLookuptxt','collapseBusStopLookuptxt');
        reArrangeWindows('SrchRouteHd');
        break;

    case 'SrchBusStopTxt':   
        CollapseMenu('SrchBusNotxt','collapseBusNo');
        CollapseMenu('BusStopLookuptxt','collapseBusStopLookuptxt');
        CollapseMenu('srchRouteText','collapseBusRoute');
         reArrangeWindows('SearchBusStop');
            break; 
     case 'SrchBusNotxt':   
        CollapseMenu('BusStopLookuptxt','collapseBusStopLookuptxt');
        CollapseMenu('srchRouteText','collapseBusRoute');
        CollapseMenu('SrchBusStopTxt','collapseSearchBusStop');
         reArrangeWindows('SrchBusNo');
         break;
     case 'BusStopLookuptxt':   
        CollapseMenu('srchRouteText','collapseBusRoute');
        CollapseMenu('SrchBusStopTxt','collapseSearchBusStop');
        CollapseMenu('SrchBusNotxt','collapseBusNo');   
        reArrangeWindows('BusStopLookup');   
     break;
    default:   
     break;
    }
    
    
}
function toggleMenu(div,image)
{

var divmenu = document.getElementById(div);
  if ((divmenu.style.visibility == "visible") || (divmenu.style.visibility == ""))
    CollapseMenu(div,image);
  else
    ExpandMenu(div,image);
    

}
function closeMenu(div)
{
document.getElementById(div).style.display = "none";
reArrangeWindows(div);
}
function closead()
{
document.getElementById("m0").style.display = "none";
}
function showProgressBox(progressText)
{
var popupboxid ="prog1";
  var Image ='<img id="imgprogId" height="25" alt="Please Wait..." src="Images/engine.gif" width="25"/>';
  
  
  	var resHtml = "<div class ='pdlg' ><table>";
		resHtml = resHtml +  "<tr class='pdlg'><td class='pdlg' width= 50px;>";
		resHtml = resHtml + Image + "</td></tr><tr class='pdlg'>";
		resHtml = resHtml +  "<td class='pdlg' width= 50px;>";
	    resHtml = resHtml + progressText + "</td>";
	    		resHtml = resHtml + "</tr></table></div>";
//progressText + '<img id="imgPostCodeSearch" style="VISIBILITY: hidden" height="25" alt="Please Wait..." src="Images/engine.gif" width="25"/>';
//new popUp(750, 50, 250, 100, popupboxid,resHtml, "white", "#00385c", "9pt serif", null, "#00385c", "white", "lightgrey", "#00568c", "black", true, false, false, false, false, false,'','','','');
//new popUp(left_position, top_position, width, height, id_of_box, content_text, content_bgcolor, content_text_color, content_font_stylestring, title_text, title_bgcolor, title_text_color, border_color,  scrollbar_color, shadow_color, is_hidden_on_start, is_draggable, is_resizeable, show_old, is_external, disable_on_return , min_image, max_image, close_image, resize_image);


}

function unloadProgressBox()
{
	        try
	        {
            unloadbox("prog1");
	         }
	            catch(err){}

}

function GetBusNoDetailsTest(busno)
{

    BUS_NO = busno;
	Imap.WebServices.BusSvc.GetBusNoDetails(document.getElementById("hidLogId").value,busno,$find('AutoCompleteEx')._contextKey, SucceededBusDetailsResultsTest, handleFailedBusNoDetailsTest);
	
}
function SucceededBusDetailsResultsTest(places, eventArgs)
{	



	var resHtml = "";
	var latitude;
	var longitude;
    var latlonString;
	var title;
    var firsttime = true;
    
    var ImageUrl ;
    var MarkerCount =0; 

	/**  if there are no results, display the error message and return */
	if(places == null)
	{
		alert("There were no results for the specified Bus No, please check your query.");
		return;
	}

	if(places.BusStops == null)
	{
		alert("There were no results for the specified Bus No, please check your query.");
		return;
	}
	if(places.BusStops.length == 0)
	{
		alert("There were no results for the specified Bus Stop, please check your query.");
		return;
	}



	
     latlonString="";
	/**  Create an entry for each VEPlace in the result set */
	for(var p = 0; p < places.BusStops.length; p++)
	{

			/**  Gather some info up front */
		var place = places.BusStops[p];
		BusStop = place.StopName;
		var Origin =  places.Origin;
		var Destination =  places.Destination;
		if (firsttime)
		{

            var JourneyTime = places.JourneyTime;
            var ServiceType = places.ServiceType;
           var str=places.Fare;
            if (str == null)
                str ="";
            var Fare =str.replace(new RegExp('<Stage',"g"), ''); ;
               Fare =Fare.replace(new RegExp('<stage',"g"), ''); ;
             Fare =Fare.replace(new RegExp('<br>',"g"), ''); ;
            Fare =Fare.replace(new RegExp('<fareInfo>',"g"), ''); ;
            Fare =Fare.replace(new RegExp('</ Stage>',"g"), ';'); ;
            
              Fare =Fare.replace(new RegExp('</ stage>',"g"), ';'); ;
             Fare =Fare.replace(new RegExp('>',"g"), ' for stage '); ;

             Fare =Fare.replace(new RegExp('>',"g"), ' for stage '); ;

             if (Fare=='0')
                Fare="NA";
             if (Fare=="")
                Fare="NA"; 
            var Distance = places.Distance ;
             if (Distance == null)
                Distance ="";
             if (Distance=='0')
                Distance="NA";
             if (Distance=="")
                Distance="NA"; 
            var DepartureFromO = places.DepartureFromO ;
            var DepartureFromD = places.DepartureFromD;
            	resHtml = resHtml + "<div class ='dlg' ><table>";
            resHtml = resHtml + "<tr class='dlg'><td class='dlgB' width= 50px;>";
            resHtml = resHtml + "Origin:";  
            resHtml = resHtml + "</td><td class='dlg' width= 250px;>";
            resHtml = resHtml + Origin;
            resHtml = resHtml + "</td></tr><tr class='dlg'><td class='dlgB' width= 50px;>";
            resHtml = resHtml + "Destination:";  
            resHtml = resHtml + "</td><td class='dlg' width= 250px;>";
            resHtml = resHtml + Destination;
            resHtml = resHtml + "</td></tr><tr class='dlg'><td class='dlgB' width= 50px;>";
            resHtml = resHtml + "Distance:";
            resHtml = resHtml + "</td><td class='dlg' width= 250px;>";
            resHtml = resHtml + Distance + " Kms";
            resHtml = resHtml + "</td></tr><tr class='dlg'><td class='dlgB' width= 50px;>";
            resHtml = resHtml + "JourneyTime:";  
            resHtml = resHtml + "</td><td class='dlg' width= 250px;>";
            resHtml = resHtml + JourneyTime;
            resHtml = resHtml + "</td></tr></table>";
            resHtml = resHtml + "<table>";
		    resHtml = resHtml + "<tr class='dlg'><th class='dlg' width= 50px;>";
		    resHtml = resHtml + "</th>";
		    resHtml = resHtml + "<th class='dlg' width= 250px;> ";
		    resHtml = resHtml + "Bus Stops";
		    resHtml = resHtml + "</th>"  
        firsttime=false;
        }      
      

		latitude = place.Latitude;
		longitude = place.Longitude;
        title =BusStop;
		ImageUrl = "";
		/**  create the info box description
		var desc = "Latitude: " + latitude + "<br/>" + "Longitude: " + longitude;  */
        var desc =  "Bus No:" + BUS_NO;



        var numberImage ="<img style='cursor:pointer' src='" +  ImageUrl + "' onclick='MarkerClick(" + MarkerCount + ")'/>";
	
		/**  Add the information to the resultsDiv html, including a link */

		/**  that recenters the map over the pin */
		resHtml = resHtml + "<tr class='dlg'><td class='dlg' width= 50px;>";
		resHtml = resHtml + numberImage + "</td><td class='dlg' width= 250px;> ";
		resHtml = resHtml + "<a href='javascript:onclick=MarkerClick(" + MarkerCount + ");'>";
		resHtml = resHtml + title ;
		resHtml = resHtml + "</a></td>";


		resHtml = resHtml + "</tr>";
	}

	resHtml = resHtml + "</table>";
	
	
            	resHtml = resHtml + "<table>";
            resHtml = resHtml + "<tr class='dlg'><td class='dlgB' width= 50px;>";
            resHtml = resHtml + "Fare:";
            resHtml = resHtml + "</td><td class='dlg' width= 250px;>";
            resHtml = resHtml + Fare;
            resHtml = resHtml + "</td></tr><tr class='dlg'><td class='dlgB' width= 50px;>";
            resHtml = resHtml + "ServiceType:";  
            resHtml = resHtml + "</td><td class='dlg' width= 250px;>";
            resHtml = resHtml + ServiceType;
            resHtml = resHtml + "</td></tr><tr class='dlg'><td class='dlgB' width= 50px;>";
            resHtml = resHtml + "Departure From Origin:";  
            resHtml = resHtml + "</td><td class='dlg' width= 250px;>";
            resHtml = resHtml + DepartureFromO;
            resHtml = resHtml + "</td></tr><tr class='dlg'><td class='dlgB' width= 50px;>";
            resHtml = resHtml + "Departure From Destination:";  
            resHtml = resHtml + "</td><td class='dlg' width= 250px;>";
            resHtml = resHtml + DepartureFromD;
            resHtml = resHtml + "</td></tr></table></div>";
	
 

document.write(resHtml);
}

// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameter.

function handleFailedBusNoDetailsTest(error)
{
	// Display the error.    
	alert("Bus No Detail Service Connection Error: " + error.get_message() + '. Please submit your query again.');
}
