setLocale(1033)
Class ChangingDataClass

	Public	InternalVehicles, InternalTrailers, InternalEmployees, InternalStops, InternalCargos
	Public	CommunityVehicles, CommunityTrailers, CommunityEmployees, CommunityStops, CommunityCargos
	Public	Vehicles, Employees, Trailers, Positions, EPositions, Companies
	Public	VehicleMaxUpdateID, EmployeeMaxUpdateID, CompanyMaxUpdateID, StopMaxUpdateID
	Public	UpdatePages
	Public	Updating
	Public	VehicleXMLDoc, VehicleNodes, VehicleNodeIndex, VehicleUpdateTimer
	Public	EmployeeXMLDoc, EmployeeNodes, EmployeeNodeIndex, EmployeeUpdateTimer
	Public	StopsXMLDoc, StopsNodes, StopsNodeIndex, StopsUpdateTimer
	Public	CompaniesXMLDoc, CompaniesNodes, CompaniesNodeIndex, CompaniesUpdateTimer
	Public	UpdateInterval
		
	Private Sub Class_Initialize()
		Set Vehicles = CreateObject("Scripting.Dictionary")
		Set Employees = CreateObject("Scripting.Dictionary")
		Set Trailers = CreateObject("Scripting.Dictionary")
		Set Positions = CreateObject("Scripting.Dictionary")
		Set EPositions = CreateObject("Scripting.Dictionary")
		Set InternalVehicles = CreateObject("Scripting.Dictionary")
		Set InternalEmployees = CreateObject("Scripting.Dictionary")
		Set InternalTrailers = CreateObject("Scripting.Dictionary")
		Set InternalStops = CreateObject("Scripting.Dictionary")
		Set InternalCargos = CreateObject("Scripting.Dictionary")
		Set CommunityVehicles = CreateObject("Scripting.Dictionary")
		Set CommunityEmployees = CreateObject("Scripting.Dictionary")
		Set CommunityTrailers = CreateObject("Scripting.Dictionary")
		Set CommunityStops = CreateObject("Scripting.Dictionary")
		Set CommunityCargos = CreateObject("Scripting.Dictionary")
		Set UpdatePages = CreateObject("Scripting.Dictionary")
		Set Companies = CreateObject("Scripting.Dictionary")

		VehicleMaxUpdateID = -1
		EmployeeMaxUpdateID = -1
		CompanyMaxUpdateID = -1
		StopMaxUpdateID = -1
		Call ReadCompanies()
		Call ReadVehicles()
		Call ReadEmployees()
		'Call ReadStops()
	End Sub
	
	public Function getCompanyClass()
		set getCompanyClass = new CompanySummaryClass
	End Function

	public Function getCargoClass()
		set getCargoClass = new CargoSummaryClass
	End Function

	public Function getVehicleClass()
		set v = new VehicleSummaryClass
		set v.Position = new PositionClass
		set v.Vehicle = new VehicleClass
		set getVehicleClass = v
	End Function

	Public sub ForceUpdate()
		if not Updating then
			VehicleUpdateTimer = Empty
			call ReadVehicles()
			CompaniesUpdateTimer = Empty
			call ReadCompanies()
		end if
	end Sub

	Public Sub Update()
		on error resume next
		'on error goto 0
		err.clear
		UpdateInterval = Data.Objects("session").UpdateInterval
		if err then
			UpdateInterval = 60
		end if
		err.clear
		window.status = "UI = " & UpdateInterval
		Updating = true
		call ReadCompanies()
		call ReadVehicles()
		'call ReadEmployees()
		'call ReadStops()
		'Call updateTrailers()
		Updating = false
	End Sub

	Public Sub RegisterID(P, ItemType, ID, Internal)
		if Internal then
			select case lcase(ItemType)
				Case "v":
							if InternalVehicles.Exists(ID) then
								InternalVehicles(ID).RegisterPage P
							end if
				Case "e":
							if InternalEmployees.Exists(ID) then
								InternalEmployees(ID).RegisterPage P
							end if
				case "h":
							Alert("TODO: Show company information")
				case "s":
							Alert("TODO: Show stop information")
			end select
		else
			select case lcase(ItemType)
				Case "v":
							if CommunityVehicles.Exists(ID) then
								CommunityVehicles(ID).RegisterPage P
							end if
				Case "e":
							if CommunityEmployees.Exists(ID) then
								CommunityEmployees(ID).RegisterPage P
							end if
				case "h":
							Alert("TODO: Show company information")
				case "s":
							Alert("TODO: Show stop information")
			end select
		end if
	End Sub
	
	Public Sub RegisterPage(P,ID)
		Set UpdatePages(ID) = P
		'Alert(join(UpdatePages.Keys,","))
	End Sub
	
	Public Sub UnRegisterPage(ID)
		If UpdatePages.Exists(ID) Then
			UpdatePages.Remove(ID)
		End If
	End Sub
	
	Private Sub ReadStops()
		on error resume next
		if not isEmpty(StopsUpdateTimer) then
			if Timer - StopsUpdateTimer < UpdateInterval then
				Exit Sub
			end if
		end if
		StopsUpdateTimer = Timer
		counter = 0
		URL = Data.Objects("session").URL_Summary & "Stop?"
		URL = URL & "UpdateID=" & StopMaxUpdateID
		'Alert(URL)
		Window.Status = "Getting stops data at " & now() & " starting from " & StopMaxUpdateID
		set XMLDoc = Tools.GetXml(URL)
		'alert(XMLDoc.xml)
		Data.Objects("session").TimeStamp = CDate(XMLDoc.selectSingleNode("//TimeStamp").GetAttribute("value"))
		Set nodes = XMLDoc.selectNodes("//T")
		if nodes.Length > 0 then
			'Window.Status = nodes.Length & " updates received at " & now()
			for each Node in nodes
				'Alert(Node.xml)
				Internal = (Node.GetAttribute("I") = "1")
				'on error resume next
				SID = Clng(Node.GetAttribute("ID"))

				if Internal then
					if InternalStops.Exists(SID) then
						Set S = InternalStops.Item(SID)
						Call S.ClearValues(Node)
					else
						Set S = New StopSummaryClass
						Set S.Renderer = StopRenderer
					End if
				else
					if CommunityStops.Exists(SID) then
						Set S = CommunityStops.Item(SID)
						Call S.ClearValues(Node)
					else
						Set S = New StopSummaryClass
						Set S.Renderer = StopRenderer
					End if
				end if				

					For Each Attr In Node.Attributes
						Select Case lcase(Attr.Name)
							Case "id":		S.ID = Clng(Attr.Value)
							case "i":		S.Internal = (Attr.Value = "1")
							Case "g":		S.GradeTypeID = Clng(Attr.Value)
							Case "sci":		S.CombinationID = Clng(Attr.Value)
							Case "cn":		S.CombinationName = Attr.Value
							Case "cl":		S.LastSequence = Clng(Attr.Value)
							Case "cid":		S.CompanyID = Clng(Attr.Value)
							Case "l":		S.LoginID = Clng(Attr.Value)
							Case "s":		S.Sequence = Clng(Attr.Value)
							Case "t":		S.TypeID = Clng(Attr.Value)
							case "ts":		S.ETA = CDate(Attr.Value)
							Case "sa":		S.SpaceAmount = Cdbl(Attr.Value)
							Case "st":		S.SpaceTypeID = Clng(Attr.Value)
							Case "cc":		S.CountryCode = Attr.Value
							case "p":		S.Position = Attr.Value
							case "x":		S.MapX = Clng(Attr.Value)
							case "y":		S.MapY = Clng(Attr.Value)
							case "lat":		S.Lat = Cdbl(Attr.Value)
							case "lon":		S.Lon = Cdbl(Attr.Value)
							case "up":		S.UpdateID = Cdbl(Attr.value)
											'Alert(MaxStopUpdateID & "," & S.UpdateID)
											if S.UpdateID > StopMaxUpdateID then StopMaxUpdateID = S.UpdateID
							case "ec":		S.ExecutorTypeCode = Attr.Value
							case "ei":		S.ExecutorID = CLng(Attr.Value)
							case "ci":		S.CheckInTime = CDate(Attr.Value)
							case "co":		S.CheckOutTime = CDate(Attr.Value)
							case "c":		S.Comments = Attr.Value
							case "a":		S.Available = (Attr.Value = "1")
						end select
						if err then
							Err.Clear
						end if
					next
					If S.Internal Then
						Set InternalStops(S.ID) = S
					Else
						Set CommunityStops(S.ID) = S
					End if

					'Call S.Update()

					if Updating then
						for each key in UpdatePages.Keys
							on error resume next
							Call UpdatePages(key).Update(S)
							if Err then
								window.status = "Page with ID " & key & " is not available." & vbcrlf & " Removing from update list." & vbcrlf & Err.Description & ":" & Err.Source
								if key <> "MAP" and key <> "GEOFENCE" then
									UpdatePages.Remove(key)
								end if
							Err.Clear
							end if
						Next
					end if
					if err then
						window.status = Err.Description & vbcrl & Err.Source & vbcrlf & "VID = " & VID & " PDID=" & PDID
						Err.Clear
					end if
			next
		End If
		'Alert(InternalStops.Count & "," & CommunityStops.Count)
	End Sub

	Private Sub ReadCompanies()
		on error resume next
		if not isEmpty(CompaniesUpdateTimer) then
			if Timer - CompaniesUpdateTimer < UpdateInterval * 10 then
				Exit Sub
			end if
		end if
		CompaniesUpdateTimer = Timer
		counter = 0
		URL = Data.Objects("session").URL_Summary & "Company?"
		URL = URL & "UpdateID=" & CompanyMaxUpdateID
		'Alert(URL)
		Window.Status = "Getting company data at " & now() & " starting from " & CompanyMaxUpdateID
		set XMLDoc = Tools.GetXml(URL)
		'Alert(XMLDoc.xml)
		Data.Objects("session").TimeStamp = CDate(XMLDoc.selectSingleNode("//TimeStamp").GetAttribute("value"))
		Set nodes = XMLDoc.selectNodes("//T")
		if nodes.Length > 0 then
			Window.Status = nodes.Length & " vehicle updates received at " & now()
			for each Node in nodes
				Internal = (Node.GetAttribute("CI") = "I")
				id = CLng(Node.getAttribute("ID"))
				if companies.exists(id) then
					set o = companies.item(id)
				else
					set o = new CompanySummaryClass
					set companies.item(id) = o
					o.ID = id
					o.Internal = Internal
				end if

				For Each Attr In Node.Attributes
					Select Case lcase(Attr.Name)
					
						Case "n":	o.Name = Attr.value
									window.status = o.Name
						Case "p":	o.Prefix = Attr.value
						Case "t":	o.TypeID = CLng(Attr.value)
						Case "cc":	o.CountryCode = Attr.value
						Case "x":	o.MapX = CLng(Attr.value)
						Case "y":	o.MapY = CLng(Attr.value)
					case "lat":		o.Lat = Cdbl(Attr.Value)
					case "lon":		o.Lon = Cdbl(Attr.Value)
						Case "g":	o.Grade = CLng(Attr.value)
