﻿<xsl:transform  version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
				exclude-result-prefixes="xsl xs">
	<xsl:output method="html"/>
	<xsl:template match="rss/channel">
		<table border="1" cellpadding="2" cellspacing="0" width="100%">
			<tr bgcolor="#EEEEEE">
				<th>Sponsor</th>
				<th>Prize</th>
				<th>Quantity</th>
				<th>Retail</th>
				<th>Total</th>
			</tr>
			<xsl:variable name="currSponsor"></xsl:variable>
			<xsl:apply-templates select="item">
				<xsl:sort select="substring-before(substring-after(description, 'Quantity:&lt;/b&gt; '), '&lt;/div&gt;') * substring-before(substring-after(description, 'RetailValue:&lt;/b&gt; '), '&lt;/div&gt;')" data-type ="number" order ="descending"/>
				<xsl:sort select="substring-before(substring-after(description, 'Sponsor:&lt;/b&gt;'), '&lt;/div&gt;')" />
				<xsl:sort select="title" />
			</xsl:apply-templates >
		</table>
	</xsl:template>

	<xsl:template match="title">
		<xsl:value-of select="text()"/>
	</xsl:template>

	<xsl:template match="item">
		<xsl:variable name="sponsor">
			<xsl:call-template name="getWSSDescriptionField">
				<xsl:with-param name="description" select="description"/>
				<xsl:with-param name="fieldName" select="'Sponsor'"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:variable name="quantity">
			<xsl:call-template name="getWSSDescriptionField">
				<xsl:with-param name="description" select="description"/>
				<xsl:with-param name="fieldName" select="'Quantity'"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:variable name="retail">
			<xsl:call-template name="getWSSDescriptionField">
				<xsl:with-param name="description" select="description"/>
				<xsl:with-param name="fieldName" select="'RetailValue'"/>
			</xsl:call-template>
		</xsl:variable>
		<!--
		<xsl:if test="$sponsor != $currSponsor">
			<tr>
				<td>New Sponsor: <xsl:value-of select="$sponsor"/>
			</td>
			</tr>
			<xsl:variable name="currSponsor" select ="$sponsor"/>
		</xsl:if>
		-->
		<tr>
			<td>
				<b>
					<a>
						<xsl:attribute name="name">
							<xsl:value-of select ="$sponsor"/>
						</xsl:attribute>
						<xsl:attribute name="href">
							<xsl:text>Sponsors.aspx#</xsl:text>
							<xsl:value-of select ="$sponsor"/>
						</xsl:attribute>
						<xsl:value-of disable-output-escaping ="yes" select ="$sponsor"/>
					</a>
				</b>
			</td>
			<td>
				<xsl:variable name="description">
					<xsl:call-template name="getWSSDescriptionField">
						<xsl:with-param name="description" select="description"/>
						<xsl:with-param name="fieldName" select="'Description'"/>
					</xsl:call-template>
				</xsl:variable>
				<b>
					<xsl:value-of disable-output-escaping ="yes" select ="$description"/>
				</b>
			</td>
			<td align="center">
				<xsl:value-of select ="$quantity"/>
			</td>
			<td align="right">
				<xsl:value-of select ="format-number($retail, '$###,###,##0.00')"/>
			</td>
			<td align="right">
				<b>
					<xsl:value-of select ="format-number($quantity * $retail, '$###,###,##0.00')"/>
				</b>
			</td>
		</tr>
	</xsl:template>

	<xsl:template name="getWSSDescriptionField">
		<xsl:param name="description"/>
		<xsl:param name="fieldName"/>

		<xsl:variable name="before" select="concat($fieldName,':&lt;/b&gt; ')" />
		<xsl:variable name="after" select ="'&lt;/div&gt;'" />
		<xsl:variable name="htmlFieldStart" select ="'&lt;div class=ExternalClass'" />

		<xsl:variable name = "leftRemoved" >
			<xsl:value-of select="substring-after($description,$before)"/>
		</xsl:variable>
		<xsl:variable name = "results">
			<xsl:value-of disable-output-escaping="yes" select = "substring-before($leftRemoved,$after)" />
		</xsl:variable>

		<!-- HTML fields have extra <div>-->
		<xsl:choose>
			<xsl:when test="starts-with($results, $htmlFieldStart)">
				<xsl:value-of select = "substring-after($results,'&gt;')" />
			</xsl:when >
			<xsl:otherwise>
				<xsl:value-of select ="$results"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:transform>
