<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:xxx="urn:my-namespace">

	<msxsl:script language="VBScript" implements-prefix="xxx">

		' ***********************************************************************************************************************
		' Global variables
		' ***********************************************************************************************************************
		Dim CellPrecision(100)
		Dim CellDataType(100)
		
		Function StoreCellPrecision( ByVal Column, ByVal Precision, ByVal DataType  )
			StoreCellPrecision = 1
			CellPrecision(Column) = Precision
			CellDataType(Column) = DataType
		End Function
		
		Function GetCellType( ByVal Column )
			GetCellType = CellDataType( Column )
		End Function

		Function GetCellPrecision( ByVal Column )
			GetCellPrecision = CellPrecision( Column )
		End Function
		
		
	</msxsl:script>

	<xsl:template match="/*">
		<html>
		<title>Method Output</title>
		<style type="text/css">
			h1 { color: blue; font-size: 16pt; font-style: italic}
			h2 { font-size: 14pt; font-style: bold}
			h3 { font-size: 18pt; font-style: bold}
		</style>
		<body>
			<h3>Method Report</h3>
			<xsl:apply-templates/>
		</body>
		</html>		
	</xsl:template>
	
	<xsl:template match="FieldList">
		<h1><xsl:value-of select="Title"/></h1>
		<table>
			<th width="50"/>
			<th width="200"/>
			<th width="200"/>
			<xsl:apply-templates select="./Field"/>
		</table>
		<xsl:apply-templates select="./Table"/>
	</xsl:template>

	<xsl:template match="Table">
		<xsl:if test="count(.//Row)>0">
				<p/>
				<table>
					<th width="50"/>
					<th width="200"/>
					<tr>
						<td/>
						<td><h2><xsl:value-of select="Title"/></h2></td>
					</tr>
				</table>
				<table>
					<th width="50"/>
					<th width="50"/>
					<xsl:apply-templates select="Columns"/>
					<xsl:apply-templates select="Rows"/>
				</table>
		</xsl:if>
	</xsl:template>

	<xsl:template match="Columns">
		<xsl:for-each select="Column">
		
			<xsl:variable name="Type" select="@type"/>
			<xsl:choose>
				<xsl:when test="$Type = 'Float'">
					<xsl:variable name="Precision" select="@precision"/>
					<xsl:variable name="Temp" select="xxx:StoreCellPrecision( position(), number($Precision), string($Type) )"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:variable name="Precision" select="0"/>
					<xsl:variable name="Temp" select="xxx:StoreCellPrecision( position(), 0, string($Type) )"/>
				</xsl:otherwise>
			</xsl:choose>
			
			<th align="left" width="100">
				<xsl:value-of select="@name"/>
				
				<xsl:variable name="TheUnits" select="@units"/>
				<xsl:if test="string-length($TheUnits)>0">
					<br/>
					<xsl:text>(</xsl:text>
					<xsl:value-of select="@units"/>
					<xsl:text>)</xsl:text>
				</xsl:if>
			</th>
			
		</xsl:for-each>
	</xsl:template>

	<xsl:template match="Rows">
		<xsl:for-each select="Row">
			<tr>
				<td/>
				<td><xsl:number format="1.  "/></td>

				<xsl:for-each select="Cell">
					<xsl:variable name="Column" select="position()"/>
					<td>
						<xsl:call-template name="Format-Cell">
							<xsl:with-param name="Type" select="xxx:GetCellType( $Column )"/>
							<xsl:with-param name="Precision" select="xxx:GetCellPrecision( $Column )"/>
						</xsl:call-template>
					</td>
				</xsl:for-each>
			</tr>
		</xsl:for-each>
	</xsl:template>

	<xsl:template match="Field">
		<tr>
			<td/>
			<td><xsl:value-of select="@name"/><xsl:text>:</xsl:text></td>
			<td>
				<xsl:call-template name="Format-Value"/>
				<xsl:text> </xsl:text>
				<xsl:value-of select="@units"/>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template name="Format-Value">
	
		<xsl:variable name="Type" select="@type"/>
		<xsl:variable name="Precision" select="@precision"/>
		<xsl:variable name="Value" select="."/>

		<b>		
		<xsl:choose>
			<xsl:when test="$Type='Int'">
				<xsl:value-of select="format-number($Value,'0')"/>
			</xsl:when>
			<xsl:when test="$Type='Float'">
				<xsl:variable name="NumberFormat">
					<xsl:choose>
						<xsl:when test="$Precision=0">
							<xsl:text>0</xsl:text>
						</xsl:when>
						<xsl:when test="$Precision=1">
							<xsl:text>0.0</xsl:text>
						</xsl:when>
						<xsl:when test="$Precision=2">
							<xsl:text>0.00</xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>0.000</xsl:text>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>					
				<xsl:choose>
					<xsl:when test="contains($Value,',')">
						<xsl:value-of select="$Value"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="format-number($Value,$NumberFormat)"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="$Type='String'">
				<xsl:value-of select="$Value"/>
			</xsl:when>
		</xsl:choose>
		</b>		
		
	</xsl:template>

	<xsl:template name="Format-Cell">
	
		<xsl:param name="Type"/>
		<xsl:param name="Precision"/>
		<xsl:variable name="Value" select="."/>

		<b>		
		<xsl:choose>
			<xsl:when test="$Type='String'">
				<xsl:value-of select="$Value"/>
			</xsl:when>
			<xsl:when test="$Type='Int'">
				<xsl:choose>
					<xsl:when test="$Value = 'Initial'">
						<xsl:value-of select="$Value"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="number($Value)"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="$Type='Float'">
				<xsl:variable name="NumberFormat">
					<xsl:choose>
						<xsl:when test="$Precision=0">
							<xsl:text>0</xsl:text>
						</xsl:when>
						<xsl:when test="$Precision=1">
							<xsl:text>0.0</xsl:text>
						</xsl:when>
						<xsl:when test="$Precision=2">
							<xsl:text>0.00</xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>0.000</xsl:text>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>					
				<xsl:choose>
					<xsl:when test="$Value = 'Initial'">
						<xsl:value-of select="$Value"/>
					</xsl:when>
					<xsl:when test="contains($Value,',')">
						<xsl:value-of select="$Value"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="format-number($Value,$NumberFormat)"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
		</xsl:choose>
		</b>		
		
	</xsl:template>

</xsl:stylesheet>