o.GradeTypeID = o.Grade
						Case "l":	o.HasLogo = (lcase(Attr.value) = "true")
						Case "u":	n = Clng(Attr.value)
									if  n > CompanyMaxUpdateID then
										CompanyMaxUpdateID = n
									end if
						Case "d":	o.Director = Attr.value
						Case "a":	o.AddressInfo = Attr.value
						Case "pc":	o.PostalCode = Attr.value
						Case "ptl":	o.PublishedTel = Attr.value
						Case "pem":	o.PublishedEMail = Attr.value
						Case "tl":	o.Tel = Attr.value
						Case "f":	o.Fax = Attr.value
						Case "e":	o.eMail = Attr.value
						Case "c":	o.City = Attr.value
						Case "s":	o.Street = Attr.value
						Case "fn":	o.FullName = Attr.value
					end Select
					if err then
						alert(Err.Description)
						err.Clear
					end if
				next
			next
		end if
	end sub

	Private Sub ReadVehicles()
		Dim Busy
		on error resume next
		if not isEmpty(VehicleXMLDoc) then
			if Busy <> "1" then
				Busy = "1"
				Call ProcessVehicles()
				Busy = "0"
			end if
			exit sub
		end if
		if not isEmpty(VehicleUpdateTimer) then
			if Timer - VehicleUpdateTimer < UpdateInterval then
				Exit Sub
			end if
		end if
		VehicleUpdateTimer = Timer
		URL = Data.Objects("session").URL_Summary & "Vehicle?"
		URL = URL & "UpdateID=" & VehicleMaxUpdateID
		'Alert(URL)
		Window.Status = "Getting vehicle data at " & now() & " starting from " & VehicleMaxUpdateID
		set VehicleXMLDoc = Tools.GetXml(URL)
		'Alert(XMLDoc.xml)
		Data.Objects("session").TimeStamp = CDate(XMLDoc.selectSingleNode("//TimeStamp").GetAttribute("value"))
		Set VehicleNodes = VehicleXMLDoc.selectNodes("//T")
		if VehicleNodes.Length > 0 then
			VehicleNodeIndex = Empty
		else
			VehicleXMLDoc = Empty
			Window.Status = "No new vehicle data available at " & now()
		'Alert(InternalVehicles.Count & "," & CommunityVehicles.Count & "," & Positions.Count)
		end if
	end sub

	Public Sub ProcessVehicles()
		on error resume next
		Window.Status = nodes.Length & " updates received at " & now()
		'Alert("Starting Process " & VehicleNodeIndex)
		if isEmpty(VehicleNodeIndex) then
			VehicleNodeIndex = 0
		end if
		Maxcounter = 10000
		window.status = "VehicleNodeIndex = " & VehicleNodeIndex & "  VehicleNodes.Length = " & VehicleNodes.Length
		for VehicleNodeIndex = VehicleNodeIndex to VehicleNodes.Length -1
			Set Node = VehicleNodes(VehicleNodeIndex)
				Internal = (Node.GetAttribute("I") = "1")
				VID = Clng(Node.GetAttribute("VID"))

