'VBScript Function file for the 
'DXtreme Station Log Active Report Viewer
'Trademark and Copyright DXtreme Software 1995-2003

<!--<BR>
	option explicit
	Dim s_lngStaticCurrentPage
	Dim s_asPreferencesArray
	Dim s_lngRecordsPerPage, s_optColumnIDer, s_sngFontColor
	Dim s_sSortMode

	'Note: The oColumnIdentifier and oColumnIdContent objects
	'must be global!
	Dim oColumnIdentifier
	Dim oColumnIdContent
	Set oColumnIdentifier = window.createPopup
	Set oColumnIdContent = oColumnIdentifier.document.body

	'Set the default number of records per page
	'in case there is no preferences cookie
	s_lngRecordsPerPage = 10

	'Initialize s_lngStaticCurrentPage to 1
	'This variable tracks the current page when
	'calling the paging functions.
	s_lngStaticCurrentPage = 1
	
	function window_onload()
		'Get the preferences data from the cookie on the user's system.
		window.status = "Please wait..." 
		Call getPreferencesData()
		if Clng(s_lngRecordsPerPage) < 1 then
			MsgBox "Before you can use the Active Report Viewer, cookies must be enabled in Internet Explorer and you must set your preferences."
			getPreferences()
		end if
		'Adjust the loop so that it displays the default number of records per page.
		'Starting with the third Select attribute, select all values whose index is
		'1) Less than the per-page value plus 1, and
		'2) Greater than or equal to 0.
		XSLstyle.XMLDocument.selectNodes("//@select")(2).value = "./*[index() $lt$ " & s_lngRecordsPerPage & " $and$ index() $ge$ 0]" 
		'Do the first transform and set the page properties for display.
		s_sSortMode="Click a column head to sort ascending or descending."
		transform()
		setPageCount()
		setColumnIdCaption()		
		window.status = "Completed" 
	end function
	
	function transform()
		'Transform the XML document to HTML and display the report within the <span>
		'element in the htm file by means of its innerHTML property.
		SpanReport.innerHTML = XMLdata.transformNode(XSLstyle.DocumentElement)
		'Display the number of records per page in the RecordsPerPage text box.
		RecordsPerPage.value = s_lngRecordsPerPage
		SortMode.innerHTML = s_sSortMode
	end function

	function redisplay(intCurrentPage)
		'Transform the document and display page information again.
		Dim strDisplay
		Dim intPageCount
		Dim intRecordCount	
		'Store page information for use after the transformation
		'because the information is otherwise discarded. 
		intPageCount = PageCount.innerHTML
		intRecordCount = RecordCount.innerHTML
		transform()						
		'Redisplay the page information from the variables set before
		'transform was called.
		PageCount.innerHTML = intPageCount
		RecordCount.innerHTML = intRecordCount
		CurrentPage.innerHTML = intCurrentPage
		setColumnIdCaption()
	end function

	function displayColumnID(strField)
		'Display the column ID in a popup window and
		'in the status bar.
		'Note: The oColumnIdentifier and oColumnIdContent objects
		'must be global!
   	 	dim leftWindow, upperWindow, sDisplay
		dim lenField, leftField, rightField, formattedField
		if instr(strField, "_") > 0 then
			lenField=len(strField)
			leftField = left(strField, instr(strField, "_")-1)
			rightField = right(strField, (lenField-instr(strField, "_")))
			formattedField = leftField & " " & rightField
		else
			formattedField = strField		
		end if

		window.status = formattedField 
		leftWindow = window.event.clientY + 15
	    	upperWindow = window.event.clientX + 15
		if s_optColumnIDer = 0 then
			sDisplay = "<div align=center style='position: absolute; top:0; left:0; width:100%; height:100%; border:1px solid black; filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=blue, EndColorStr=silver); padding:1px' ><p style='margin-top: 0%;padding-top: 0pt;font-family: Comic Sans MS;color: white;font-size: 10pt;margin-bottom: 0%;padding-bottom: 0pt;'>Column ID </p> <hr size='1' style='border:1px solid black;'><p style='margin-top: 0%;padding-top: 0pt;margin-bottom: 0%;padding-bottom: 0pt;font-family: Comic Sans MS;color: black;font-size: 10pt;'>" & formattedField  & "</p></div>"		
		    	oColumnIdContent.innerHTML = sDisplay
			oColumnIdentifier.show upperWindow, leftWindow, 150, 55, document.body
		end if			


	end function 

	function undisplayColumnID(strField)
		'Remove the popup window and clear the status bar.
		window.status = ""
		oColumnIdentifier.hide
	end function 

	function sortRecords(strField)
		'Change the sort order and redisplay the report.
		Dim sortField, sortOrder, lenField, leftField, rightField, formattedField
		window.status = "Please wait..." 
		'Create a reference to the order-by attribute.				
		set sortField = XSLstyle.XMLDocument.selectSingleNode("//@order-by")	
		'Check whether the report is already being sorted by passed strField. If it
		'is, perform a descending sort; otherwise, perform an ascending sort.
		if sortField.value = strField then 						
			'Sort descending by prefixing the field name with a minus character.
			sortField.value = "-" & sortField.value
			sortOrder = "Descending"
		else
			'Sort ascending by assigning the name of the field.						
			sortField.value = strField
			sortOrder = "Ascending"
		end if
		if instr(strField, "_") > 0 then
			lenField=len(strField)
			leftField = left(strField, instr(strField, "_")-1)
			rightField = right(strField, (lenField-instr(strField, "_")))
			formattedField = leftField & " " & rightField
			s_sSortMode = formattedField & " " & Chr(151) & " " & sortOrder 
		else
			s_sSortMode = strField & " " & Chr(151) & " " & sortOrder 
		end if
		'Redisplay the current page to sort it.
		redisplay(CurrentPage.innerHTML)								
		window.status = "Completed" 
	end function 

	function setRecordsPerPage()
		'Reset the number of records per page.
		dim X
		if isnumeric(RecordsPerPage.value) then
			if CLng(RecordsPerPage.value) > 0 then
				s_lngRecordsPerPage = Clng(RecordsPerPage.value)
				'Adjust the loop so that it displays the default number of records per page.
				XSLstyle.XMLDocument.selectNodes("//@select")(2).value = "./*[index() $lt$ " & s_lngRecordsPerPage & " $and$ index() $ge$ 0]" 
				'Do the first transform and set the page properties for display.
				transform()
				setPageCount()		
			else
				'If the entry was not greater than 0, inform the user and set s_sSafeToUpdate to False.
				MsgBox "Your entry (" & RecordsPerPage.value & ") is not greater than 0. Try again."
			end if 	
		else
			'If the entry is not numeric, inform the user.
			MsgBox "Your entry (" & RecordsPerPage.value & ") is not a number. Be sure to enter a number."
		end if
		setColumnIdCaption()
	end function

	function setSessionColumnIDer()
		'Toggles the Column IDer for the current session
		if s_optColumnIDer=0 then
			s_optColumnIDer=1
		else
			s_optColumnIDer=0
		end if		
		setColumnIdCaption()
	end function
	
	function setColumnIdCaption()
		'Sets the caption of the ColumnId button (called from a few places)
		if s_optColumnIDer = 0 then
			SessionColumnIDer.value="Disable IDer"
		else
			SessionColumnIDer.value="Enable IDer"
		end if
	end function

	function setPageCount()
		'Display the page count information in the appropriate
		'<span> elements when the window_onload event calls 
		'this function
		Dim intTotalRecords		
		PageCount.innerHTML = getNumberOfPages(intTotalRecords)
		RecordCount.innerHTML = intTotalRecords
		CurrentPage.innerHTML = 1 'Initialize to 1.
	end function

	function getNumberOfPages(intTotalRecords)	
		'Calculate and return the total number of records in the report
		'and total number of pages in the report.
		Dim curPages
		'Use the length property to get the record count (the length
		'property contains the total number of items in the collection).
		intTotalRecords = XMLdata.XMLDocument.selectNodes("/*/*").length
		'Now calculate the total number of pages.
		curPages = CCur(intTotalRecords/s_lngRecordsPerPage)
		if curPages = 0 then
			curPages=1
		end if			
		'Check for a decimal value. If found, store the just the integer value.
		if instr(curPages, ".") > 0 then
			curPages = CLng(left(curPages, instr(curPages, "."))) + 1
		elseif instr(curPages, ",") > 0 then
			curPages = CLng(left(curPages, instr(curPages, ","))) + 1
		end if
		getNumberOfPages = curPages
	end function

	function nextPage()
		Dim strDisplay
		Dim strDateRange	
		'Redisplay the next page if not already displaying the last page.
		if Clng(cstr(s_lngStaticCurrentPage) * s_lngRecordsPerPage) <  XMLdata.selectNodes("/*/*").length then
			'Update the current page variable to the next page number, select the
			'appropriate nodes, and redisplay.	
			s_lngStaticCurrentPage = Clng(s_lngStaticCurrentPage) + 1						
			'Starting with the third Select attribute, select all values whose INDEX value is
			'1) Less than the number of next page x the number of records per page, and
			'2) Greater than or equal to the NUMBER of records on the current page (Index-1).
			'For example, if you are displaying 5 records per page and you are moving from
			'the first page to the second page:
				's_lngStaticCurrentPage(2) * s_lngRecordsPerPage(5) = 10
				's_lngStaticCurrentPage - 1 (1) * s_lngRecordsPerPage(5) = 5
			'Report displays records with Index values of 5-9 (which translates to records
			'with IDs of 6 through 10).
				'1) Less than 10, and
				'2) Greater than or equal to 5.
			XSLstyle.XMLDocument.selectNodes("//@select")(2).value  = "./*[index() $lt$ " & cstr(s_lngStaticCurrentPage) * s_lngRecordsPerPage & " $and$ index() $ge$ " & (Clng(s_lngStaticCurrentPage) - 1) * s_lngRecordsPerPage & "]"
			redisplay(s_lngStaticCurrentPage)
		end if		
	
	end function

	function previousPage()
		Dim strDisplay
		Dim strDateRange	
		'Redisplay the previous page if not already displaying the first page.
		if s_lngStaticCurrentPage > 1 then
			'Update the current page variable to the previous page number, select the
			'appropriate nodes, and redisplay.	
			s_lngStaticCurrentPage = Clng(s_lngStaticCurrentPage) - 1						
			'Set the new nodes.	
			'For example, if you are displaying 5 records per page and you are moving from
			'the second page to the first page:
				's_lngStaticCurrentPage(1) * s_lngRecordsPerPage(5) = 5
				's_lngStaticCurrentPage - 1 (0) * s_lngRecordsPerPage(5) = 0
			'Report displays records with Index values of 0-4 (which translates to records
			'with IDs of 0 through 4).
				'1) Less than 5, and
				'2) Greater than or equal to 0.
			XSLstyle.XMLDocument.selectNodes("//@select")(2).value  = "./*[index() $lt$ " & cstr(s_lngStaticCurrentPage) * s_lngRecordsPerPage & " $and$ index() $ge$ " & (Clng(s_lngStaticCurrentPage) - 1) * s_lngRecordsPerPage & "]"				
			redisplay(s_lngStaticCurrentPage)
		end if
	end function

	function firstPage()
		'Redisplay the first page.
		s_lngStaticCurrentPage = 1
		'Set the new nodes.	
		'For example, if you are displaying 5 records per page and you are moving from
		'the third page to the first page:
			's_lngStaticCurrentPage(1) * s_lngRecordsPerPage(5) = 5
			's_lngStaticCurrentPage - 1 (0) * s_lngRecordsPerPage(5) = 0
		'Report displays records with Index values of 0-4 (which translates to records
		'with IDs of 0 through 4).
			'1) Less than 5, and
			'2) Greater than or equal to 0.
		XSLstyle.XMLDocument.selectNodes("//@select")(2).value  = "./*[index() $lt$ " & cstr(s_lngStaticCurrentPage) * s_lngRecordsPerPage & " $and$ index() $ge$ " & (Clng(s_lngStaticCurrentPage) - 1) * s_lngRecordsPerPage & "]"				
		redisplay(s_lngStaticCurrentPage)
	end function

	function lastPage()						
		Dim intTotalRecords
		'Update the current page variable to the last page number, select the
		'appropriate nodes, and redisplay.	
		s_lngStaticCurrentPage = getNumberOfPages(intTotalRecords)
		'Starting with the third Select attribute, select all values whose INDEX value is
		'1) Less than the number of next page x the number of records per page, and
		'2) Greater than or equal to the NUMBER of records on the current page (Index-1).
		'For example, if you are displaying 5 records per page, you have 18 records,
		'and you are moving to the last page from page 2:
			's_lngStaticCurrentPage(4) * s_lngRecordsPerPage(5) = 20
			's_lngStaticCurrentPage - 1 (3) * s_lngRecordsPerPage(5) = 15
		'Report displays records with Index values of 5-9 (which translates to records
		'with IDs of 16 through 18 [records with index values that are greater than 17
		'don't exist!).
			'1) Less than 20, and
			'2) Greater than or equal to 15.
		XSLstyle.XMLDocument.selectNodes("//@select")(2).value  = "./*[index() $lt$ " & cstr(s_lngStaticCurrentPage) * s_lngRecordsPerPage & " $and$ index() $ge$ " & (Clng(s_lngStaticCurrentPage) - 1) * s_lngRecordsPerPage & "]"
		redisplay(s_lngStaticCurrentPage)
	end function

	Sub getPreferences()
		'Show the Preferences dialog box
		window.showModalDialog "activereportviewerprefs.htm",0,"dialogWidth:450px;dialogHeight:325px;help:no;resizable:yes;status:no"
		'Call window_onload to redisplay after setting preferences.
		window_onload()
	End Sub

	Sub getPreferencesData()
		'Get the preferences data from the cookie on the user's system.
		dim sPreferencesCookie, X
		sPreferencesCookie = document.cookie
		if sPreferencesCookie <> "CP=null*" and sPreferencesCookie <> "" and sPreferencesCookie <> "null" then
			on error resume next
			s_asPreferencesArray = Split(sPreferencesCookie, "Z", -1, 1)
		Else
			ReDim s_asPreferencesArray(3)
			For X = 1 to 3
				if s_asPreferencesArray(X) = "" then
					s_asPreferencesArray(X) = "0"
				End If
			Next
		End If
		s_lngRecordsPerPage = Clng(s_asPreferencesArray(1))
		s_optColumnIDer = Clng(s_asPreferencesArray(2))
'Perhaps we'll add more later	
'		s_sngFontSize = Clng(s_asPreferencesArray(3)) + 8
	End Sub

'	Function GetRGBColor(iColorIndex)
'		Select Case iColorIndex
'			Case 0 'Black
'				GetRGBColor = RGB(0,0,0)
'			Case 1 'Blue
'				GetRGBColor = RGB(0,0,255)
'			Case 2 'Cyan
'				GetRGBColor = RGB(0,255,255)
'			Case 3 'Gray
'				GetRGBColor = RGB(204,204,204)
'			Case 4 'Green
'				GetRGBColor = RGB(0,255,0)
'			Case 5 'Red
'				GetRGBColor = RGB(255,0,0)
'			Case 6 'Teal
'				GetRGBColor = RGB(102,153,153)
'			Case 7 'White
'				GetRGBColor = RGB(255,255,255)
'			Case 8 'Yellow
'				GetRGBColor = RGB(255,255,0)
'		End Select
'	End function

-->
