﻿<?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:key name="distinctScoreboardGroupKey" match="CRMOScoreboardGoalMetric" use="./GroupKeyID"></xsl:key>
    <xsl:key name="distinctScoreboardGoalMetric" match="CRMOScoreboardGoalMetric" use="./Metric"></xsl:key>

    <xsl:template match="/">

        <xsl:variable name="SourceItemID" select="AjaxRequest/Params/PortalItemID"></xsl:variable>
        <xsl:variable name="EmployeeID" select="AjaxRequest/Params/EmployeeID"></xsl:variable>
        <xsl:variable name="ViewMode" select="AjaxRequest/Params/ViewMode"></xsl:variable>
        <xsl:variable name="CRMOScoreboardGoalMetric" select="AjaxRequest/CRMOScoreboard/."></xsl:variable>
        <xsl:variable name="ActivePeriodKeys" select="AjaxRequest/CRMOScoreboard/ActivePeriodKeys/."></xsl:variable>
        <xsl:variable name="HostName" select="AjaxRequest/HostName"></xsl:variable>
		<xsl:variable name="BetaVersion" select="AjaxRequest/Settings/BetaVersion"></xsl:variable>
        <xsl:variable name="TemplateID" select="AjaxRequest/Params/TemplateID"></xsl:variable>
		<xsl:variable name="NumberPrecision" select="AjaxRequest/Params/NumberPrecision"></xsl:variable>
		<xsl:variable name="class">
			<xsl:choose>
				<xsl:when test="$BetaVersion &gt;3.0 and $TemplateID!=''">
					<xsl:text>Customtable bordernone   table-hover table keyvalue-table  ng-isolate-scope dataTable no-footer tablresponsive</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>tableMaster scoreboardBorder</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>


        <div id="div_{$SourceItemID}_Scoreboard">
            <div id="div_{$SourceItemID}_ScoreboardMenu" class="goalTabsBackground marginTop10">
                <xsl:call-template name="ScoreBoardTabs">
                    <xsl:with-param name="ViewMode" select="$ViewMode"></xsl:with-param>
                    <xsl:with-param name="SourceItemID" select="$SourceItemID"></xsl:with-param>
                    <xsl:with-param name="EmployeeID" select="$EmployeeID"></xsl:with-param>
					<xsl:with-param name="BetaVersion" select="$BetaVersion"></xsl:with-param>
					<xsl:with-param name="TemplateID" select="$TemplateID"></xsl:with-param>					
                    <xsl:with-param name="NumberPrecision" select="$NumberPrecision"></xsl:with-param>
				</xsl:call-template>
            </div>
            <div id="div_{$SourceItemID}_ScoreboardContent" class="marginTop10">
				
                <table class="{$class}" cellpadding="2" cellspacing="0" align="center">

                    <xsl:call-template name="ScoreBoardHeader">
                        <xsl:with-param name="ActivePeriodKeys" select="$ActivePeriodKeys"></xsl:with-param>
                        <xsl:with-param name="ViewMode" select="$ViewMode"></xsl:with-param>
                    </xsl:call-template>


                    <xsl:variable name="TotalNumberOfCell">
                        <xsl:call-template name="TotalNumberOfCell">
                            <xsl:with-param name="ActivePeriodKeys" select="$ActivePeriodKeys"></xsl:with-param>
                            <xsl:with-param name="ViewMode" select="$ViewMode"></xsl:with-param>
                        </xsl:call-template>
                    </xsl:variable>

                    <xsl:for-each select="$CRMOScoreboardGoalMetric/CRMOScoreboardGoalMetric
                                  [generate-id() = generate-id(key('distinctScoreboardGroupKey', ./GroupKeyID)) and ($TemplateID= ./TemplateSourceID or $TemplateID='')]">

                        <xsl:variable name="GroupKeyID" select="GroupKeyID"></xsl:variable>

                        <xsl:variable name="ScoreoardGoalMetricData">
                            <xsl:copy-of select="$CRMOScoreboardGoalMetric/CRMOScoreboardGoalMetric[GroupKeyID = $GroupKeyID]"/>
                        </xsl:variable>

                        <xsl:if test ="BrandKey != preceding-sibling::CRMOScoreboardGoalMetric[1] or count(preceding-sibling::CRMOScoreboardGoalMetric[1]) = 0">
                            <tr style="background-color:#FFFFFF;" height="20px">
                                <td colspan="{$TotalNumberOfCell}">
                                </td>
                            </tr>
                            <tr style="background-color:#808285;" class="packageTitleWhite" height="20px">
                                <td colspan="{$TotalNumberOfCell}">
                                    <xsl:value-of select="BrandKey"/>
                                </td>
                            </tr>
                        </xsl:if>

                        <xsl:call-template name="ScoreboradGoal">
                            <xsl:with-param name="CRMOScoreboardGoalMetric" select="$CRMOScoreboardGoalMetric"></xsl:with-param>
                            <xsl:with-param name="GroupKeyID" select="$GroupKeyID"></xsl:with-param>
                            <xsl:with-param name="SourceItemID" select="$SourceItemID"></xsl:with-param>
                            <xsl:with-param name="ActivePeriodKeys" select="$ActivePeriodKeys"></xsl:with-param>
                            <xsl:with-param name="ScoreoardGoalMetricData" select="msxsl:node-set($ScoreoardGoalMetricData)"></xsl:with-param>
                            <xsl:with-param name="HostName" select="$HostName"></xsl:with-param>
                            <xsl:with-param name="ViewMode" select="$ViewMode"></xsl:with-param>
							<xsl:with-param name="NumberPrecision" select="$NumberPrecision"></xsl:with-param>
                        </xsl:call-template>
                    </xsl:for-each>
                </table>
            </div>
        </div>
    </xsl:template>


    <xsl:template name="ScoreBoardTabs">
        <xsl:param name="ViewMode" select="'N/A'"></xsl:param>
        <xsl:param name="SourceItemID" select="'N/A'"></xsl:param>
        <xsl:param name="EmployeeID" select="'N/A'"></xsl:param>
		<xsl:param name="BetaVersion" select="'N/A'"></xsl:param>
		<xsl:param name="TemplateID" select="'N/A'"></xsl:param>
		<xsl:param name="NumberPrecision" select="'N/A'"></xsl:param>
		
		<xsl:variable name="WeeklySelectionClass">
            <xsl:choose>
                <xsl:when test="$ViewMode = 'Weekly'">
                    <xsl:text>selectedMenuGoalTabs</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>defaultMenuGoal</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="MonthlySelectionClass">
            <xsl:choose>
                <xsl:when test="$ViewMode = 'Monthly'">
                    <xsl:text>selectedMenuGoalTabs</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>defaultMenuGoal</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="QuarterlySelectionClass">
            <xsl:choose>
                <xsl:when test="$ViewMode = 'Quarterly'">
                    <xsl:text>selectedMenuGoalTabs</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>defaultMenuGoal</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="YearlySelectionClass">
            <xsl:choose>
                <xsl:when test="$ViewMode = 'Yearly'">
                    <xsl:text>selectedMenuGoalTabs</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>defaultMenuGoal</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <table class="tableMaster textCenter verticallyMiddle capitalFont" height="32px" cellSpacing="0" cellPadding="2" >
            <td width="20%" id="cell_Weekly" class="textCenter verticallyMiddle {$WeeklySelectionClass}"
                       name="ScoreboardMenuWeekly">
				<xsl:choose>
					<xsl:when test="$BetaVersion &gt;3.0 and $TemplateID!=''">
						<span id="span_Goals" class="onMouseOver"
				   onClick="goal_HightlightSelectedLink('cell_Weekly','defaultMenuGoal','selectedMenuGoalTabs','ScoreboardMenuWeekly',0);
                               portal_LoadMyScoreboard_3_5({$SourceItemID}, 'div_SuperPage_Operaions', {$EmployeeID}, 'Weekly',{$TemplateID},'{$NumberPrecision}');">
							<xsl:text>Weekly</xsl:text>
						</span>
					</xsl:when>
					<xsl:otherwise>
						<span id="span_Goals" class="onMouseOver"
				   onClick="goal_HightlightSelectedLink('cell_Weekly','defaultMenuGoal','selectedMenuGoalTabs','ScoreboardMenuWeekly',0);
                               portal_LoadMyScoreboard({$SourceItemID}, 'div_SuperPage_Operaions', {$EmployeeID}, 'Weekly');">
							<xsl:text>Weekly</xsl:text>
						</span>
				</xsl:otherwise>
				</xsl:choose>
             
            </td>
            <td width="20%" id="cell_Monthly" class="textCenter verticallyMiddle {$MonthlySelectionClass}"
                               name="ScoreboardMenuWeekly">
				<xsl:choose>
					<xsl:when test="$BetaVersion &gt;3.0 and $TemplateID!=''">
						<span id="span_Goals" class="onMouseOver"
											onClick="goal_HightlightSelectedLink('cell_Monthly','defaultMenuGoal','selectedMenuGoalTabs','ScoreboardMenuWeekly',0);
                              portal_LoadMyScoreboard_3_5({$SourceItemID}, 'div_SuperPage_Operaions', {$EmployeeID}, 'Monthly',{$TemplateID},'{$NumberPrecision}');">
							<xsl:text>Monthly</xsl:text>
						</span>
					</xsl:when>
					<xsl:otherwise>
						  <span id="span_Goals" class="onMouseOver"
                      onClick="goal_HightlightSelectedLink('cell_Monthly','defaultMenuGoal','selectedMenuGoalTabs','ScoreboardMenuWeekly',0);
                              portal_LoadMyScoreboard({$SourceItemID}, 'div_SuperPage_Operaions', {$EmployeeID}, 'Monthly');">
                    <xsl:text>Monthly</xsl:text>
                </span>
					</xsl:otherwise>
				</xsl:choose>
              
            </td>
            <td width="20%" id="cell_Quarterly" class="textCenter verticallyMiddle {$QuarterlySelectionClass}"
                               name="ScoreboardMenuWeekly">
				<xsl:choose>
					<xsl:when test="$BetaVersion &gt;3.0 and $TemplateID!=''">
						<span id="span_Goals" class="onMouseOver"
											onClick="goal_HightlightSelectedLink('cell_Quarterly','defaultMenuGoal','selectedMenuGoalTabs','ScoreboardMenuWeekly',0);
                              portal_LoadMyScoreboard_3_5({$SourceItemID}, 'div_SuperPage_Operaions', {$EmployeeID}, 'Quarterly',{$TemplateID},'{$NumberPrecision}');">
							<xsl:text>Quarterly</xsl:text>
						</span>
					</xsl:when>
					<xsl:otherwise>
						<span id="span_Goals" class="onMouseOver"
					onClick="goal_HightlightSelectedLink('cell_Quarterly','defaultMenuGoal','selectedMenuGoalTabs','ScoreboardMenuWeekly',0);
                              portal_LoadMyScoreboard({$SourceItemID}, 'div_SuperPage_Operaions', {$EmployeeID}, 'Quarterly');">
							<xsl:text>Quarterly</xsl:text>
						</span>
					</xsl:otherwise>
				</xsl:choose>
              
            </td>
            <td width="20%" id="cell_Yearly" class="textCenter verticallyMiddle {$YearlySelectionClass}"
                               name="ScoreboardMenuWeekly">
				<xsl:choose>
					<xsl:when test="$BetaVersion &gt;3.0 and $TemplateID!=''">
						<span id="span_Goals" class="onMouseOver"
											onClick="goal_HightlightSelectedLink('cell_Yearly','defaultMenuGoal','selectedMenuGoalTabs','ScoreboardMenuWeekly',0);
                              portal_LoadMyScoreboard_3_5({$SourceItemID}, 'div_SuperPage_Operaions', {$EmployeeID}, 'Yearly',{$TemplateID},'{$NumberPrecision}');">
							<xsl:text>YTD</xsl:text>
						</span>
					</xsl:when>
					<xsl:otherwise>
						<span id="span_Goals" class="onMouseOver"
					onClick="goal_HightlightSelectedLink('cell_Yearly','defaultMenuGoal','selectedMenuGoalTabs','ScoreboardMenuWeekly',0);
                              portal_LoadMyScoreboard({$SourceItemID}, 'div_SuperPage_Operaions', {$EmployeeID}, 'Yearly');">
							<xsl:text>YTD</xsl:text>
						</span>
					</xsl:otherwise>
				</xsl:choose>
              
            </td>
            <td width="20%" class="textCenter verticallyMiddle defaultMenuGoal"></td>
        </table>
    </xsl:template>

    <xsl:template name="ScoreBoardHeader">
        <xsl:param name="ActivePeriodKeys"></xsl:param>
        <xsl:param name="ViewMode" select="'N/A'"></xsl:param>

        <xsl:variable name="YTDTabOn">
            <xsl:call-template name="YTDTabOn">
                <xsl:with-param name="ViewMode" select="$ViewMode"></xsl:with-param>
            </xsl:call-template>
        </xsl:variable>

        <xsl:variable name="TotalNumberOfCell">
            <xsl:call-template name="TotalNumberOfCell">
                <xsl:with-param name="ActivePeriodKeys" select="$ActivePeriodKeys"></xsl:with-param>
                <xsl:with-param name="ViewMode" select="$ViewMode"></xsl:with-param>
            </xsl:call-template>
        </xsl:variable>


        <xsl:variable name="DataCellWidth" select="63 div ($TotalNumberOfCell - 2)"></xsl:variable>


        <tr>
            <td width="2%" class="scoreboardArrowBorder"></td>
            <td class="verticallyMiddle textCenter scoreboardGoalBorder" width="35%">
            </td>
            <td class="verticallyMiddle textCenter scoreboardCellBorder scoreboardGoalTargetCell" width="{$DataCellWidth}%">
                <span class="packageTitle">Goal</span>
            </td>
            <xsl:for-each select="$ActivePeriodKeys/PeriodKey">
                <td class="verticallyMiddle textCenter scoreboardCellBorder" width="{$DataCellWidth}%">
                    <span class="packageTitle">
                        <xsl:choose>
                            <xsl:when test="$ViewMode = 'Yearly' and ActiveYear = 1">
                                <xsl:value-of select="DisplayPeriodKey"/> YTD
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="DisplayPeriodKey"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </span>
                </td>
                <xsl:choose>
                    <xsl:when test="$ViewMode = 'Yearly' and ActiveYear = 0">
                        <td class="verticallyMiddle textCenter scoreboardCellBorder" width="{$DataCellWidth}%">
                            <span class="packageTitle">
                                <xsl:value-of select="DisplayPeriodKey"/> YTD
                            </span>
                        </td>
                    </xsl:when>
                </xsl:choose>
            </xsl:for-each>
            <xsl:choose>
                <xsl:when test="$YTDTabOn = 1">

                    <td class="verticallyMiddle textCenter scoreboardCellBorder" width="{$DataCellWidth}%">
                        <span class="packageTitle">YTD</span>
                    </td>
                </xsl:when>
            </xsl:choose>

        </tr>
    </xsl:template>

    <xsl:template name="ScoreboradGoal">
        <xsl:param name="CRMOScoreboardGoalMetric" select="'N/A'"></xsl:param>
        <xsl:param name="GroupKeyID" select="'N/A'"></xsl:param>
        <xsl:param name="SourceItemID" select="'N/A'"></xsl:param>
        <xsl:param name="ActivePeriodKeys" select="'N/A'"></xsl:param>
        <xsl:param name="ScoreoardGoalMetricData" select="'N/A'"></xsl:param>
        <xsl:param name="HostName" select="'N/A'"></xsl:param>
        <xsl:param name="ViewMode" select="'N/A'"></xsl:param>
		<xsl:param name="NumberPrecision" select="'N/A'"></xsl:param>


		

        <!--Here we are checking
            If there is any active Goal among the Group then pull latest Goal from the Active
            Otherwise pick latest Goal frm whole Group
        -->
        <xsl:variable name="ActiveGoalItemID">

            <xsl:choose>
                <xsl:when test="count($ScoreoardGoalMetricData/CRMOScoreboardGoalMetric[GroupKeyID = $GroupKeyID and GoalStatusID &lt; 4]) &gt; 0">
                    <xsl:for-each select="$ScoreoardGoalMetricData/CRMOScoreboardGoalMetric[GroupKeyID = $GroupKeyID and GoalStatusID &lt; 4]">
                        <xsl:sort select="GoalItemID" data-type="number" order="descending"/>

                        <xsl:if test="position() = 1">
                            <xsl:value-of select="GoalItemID"/>
                        </xsl:if>
                    </xsl:for-each>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:for-each select="$ScoreoardGoalMetricData/CRMOScoreboardGoalMetric[GroupKeyID = $GroupKeyID]">
                        <xsl:sort select="GoalItemID" data-type="number" order="descending"/>

                        <xsl:if test="position() = 1">
                            <xsl:value-of select="GoalItemID"/>
                        </xsl:if>
                    </xsl:for-each>
                </xsl:otherwise>
            </xsl:choose>


        </xsl:variable>

        <xsl:variable name="YTDTabOn">
            <xsl:call-template name="YTDTabOn">
                <xsl:with-param name="ViewMode" select="$ViewMode"></xsl:with-param>
            </xsl:call-template>
        </xsl:variable>

        <xsl:variable name="TotalNumberOfCell">
            <xsl:call-template name="TotalNumberOfCell">
                <xsl:with-param name="ActivePeriodKeys" select="$ActivePeriodKeys"></xsl:with-param>
                <xsl:with-param name="ViewMode" select="$ViewMode"></xsl:with-param>
            </xsl:call-template>
        </xsl:variable>

        <tr style="background-color:#FFFFFF;" height="20px">
            <td colspan="{$TotalNumberOfCell}">
                <!--<xsl:choose>
                    <xsl:when test="count($ScoreoardGoalMetricData/CRMOScoreboardGoalMetric[GroupKeyID = $GroupKeyID and GoalStatusID &lt; 4]) &gt; 0">Y</xsl:when>
                    <xsl:otherwise>N</xsl:otherwise>
                </xsl:choose>
                (<xsl:value-of select="$ActiveGoalItemID"/>)
                (<xsl:value-of select="count($ScoreoardGoalMetricData/CRMOScoreboardGoalMetric[GroupKeyID = $GroupKeyID])"/>)
                (<xsl:value-of select="$GroupKeyID"/>)-->
            </td>
        </tr>
        <tr id="trScoreboardGoal_{$ActiveGoalItemID}">
            <td class="scoreboardArrowBorder">
                <img id="trScoreboardGoalMetric_{$ActiveGoalItemID}_Expand" width="20" height="20" title="Click to Expand"
                     src="http://{$HostName}/DesktopModules/LifeSpaceTemplates/Web/Images/Ver_2_0/arrow_right.png"
                     style="display:none;cursor:pointer;"
                     onClick="scoreboard_ExpandGoal('trScoreboardGoalMetric_{$ActiveGoalItemID}')"></img>
                <img id="trScoreboardGoalMetric_{$ActiveGoalItemID}_Collapse" width="20" height="20" title="Click to Collapse"
                     src="http://{$HostName}/DesktopModules/LifeSpaceTemplates/Web/Images/Ver_2_0/arrow_down.png"
                     style="display:block;cursor:pointer;"
                     onClick="scoreboard_CollapseGoal('trScoreboardGoalMetric_{$ActiveGoalItemID}')"></img>
            </td>
            <td class="scoreboardGoalBorder">
                <span class="packageTitle" style="text-decoration: none;color: #003366;">
                    <!--<a href="http://{$HostName}/Goal.aspx?ID={$ActiveGoalItemID}" target="_blank">-->
                        <xsl:value-of select="$ScoreoardGoalMetricData/CRMOScoreboardGoalMetric[GoalItemID = $ActiveGoalItemID]/ProjectName"/>
                    <!--</a>-->
                </span>
            </td>
            <td class="verticallyMiddle textLeft scoreboardGoalTargetCell">
            </td>
            <xsl:for-each select="$ActivePeriodKeys/PeriodKey">
                <td class="verticallyMiddle textCenter scoreboardCellBorder">
                </td>
                <xsl:if test="$ViewMode = 'Yearly' and ActiveYear = 0">
                    <td class="verticallyMiddle textCenter scoreboardCellBorder">
                    </td>
                </xsl:if>
            </xsl:for-each>
            <xsl:choose>
                <xsl:when test="$YTDTabOn = 1">
                    <td class="verticallyMiddle textLeft scoreboardCellBorder">
                    </td>
                </xsl:when>
            </xsl:choose>
        </tr>
        <xsl:for-each select="$ScoreoardGoalMetricData/CRMOScoreboardGoalMetric
                      [generate-id() = generate-id(key('distinctScoreboardGoalMetric', ./Metric))]">


            <xsl:variable name="CurrentMetric" select="Metric"></xsl:variable>
            <xsl:variable name="CompiledTarget">
                <xsl:call-template name="CalculateMetricTargetPerView">
                    <xsl:with-param name="Target" select="$ScoreoardGoalMetricData/CRMOScoreboardGoalMetric[Metric = $CurrentMetric and GoalItemID = $ActiveGoalItemID]/Target"></xsl:with-param>
                    <xsl:with-param name="SourcePeriodType" select="$ScoreoardGoalMetricData/CRMOScoreboardGoalMetric[Metric = $CurrentMetric and GoalItemID = $ActiveGoalItemID]/TargetPeriodType"></xsl:with-param>
                    <xsl:with-param name="ResultantPeriodType" select="$ViewMode"></xsl:with-param>
                    <xsl:with-param name="TotalProjectDays" select="$ScoreoardGoalMetricData/CRMOScoreboardGoalMetric[Metric = $CurrentMetric and GoalItemID = $ActiveGoalItemID]/TotalProjectDays"></xsl:with-param>
                </xsl:call-template>
            </xsl:variable>
            <xsl:variable name="MetricNumberFormat">
                <xsl:choose>
                    <xsl:when test="NumberFormat = '$'">
                        <xsl:value-of select="NumberFormat"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:text></xsl:text>
                    </xsl:otherwise>
                </xsl:choose>

            </xsl:variable>
			<xsl:variable name="NumberPrecisionVal">
				<xsl:choose>
					<xsl:when test="$NumberPrecision">
						<xsl:value-of select="$NumberPrecision"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>#,###</xsl:text>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>


            <tr id="trScoreboardGoalMetric_{$ActiveGoalItemID}{position()}">
                <td class="scoreboardArrowBorder"></td>
                <td class="verticallyMiddle textLeft scoreboardGoalBorder">
                    <span class="labelDarkGraySmall">
                        <xsl:value-of select="Metric"/>
                    </span>
                </td>
                <td class="verticallyMiddle textRight scoreboardGoalTargetCell packageTitle">


                    <xsl:choose>
                        <xsl:when test="$CompiledTarget &gt; 0">

                            <xsl:choose>

                                <xsl:when test="string(number($CompiledTarget)) != 'NaN'">
                                    <xsl:value-of select="$MetricNumberFormat"/>
                                    <xsl:value-of select="format-number($CompiledTarget ,$NumberPrecisionVal)"/>
                                </xsl:when>
                            </xsl:choose>
                        </xsl:when>
                    </xsl:choose>
                    <xsl:choose>
                        <xsl:when test="TotalProjectDays = 1">*</xsl:when>
                    </xsl:choose>
                </td>
                <xsl:for-each select="$ActivePeriodKeys/PeriodKey">

                    <xsl:variable name="CurrentPeriodKey" select="PeriodKey"></xsl:variable>
                    <xsl:variable name="CurrentPeriodKeyValue" select="sum($ScoreoardGoalMetricData/CRMOScoreboardGoalMetric
                                      [Metric = $CurrentMetric and PeriodKey = $CurrentPeriodKey]/TotalAmount[text()])"></xsl:variable>
                    <xsl:variable name="CurrentPeriodKeyYTDValue" select="sum($ScoreoardGoalMetricData/CRMOScoreboardGoalMetric
                                      [Metric = $CurrentMetric and PeriodKey = $CurrentPeriodKey]/YTD[text()])"></xsl:variable>

                    <td class="verticallyMiddle textRight scoreboardCellBorder">
                        <xsl:choose>
                            <xsl:when test="string(number($CurrentPeriodKeyValue)) != 'NaN' and $CurrentPeriodKeyValue &gt; 0">
                                <xsl:value-of select="$MetricNumberFormat"/>
                                <xsl:value-of select="format-number($CurrentPeriodKeyValue, $NumberPrecisionVal)"/>
                            </xsl:when>
                        </xsl:choose>
                    </td>
                    <xsl:choose>
                        <xsl:when test="$ViewMode = 'Yearly' and ActiveYear = 0">
                            <td class="verticallyMiddle textRight scoreboardCellBorder">
                                <xsl:choose>
                                    <xsl:when test="string(number($CurrentPeriodKeyValue)) != 'NaN' and $CurrentPeriodKeyValue &gt; 0">
                                        <xsl:value-of select="$MetricNumberFormat"/>
                                        <xsl:value-of select="format-number($CurrentPeriodKeyYTDValue, $NumberPrecisionVal)"/>
                                    </xsl:when>
                                </xsl:choose>
                            </td>
                        </xsl:when>
                    </xsl:choose>
                </xsl:for-each>

                <xsl:choose>
                    <xsl:when test="$YTDTabOn = 1">
                        <xsl:variable name="YTDValueSum" select="sum($ScoreoardGoalMetricData/CRMOScoreboardGoalMetric
                                        [Metric = $CurrentMetric and ActiveYear = 1]/TotalAmount[text()])"></xsl:variable>

                        <xsl:variable name="YTDValueCount">
                            <xsl:choose>
                                <xsl:when test="contains($CurrentMetric, '(AVG)') and $YTDValueSum &gt; 0">
                                    <xsl:value-of  select="count($ScoreoardGoalMetricData/CRMOScoreboardGoalMetric
                                            [Metric = $CurrentMetric and ActiveYear = 1])"></xsl:value-of>
                                    </xsl:when>		
                                <xsl:otherwise>
                                    <xsl:value-of  select="1"></xsl:value-of>
                                </xsl:otherwise>					
                            </xsl:choose>
                        </xsl:variable>

                         <xsl:variable name="YTDValue" select="sum($ScoreoardGoalMetricData/CRMOScoreboardGoalMetric
                                      [Metric = $CurrentMetric and ActiveYear = 1]/TotalAmount[text()]) div $YTDValueCount"></xsl:variable>


                        <td class="verticallyMiddle textRight scoreboardCellBorder">
                            <xsl:choose>
                                <xsl:when test="string(number($YTDValue)) != 'NaN' and $YTDValue &gt; 0">
                                    <xsl:value-of select="$MetricNumberFormat"/>
                                    <xsl:value-of select="format-number($YTDValue, $NumberPrecisionVal)"/>
                                </xsl:when>
                            </xsl:choose>
                        </td>
                    </xsl:when>
                </xsl:choose>
            </tr>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="CalculateMetricTargetPerView">
        <xsl:param name="Target" select="'N/A'"></xsl:param>
        <xsl:param name="SourcePeriodType" select="'N/A'"></xsl:param>
        <xsl:param name="ResultantPeriodType" select="'N/A'"></xsl:param>
        <xsl:param name="TotalProjectDays" select="'N/A'"></xsl:param>

        <xsl:choose>
            <xsl:when test="$TotalProjectDays[text()]">
                <xsl:choose>
                    <xsl:when test="$TotalProjectDays &lt;= 1">
                        <xsl:number value="$Target"/>
                    </xsl:when>
                    <xsl:when test="$ResultantPeriodType = 'Weekly'">
                        <xsl:choose>
                            <xsl:when test="$TotalProjectDays &gt;=360 and $TotalProjectDays &lt;= 365">
                                <xsl:value-of select="$Target div 52"/>
                            </xsl:when>
                            <xsl:when test="$TotalProjectDays &gt;=178 and $TotalProjectDays &lt;= 185">
                                <xsl:value-of select="$Target div 26"/>
                            </xsl:when>
                            <xsl:when test="$TotalProjectDays &gt;=88 and $TotalProjectDays &lt;= 93">
                                <xsl:value-of select="$Target div 13"/>
                            </xsl:when>
                            <xsl:when test="$TotalProjectDays &gt;=58 and $TotalProjectDays &lt;= 63">
                                <xsl:value-of select="($Target * 6) div 52"/>
                            </xsl:when>
                            <xsl:when test="$TotalProjectDays &gt;=29 and $TotalProjectDays &lt;= 32">
                                <xsl:value-of select="($Target * 12) div 52"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="($Target div $TotalProjectDays) * 7"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:when test="$ResultantPeriodType = 'Monthly'">
                        <xsl:choose>
                            <xsl:when test="$TotalProjectDays &gt;=360 and $TotalProjectDays &lt;= 365">
                                <xsl:value-of select="$Target div 12"/>
                            </xsl:when>
                            <xsl:when test="$TotalProjectDays &gt;=178 and $TotalProjectDays &lt;= 185">
                                <xsl:value-of select="$Target div 6"/>
                            </xsl:when>
                            <xsl:when test="$TotalProjectDays &gt;=88 and $TotalProjectDays &lt;= 93">
                                <xsl:value-of select="$Target div 3"/>
                            </xsl:when>
                            <xsl:when test="$TotalProjectDays &gt;=58 and $TotalProjectDays &lt;= 63">
                                <xsl:value-of select="$Target div 2"/>
                            </xsl:when>
                            <xsl:when test="$TotalProjectDays &gt;=29 and $TotalProjectDays &lt;= 32">
                                <xsl:value-of select="$Target * 1"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="($Target div $TotalProjectDays) * 30"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:when test="$ResultantPeriodType = 'Quarterly'">
                        <xsl:choose>
                            <xsl:when test="$TotalProjectDays &gt;=360 and $TotalProjectDays &lt;= 365">
                                <xsl:value-of select="$Target div 4"/>
                            </xsl:when>
                            <xsl:when test="$TotalProjectDays &gt;=178 and $TotalProjectDays &lt;= 185">
                                <xsl:value-of select="$Target div 2"/>
                            </xsl:when>
                            <xsl:when test="$TotalProjectDays &gt;=88 and $TotalProjectDays &lt;= 93">
                                <xsl:value-of select="$Target * 1"/>
                            </xsl:when>
                            <xsl:when test="$TotalProjectDays &gt;=58 and $TotalProjectDays &lt;= 63">
                                <xsl:value-of select="$Target * 1.5"/>
                            </xsl:when>
                            <xsl:when test="$TotalProjectDays &gt;=29 and $TotalProjectDays &lt;= 32">
                                <xsl:value-of select="$Target * 3"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="($Target div $TotalProjectDays) * 91"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:when test="$ResultantPeriodType = 'Yearly'">
                        <xsl:choose>
                            <xsl:when test="$TotalProjectDays &gt;=360 and $TotalProjectDays &lt;= 365">
                                <xsl:value-of select="$Target"/>
                            </xsl:when>
                            <xsl:when test="$TotalProjectDays &gt;=178 and $TotalProjectDays &lt;= 185">
                                <xsl:value-of select="$Target * 2"/>
                            </xsl:when>
                            <xsl:when test="$TotalProjectDays &gt;=88 and $TotalProjectDays &lt;= 93">
                                <xsl:value-of select="$Target * 4"/>
                            </xsl:when>
                            <xsl:when test="$TotalProjectDays &gt;=58 and $TotalProjectDays &lt;= 63">
                                <xsl:value-of select="$Target * 6"/>
                            </xsl:when>
                            <xsl:when test="$TotalProjectDays &gt;=29 and $TotalProjectDays &lt;= 32">
                                <xsl:value-of select="$Target * 12"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="($Target div $TotalProjectDays) * 365"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:when>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:choose>
                    <xsl:when test="$SourcePeriodType = 'Daily'">
                        <xsl:choose>
                            <xsl:when test="$ResultantPeriodType = 'Daily'">
                                <xsl:value-of select ="$Target"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Weekly'">
                                <xsl:value-of select ="$Target * 7"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Monthly'">
                                <xsl:value-of select="$Target * 30"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Quarterly'">
                                <xsl:value-of select="$Target * 91"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Yearly'">
                                <xsl:value-of select="$Target * 365"/>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:when test="$SourcePeriodType = 'Weekly'">
                        <xsl:choose>
                            <xsl:when test="$ResultantPeriodType = 'Daily'">
                                <xsl:value-of select ="$Target div 7"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Weekly'">
                                <xsl:value-of select ="$Target"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Monthly'">
                                <xsl:value-of select="($Target div 7) * 30"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Quarterly'">
                                <xsl:value-of select="$Target * 13"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Yearly'">
                                <xsl:value-of select="($Target div 7) * 365"/>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:when test="$SourcePeriodType = 'Monthly'">
                        <xsl:choose>
                            <xsl:when test="$ResultantPeriodType = 'Daily'">
                                <xsl:value-of select ="$Target div 30"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Weekly'">
                                <xsl:value-of select ="($Target div 30) * 7"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Monthly'">
                                <xsl:value-of select="$Target"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Quarterly'">
                                <xsl:value-of select="$Target * 3"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Yearly'">
                                <xsl:value-of select="$Target * 12"/>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:when test="$SourcePeriodType = 'Quarterly'">
                        <xsl:choose>
                            <xsl:when test="$ResultantPeriodType = 'Daily'">
                                <xsl:value-of select ="$Target div 91"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Weekly'">
                                <xsl:value-of select ="$Target div 13"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Monthly'">
                                <xsl:value-of select="$Target div 3"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Quarterly'">
                                <xsl:value-of select="$Target"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Yearly'">
                                <xsl:value-of select="$Target * 4"/>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:when test="$SourcePeriodType = 'Yearly'">
                        <xsl:choose>
                            <xsl:when test="$ResultantPeriodType = 'Daily'">
                                <xsl:value-of select ="$Target div 365"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Weekly'">
                                <xsl:value-of select ="($Target div 365) * 7"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Monthly'">
                                <xsl:value-of select="$Target div 12"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Quarterly'">
                                <xsl:value-of select="$Target div 4"/>
                            </xsl:when>
                            <xsl:when test="$ResultantPeriodType = 'Yearly'">
                                <xsl:value-of select="$Target"/>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:when>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>



    </xsl:template>

    <xsl:template name="YTDTabOn">
        <xsl:param name="ViewMode" select="'N/A'"></xsl:param>
        <xsl:choose>
            <xsl:when test="$ViewMode = 'Yearly'">
                <xsl:number value="0"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:number value="1"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="TotalNumberOfCell">
        <xsl:param name="ActivePeriodKeys"></xsl:param>
        <xsl:param name="ViewMode" select="'N/A'"></xsl:param>

        <xsl:choose>
            <xsl:when test="$ViewMode = 'Yearly'">
                <xsl:value-of select="count($ActivePeriodKeys/PeriodKey)+5"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="count($ActivePeriodKeys/PeriodKey)+4"/>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:template>

</xsl:stylesheet>