'				if not isnumeric(VID) then
'					alert(Node.innerXML)
'				end if

				if Internal then
					if InternalVehicles.Exists(VID) then
						Set V = InternalVehicles.Item(VID)
						Call V.ClearValues(Node)
					else
						Set V = New VehicleSummaryClass
					End if
				else
					if CommunityVehicles.Exists(VID) then
						Set V = CommunityVehicles.Item(VID)
						Call V.ClearValues(Node)
					else
						Set V = New VehicleSummaryClass
					End if
				end if				
				
				if isNull(Node.GetAttribute("PDID")) then
					PDID = V.Position.DeviceID
				else
					PDID = Clng(Node.GetAttribute("PDID"))
				End If

				If not Positions.Exists(PDID) Then
					Set Positions(PDID) = New PositionClass
				End If
				Set P = Positions.Item(PDID)

				if Node.getAttribute("PDNEW") = "1" then
					set V.Destination = new DestinationClass
				end if

				Set V.Position = P
				if not Vehicles.Exists(VID) then
					Set Vehicles(VID) = New VehicleClass
				End If
				Set V.Vehicle = Vehicles.Item(VID)
				Counter = Counter + 1
					For Each Attr In Node.Attributes
						Select Case lcase(Attr.Name)
							case "i":		V.Internal = (Attr.Value = "1")
											V.Vehicle.Internal = V.Internal
							Case "g":		V.GradeTypeID = Clng(Attr.Value)
							Case "vid":		V.ID = Clng(Attr.Value)
							Case "cid":		V.Vehicle.CompanyID = Clng(Attr.Value)
							Case "vtid":	V.Vehicle.TypeID = Clng(Attr.Value)
											V.GifFileName = Data.Objects("vehicletype").item(V.Vehicle.TypeID).GifFileName
							Case "vcn":		V.Vehicle.CommunityName = Attr.Value
							Case "vn":		V.Vehicle.Name = Attr.Value
											V.Description = Attr.Value
							Case "voc":		V.Vehicle.Origincity = Attr.Value
							Case "vox":		V.Vehicle.OriginMapX = Clng(Attr.Value)
							Case "voy":		V.Vehicle.OriginMapY = Clng(Attr.Value)
							Case "tid":		V.Vehicle.TrailerID = Clng(Attr.Value)
							Case "vcc":		V.Vehicle.CountryCode = Attr.Value
							Case "vc":		V.Vehicle.City = Attr.Value
							Case "emp":		V.Vehicle.EmployeeIDs = Attr.Value
							case "vup":		V.Vehicle.UpdateID = Cdbl(Attr.Value)
											V.UpdateID = Cdbl(Attr.Value)
											if V.UpdateID > VehicleMaxUpdateID then VehicleMaxUpdateID = V.UpdateID
							case "pdid":	P.DeviceID = Clng(Attr.Value)

							case "prx":		P.PrevMapX = Clng(Attr.Value)
							case "pry":		P.PrevMapY = Clng(Attr.Value)
							case "prlat":		P.PrevLat = Cdbl(Attr.Value)
							case "prlon":		P.PrevLon = Cdbl(Attr.Value)
							case "pstl":		P.Stealth = Attr.Value

							case "pdt":		V.Destination.StopTypeID = CLng(Attr.Value)
							case "pdsid":	V.Destination.StopID = CLng(Attr.Value)
							case "pdspt":	V.Destination.SpaceTypeID = CLng(Attr.Value)
							case "pdspa":	V.Destination.SpaceAmount = CLng(Attr.Value)

							case "pds":		V.Destination.Position = Attr.Value
							case "pdcc":	V.Destination.CountryCode = Attr.Value
							case "pdsx":	V.Destination.MapX = Clng(Attr.Value)
							case "pdsy":	V.Destination.MapY = Clng(Attr.Value)
							case "pdslat":	V.Destination.Lat = Cdbl(Attr.Value)
							case "pdslon":	V.Destination.Lon = Cdbl(Attr.Value)
							case "pdst":	V.Destination.ETA = CDate(Attr.Value)
							case "pdms":
											'if Objects("session").CompanyID = 343 then
												'alert(Attr.Value)
											'end if
											V.Destination.MoreStops = (lcase(Attr.Value) = "true")
											'if Objects("session").CompanyID = 343 then
												'alert(V.Destination.MoreStops)
											'end if
							case "pdar":	V.Destination.ArrivalTime = CDate(Attr.Value)
							case "pdcm":	V.Destination.Comments = Attr.Value
							case "pdup":
											if Cdbl(Attr.Value) > VehicleMaxUpdateID then VehicleMaxUpdateID = Cdbl(Attr.Value)


							case "pdtid":	P.DeviceTypeID = CLng(Attr.Value)
							case "px":		P.MapX = Clng(Attr.Value)
							case "py":		P.MapY = Clng(Attr.Value)
							case "lat":		P.Lat = Cdbl(Attr.Value)
							case "lon":		P.Lon = Cdbl(Attr.Value)
							case "ps":		P.Speed = Clng(Attr.Value)
							case "pps":		P.PeakSpeed = Clng(Attr.Value)
							case "pd":		P.Direction = Attr.Value
							case "po":		P.OdoMeter = Cdbl(Attr.Value)
							case "pp":		P.Position = Attr.Value
							case "pcc":		P.CountryCode = Attr.Value
							case "pbcc":	P.BigCountryCode = Attr.Value
							case "pst":		P.State = Attr.Value
											P.StateColor = Data.Objects("statecolors").item(Attr.Value)
											P.StateDescription = Data.Objects("statedescriptions").item(Attr.Value)
											ChangeSpeed = False
											Select case Attr.Value
												case "F":	ChangeSpeed = True
												case "S":	ChangeSpeed = True
												case "T":	ChangeSpeed = True
												case "U":	ChangeSpeed = True
											end select
							case "lm":		
											if Attr.Value <> "" then
												P.Landmark = Attr.Value
											else
												P.Landmark = Empty
											end if
							case "pas":		P.AverageSpeed = CDbl(Attr.Value)
							case "pig":		P.Ignition = CInt(Attr.Value)
							case "pt":		P.TimeStamp = CDate(Attr.Value)
							case "pup":		P.UpdateID = Cdbl(Attr.value)
											if P.UpdateID > VehicleMaxUpdateID then VehicleMaxUpdateID = P.UpdateID
							case "sdid":	V.Status.DeviceID = Clng(Attr.Value)
							case "st":		V.Status.TypeID = Clng(Attr.Value)
							case "sa":		V.Status.Area = Attr.Value
							case "sc":		V.Status.Clear = (Attr.Value = "true")
							case "sv":		V.Status.Value = Clng(Attr.Value)
							case "su":		V.Status.Undefined = (Attr.Value = "true")
							case "sts":		V.Status.TimeStamp = CDate(Attr.Value)
							case "svt":		V.Status.ValueText = Attr.Value
							case "svd":		V.Status.ValueDescription = Attr.Value
							case "svts":	V.Status.ValueTimeStamp = CDate(Attr.Value)
							case "scc":		V.Status.CenterColor = Attr.Value
							case "scv":		V.Status.CenterValue = Attr.Value
							case "scd":		V.Status.CenterDescription = Attr.Value
							case "sct":		V.Status.CenterTimeStamp = CDate(Attr.Value)
							case "slc":		V.Status.LeftColor = Attr.Value
							case "slv":		V.Status.LeftValue = Attr.Value
							case "sld":		V.Status.LeftDescription = Attr.Value
							case "slt":		V.Status.LeftTimeStamp = CDate(Attr.Value)
							case "sup":		V.Status.UpdateID = CDbl(Attr.Value)
											if V.Status.UpdateID > VehicleMaxUpdateID then VehicleMaxUpdateID = V.Status.UpdateID
							case "smup":	
											if CDbl(Attr.Value) > VehicleMaxUpdateID then VehicleMaxUpdateID = CDbl(Attr.Value)
							case "tdid":	V.Temperature.DeviceID = Clng(Attr.Value)
							case "tv":		V.Temperature.Value = CDbl(Attr.Value)
							case "tt":		V.Temperature.TimeStamp = CDate(Attr.Value)
							case "tup":		V.Temperature.UpdateID = CDbl(Attr.Value)
											if V.Temperature.UpdateID > VehicleMaxUpdateID then VehicleMaxUpdateID = V.Temperature.UpdateID
							case "mdid":	V.Message.DeviceID = Clng(Attr.Value)
							case "mt":		V.Message.Value = Attr.Value
							case "mts":		V.Message.TimeStamp = CDate(Attr.Value)
							case "mup":		V.Message.UpdateID = Cdbl(Attr.Value)
											if V.Message.UpdateID > VehicleMaxUpdateID then VehicleMaxUpdateID = V.Message.UpdateID
							'case "adid":	V.Alert.DeviceID = Clng(Attr.Value)
							case "av":		V.Alert.Value = Attr.Value
							case "at":		V.Alert.TimeStamp = CDate(Attr.Value)
							'case "aup":		V.Alert.UpdateID = Cdbl(Attr.Value)
							'				if V.Alert.UpdateID > VehicleMaxUpdateID then VehicleMaxUpdateID = V.Alert.UpdateID
							case "vdid":	V.Voice.DeviceID = Clng(Attr.Value)
							case "vv":		V.Voice.Value = Attr.Value
							case "vt":		V.Voice.TimeStamp = CDate(Attr.Value)
							case "vup":		V.Voice.UpdateID = Cdbl(Attr.Value)
											if V.Voice.UpdateID > VehicleMaxUpdateID then VehicleMaxUpdateID = V.Voice.UpdateID
						end select
						if err then
