﻿<?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:import href="../GenericTemplates/StringReplace.xslt"/>
    
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <xsl:variable name="NumberGoalDataXml" select="AjaxRequest/NumberGoalData/."></xsl:variable>
        <xsl:variable name="UserID" select="AjaxRequest/Params/UserID"></xsl:variable>
        <xsl:variable name="MeticKey" select="AjaxRequest/Params/MetricKey"></xsl:variable>
        <xsl:variable name="SourceItemID" select="AjaxRequest/Params/SourceItemID"></xsl:variable>
        <xsl:variable name="BaseYear">
            <xsl:choose>
                <xsl:when test="AjaxRequest/Params/BaseYear[text()]">
                    <xsl:value-of select="AjaxRequest/Params/BaseYear"/>
                </xsl:when>
                <xsl:when test="AjaxRequest/BaseYear[text()]">
                    <xsl:value-of select="AjaxRequest/BaseYear"/>
                </xsl:when>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="TemplateURL" select="AjaxRequest/TemplateURL"></xsl:variable>
        <xsl:variable name="PermissionID" select="AjaxRequest/PermissionTypeID"></xsl:variable>

        <!--NumbersEnteredBy Determine whether user can enter or edit data
            If NumbersEnteredBy = 1 or -99 then user can add/edit numbers
            If NumbersEnteredBy = 0 then user can't add with-->
        <xsl:variable name="NumbersEnteredBy">
            <xsl:choose>
                <!--NumbersEnteredBy = -99 allows user to Add/Edit as in this Goal this field isn't implemented yet-->
                <xsl:when test="AjaxRequest/NumbersEnteredBy= -99">
                    <xsl:number value="1" />
                </xsl:when>
                <!--User is Participant and NumbersEnteredBy = 1 means only for Participants-->
                <xsl:when test="AjaxRequest/NumbersEnteredBy= 1 and AjaxRequest/GoalPermissions/Permissions/ItemPermission/Participant = 1">
                    <xsl:number value="1"/>
                </xsl:when>
                <!--User is Coach and NumbersEnteredBy = 1,2,3 means only for Coach-->
                <xsl:when test="(AjaxRequest/NumbersEnteredBy= 1 or AjaxRequest/NumbersEnteredBy= 2) 
                            and AjaxRequest/GoalPermissions/Permissions/ItemPermission/Coach = 1">
                    <xsl:number value="1"/>
                </xsl:when>
                <!--User is Portal Admin and NumbersEnteredBy = 1,2,3 means only for Portal Admin-->
                <xsl:when test="(AjaxRequest/NumbersEnteredBy= 1 or AjaxRequest/NumbersEnteredBy= 2 or AjaxRequest/NumbersEnteredBy= 3)  and $PermissionID &gt;= 4">
                    <xsl:number value="1"/>
                </xsl:when>
                <!--NumbersEnteredBy = 4 means Auto not for System-->
                <xsl:when test="AjaxRequest/NumbersEnteredBy= 4">
                    <xsl:number value="0"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:number value ="0"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <div id="div_{$SourceItemID}_NumbersPanel">
            <xsl:choose>
                <xsl:when test="$NumberGoalDataXml/NumberGoal">
                    <div id="div_{$SourceItemID}_NumbersPanel_Header" class="padding5" style="text-align: center;">
                        <div>
                            <span class="labelGoalActionsText">
                                <xsl:value-of select="$BaseYear"></xsl:value-of>&#xa0;<xsl:text>Income Goal</xsl:text>
                            </span>
                        </div>
                        <div>
                            <span class="labelGoalActionsText">
                                <!--&#xa0;-->
                            </span>
                        </div>
                    </div>
                    <div id="div_{$SourceItemID}_NumbersPanel_Body">
                        <xsl:call-template name="NumberTabDataEntryPanel">
                            <xsl:with-param name="NumberGoalDataXml" select="$NumberGoalDataXml"></xsl:with-param>
                            <xsl:with-param name="UserID" select="$UserID"></xsl:with-param>
                            <xsl:with-param name="MeticKey" select="$MeticKey"></xsl:with-param>
                            <xsl:with-param name="SourceItemID" select="$SourceItemID"></xsl:with-param>
                            <xsl:with-param name="BaseYear" select="$BaseYear"></xsl:with-param>
                            <xsl:with-param name="TemplateURL" select="$TemplateURL"></xsl:with-param>
                            <xsl:with-param name="NumbersEnteredBy" select="$NumbersEnteredBy"></xsl:with-param>
                            <xsl:with-param name="PermissionID" select="$PermissionID"></xsl:with-param>
                        </xsl:call-template>
                    </div>
                </xsl:when>
            </xsl:choose>
        </div>
    </xsl:template>

    <xsl:template name="NumberTabDataEntryPanel">
        <xsl:param name="NumberGoalDataXml" select="'N/A'"></xsl:param>
        <xsl:param name="UserID" select="'N/A'"></xsl:param>
        <xsl:param name="MeticKey" select="'N/A'"></xsl:param>
        <xsl:param name="SourceItemID" select="'N/A'"></xsl:param>
        <xsl:param name="BaseYear" select="'N/A'"></xsl:param>
        <xsl:param name="TemplateURL" select="'N/A'"></xsl:param>
        <xsl:param name="NumbersEnteredBy" select="'N/A'"></xsl:param>
        <xsl:param name="PermissionID" select="'N/A'"></xsl:param>

        <xsl:variable name="Divisor">
            <xsl:choose>
                <xsl:when test="$NumberGoalDataXml/PeriodType = 'Monthly'">
                    <xsl:number value="2"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:number value="0"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <table class="tableMaster" cellpadding="2" cellspacing="0">

            <xsl:for-each select="$NumberGoalDataXml/NumberGoal">
                <xsl:variable name="NumberFormat" select="../NumberFormat"></xsl:variable>

                <tr>
                    <xsl:call-template name="NumberPanelCell">
                        <xsl:with-param name="DisplayLabel" select="DisplayLabel"></xsl:with-param>
                        <xsl:with-param name="DefaultPeriodKey" select="DefaultPeriodKey"></xsl:with-param>
                        <xsl:with-param name="Amount" select="round(Amount)"></xsl:with-param>
                        <xsl:with-param name="NewPeriodKey" select="NewPeriodKey"></xsl:with-param>
                        <xsl:with-param name="SourceItemID" select="$SourceItemID"></xsl:with-param>
                        <xsl:with-param name="NumberFormat" select="$NumberFormat"></xsl:with-param>
                        <xsl:with-param name="UserID" select="$UserID"></xsl:with-param>
                        <xsl:with-param name="NumbersEnteredBy" select="$NumbersEnteredBy"></xsl:with-param>
                        <xsl:with-param name="MeticKey" select="$MeticKey"></xsl:with-param>
                        <xsl:with-param name="TemplateURL" select="$TemplateURL"></xsl:with-param>
                        <xsl:with-param name="TabIndex" select="position()"></xsl:with-param>
                    </xsl:call-template>
                   <!-- <xsl:call-template name="NumberPanelCell">
                        <xsl:with-param name="DisplayLabel" select="DisplayLabel"></xsl:with-param>
                        <xsl:with-param name="DefaultPeriodKey" select="DefaultPeriodKey"></xsl:with-param>
                        <xsl:with-param name="Amount" select="round(Amount)"></xsl:with-param>
                        <xsl:with-param name="NewPeriodKey" select="NewPeriodKey"></xsl:with-param>
                        <xsl:with-param name="SourceItemID" select="$SourceItemID"></xsl:with-param>
                        <xsl:with-param name="NumberFormat" select="$NumberFormat"></xsl:with-param>
                        <xsl:with-param name="UserID" select="$UserID"></xsl:with-param>
                        <xsl:with-param name="NumbersEnteredBy" select="$NumbersEnteredBy"></xsl:with-param>
                        <xsl:with-param name="MeticKey" select="$MeticKey"></xsl:with-param>
                        <xsl:with-param name="TemplateURL" select="$TemplateURL"></xsl:with-param>
                        <xsl:with-param name="TabIndex" select="position()"></xsl:with-param>
                    </xsl:call-template>

                    <xsl:variable name="nextRightCell">
                        <xsl:value-of select="position()+6"/>
                    </xsl:variable>
                    
                    <xsl:if test="following-sibling::node()[2] and $Divisor &gt; 0">
                        <td style="border-right:1px solid #5E5E5E;"></td>
                        <xsl:call-template name="NumberPanelCell">
                            <xsl:with-param name="DisplayLabel" select="following-sibling::node()[6]/DisplayLabel"></xsl:with-param>
                            <xsl:with-param name="DefaultPeriodKey" select="following-sibling::node()[6]/DefaultPeriodKey"></xsl:with-param>
                            <xsl:with-param name="Amount" select="round(following-sibling::node()[6]/Amount)"></xsl:with-param>
                            <xsl:with-param name="NewPeriodKey" select="following-sibling::node()[6]/NewPeriodKey"></xsl:with-param>
                            <xsl:with-param name="SourceItemID" select="$SourceItemID"></xsl:with-param>
                            <xsl:with-param name="NumberFormat" select="$NumberFormat"></xsl:with-param>
                            <xsl:with-param name="UserID" select="$UserID"></xsl:with-param>
                            <xsl:with-param name="NumbersEnteredBy" select="$NumbersEnteredBy"></xsl:with-param>
                            <xsl:with-param name="MeticKey" select="$MeticKey"></xsl:with-param>
                            <xsl:with-param name="TemplateURL" select="$TemplateURL"></xsl:with-param>
                            <xsl:with-param name="TabIndex" select="position()+6"></xsl:with-param>                            
                        </xsl:call-template>
                    </xsl:if>-->

                </tr>
            </xsl:for-each>

            <xsl:variable name="TotalAmount" select="sum($NumberGoalDataXml/NumberGoal/Amount)"></xsl:variable>
            <xsl:variable name="TotalRowDisplay">
                <xsl:choose>
                    <xsl:when test="string($TotalAmount)!='NaN' and $TotalAmount &gt; 0">
                        <xsl:text>display:table-Row;</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>display:none;</xsl:otherwise>
                </xsl:choose>
            </xsl:variable>

            <tr id="row_Amount_{$SourceItemID}_Total" style="{$TotalRowDisplay}">
                <td></td>
                <td>
                    <span class="labelGoalActionsText">Total :</span>
                </td>
                <td class="verticallyMiddle textRight" nowrap="nowrap">


                    <span class="labelGoalActionsText">
                        <xsl:choose>
                            <xsl:when test ="$NumberGoalDataXml/NumberFormat = '$' and string($TotalAmount)!='NaN'">
                                <span style="margin-right:2px;">$</span>
                            </xsl:when>
                            <xsl:when test ="$NumberGoalDataXml/NumberFormat = '#' and string($TotalAmount)!='NaN'">
                                <span style="margin-right:2px;"></span>
                            </xsl:when>
                        </xsl:choose>
                        <span id="span_Amount_{$SourceItemID}_Total">
                            <xsl:value-of select="format-number($TotalAmount, '###,###')"/>
                        </span>
                    </span>

                </td>
                <td></td>
            </tr>
        </table>
    </xsl:template>


    <xsl:template name="NumberPanelCell">
        <xsl:param name="DisplayLabel" select="'N/A'"></xsl:param>
        <xsl:param name="DefaultPeriodKey" select="'N/A'"></xsl:param>
        <xsl:param name="Amount" select="'N/A'"></xsl:param>
        <xsl:param name="NewPeriodKey" select="'N/A'"></xsl:param>

        <xsl:param name="SourceItemID" select="'N/A'"></xsl:param>
        <xsl:param name="NumberFormat" select="'N/A'"></xsl:param>
        <xsl:param name="UserID" select="'N/A'"></xsl:param>
        <xsl:param name="NumbersEnteredBy" select="'N/A'"></xsl:param>
        <xsl:param name="MeticKey" select="'N/A'"></xsl:param>
        <xsl:param name="TemplateURL" select="'N/A'"></xsl:param>
        <xsl:param name="TabIndex" select="'N/A'"></xsl:param>


        <xsl:variable name="PeriodKey">
            <xsl:choose>
                <xsl:when test="$DefaultPeriodKey != ''">
                    <xsl:value-of select="$DefaultPeriodKey"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$NewPeriodKey"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="DisplayTextBox">
            <xsl:choose>
                <xsl:when test ="string($Amount)!='NaN' or $NumbersEnteredBy = 0">
                    <xsl:text>display:none;</xsl:text>
                </xsl:when>
                <xsl:otherwise>display:inline;</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="DisplayImage">
            <xsl:choose>
                <xsl:when test ="string($Amount)!='NaN'">
                    <xsl:text>display:inline;</xsl:text>
                </xsl:when>
                <xsl:otherwise>display:none;</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>


        <xsl:variable name="TextFirstPeriodVisible">
            <xsl:choose>
                <xsl:when test ="format-number($Amount, '###,###') != 'NaN'">
                    <xsl:text>display:inline;</xsl:text>
                </xsl:when>
                <xsl:otherwise>display:none;</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <td>
        </td>
        <td width="30%">
            <span class="labelGoalActionsText">
                <xsl:value-of select="$DisplayLabel"/>
            </span>
        </td>
        <td width="15%" class="verticallyMiddle textRight" nowrap="nowrap" id="NumDtPnWid55">
            <span class="labelGoalActionsText">
                <span id="span_Amount_{$SourceItemID}_{$PeriodKey}">
                    <xsl:choose>
                        <xsl:when test ="$NumberFormat = '$' and string($Amount)!='NaN'">
                            <span style="margin-right:2px;">$</span>
                        </xsl:when>
                        <xsl:when test ="$NumberFormat = '#' and string($Amount)!='NaN'">
                            <span style="margin-right:2px;"></span>
                        </xsl:when>
                    </xsl:choose>
                    <span id="span_Amount_lbl_{$SourceItemID}_{$PeriodKey}" style="{$TextFirstPeriodVisible}">
                        <xsl:choose>
                            <xsl:when test="$Amount = 0">
                                <xsl:value-of select="$Amount"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select ="format-number($Amount, '###,###')"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </span>
                </span>
            </span>

            <xsl:variable name="EscapedMetricKey">
                <xsl:call-template name="replaceCharsInString">
                    <xsl:with-param name="stringIn" select="$MeticKey"></xsl:with-param>
                    <xsl:with-param name="charsIn">
                        <xsl:text>'</xsl:text>
                    </xsl:with-param>
                    <xsl:with-param name="charsOut">
                        <xsl:text>\'</xsl:text>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:variable>
            
            <xsl:choose>
                <xsl:when test ="string($Amount)!='NaN'">
                    <input id="txt_PeriodKeyValue_{$SourceItemID}_{$PeriodKey}" type="text" value="{$Amount}" style="width:50px;{$DisplayTextBox}"
                           name="txt_PeriodKeyValue_{$SourceItemID}_Value"
                           tabindex="{$TabIndex}"
                               onblur="SaveAmountForPeriodKey('txt_PeriodKeyValue_{$SourceItemID}_{$PeriodKey}','{$PeriodKey}','{$SourceItemID}','{$SourceItemID}','{$UserID}','{$NewPeriodKey}','{$NumberFormat}','{$EscapedMetricKey}',-99,'');
                                            numberOnlyForNumbers('txt_PeriodKeyValue_{$SourceItemID}_{$PeriodKey}');"></input>
                </xsl:when>
                <xsl:otherwise>
                    <input id="txt_PeriodKeyValue_{$SourceItemID}_{$PeriodKey}" type="text" style="width:50px;{$DisplayTextBox}"
                           name="txt_PeriodKeyValue_{$SourceItemID}_Value"
                           tabindex="{$TabIndex}"
                           onblur="SaveAmountForPeriodKey('txt_PeriodKeyValue_{$SourceItemID}_{$PeriodKey}','{$PeriodKey}','{$SourceItemID}','{$SourceItemID}','{$UserID}','{$NewPeriodKey}','{$NumberFormat}','{$EscapedMetricKey}',-99,'');                                                      
                                            numberOnlyForNumbers('txt_PeriodKeyValue_{$SourceItemID}_{$PeriodKey}');"></input>
                </xsl:otherwise>
            </xsl:choose>

        </td>
        <td width="10%">
            <!--NumbersEnteredBy Determine whether user can enter or edit data
                        If NumbersEnteredBy = 1 or -99 then user can add/edit numbers
                        If NumbersEnteredBy = 0 then user can't add with-->
            <xsl:if test="$NumbersEnteredBy = 1">
                <img id="img_EditIcon_{$SourceItemID}_{$PeriodKey}" src="{$TemplateURL}Web/Images/Ver_2_0/edit_goal.png"
                         style="display:inline;cursor:pointer;{$DisplayImage}"
                         class="marginLeft5 marginRight5 verticallyMiddle paddingBottom5"
                         onclick="document.getElementById('span_Amount_{$SourceItemID}_{$PeriodKey}').style.display = 'none';
                              document.getElementById('txt_PeriodKeyValue_{$SourceItemID}_{$PeriodKey}').style.display = 'inline';
                              document.getElementById('txt_PeriodKeyValue_{$SourceItemID}_{$PeriodKey}').focus();"/>
            </xsl:if>
        </td>
        <td width="45%" id="NumHideInMbp">
        </td>

    </xsl:template>

</xsl:stylesheet>
