﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
	<xsl:output method="xml" indent="yes"/>

	<xsl:template match="/">

		<xsl:variable name="GoalItemID" select="AjaxRequest/Goals/Goal/ItemID"></xsl:variable>

		<xsl:variable name="BusinessPlanGoalItemID" select="AjaxRequest/BusinessPlanGoalItemID"></xsl:variable>
		<xsl:variable name="SalesActivityGoalItemID" select="AjaxRequest/SalesActivityGoalItemID"></xsl:variable>

		<xsl:variable name="MasterPortalItemID" select="AjaxRequest/Params/MasterPortalItemID"></xsl:variable>
		<xsl:variable name="PortalItemID" select="AjaxRequest/Params/PortalItemID"></xsl:variable>
		<xsl:variable name="DivID" select="AjaxRequest/Params/DivID"></xsl:variable>
		<xsl:variable name="UserID" select="AjaxRequest/Params/UserID"></xsl:variable>
		<xsl:variable name="CurrentDate">
			<xsl:choose>
				<xsl:when test="AjaxRequest/Params/KeyDate[text()]">
					<xsl:value-of select="AjaxRequest/Params/KeyDate"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="AjaxRequest/BusinessPlanDate"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="MetricList" select="AjaxRequest/Metrics"></xsl:variable>
		<xsl:variable name="NumberMetricList" select="AjaxRequest/NumberMetricList/Metrics"></xsl:variable>
		<xsl:variable name="CheckKeyDate">
			<xsl:choose>
				<xsl:when test="AjaxRequest/Params/CheckKeyDate[text()]">
					<xsl:value-of select="AjaxRequest/Params/CheckKeyDate"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>null</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="Metric">
			<xsl:choose>
				<xsl:when test="AjaxRequest/Params/MetricKey != ''">
					<xsl:value-of select="AjaxRequest/Params/MetricKey"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="DefaultMetric">
						<xsl:with-param name="NumberMetricList" select="$NumberMetricList"></xsl:with-param>
						<xsl:with-param name="MetricList" select="$MetricList"></xsl:with-param>
						<xsl:with-param name="CurrentControlKey" select="$MetricList/Metric[position()=1]/ControlKey"></xsl:with-param>
					</xsl:call-template>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:variable name="DisplayControlKey">
			<xsl:value-of select="$NumberMetricList/Metric[MetricKey=$Metric]/ControlKey"/>
		</xsl:variable>
		<xsl:variable name="DisplayMetricLabel">
			<xsl:value-of select="$MetricList/Metric[ControlKey=$DisplayControlKey]/MetricKey"/>
		</xsl:variable>
		<xsl:variable name="GoalMetrics" select="AjaxRequest/Metrics/."></xsl:variable>
		<xsl:variable name="Classification" select="AjaxRequest/Params/Classification"></xsl:variable>
		<xsl:variable name="PeriodType" select="$MetricList/Metric[MetricKey = $Metric]/PeriodType"></xsl:variable>
		<xsl:variable name="PageNumber">
			<xsl:number value="1"/>
		</xsl:variable>
		<xsl:variable name="PageSize">
			<xsl:choose>
				<xsl:when test="$PeriodType = 'Monthly'">
					<xsl:number value="12"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:number value="12"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="SelectedYear">
			<xsl:value-of select="substring($CurrentDate,7,10)"/>
		</xsl:variable>
		<xsl:variable name="GraphCall">
			<xsl:text>LoadActualAndTargetNumbersGraph_3_0(</xsl:text>
			<xsl:value-of select="$MasterPortalItemID"></xsl:value-of>
			<xsl:text>,</xsl:text>
			<xsl:value-of select="$PortalItemID"></xsl:value-of>
			<xsl:text>,</xsl:text>
			<xsl:value-of select="$GoalItemID"></xsl:value-of>
			<xsl:text>,</xsl:text>
			<xsl:value-of select="$UserID"></xsl:value-of>
			<xsl:text>,</xsl:text>
			<xsl:value-of select="$SelectedYear"></xsl:value-of>
			<xsl:text>,'Monthly','divTargetActualGraph','</xsl:text>
			<xsl:value-of select="$Metric"/>
			<xsl:text>');</xsl:text>
		</xsl:variable>
		<xsl:variable name="updateYTDCall">
			<xsl:text>updateFlattenDVF_BusinessPlan(</xsl:text>
			<xsl:value-of select="$UserID"></xsl:value-of>
			<xsl:text>,</xsl:text>
			<xsl:value-of select="$BusinessPlanGoalItemID"></xsl:value-of>
			<xsl:text>,</xsl:text>
			<xsl:value-of select="$GoalItemID"></xsl:value-of>
			<xsl:text>,</xsl:text>
			<xsl:value-of select="$SalesActivityGoalItemID"></xsl:value-of>
			<xsl:text>);</xsl:text>
		</xsl:variable>
		<xsl:variable name="YTDCall">
			<xsl:text>getMetricYTD('Quarterly',</xsl:text>
			<xsl:value-of select="$UserID"></xsl:value-of>
			<xsl:text>,'</xsl:text>
			<xsl:value-of select="$Metric"/>
			<xsl:text>',</xsl:text>
			<xsl:value-of select="$GoalItemID"></xsl:value-of>
			<xsl:text>,'divTargetResultYTD',</xsl:text>
			<xsl:value-of select="$SelectedYear"></xsl:value-of>
			<xsl:text>);</xsl:text>
		</xsl:variable>

		<xsl:choose>
			<xsl:when test="$GoalItemID !=''">

				<div class="container abc">
					<div class="row abcd">
						<div class="col-md-2 col-sm-12 ">
							<div class="row">
								<h4 class="text-center business-targ" style="padding: 3px 0px;">
									<span id="spanBPResultMetric" class="onMouseOver nwstyle" onclick="$('#optBPResultMetric').css('display','inline-block');
								  $('#spanBPResultMetric').css('display','none');">
										<xsl:value-of select="$DisplayMetricLabel"/>
									</span>
									<select style="display:none" class="form-control fornw"  id="optBPResultMetric" onChange="LoadBusinessPlanTab_3_0('Results', '{$Classification}'
									, {$MasterPortalItemID}, {$PortalItemID}, {$UserID}, '{$DivID}', null,{$CheckKeyDate});">    

										<!--<xsl:for-each select="$NumberMetricList/Metric">-->
										<xsl:for-each select="$MetricList/Metric">
											<xsl:variable name="varControlKey" select="ControlKey"></xsl:variable>
											<xsl:variable name="varBPMetric" select="MetricKeyDisplay"></xsl:variable>
											<xsl:variable name="varBNMetricKey" select="$NumberMetricList/Metric[ControlKey=$varControlKey]/MetricKey"></xsl:variable>
											<xsl:if test="$NumberMetricList/Metric[ControlKey=$varControlKey]/ControlKey != ''">
												<xsl:choose>
													<xsl:when test="$Metric = $varBNMetricKey">
														<option value="{$varBNMetricKey}" selected="selected">
															<xsl:value-of select="$varBPMetric"/>
														</option>
													</xsl:when>
													<xsl:otherwise>
														<option value="{$varBNMetricKey}">
															<xsl:value-of select="$varBPMetric"/>
														</option>
													</xsl:otherwise>
												</xsl:choose>
											</xsl:if>
										</xsl:for-each>
									</select>
								</h4>
								<div id="divNumbersLeaderBoard">
									<xsl:choose>
										<xsl:when test="$GoalItemID !=''">
											<script>
												<xsl:text>goalNumberDataEntryPanelForBusinessPlanMetric(</xsl:text>
												<xsl:value-of select="$GoalItemID"></xsl:value-of>
												<xsl:text>,</xsl:text>
												<xsl:value-of select="$PageNumber"></xsl:value-of>
												<xsl:text>,</xsl:text>
												<xsl:value-of select="$PageSize"></xsl:value-of>
												<xsl:text>,</xsl:text>
												<xsl:value-of select="$UserID"></xsl:value-of>
												<xsl:text>,'</xsl:text>
												<xsl:value-of select="$Metric"></xsl:value-of>
												<xsl:text>',</xsl:text>
												<xsl:text>'divNumbersLeaderBoard'</xsl:text>
												<xsl:text>,</xsl:text>
												<xsl:value-of select="$MasterPortalItemID"></xsl:value-of>
												<xsl:text>,</xsl:text>
												<xsl:value-of select="$SelectedYear"/>
												<xsl:text>,</xsl:text>
												<xsl:value-of select="$PortalItemID"/>
												<xsl:text>,'</xsl:text>
												<xsl:value-of select="$Classification"/>
												<xsl:text>');</xsl:text>
											</script>
										</xsl:when>
									</xsl:choose>
								</div>
							</div>
						</div>
						<div class="col-md-5 col-sm-12" id="divTargetGoalDescription">
							<xsl:choose>
								<xsl:when test="$GoalItemID !=''">
									<script>
										<xsl:text>getGoalTargetDetail(</xsl:text>
										<xsl:value-of select="$GoalItemID"></xsl:value-of>
										<xsl:text>,</xsl:text>
										<xsl:value-of select="$UserID"></xsl:value-of>
										<xsl:text>,'divTargetGoalDescription',</xsl:text>
										<xsl:value-of select="$MasterPortalItemID"></xsl:value-of>
										<xsl:text>,</xsl:text>
										<xsl:value-of select="$SelectedYear"/>
										<xsl:text>,'</xsl:text>
										<xsl:value-of select="$Metric"></xsl:value-of>
										<xsl:text>');</xsl:text>
									</script>
								</xsl:when>
							</xsl:choose>
						</div>
						<div class="col-md-5 col-sm-12">
							<div id="divYTDLoading" style="width:100%;float:left;background:#000;z-index:999;position:absolute;height: 571px;opacity: 0.5;text-align: center;display:none;">
								<img src="http://blog.teamtreehouse.com/wp-content/uploads/2015/05/InternetSlowdown_Day.gif" style=" width: 79px; text-align: center; top: 270px; position: absolute;"></img>
							</div>
							<div class="row M-L10">
								<h4 class="text-center business-targ">
									Visualize my Results
								</h4>
								<div style="height: 300px; padding: 0px; position: relative;" id="divTargetActualGraph" class="chart clsDivTargetActualGraph">
									<script>
										<xsl:value-of select="$GraphCall"/>
									</script>
								</div>
								<div id="no-more-tables" class="newtable">
									<input type="hidden" id="inputYTDCall_Refresh" onclick="{$YTDCall}{$GraphCall} "></input>

									<div id="divTargetResultYTD">
										<script>
											<xsl:value-of select="$YTDCall"/>
										</script>
									</div>
								</div>
								<h5 class="txtdetail">
									<span>
										<button class="btn btn-greenw" onclick="{$updateYTDCall}">Recalc</button>
									</span>
								</h5>
							</div>
						</div>

					</div>
				</div>
			</xsl:when>
			<xsl:otherwise>
				<div class="container">
					There is no goal
				</div>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="DefaultMetric">
		<xsl:param name="NumberMetricList" select="'N/A'"></xsl:param>
		<xsl:param name="MetricList" select="'N/A'"></xsl:param>
		<xsl:param name="CurrentControlKey" select="'N/A'"></xsl:param>

		<xsl:choose>
			<xsl:when test="$NumberMetricList/Metric[ControlKey=$CurrentControlKey]">
				<xsl:value-of select="$NumberMetricList/Metric[ControlKey=$CurrentControlKey]/MetricKey"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:choose>
					<xsl:when test="$MetricList/Metric[ControlKey=$CurrentControlKey]/following-sibling::Metric[1]/ControlKey[text()]">
						<xsl:call-template name="DefaultMetric">
							<xsl:with-param name="NumberMetricList" select="$NumberMetricList"></xsl:with-param>
							<xsl:with-param name="MetricList" select="$MetricList"></xsl:with-param>
							<xsl:with-param name="CurrentControlKey"
											select="$MetricList/Metric[ControlKey=$CurrentControlKey]/following-sibling::Metric[1]/ControlKey"></xsl:with-param>
						</xsl:call-template>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text></xsl:text>
					</xsl:otherwise>
				</xsl:choose>

			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