'							Alert(Attr.Name & "=" & Attr.Value)
'							Alert(Err.Description & vbcrl & Err.Source & vbcrlf & "VID = " & VID & " PDID=" & PDID)
							Err.Clear
						end if
					next
					If V.Internal Then
						Set InternalVehicles(V.ID) = V
					Else
						Set CommunityVehicles(V.ID) = V
					End if

					if ChangeSpeed then P.Speed = CLng("0")

					Call V.Update()

					if Updating then
						for each key in UpdatePages.Keys
							on error resume next
							Call UpdatePages(key).Update(V)
							if Err then
								window.status = "Page with ID " & key & " is not available." & vbcrlf & " Removing from update list." & vbcrlf & Err.Description & ":" & Err.Source
								if key <> "MAP" and key <> "GEOFENCE" then
									UpdatePages.Remove(key)
								end if
							Err.Clear
							end if
						Next
					end if
					if err then
						window.status = Err.Description & vbcrl & Err.Source & vbcrlf & "VID = " & VID & " PDID=" & PDID
						Err.Clear
					end if
				MaxCounter = MaxCounter - 1
				if VehicleNodeIndex >= VehicleNodes.Length - 1 then
					VehicleNodeIndex = Empty
					VehicleXMLDoc = Empty
					MaxCounter = 0
				end if
				if MaxCounter = 0 then
					exit for
				end if
			next
	End Sub

	Private Sub ReadEmployees()
		on error resume next
		if not isEmpty(EmployeeUpdateTimer) then
			if Timer - EmployeeUpdateTimer < UpdateInterval then
				Exit Sub
			end if
		end if
		EmployeeUpdateTimer = Timer
		counter = 0
		URL = Data.Objects("session").URL_Summary & "Employee?"
		URL = URL & "UpdateID=" & EmployeeMaxUpdateID
		'Alert(URL)
		Window.Status = "1) Getting employee data at " & now() & " starting from " & EmployeeMaxUpdateID
		set XMLDoc = Tools.GetXml(URL)
		'Alert(XMLDoc.xml)
		Data.Objects("session").TimeStamp = CDate(XMLDoc.selectSingleNode("//TimeStamp").GetAttribute("value"))
		Set nodes = XMLDoc.selectNodes("//T")
		if nodes.Length > 0 then
			Window.Status = nodes.Length & " employee updates received at " & now()
			for each Node in nodes
				Internal = (Node.GetAttribute("I") = "1")
				on error resume next
				EID = Clng(Node.GetAttribute("EID"))

				if Internal then
					if InternalEmployees.Exists(EID) then
						Set E = InternalEmployees.Item(EID)
						Call E.ClearValues(Node)
					else
						Set E = New EmployeeSummaryClass
					End if
				else
					if CommunityEmployees.Exists(EID) then
						Set E = CommunityEmployees.Item(EID)
						Call E.ClearValues(Node)
					else
						Set E = New EmployeeSummaryClass
					End if
				end if				
				
				if isNull(Node.GetAttribute("PDID")) then
					PDID = E.Position.DeviceID
				else
					PDID = Clng(Node.GetAttribute("PDID"))
				End If
				If not EPositions.Exists(PDID) Then
					Set EPositions(PDID) = New PositionClass
				End If
				Set P = EPositions.Item(PDID)
					
				Set E.Position = P
				if not Employees.Exists(EID) then
					Set Employees(EID) = New EmployeeClass
				End If

				Set E.Employee = Employees.Item(EID)
				Counter = Counter + 1
					For Each Attr In Node.Attributes
						Select Case lcase(Attr.Name)
						
							case "i":		E.Internal = (Attr.Value = "1")
							Case "g":		E.GradeTypeID = Clng(Attr.Value)
							Case "eid":		E.ID = Clng(Attr.Value)
							Case "cid":		E.Employee.CompanyID = Clng(Attr.Value)
							Case "eg":		E.Employee.Gender = Attr.Value
							Case "ecn":		E.Employee.CommunityName = Attr.Value
							Case "en":		E.Employee.Name = Attr.Value
							Case "esn":		E.Employee.ShortName = Attr.Value
							Case "vid":		E.Employee.VehicleID = CLng(Attr.Value)

							Case "sumup":
											if CDbl(Attr.value) > EmployeeMaxUpdateID then EmployeeMaxUpdateID = CDbl(Attr.Value)

							Case "hltid":	E.LandmarkTypeID = CLng(Attr.Value)
							Case "hln":		E.LandMarkName = Attr.Value
							Case "hsh":		E.ShiftDescription = Attr.Value
							Case "hst":		E.StartTime = CDate(Attr.Value)
							Case "hrst":	E.RegisterStartTime = CDate(Attr.Value)
							Case "het":		E.EndTime = CDate(Attr.Value)
							Case "hret":	E.RegisterEndTime = CDate(Attr.Value)

							case "eup":		E.UpdateID = Cdbl(Attr.Value)
											if E.UpdateID > EmployeeMaxUpdateID then EmployeeMaxUpdateID = E.UpdateID
							case "pdid":	P.DeviceID = Clng(Attr.Value)

							case "prx":		P.PrevMapX = Clng(Attr.Value)
							case "pry":		P.PrevMapY = Clng(Attr.Value)
							case "pstl":		P.Stealth = Attr.Value

							case "pds":		E.Destination.Position = Attr.Value
							case "pdcc":	E.Destination.CountryCode = Attr.Value
							case "pdsx":	E.Destination.MapX = Clng(Attr.Value)
							case "pdsy":	E.Destination.MapY = Clng(Attr.Value)
							case "pdlat":	E.Destination.Lat = Cdbl(Attr.Value)
							case "pdlon":	E.Destination.Lon = Cdbl(Attr.Value)
							case "pdst":	E.Destination.ETA = CDate(Attr.Value)
							case "pdar":	E.Destination.ArrivalTime = CDate(Attr.Value)
							Case "pdup":
											if CDbl(Attr.value) > EmployeeMaxUpdateID then EmployeeMaxUpdateID = CDbl(Attr.Value)

							case "px":		P.MapX = Clng(Attr.Value)
							case "py":		P.MapY = Clng(Attr.Value)
							case "lat":		P.Lat = Cdbl(Attr.Value)
							case "lon":		P.Lon = Cdbl(Attr.Value)
							case "ps":		P.Speed = Clng(Attr.Value)
							case "pps":		P.PeakSpeed = Clng(Attr.Value)
							case "pd":		P.Direction = Attr.Value
							case "po":		P.OdoMeter = Cdbl(Attr.Value)
							case "pp":		P.Position = Attr.Value
							case "pcc":		P.CountryCode = Attr.Value
							case "pbcc":	P.BigCountryCode = Attr.Value
							case "pst":		P.State = Attr.Value
											P.StateColor = Data.Objects("statecolors").item(Attr.Value)
											P.StateDescription = Data.Objects("statedescriptions").item(Attr.Value)
											ChangeSpeed = False
											Select case Attr.Value
												case "F":	ChangeSpeed = True
												case "S":	ChangeSpeed = True
												case "T":	ChangeSpeed = True
												case "U":	ChangeSpeed = True
											end select
							case "pas":		P.AverageSpeed = CDbl(Attr.Value)
							case "pig":		P.Ignition = CInt(Attr.Value)
							case "pt":		P.TimeStamp = CDate(Attr.Value)
							case "pup":		P.UpdateID = Cdbl(Attr.value)
											if P.UpdateID > EmployeeMaxUpdateID then EmployeeMaxUpdateID = P.UpdateID
							case "sdid":	E.Status.DeviceID = Clng(Attr.Value)
							case "st":		E.Status.TypeID = Clng(Attr.Value)
							case "sa":		E.Status.Area = Attr.Value
							case "sc":		E.Status.Clear = (Attr.Value = "true")
							case "sv":		E.Status.Value = Clng(Attr.Value)
							case "su":		E.Status.Undefined = (Attr.Value = "true")
							case "sts":		E.Status.TimeStamp = CDate(Attr.Value)
							case "svt":		E.Status.ValueText = Attr.Value
							case "svd":		E.Status.ValueDescription = Attr.Value
							case "svts":	E.Status.ValueTimeStamp = CDate(Attr.Value)
							case "scc":		E.Status.CenterColor = Attr.Value
							case "scv":		E.Status.CenterValue = Attr.Value
							case "scd":		E.Status.CenterDescription = Attr.Value
							case "sct":		E.Status.CenterTimeStamp = CDate(Attr.Value)
							case "slc":		E.Status.LeftColor = Attr.Value
							case "slv":		E.Status.LeftValue = Attr.Value
							case "sld":		E.Status.LeftDescription = Attr.Value
							case "slt":		E.Status.LeftTimeStamp = CDate(Attr.Value)
							case "sup":		E.Status.UpdateID = CDbl(Attr.Value)
											if E.Status.UpdateID > EmployeeMaxUpdateID then EmployeeMaxUpdateID = E.Status.UpdateID
							case "tdid":	E.Temperature.DeviceID = Clng(Attr.Value)
							case "tv":		E.Temperature.Value = CDbl(Attr.Value)
							case "tt":		E.Temperature.TimeStamp = CDate(Attr.Value)
							case "tup":		E.Temperature.UpdateID = CDbl(Attr.Value)
											if E.Temperature.UpdateID > EmployeeMaxUpdateID then EmployeeMaxUpdateID = E.Temperature.UpdateID
							case "mdid":	E.Message.DeviceID = Clng(Attr.Value)
							case "mt":		E.Message.Value = Attr.Value
							case "mts":		E.Message.TimeStamp = CDate(Attr.Value)
							case "mup":		E.Message.UpdateID = Cdbl(Attr.Value)
											if E.Message.UpdateID > EmployeeMaxUpdateID then EmployeeMaxUpdateID = E.Message.UpdateID
							case "adid":	E.Alert.DeviceID = Clng(Attr.Value)
							case "av":		E.Alert.Value = Attr.Value
							case "at":		E.Alert.TimeStamp = CDate(Attr.Value)
							case "aup":		E.Alert.UpdateID = Cdbl(Attr.Value)
											if E.Alert.UpdateID > EmployeeMaxUpdateID then EmployeeMaxUpdateID = V.Alert.UpdateID
							case "vdid":	E.Voice.DeviceID = Clng(Attr.Value)
							case "vv":		E.Voice.Value = Attr.Value
							case "vt":		E.Voice.TimeStamp = CDate(Attr.Value)
							case "vup":		E.Voice.UpdateID = Cdbl(Attr.Value)
											if E.Voice.UpdateID > EmployeeMaxUpdateID then EmployeeMaxUpdateID = E.Voice.UpdateID
						end select
						if err then
							'Alert(Err.Description & vbcrl & Err.Source & vbcrlf & "VID = " & VID & " PDID=" & PDID)
							Err.Clear
						end if
					next
					If E.Internal Then
						Set InternalEmployees(E.ID) = E
					Else
						Set CommunityEmployees(E.ID) = E
					End if

					if ChangeSpeed then P.Speed = CLng("0")
					Call E.Update()

					if Updating then
						for each key in UpdatePages.Keys
							on error resume next
							Call UpdatePages(key).Update(E)
							if Err then
								window.status = "Page with ID " & key & " is not available." & vbcrlf & " Removing from update list." & vbcrlf & Err.Description & ":" & Err.Source
								if key <> "MAP" and key <> "GEOFENCE" then
									UpdatePages.Remove(key)
								end if
							Err.Clear
							end if
						Next
					end if
					if err then
						window.status = Err.Description & vbcrl & Err.Source & vbcrlf & "VID = " & VID & " PDID=" & PDID
						Err.Clear
					end if
			next
		else
			Window.Status = "No new employee data available at " & now()
		End If
		'Alert(InternalEmployees.Count & "," & CommunityEmployees.Count & "," & Positions.Count)
	End Sub
