<!--                                                                  -->
<!--  Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved     -->
<!--  $Id$  -->
<!--                                                                  -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:dyn="http://exslt.org/dynamic" extension-element-prefixes="dyn">

	<xsl:variable name="pendingTransactionCount" select="count(/Root/Form/Account/FinanceAccount/PendingTransactionList/PendingTransaction)" />

	<xsl:variable name="pendingPrefix">Form_Account_FinanceAccount_PendingTransactionList_PendingTransaction_</xsl:variable>

    <xsl:template name="payorTransactionLayout">
        <form name="payorTransaction" id="payorTransaction" method="post" action="{$area}">
            <xsl:call-template name="payorTransactionEdit" />
            <xsl:call-template name="buttons" />
        </form>
    </xsl:template>


    <xsl:template name="payorTransactionEdit">
        <div class="section">
            <xsl:if test="$area = 'license' or $area = 'ca_license' or $area = 'publisher' or $area = 'ca_publisher'">
                <xsl:call-template name="helpLink" />
            </xsl:if>

            <input type="hidden" name="formSubmit" value="1" />
            <input type="hidden" name="c" value="{$cmd}" />

		    <xsl:if test="Params/PayorID">
			    <input type="hidden" name="PayorID" value="{Params/PayorID}" />
		    </xsl:if>

            <xsl:if test="Params/ArtistPayeeID">
                <input type="hidden" name="ArtistPayeeID" value="{Params/ArtistPayeeID}" />
            </xsl:if>

            <xsl:if test="Params/LabelPayeeID">
                <input type="hidden" name="LabelPayeeID" value="{Params/LabelPayeeID}" />
            </xsl:if>

            <xsl:if test="Params/PublisherID">
                <input type="hidden" name="PublisherID" value="{Params/PublisherID}" />
            </xsl:if>

            <xsl:if test="Params/TrackLicenseID">
                <input type="hidden" name="TrackLicenseID" value="{Params/TrackLicenseID}" />
            </xsl:if>

            <xsl:if test="$area = 'publisher' or $area = 'ca_publisher'">
                <xsl:call-template name="publisherView">
                    <xsl:with-param name="publisher" select="Form/Publisher" />
                </xsl:call-template>
            </xsl:if>

            <xsl:if test="$area = 'artist_payee'">
                <xsl:call-template name="payeeView">
                    <xsl:with-param name="payee" select="Form/ArtistPayee" />
                </xsl:call-template>
            </xsl:if>

            <xsl:if test="$area = 'label_payee'">
                <xsl:call-template name="payeeView">
                    <xsl:with-param name="payee" select="Form/LabelPayee" />
                </xsl:call-template>
            </xsl:if>

            <xsl:if test="($area = 'license' or $area = 'ca_license') and $cmd = 'account'">
                <xsl:call-template name="licenseView">
                    <xsl:with-param name="licenseTitle"><xsl:value-of select="$licenseHeading" /></xsl:with-param>
                    <xsl:with-param name="license" select="Form/TrackLicense" />
    		        <xsl:with-param name="track" select="Form/Track" />
                </xsl:call-template>
            </xsl:if>

            <xsl:if test="($area = 'license' or $area = 'ca_license') and $cmd = 'crossed_account'">
                <xsl:call-template name="licenseView">
                    <xsl:with-param name="crossed" select="'1'" />
                    <xsl:with-param name="licenseTitle">Crossed <xsl:value-of select="$licenseHeading" />s</xsl:with-param>
                    <xsl:with-param name="license" select="Form/Account/CrossedLicenseList" />
    			    <xsl:with-param name="track" select="Form/Track" />
                </xsl:call-template>
            </xsl:if>

  			<xsl:call-template name="openRunStatusMessage" />

    		<xsl:if test="$area = 'publisher' or $area = 'ca_publisher' or $area = 'artist_payee' or $area = 'label_payee'">
    			<br />
    			<xsl:call-template name="payorMinPayment" />
    		</xsl:if>

    		<xsl:if test="$area = 'publisher' or $area = 'ca_publisher' or $area = 'artist_payee' or $area = 'label_payee' or $area = 'license' or $area = 'ca_license'">
    			<br />
    			<script type="text/javascript" src="/production/javascript/validateTransactions.js?t={$now}" />
      	        <xsl:call-template name="pendingTransactions" />
    		</xsl:if>

            <xsl:if test="Form/Account/FinanceAccount/TransactionList/Transaction">
                <br />
                <xsl:call-template name="payorTransactionHistory" />
            </xsl:if>
            <br /><br />
        </div>
    </xsl:template>


	<xsl:template name="payorMinPayment">
        <fieldset id="payorMinPayment">
            <legend>Payor</legend>
            <table class="textChart">
				<tr>
					<th>Name</th>
					<th>Client Payor #</th>
					<th>Status</th>
					<th>Default</th>
					<th>Minimum Payment (<xsl:value-of select="Form/Account/FinanceAccount/CurrencyCode" />)</th>
				</tr>
                <tr class="row1">
					<td>
						<xsl:value-of select="Form/Payor/Name" />
					</td>
					<td>
						<xsl:value-of select="Form/Payor/ClientPayorID" />
					</td>
					<td>
						<xsl:if test="Form/Payor/Inactive = 1">Inactive</xsl:if>
						<xsl:if test="Form/Payor/Inactive = 0">Active</xsl:if>
					</td>
                    <td>
                        <xsl:if test="Form/Payor/IsDefault = 1">Yes</xsl:if>
                        <xsl:if test="Form/Payor/IsDefault = 0">No</xsl:if>
                    </td>
 		            <td>
                        <xsl:call-template name="inlineError">
                            <xsl:with-param name="path">Form/Account/MinPayment</xsl:with-param>
                            <xsl:with-param name="displayName">Minimum Payment</xsl:with-param>
                        </xsl:call-template>
                        <input name="Form_Account_MinPayment" class="dollarField" id="Form_Account_MinPayment" type="text" value="{Form/Account/MinPayment}">
                            <xsl:if test="$openRuns &gt; 0">
                                <xsl:attribute name="disabled">disabled</xsl:attribute>
                            </xsl:if>
                        </input>
                    </td>
                </tr>
			</table>
		</fieldset>
	</xsl:template>


    <xsl:template name="payorTransactionHistory">
        <fieldset>
            <legend>Transaction History</legend>
            <table class="textChart hiOn" id="transactionHistory">
                <tr>
                    <th class="rowLeft typeCol">Type</th>
                    <th class="amount amountCol">Amount</th>
			        <th class="dateCol">Transaction Date</th>
			        <th class="checkCol">Check #</th>
                    <th class="memoCol">Memo</th>
                    <th class="amount balanceCol">Balance</th>
                </tr>
                <xsl:for-each select="Form/Account/FinanceAccount/TransactionList/Transaction">
                    <tr class="row{position() mod 2}">
                        <td class="rowLeft">
            	            <xsl:call-template name="transactionTypeCode">
            		            <xsl:with-param name="typeCode" select="TypeCode" />
            	            </xsl:call-template>
                        </td>
                        <td class="amount amountCol">
                            <xsl:value-of select="Amount" />
                        </td>
			            <td><xsl:value-of select="TransactionDate" /></td>
			            <td><xsl:value-of select="CheckNumber" /></td>
                        <td>
                            <xsl:call-template name="replaceLineBreak">
                                <xsl:with-param name="string" select="Memo"/>
                            </xsl:call-template>
                        </td>
                        <td class="amount">
                            <xsl:value-of select="Balance" />
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </fieldset>
    </xsl:template>


	<xsl:template name="transactionTypeCode">
		<xsl:param name="typeCode" />
		<xsl:if test="$typeCode = 1">Opening Balance</xsl:if>
		<xsl:if test="$typeCode = 2">Adjustment</xsl:if>
		<xsl:if test="$typeCode = 3">Advance</xsl:if>
		<xsl:if test="$typeCode = 4">Payment</xsl:if>
		<xsl:if test="$typeCode = 5">Closing Balance</xsl:if>
	</xsl:template>


    <xsl:template name="pendingTransactions">
        <fieldset>
            <legend>Pending Transactions</legend>
            <xsl:if test="//*[@e]">
                <xsl:call-template name="errorMessageGeneral" />
                <xsl:call-template name="pendingTransactionErrors" />
            </xsl:if>
            <p class="formHeader">Enter transaction information here.</p>
            <hr />
            <table class="textChart" id="transactionTable">
      	        <tbody>
        	        <tr>
        		        <th class="typeCol">Type</th>
        		        <th class="amount amountCol">
        			        Amount (<xsl:value-of select="Form/Account/FinanceAccount/CurrencyCode" />)
					        <xsl:call-template name="helpIcon">
						        <xsl:with-param name="tip">Positive Adjustment adds to the payee balance, a negative&lt;br&gt;Adjustment subtracts from the payee balance</xsl:with-param>
					        </xsl:call-template>
        		        </th>
        		        <th class="dateCol">Transaction Date</th>
        		        <th class="checkCol">Check #</th>
        		        <th class="memoCol">Memo</th>
        		        <xsl:if test="$pendingTransactionCount &gt; 0">
       				        <th class="icon balanceCol">Delete</th>
       			        </xsl:if>
        	        </tr>
      	            <xsl:choose>
						<xsl:when test="$pendingTransactionCount &gt; 0">
     					    <xsl:for-each select="Form/Account/FinanceAccount/PendingTransactionList/PendingTransaction">
              	                <xsl:call-template name="pendingTransactionRow" />
            	            </xsl:for-each>
          	            </xsl:when>
          	            <xsl:otherwise>
            	            <xsl:call-template name="pendingTransactionRow" />
          	            </xsl:otherwise>
        	        </xsl:choose>
                </tbody>
			</table>
			<xsl:if test="$openRuns = 0">
    			<xsl:variable name="addLink">
    				<xsl:choose>
    					<xsl:when test="$pendingTransactionCount = 0">javascript:addTransaction(1, '<xsl:value-of select="$calendarDateFormat" />')</xsl:when>
    					<xsl:otherwise>javascript:addTransaction(<xsl:value-of select="$pendingTransactionCount" />, '<xsl:value-of select="$calendarDateFormat" />')</xsl:otherwise>
    				</xsl:choose>
    			</xsl:variable>
                <div class="insideFieldset">
    		        <p class="addLink">
    		    	    <a href="{$addLink}" title="Add Another Transaction" id="addImageLink">
    		    		    <img src="/production/images/icons/office/Add-(16x16).gif" height="16" width="16" />
    		    	    </a> <a href="{$addLink}" id="addTextLink">Add Another Transaction</a>
        		    </p>
    			</div>
			</xsl:if>
        </fieldset>
    </xsl:template>


    <xsl:template name="pendingTransactionRow">
        <xsl:variable name="transactionPosition" select="position() - 1" />
        <tr class="row{position() mod 2}" id="transaction{position()}">
            <td class="rowLeft">
                <xsl:if test="TypeCode[@e]">
                    <xsl:attribute name="class">rowLeft errorInput</xsl:attribute>
                </xsl:if>
                <select name="{$pendingPrefix}{$transactionPosition}_TypeCode" id="{$pendingPrefix}{$transactionPosition}_TypeCode">
                    <xsl:if test="$openRuns &gt; 0">
                        <xsl:attribute name="disabled">disabled</xsl:attribute>
                    </xsl:if>
    	            <option value="">Choose...</option>
    	            <option value="2">
					    <xsl:if test="TypeCode = 2">
						    <xsl:attribute name="selected">selected</xsl:attribute>
					    </xsl:if>
    		            Adjustment
    	            </option>
    	            <option value="3">
					    <xsl:if test="TypeCode = 3">
						    <xsl:attribute name="selected">selected</xsl:attribute>
					    </xsl:if>
    		            Advance
    	            </option>
    	            <xsl:if test="$area != 'license'">
        	            <option value="4">
						    <xsl:if test="TypeCode = 4">
							    <xsl:attribute name="selected">selected</xsl:attribute>
						    </xsl:if>
        		            Payment
        	            </option>
    	            </xsl:if>
                </select>
		    </td>
            <td class="amount amountCol">
                <xsl:if test="Amount[@e]">
                    <xsl:attribute name="class">amount amountCol errorInput</xsl:attribute>
                </xsl:if>
                <input name="{$pendingPrefix}{$transactionPosition}_Amount" class="amountField" id="{$pendingPrefix}{$transactionPosition}_Amount" type="text" value="{Amount}">
                    <xsl:if test="$openRuns &gt; 0">
                        <xsl:attribute name="disabled">disabled</xsl:attribute>
                    </xsl:if>
                </input>
		    </td>
		    <td>
                <xsl:if test="TransactionDate[@e]">
                    <xsl:attribute name="class">errorInput</xsl:attribute>
                </xsl:if>
                <input type="text" class="dateField" name="{$pendingPrefix}{$transactionPosition}_TransactionDate" id="{$pendingPrefix}{$transactionPosition}_TransactionDate" value="{TransactionDate}">
                    <xsl:if test="$openRuns &gt; 0">
                        <xsl:attribute name="disabled">disabled</xsl:attribute>
                    </xsl:if>
                </input>
                <xsl:choose>
                    <xsl:when test="$openRuns &gt; 0">
                        <img src="/production/images/icons/office/Calendar-Disabled-(16x16).gif" height="16" width="16" class="disabledCalendarIcon" />
                    </xsl:when>
                    <xsl:otherwise>
        			    <a title="Select Transaction Date">
        				    <img src="/production/images/icons/office/Calendar-(16x16).gif" height="16" width="16" class="calendarIcon" id="calendarIcon{$transactionPosition}" />
        			    </a>
        			    <xsl:call-template name="calendar">
        				    <xsl:with-param name="fieldID"><xsl:value-of select="$pendingPrefix" /><xsl:value-of select="$transactionPosition" />_TransactionDate</xsl:with-param>
        				    <xsl:with-param name="buttonID">calendarIcon<xsl:value-of select="$transactionPosition" /></xsl:with-param>
        			    </xsl:call-template>
        			</xsl:otherwise>
                </xsl:choose>
		    </td>
            <td>
                <xsl:if test="CheckNumber[@e]">
                    <xsl:attribute name="class">errorInput</xsl:attribute>
                </xsl:if>
                <input name="{$pendingPrefix}{$transactionPosition}_CheckNumber" id="{$pendingPrefix}{$transactionPosition}_CheckNumber" type="text" value="{CheckNumber}" class="dollarField">
                    <xsl:if test="$openRuns &gt; 0">
                        <xsl:attribute name="disabled">disabled</xsl:attribute>
                    </xsl:if>
                </input>
		    </td>
            <td>
                <xsl:if test="Memo[@e]">
                    <xsl:attribute name="class">errorInput</xsl:attribute>
                </xsl:if>
                <input name="{$pendingPrefix}{$transactionPosition}_Memo" id="{$pendingPrefix}{$transactionPosition}_Memo" type="text" value="{Memo}" class="memoField">
                    <xsl:if test="$openRuns &gt; 0">
                        <xsl:attribute name="disabled">disabled</xsl:attribute>
                    </xsl:if>
                </input>
		    </td>
		    <xsl:if test="$pendingTransactionCount &gt; 0">
			    <td class="icon">
				    <input type="checkbox" name="{$pendingPrefix}{$transactionPosition}_Delete" id="{$pendingPrefix}{$transactionPosition}_Delete" value="1">
                        <xsl:if test="$openRuns &gt; 0">
                            <xsl:attribute name="disabled">disabled</xsl:attribute>
                        </xsl:if>
				    </input>
			    </td>
		    </xsl:if>
	    </tr>
	</xsl:template>


    <xsl:template name="pendingTransactionErrors">
        <div class="error">
            <xsl:if test="//TypeCode[@e]">Type must be selected.<br /></xsl:if>
            <xsl:if test="//Amount[@e = 'field_blank']">Amount cannot be blank.<br /></xsl:if>
            <xsl:if test="//Amount[@e = 'field_invalid']">Amount is invalid.<br /></xsl:if>
            <xsl:if test="//Amount[@e = 'field_out_of_range']">Amount must be a positive number.<br /></xsl:if>
            <xsl:if test="//TransactionDate[@e]">Transaction Date is invalid.<br /></xsl:if>
            <xsl:if test="//CheckNumber[@e]">Check # is invalid.<br /></xsl:if>
            <br />
        </div>
    </xsl:template>


    <xsl:template name="payorList">
        <xsl:variable name="accountCurrencyCode">
            <xsl:choose>
                <xsl:when test="$area = 'publisher'">USD</xsl:when>
                <xsl:when test="$area = 'ca_publisher'">CAD</xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="Client/Locale/CurrencyFormat/CurrencyCode" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <fieldset id="payorViewTable">
            <legend>Payors</legend>
            <table class="textChart">
                <tbody>
                    <tr>
                        <th>Payor</th>
                        <xsl:if test="$area = 'publisher' or $area = 'ca_publisher'">
                            <th class="amount">License Count</th>
                        </xsl:if>
                        <th class="amount">Minimum Payment (<xsl:value-of select="$accountCurrencyCode" />)</th>
                        <th class="amount">
            	            Pending Transactions (<xsl:value-of select="$accountCurrencyCode" />)
							<xsl:call-template name="helpIcon">
								<xsl:with-param name="tip">Sum of all non-committed transactions</xsl:with-param>
							</xsl:call-template>
                        </th>
                        <th class="icon">Edit</th>
                    </tr>
                    <xsl:for-each select="Form/PayorList/Payor">
						<xsl:variable name="payorID" select="PayorID" />
                        <tr class="row{position() mod 2}">
                            <td>
                                <xsl:value-of select="Name" />
                            </td>
                            <xsl:if test="$area = 'publisher' or $area = 'ca_publisher'">
                                <td class="amount">
                                    <xsl:choose>
                                        <xsl:when test="PublisherLicenseCount &gt; 0">
                                            <a href="?c=list_licenses&amp;publisherID={/Root/Params/PublisherID}&amp;payorID={$payorID}"><xsl:value-of select="PublisherLicenseCount" /></a>
                                        </xsl:when>
                                        <xsl:otherwise>0</xsl:otherwise>
                                    </xsl:choose>
                                </td>
                            </xsl:if>
                            <td class="amount">
								<xsl:if test="/Root/Form/Publisher/Account[PayorID = $payorID]">
		                            <xsl:if test="normalize-space(/Root/Form/Publisher/Account[PayorID = $payorID]/MinPayment)"><xsl:value-of select="/Root/Form/Publisher/Account[PayorID = $payorID]/FinanceAccount/Balance/@currency_symbol" /></xsl:if>
    		                        <xsl:value-of select="/Root/Form/Publisher/Account[PayorID = $payorID]/MinPayment" />
								</xsl:if>
								<xsl:if test="/Root/Form/ArtistPayee/Account[PayorID = $payorID]">
									<xsl:if test="normalize-space(/Root/Form/ArtistPayee/Account[PayorID = $payorID]/MinPayment)"><xsl:value-of select="/Root/Form/ArtistPayee/Account[PayorID = $payorID]/FinanceAccount/Balance/@currency_symbol" /></xsl:if>
                                    <xsl:value-of select="/Root/Form/ArtistPayee/Account[PayorID = $payorID]/MinPayment" />
                                </xsl:if>
								<xsl:if test="/Root/Form/LabelPayee/Account[PayorID = $payorID]">
									<xsl:if test="normalize-space(/Root/Form/LabelPayee/Account[PayorID = $payorID]/MinPayment)"><xsl:value-of select="/Root/Form/LabelPayee/Account[PayorID = $payorID]/FinanceAccount/Balance/@currency_symbol" /></xsl:if>
                                    <xsl:value-of select="/Root/Form/LabelPayee/Account[PayorID = $payorID]/MinPayment" />
                                </xsl:if>
                            </td>
                            <td class="amount pendingCol">
                                <xsl:if test="/Root/Form/Publisher/Account[PayorID = $payorID]">
                                    <xsl:call-template name="formatDollars">
                                        <xsl:with-param name="dollarAmount" select="/Root/Form/Publisher/Account[PayorID = $payorID]/FinanceAccount/PendingBalance" />
                                    </xsl:call-template>
                                </xsl:if>
                                <xsl:if test="/Root/Form/ArtistPayee/Account[PayorID = $payorID]">
                                    <xsl:call-template name="formatDollars">
                                        <xsl:with-param name="dollarAmount" select="/Root/Form/ArtistPayee/Account[PayorID = $payorID]/FinanceAccount/PendingBalance" />
                                    </xsl:call-template>
                                </xsl:if>
                                <xsl:if test="/Root/Form/LabelPayee/Account[PayorID = $payorID]">
                                    <xsl:call-template name="formatDollars">
                                        <xsl:with-param name="dollarAmount" select="/Root/Form/LabelPayee/Account[PayorID = $payorID]/FinanceAccount/PendingBalance" />
                                    </xsl:call-template>
                                </xsl:if>
                            </td>
                            <td class="icon">
                                <a title="Edit">
									<xsl:attribute name="href">
										<xsl:value-of select="$area" />?PayorID=<xsl:value-of select="PayorID" />&amp;c=account&amp;<xsl:if test="$area = 'publisher' or $area = 'ca_publisher'">PublisherID=<xsl:value-of select="/Root/Form/Publisher/PublisherID" /></xsl:if><xsl:if test="$area = 'artist_payee'">ArtistPayeeID=<xsl:value-of select="/Root/Form/ArtistPayee/ArtistPayeeID" /></xsl:if><xsl:if test="$area = 'label_payee'">LabelPayeeID=<xsl:value-of select="/Root/Form/LabelPayee/LabelPayeeID" /></xsl:if>
									</xsl:attribute>
									<img src="/production/images/icons/office/Edit-(16x16).gif" height="16" width="16" />
								</a>
                            </td>
                        </tr>
                    </xsl:for-each>
                </tbody>
            </table>
        </fieldset>
    </xsl:template>

</xsl:stylesheet>
