<?xml version="1.0"?>
<!--
	WMIBook.wsc - Utility components written for WMI Book
	Copyright 2000 Matthew M Lavy
	THIS SCRIPT COMPONENT COMES WITH NO WARRANTY WHATSOEVER
-->
<component>
	<registration
		progid="WMIBook.SimpleReport"
		description="Create simple HTML reports"
		classid="{9a037d20-7c48-4acd-a16c-fc716f4b58fc}"
		version="1.0"
	/>
	
	<public>
		<property name="comment"/>
		<property name="title"/>
		<property name="type"/>
		<method name="addEntry">
		        <parameter name="index"/>
			<parameter name="short"/>
			<parameter name="full"/>
		</method>
		<method name="addSection">
			<parameter name="title"/>
		</method>
		<method name="saveAs">
			<parameter name="filename"/>
		</method>
		<method name="browse">
		        <parameter name="filename"/>
		</method>
		<method name="getHTML"/>
	</public>
	
	<script language="VBScript">
	<![CDATA[
		option explicit
		const BLOCK_SZ = 20
		const SR_SECTION = "X-SECTION-X"

		Class Element
		        public index
			public short
			public full
		End Class
		
		Dim comment 'public
		Dim title 'public
		Dim items()
		Dim currentItem
		Dim fullHTML
		Dim hasChanged 'if HTML needs recompiling
		
		fullHTML = ""
		comment = ""
		title = ""
		ReDim items(BLOCK_SZ)
		
		hasChanged = 0
		currentItem = 0
		
		Function addEntry(index,short,full)
			dim szItems
			szItems = UBound(items) - LBound(items) + 1
			if currentItem >= szItems Then
				ReDim Preserve items(szItems + BLOCK_SZ)
			end If
			Set items(currentItem) = new Element
			items(currentItem).index = index
			items(currentItem).short = short
			items(currentItem).full = full
			currentItem = currentItem + 1
			hasChanged = -1
		End Function
		
		Function addSection(title)
			addEntry title,SR_SECTION,SR_SECTION
		End Function
		
		Function saveAs(filename)
			Dim fs,stream
			Set fs = CreateObject("Scripting.FileSystemObject")
			Set stream = fs.createTextFile(filename, -1)
			stream.write(getHTML())
			stream.close
			Set stream = Nothing
			Set fs = Nothing
		End Function
		
		Function browse(filename)
			Dim ie
			saveAs filename 
			Set ie = CreateObject("InternetExplorer.Application")
			ie.Navigate("file:///" & filename)
			ie.visible = true
			Set ie = Nothing
		End Function
		
		Function getHTML()
			Dim i 'counter
			if hasChanged = -1 Then
				
				'build header
				fullHTML = "<HTML><HEAD><TITLE>" & title & "</TITLE></HEAD>" &_
					vbNewLine & "<BODY><H1>" & title & "</H1>" &_
					vbNewLine & comment & "<BL>" & vbNewLine
				
				'build index
				i = LBound(items)
				While i <= UBound(items) And Not IsEmpty (items(i))
				        If items(i).full = SR_SECTION Then
				        	fullHTML = fullHTML & "</BL><HR><H2>" & _
				        	    items(i).index & "</H2><BL>" & vbNewLine
				        Else
						fullHTML = fullHTML & "<LI><A HREF=""#item" & i & """>" &_
							items(i).index & "</A>" & vbNewLine
					End If
					i = i + 1
				Wend
				fullHTML = fullHTML & "</BL><BR>" & vbNewLine
				
				'build contents
				i = LBound(items)
				While i <= UBound(items) And Not IsEmpty(items(i))
					If items(i).full = SR_SECTION Then
						fullHTML = fullHTML & "<HR><H2>" & items(i).index & _
						  "</H2>" & vbNewLine
					Else
						fullHTML = fullHTML & "<H3><A NAME=""#item" & i & """>" &_
						  items(i).short & "</A></H3>" &_
						  vbNewLine & items(i).full & "<p>" & vbNewLine
					End If
					i = i + 1
				Wend
				
				'build footer
				fullHTML = fullHTML + "<HR>Report as at " & Now & "</BODY></HTML>"
				hasChanged = 0
			end If
			getHTML = fullHTML
		End Function
	]]>
	</script>
	
</component>