End Class

Class DestinationClass
	Public	TypeName
	Public	DeviceID, StopTypeID, StopID, DeviceTypeID, Position, CountryCode, MapX, MapY, ETA, ArrivalTime
	Public	SpaceTypeID, SpaceAmount, MoreStops, Comments
	Public	Lat, Lon

	Private Sub Class_Initialize()
		TypeName = "DestinationClass"
		DeviceID = Empty
		DeviceTypeID = Empty
		Position = Empty
		CountryCode = Empty
		StopTypeID = Empty
		StopID = Empty
		Lat = Empty
		Lon = Empty
		MapX = Empty
		MapY = Empty
		ETA = Empty
		ArrivalTime = Empty
		SpaceTypeID = Empty
		SpaceAmount = Empty
		MoreStops = false
		Comments = Empty
	End Sub
End Class

Class VehicleSummaryClass
	Public	TypeName
	Public	Internal, GradeTypeID, ID, Vehicle, Position, Status, Temperature, Message, Alert, Voice
	Public	UpdatePages, UpdateClientID, UpdateID, UpdateTimeStamp
	Public	Destination, GifFileName, Description
	Public	AlertWindow
	
	Private Sub Class_Initialize()
		TypeName = "VehicleSummaryClass"
		Set Status = new StatusClass
		Set Temperature = new TemperatureClass
		Set Message = new MessageClass
		Set Alert = new AlertClass
		Set Voice = new VoiceClass
		Set Destination = new DestinationClass
		UpdateClientID = 0
		UpdateTimeStamp = Empty
	End Sub

	Public Sub RegisterPage(P)
		'Window.Status = "1"
		if isEmpty(UpdatePages) then
			'Window.Status = Window.Status & ",2"
			Set UpdatePages = CreateObject("Scripting.Dictionary")
		end if
		'Window.Status = Window.Status & ",3"
		UpdatePages.Add UpdateClientID, P
		'Window.Status = Window.Status & "=" & join(UpdatePages.keys,",")
		UpdateClientID = UpdateClientID + 1
		Call Update()
	End Sub

	Public Sub Update()
		if len(Alert.Value) then
			on error resume next
			WindowFeatures = "height=500,width=400,status=no,toolbar=no,menubar=no,location=no"
			url = "/ClickFind/Follow.aspx?SessionID=alert&ID=V" & ID & "&Internal=true"
			'url = "/clickfind/GeofenceEnter.html?ID=" & ID
			if isEmpty(AlertWindow) then
				set AlertWindow = window.open(url, "ALERT" & ID, WindowFeatures)
			else
				Err.Clear
				l = AlertWindow.screenLeft
				if Err then
					Err.Clear
					set AlertWindow = window.open(url, "ALERT" & ID, WindowFeatures)
				end if
			end if
		end if


		if not isEmpty(UpdatePages) then
			on error resume next
			for each Key in UpdatePages.Keys
				UpdatePages(Key).Update(Me)
				if Err then
					UpdatePages.Remove(Page)
					Err.Clear
				end if
			next
		end if
	End Sub

	Public Sub ClearValues(Node)
		if Node.GetAttribute("NEW") = "1" then
			Call Vehicle.ClearValues()
		end if
