﻿<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">

	<xsl:param name="URL"/>
	<xsl:param name="GraphBy"/>
	<xsl:variable name="GraphByClean" select="translate($GraphBy, '_x0020_', ' ')"/>

	<xsl:template match="/" xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:SharePoint="Microsoft.SharePoint.WebControls">
		<xsl:call-template name="dvt_1"/>
	</xsl:template>

	<xsl:template name="dvt_1">
		<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row[@Potential_x0020_Value &gt; 0]"/>
		<table border="0" width="100%" cellpadding="2" cellspacing="0">
			<tr valign="top">
				<td class="ms-vb" colspan="99">
					<h3>By <xsl:value-of select="$GraphByClean"/></h3>				
				</td>
			</tr>
			<tr valign="top">
				<th class="ms-vh" nowrap=""><xsl:value-of select="$GraphByClean"/></th>
				<th class="ms-vh" nowrap="">Potential Value</th>
			</tr>
			<xsl:call-template name="dvt_1.body">
				<xsl:with-param name="Rows" select="$Rows" />
			</xsl:call-template>
		    <tr>
		        <td class="ms-vb" colspan="99" valign="middle" style="padding-top:30px;">
					<input type="button" value="Graph by Lead Source" name="btnGraphByLeadSource" class="ms-buttonheightwidth" style="width:auto;vertical-align:middle;">
		        		<xsl:attribute name="onclick">
		        			window.location.href = &apos;<xsl:value-of select="$URL"/>&apos; +
		        				&apos;?GraphBy=Lead_x0020_Source&apos;;
		        		</xsl:attribute>
		        	</input>
					<input type="button" value="Graph by Lead Date" name="btnGraphByLeadDate" class="ms-buttonheightwidth" style="width:auto;vertical-align:middle;">
		        		<xsl:attribute name="onclick">
		        			window.location.href = &apos;<xsl:value-of select="$URL"/>&apos; +
		        				&apos;?GraphBy=Lead_x0020_Date&apos;;
		        		</xsl:attribute>
		        	</input>
					<input type="button" value="Graph by Region" name="btnGraphByRegion" class="ms-buttonheightwidth" style="width:auto;vertical-align:middle;">
		        		<xsl:attribute name="onclick">
		        			window.location.href = &apos;<xsl:value-of select="$URL"/>&apos; +
		        				&apos;?GraphBy=Region&apos;;
		        		</xsl:attribute>
		        	</input>
					<input type="button" value="Graph by State" name="btnGraphByState" class="ms-buttonheightwidth" style="width:auto;vertical-align:middle;">
		        		<xsl:attribute name="onclick">
		        			window.location.href = &apos;<xsl:value-of select="$URL"/>&apos; +
		        				&apos;?GraphBy=State&apos;;
		        		</xsl:attribute>
		        	</input>
					<input type="button" value="Graph by Title" name="btnGraphByTitle" class="ms-buttonheightwidth" style="width:auto;vertical-align:middle;">
		        		<xsl:attribute name="onclick">
		        			window.location.href = &apos;<xsl:value-of select="$URL"/>&apos; +
		        				&apos;?GraphBy=Title&apos;;
		        		</xsl:attribute>
		        	</input>
		        </td>
		    </tr>
		</table>
	</xsl:template>
	
	<xsl:template name="dvt_1.body">
		<xsl:param name="Rows"/>
		<xsl:for-each select="$Rows">
			<xsl:sort select="sum($Rows[@*[name()=$GraphBy] = current()/@*[name()=$GraphBy]]/@Potential_x0020_Value)" data-type="number" order="descending" />
			<xsl:call-template name="dvt_1.rowview">
				<xsl:with-param name="Rows" select="$Rows"/>
			</xsl:call-template>
		</xsl:for-each>
	</xsl:template>

	<xsl:template name="dvt_1.rowview">
		<xsl:param name="Rows"/>
		<xsl:variable name="MaxHistWidth" select="sum($Rows/@Potential_x0020_Value)"/>
		<xsl:variable name="HistWidth" select="500" />
		<xsl:variable name="NewGraphBy" select="ddwrt:NameChanged(string(@*[name()=$GraphBy]), 0)"/> 
		<xsl:if test="string-length($NewGraphBy) &gt; 0">
	    	<tr>
				<td class="ms-vb">
					<xsl:choose>
						<xsl:when test="$GraphBy = 'Lead_x0020_Date'">
							<xsl:value-of select="ddwrt:FormatDateTime(string(@Lead_x0020_Date) ,1033 ,'MMMM dd, yyyy ')"/>
						</xsl:when>
						<xsl:otherwise>
			                <xsl:value-of select="@*[name()=$GraphBy]"/>						
						</xsl:otherwise>
					</xsl:choose>
	           </td>
				<td class="ms-vb" width="{$HistWidth}px;">
					<xsl:variable name="ThisBarValue" select="sum($Rows[@*[name()=$GraphBy] = current()/@*[name()=$GraphBy]]/@Potential_x0020_Value)"/>
					<xsl:variable name="ThisBarWidth" select="round(($ThisBarValue div $MaxHistWidth) * $HistWidth)"/>
					<table cellpadding="0" cellspacing="0" border="0">
						<tr>
							<td class="ms-vb" style="background-image:url(/_layouts/images/topnavselected.gif);" width="{$ThisBarWidth}px;">
								<xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>							
							</td>
							<td class="ms-vb">
								<xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>
								<xsl:value-of select="format-number(round($ThisBarValue), &quot;$#,##0.;-$#,##0.&quot;)"/>
							</td>
						</tr>
					</table>
				</td>
			</tr>
		</xsl:if>
	</xsl:template>
	
</xsl:stylesheet>