'Class XMLRailMonitor

Private green, red, yellow, orange, blue
Private html()
Private NumRepeats, MaxRepeats
Dim IntervalID, CorridorStatus



'On error resume next


'Private Sub Class_Initialize()

	Redim html(25)
	green="00FF00"
	red="FF0000"
	yellow="FFFF00"
	orange="FFA000"
	blue = "00FFFF"
	
	MaxRepeats = 5

	Call InitiallySetupTrainTable

	'IntervalID = Window.SetInterval("Update_onclick", 20000, VBScript)

'End Sub





Private Sub InitiallySetupTrainTable
Dim TrainXML


	html(0)="<table align=left id=SiteTable BORDER=7 cellpadding=2 cellspacing=1 bgcolor=FFE0C0>"
	html(1)="<THEAD><TH colspan=4>Railroad Corridor Monitor</TH></THEAD>"
	html(2)="<TR><TD colspan=1 align=center title=The_current_state_of_the_rail_corridor_-_Train_Event_or_CLEAR.><B>Status</b></TD>"
	html(3)="<TD colspan=3 Id=State align=center bgcolor=0><!--input type=text name=CorridorState size=13 align=center readonly--><img src=http://railview.tamu.edu/pics/stat_unknown.gif name=CorridorState></img></TD></TR>"
	html(4)="<TR><TD colspan=4 height=7 bgcolor=000000></TD></TR>"
	html(5)="<tr><td align=center title=A_list_of_all_the_places_in_which_arrival_and_departure_times_will_be_generated><B>Site</B></td><td align=center title=Estimated_Time_Of_Arrival_of_the_train><B>ETA</B></td>"
	html(6)="<td align=center title=Estimated_Time_Of_Departure_of_the_train><B>ETD</B></td><td align=center title=A_measure_of_the_confidence_in_the_predictions.__Range_is_0_to_100%><B>Conf</B></td></tr>"

	html(7)="<TR><TD colspan=4 height=7 bgcolor=000000></TD></TR>"
	html(8)="<TR><TD colspan=2 id=TSPd align=center title=The_trains_current_speed><B><font size=-1>Train Speed</font></b></TD>"
	html(9)="<TD colspan=2 align=center><span id=TrainSpeed></span></TD></TR>"
	html(10)="<TR><TD colspan=2 align=center><B><font size=-1>Train Direction</font></b></TD>"
	html(11)="<TD colspan=2 Id=Dir align=center><span id=Direction></span></TD></TR>"
	html(12)="<TR><TD colspan=2 align=center><B><font size=-1>Train Length</font></b></TD>"
	html(13)="<TD colspan=2 align=center><span id=TrainLength></span></TD></TR>"
	html(14)="<TR><TD colspan=2 align=center><B><font size=-1>Confidence</font></b></TD>"
	html(15)="<TD colspan=2 align=center id=TrainConfidCell><span id=TrainConfid></span></TD></TR>"
	html(16)="<TR><TD colspan=2 align=center><B><font size=-1>Update Time</font></b></TD>"
	html(17)="<TD colspan=2 align=center><span id=UpdateTime></span></TD></TR>"
	html(18)="<TR><TD colspan=4 height=7 bgcolor=000000></TD></TR>"
	'html(19)="<TR><TD colspan=2 align=center><B><font size=-1>Last Train Time</font></b></TD>"
	'html(20)="<TD colspan=2 align=center><span id=LastTrainTime></span></TD></TR>"
	'html(21)="<TR><TD colspan=2 align=center><B><font size=-1>Last Train Dir</font></b></TD>"
	'html(22)="<TD colspan=2 align=center><span id=LastTrainDirection></span></TD></TR>"
	html(19)="<tr><td colspan=4 align=center><input type=button id=Update value=Update&nbsp;Corridor></input></td></tr></table><!--input type=text name=TTT></input><input type=button value=Choose onclick=TT></input-->"


	<!--  Dynamically draw the train table  -->
	Set TrainXML = GetTrainXML("http://railview.tamu.edu/xml/rrcorridorstatus.xml")

	Call BuildTrainTable(TrainXML.documentelement.selectnodes("Sitelist/Site"))
	


	<!--  Populate table with values from XML -->
	Call ManageXML(TrainXML)


	Set TrainXML = Nothing
End Sub




Private Sub TT

	CorridorState.src=TTT.value
End Sub



Public Function ConvertToMinSec(secs)
Dim min
Dim remndr
Dim r

 
	If IsNumeric(secs) = False Then
		ConvertToMinSec = "-"
		Exit Function
	End If

	min = Fix(secs / 60)

	If secs < 0 then
		ConvertToMinSec = " "
		Exit Function
	End If

	If min > 30 then
		ConvertToMinSec = "-"
		Exit Function
	End If


	If min > 10 then
		ConvertToMinSec = "10:00+"
		Exit Function
	End If


	remndr = secs - (min * 60)
	r = remndr
	If Len(r) = 1 Then r = "0" & r

	ConvertToMinSec = min & ":" & r
End Function





Public Sub Update_onclick


	Call ManageXML(GetTrainXML("http://railview.tamu.edu/xml/RRCorridorStatus.xml"))
End Sub