'		if Node.GetAttribute("PNEW") then
'			Call Position.ClearValues()
'		End if
		if Node.GetAttribute("SNEW") = "1" then
			Call Status.ClearValues()
		End if
		if Node.GetAttribute("TNEW") = "1" then
			Call Temperature.ClearValues()
		End if
		if Node.GetAttribute("MNEW") = "1" then
			Call Message.ClearValues()
		End if
		if Node.GetAttribute("ANEW") = "1" then
			Call Alert.ClearValues()
		End if
		if Node.GetAttribute("VNEW") = "1" then
			Call Voice.ClearValues()
		End if
	End Sub

End Class

Class VehicleClass
	Public	TypeName
	Public	GroupCode, Internal, GradeTypeID, CompanyID, TypeID, CommunityName, Name, CountryCode, City ,EmployeeIDs, TrailerID, Attributes, UpdateID
	Public	OriginCity, OriginMapX, OriginMapY
	Public	OriginLat, OriginLon	

	Private Sub Class_Initialize()
		TypeName = "VehicleClass"
	End Sub

	Public Sub ClearValues()
		GroupCode = Empty
		CompanyID = Empty
		TypeID = Empty
		Name = Empty
		OriginCity = Empty
		OriginMapX = Empty
		OriginMapY = Empty
		OriginLat = Empty
		OriginLon = Empty
		CountryCode = Empty
		City = Empty
		EmployeeIDs = Empty
		TrailerID = Empty
		Attributes = Empty
		UpdateID = Empty
	End Sub