Private Sub ManageXML(TrainXML)
Dim dataholder
On error resume next


	Set nodeSL = TrainXML.documentelement.selectnodes("Sitelist/Site")

	dataholder = Right(TrainXML.documentelement.selectsinglenode("UpdateTime").text,8)
	If UpdateTime.innerText = dataholder Then
		NumRepeats = NumRepeats + 1
	Else
		NumRepeats = 0
	End If

	UpdateTime.innerText = dataholder

	If NumRepeats > MaxRepeats Then
		For i = 0 to nodeSL.Length - 1
			document.all("SiteStatus",i).bgcolor = Yellow
			document.all("A",i).innerText = "-"
			document.all("D",i).innerText = "-"
			document.all("C",i).innerText = "-"
		Next
		
		TrainSpeed.innerText = "-"
		TrainLength.innerText = "-"
		TrainConfid.innerText = "-"
		Direction.innerText = "-"
		CorridorState.src = "http://railview.tamu.edu/pics/stat_unknown.gif"
		Set nodeSL = Nothing
		Exit Sub	
	End If


	For i = 0 to nodeSL.Length - 1
		If nodeSL.item(i).selectnodes("PredictedTrainList/PredictedTrain").length <> 0 Then
			document.all("A",i).innerText = ConvertToMinSec(nodeSL.item(i).selectsinglenode("PredictedTrainList/PredictedTrain/ETA").text)
			document.all("D",i).innerText = ConvertToMinSec(nodeSL.item(i).selectsinglenode("PredictedTrainList/PredictedTrain/ETD").text)
			document.all("C",i).innerText = nodeSL.item(i).selectsinglenode("PredictedTrainList/PredictedTrain/Confid").text

		Else
			document.all("A",i).innerText = "-"
			document.all("D",i).innerText = "-"
			document.all("C",i).innerText = "-"

		End If

		Select Case nodeSL.item(i).selectSingleNode("Status").getattribute("Code")
		Case "G"
			document.all("SiteStatus",i).bgcolor = green
		Case "Y"
			document.all("SiteStatus",i).bgcolor = Yellow
		Case "R"
			document.all("SiteStatus",i).bgcolor = Red
		Case "C"
			document.all("SiteStatus",i).bgcolor = orange
		Case Else
			document.all("SiteStatus",i).bgcolor = Blue
		End Select

	Next

	

	If TrainXML.documentelement.selectnodes("Trainlist/Train").length <> 0 Then
		TrainSpeed.innerText = TrainXML.documentelement.selectsinglenode("Trainlist/Train/Speed").text
		TrainLength.innerText = TrainXML.documentelement.selectsinglenode("Trainlist/Train/Length").text
		TrainConfid.innerText = TrainXML.documentelement.selectsinglenode("Trainlist/Train/Confid").text
		Direction.innerText = TrainXML.documentelement.selectsinglenode("Trainlist/Train/Direction").text
	Else
		TrainSpeed.innerText = "-"
		TrainLength.innerText = "-"
		TrainConfid.innerText = "-"
		Direction.innerText = "-"
	End If


		CorridorStatus = TrainXML.documentelement.selectsinglenode("CorridorStatus").text
		Select Case CorridorStatus

		Case "Train_Detected"
			CorridorState.src = "http://railview.tamu.edu/pics/trainana.gif"

		Case "Clear"
			CorridorState.src = "http://railview.tamu.edu/pics/Corridor_Clear.gif"

		Case "Intersection_Blocked"
			CorridorState.src = "http://railview.tamu.edu/pics/Intersection_Blocked.gif"

		Case "Unknown"
			CorridorState.src = "http://railview.tamu.edu/pics/stat_unknown.gif"

		Case Else
			CorridorState.src = "http://railview.tamu.edu/pics/stat_unknown.gif"

		End Select
 



	Set nodeSL = Nothing
End Sub





Public Function GetTrainXML(website)
Dim xmlTrain, result

	<!--  Get Corridor Status XML File -->
	Set xmlTrain = CreateObject("microsoft.xmldom")
	xmlTrain.async = False
	result = xmlTrain.load(website)

	If result <> True Then
		'Window.Alert xmlTrain.parseError.reason
		Set xmlTrain = Nothing
		Exit Function
	End If

	Set GetTrainXML = xmlTrain

	Set xmlTrain = Nothing
End Function





Private Sub BuildTrainTable(nodeSL)

Dim s_name, s_eta, s_etd, s_confid
Dim i
	<!--  Build top part of table -->
	For i = 0 to 6
		document.write html(i)
	Next


	<!--  Dynamic rows determined by XML file -->
For i = 0 to nodeSL.Length - 1
	s_name = nodeSL.item(i).selectsinglenode("Name").text
	's_eta = ConvertToMinSec(nodeSL.item(i).selectsinglenode("ETA").text)
	's_etd = ConvertToMinSec(nodeSL.item(i).selectsinglenode("ETD").text)
	's_confid = nodeSL.item(i).selectsinglenode("Confid").text
	s_eta = "-"
	s_etd="-"
	s_confid="-"
	document.write "<tr id=SiteStatus><td align=center><B><font size=-1>" & s_name & "</font></B></td><td align=center><span id=A innerText=" & s_eta & "></span></td><td align=center><span id=D innerText=" & s_etd & "></span></td><td align=center><span id=C innerText=" & s_confid & "></span></td></tr>"

Next

	<!--  Build the bottom of the table -->
	For i = 7 to 19
		document.write html(i)
	Next
End Sub



'End Class