End Class

Class EmployeeClass
	Public	TypeName
	Public	CompanyID, Gender, CommunityName, Name, ShortName, VehicleID, Attributes

	Private Sub Class_Initialize()
		TypeName = "EmployeeClass"
	End Sub

	Public Sub ClearValues()
		CompanyID = Empty
		Gender = Empty
		CommunityName = Empty
		Name = Empty
		ShortName = Empty
		VehicleID = Empty
		Attributes = Empty
	End Sub
End Class

Class EmployeeSummaryClass
	Public	TypeName
	Public	Internal, GradeTypeID, ID, Employee, Position, Status, Temperature, Message, Alert, Voice
	Public	UpdatePages, UpdateClientID, UpdateID, UpdateTimeStamp
	Public	LandMarkTypeID, LandMarkName, ShiftDescription, StartTime, EndTime, RegisterStartTime, RegisterEndTime
	Public	Destination

	Private Sub Class_Initialize()
		TypeName = "EmployeeSummaryClass"
		Set Status = new StatusClass
		Set Temperature = new TemperatureClass
		Set Message = new MessageClass
		Set Alert = new AlertClass
		Set Voice = new VoiceClass
		Set Destination = new DestinationClass
		LandMarkTypeID = Empty
		UpdateClientID = 0
		UpdateTimeStamp = Empty
	End Sub

	Public Sub RegisterPage(P)
'		Window.Status = "1"
		if isEmpty(UpdatePages) then
'			Window.Status = Window.Status & ",2"
			Set UpdatePages = CreateObject("Scripting.Dictionary")
		end if
'		Window.Status = Window.Status & ",3"
		UpdatePages.Add UpdateClientID, P
'		Window.Status = Window.Status & "=" & join(UpdatePages.keys,",")
		UpdateClientID = UpdateClientID + 1
		Call Update()
	End Sub

	Public Sub Update()
		if not isEmpty(UpdatePages) then
			on error resume next
			for each Key in UpdatePages.Keys
				UpdatePages(Key).Update(Me)
				if Err then
					UpdatePages.Remove(Page)
					Err.Clear
				end if
			next
		end if
	End Sub

	Public Sub ClearValues(Node)
		if Node.GetAttribute("NEW") = "1" then
			Call Employee.ClearValues()
		end if
		if Node.GetAttribute("HNEW") = "1" then
			LandMarkTypeID = Empty
			LandMarkName = Empty
			ShiftDescription = Empty
			StartTime = Empty
			EndTime = Empty
			Call Employee.ClearValues()
		end if
		if Node.GetAttribute("SNEW") = "1" then
			Call Status.ClearValues()
		End if
		if Node.GetAttribute("TNEW") = "1" then
			Call Temperature.ClearValues()
		End if
		if Node.GetAttribute("MNEW") = "1" then
			Call Message.ClearValues()
		End if
		if Node.GetAttribute("ANEW") = "1" then
			Call Alert.ClearValues()
		End if
		if Node.GetAttribute("VNEW") = "1" then
			Call Voice.ClearValues()
		End if
	End Sub
End Class

Class TrailerSummaryClass
	Public	TypeName
	Public	ID, CompanyID
	Public	Position, Status, Temperature, Message, Alert, Voice

	Private Sub Class_Initialize()
			TypeName = "railerSummaryClass"
			Set Position = new PositionClass
			Set Status = new StatusClass
			Set Temperature = new TemperatureClass
			Set Message = new MessageClass
			Set Alert = new AlertClass
			Set Voice = new VoiceClass
	End Sub
End Class

Class PositionClass
	Public	TypeName
	Public	Landmark
	Public	DeviceID, DeviceTypeID, MapX, MapY, Speed, PeakSpeed, Direction, OdoMeter, PrevMapX, PrevMapY
	Public	Position, CountryCode, BigCountryCode, Ignition
	Public	State, StateDescription, StateColor, AverageSpeed, TimeStamp, UpdateID
	Public	Stealth
	Public	Lat, Lon, PrevLat, PrevLon

	Private Sub Class_Initialize()
		TypeName = "PositionClass" 
	End Sub

	Public Sub ClearValues()
		MapX = Empty
		MapY = Empty
		Lat = Empty
		Lon = Empty
		Speed = Empty
		PeakSpeed = Empty
		Direction = Empty
		OdoMeter = Empty
		Position = Empty
		CountryCode = Empty
		BigCountryCode = Empty
		State = Empty
		StateDescription = Empty
		StateColor = Empty
		Ignition = Empty
		AverageSpeed = Empty
		TimeStamp = Empty
		UpdateID = Empty
		PrevMapX = Empty
		PrevMapY = Empty
		PrevLat = Empty
		PrevLon = Empty
		Landmark = Empty
		Stealth = Empty
	End Sub
End Class

Class StatusClass
	Public	TypeName
	Public	DeviceID, TypeID, Area, Clear, Value, Undefined, TimeStamp
	Public	ValueText, ValueDescription, ValueTimeStamp
	Public	CenterColor, CenterValue, CenterDescription, CenterTimeStamp
	Public	LeftColor, LeftValue, LeftDescription, LeftTimeStamp
	Public	UpdateID

	Private Sub Class_Initialize()
		TypeName = "StatusClass"
	End Sub

	Public Sub ClearValues()
		DeviceID = Empty
		TypeID = Empty
		Area = Empty
		Clear = Empty
		Value = Empty
		Undefined = Empty
		TimeStamp = Empty
		ValueText = Empty
		ValueDescription = Empty
		ValueTimeStamp = Empty
		CenterColor = Empty
		CenterValue = Empty
		CenterDescription = Empty
		CenterTimeStamp = Empty
		LeftColor = Empty
		LeftValue = Empty
		LeftDescription = Empty
		LeftTimeStamp = Empty
	End Sub
End Class

Class TemperatureClass
	Public	TypeName
	Public	DeviceID, Value, TimeStamp, UpdateID

	Private Sub Class_Initialize()
		TypeName = "TemperatureClass"
	End Sub

	Public Sub ClearValues()
		DeviceID = Empty
		Value = Empty
		TimeStamp = Empty
		UpdateID = Empty
	End Sub
End Class

Class MessageClass
	Public	TypeName
	Public	DeviceID, Value, TimeStamp, UpdateID

	Private Sub Class_Initialize()
		TypeName = "MessageClass"
	End Sub

	Public Sub ClearValues()
		DeviceID = Empty
		Value = Empty
		TimeStamp = Empty
		UpdateID = Empty
	End Sub
End Class

Class AlertClass
	Public	TypeName
	Public 	DeviceID, Value, TimeStamp, UpdateID
	
	Private Sub Class_Initialize()
		TypeName = "AlertClass"
	End Sub

	Public Sub ClearValues()
		DeviceID = Empty
		Value = Empty
		TimeStamp =  Empty
		UpdateID = Empty
	End Sub
	
End Class

Class VoiceClass
	Public	TypeName
	Public	DeviceID, Value, TimeStamp, UpdateID

	Private Sub Class_Initialize()
		TypeName = "VoiceClass"
	End Sub

	Public Sub ClearValues()
		DeviceID = Empty
		Value = Empty
		TimeStamp = Empty
		UpdateID = Empty
	End Sub

End Class

Class CargoSummaryClass
	Public	TypeName
	public	ID, GradeTypeID, Internal, CompanyID, CompanyName, CompanyPrefix, LoginID
	public	VehicleTypeID, VehicleSubTypeID
	public	CraneTypeID, CraneLift, CraneLength
	public	Tools, Accessoires, Licenses
	public	PickupPosition, PickupCountryCode, PickupMapX, PickupMapY
	public	PickupStartTime, PickupEndTime
	public	DeliveryPosition, DeliveryCountryCode, DeliveryMapX, DeliveryMapY
	public	DeliveryStartTime, DeliveryEndTime
	public	MaterialTypeID, MaterialSubTypeID, MaterialCode
	public	FreightType
	public	Weight, Length, Height
	public	Quantity, PackageTypeID
	public	ShortRemark, LongRemark
	public	UpdateID, Deleted
	'CI,CC,CL,CVT,CVST,CRT,CRL,CRLT,CT,CA,CLC,CPP,CPCC,CPX,CPY,CPS,CPE,CDP,CDCC,CDX,CDY,CDS,CDE,CMT,CMST,CFT,CW,CLT,CH,CQ,CPT,CSR,CLR,CUP,CD

	public	DeliveryLat, DeliveryLon, PickupLat, PickupLon

	Private Sub Class_Initialize()
		TypeName = "CargoSummaryClass"
	End Sub
End Class

Class CompanySummaryClass
	Public	TypeName
	Public	ID, Internal, Name, Prefix, TypeID, CountryCode, MapX, MapY, Grade, GradeTypeID, HasLogo, UpdateID
	Public	Director, Street, PostalCode, AddressInfo , PublishedTel, PublishedEMail, Tel, Fax, eMail, City, FullName
	Public	Lat, Lon

	Private Sub Class_Initialize()
		TypeName = "CompanySummaryClass"
	End Sub
end Class

Class StopSummaryClass
	Public	TypeName
	Public	ID, Internal, CombinationID, CombinationName, LastSequence
	Public	CompanyID, LoginID
	Public	Sequence, TypeID, ETA, SpaceAmount, SpaceTypeID
	Public	CountryCode, Position, MapX, MapY, UpdateID
	Public	CheckInTime, CheckOutTime, ExecutorTypeCode, ExecutorID
	Public	Comments, Available

	Public	Lat, Lon

	Private Sub Class_Initialize()
		TypeName = "StopSummaryClass"
	End Sub
End Class
