<!--                                                                  -->
<!--  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:output method="html" indent="no" doctype-system="http://www.w3.org/TR/html4/strict.dtd" doctype-public="-//W3C//DTD HTML 4.01//EN" encoding="utf-8" />

    <xsl:include href="/app/tools/rps/templates/global.xsl" />
	<xsl:include href="/app/tools/rps/templates/publisher/shared.xsl" />	

    <xsl:template name="pickLayout">  
        <script type="text/javascript" src="/production/javascript/validate/payorTransactions.js?v=1" />  
        <form name="payorTransaction" id="payorTransaction" method="post" action="{$area}">
            <div class="section">   
                <xsl:call-template name="payeeInformationSection" />              
                <xsl:call-template name="payorTransactionEdit" />
            </div>
            <xsl:call-template name="buttons" />
        </form>      
        <xsl:call-template name="transactionTypeArray" />
    </xsl:template>


    <xsl:template name="payorTransactionEdit">

        <input type="hidden" name="formSubmit" value="1" />
        <input type="hidden" name="c" value="{$cmd}" />			
		<input type="hidden" name="PayorID" value="{Params/PayorID}" />
        <input type="hidden" name="PublisherID" value="{Params/PublisherID}" />	
        
        <xsl:if test="//*[@e]">
            <xsl:call-template name="errorMessageGeneral" />
            <xsl:call-template name="pendingTransactionErrors" />
        </xsl:if>        	
		
		<xsl:if test="$cmd = 'edit_indirect'">
		
    		<!-- first let's do the active admin/agent -->
    		
    		<xsl:for-each select="Form/AccountList/Account[IndirectPublisherID = $indirectPublisherID]">
                <xsl:call-template name="transactions" /> 
            </xsl:for-each>
            
            <!-- and now the rest -->
    
    		<xsl:for-each select="Form/AccountList/Account[IndirectPublisherID != $indirectPublisherID]">
                <xsl:call-template name="transactions" /> 
            </xsl:for-each>  
        </xsl:if>
 
		<xsl:if test="$cmd = 'edit_direct'">
		
    		<!-- only one account, but let's reference it the same way -->
    		
    		<xsl:for-each select="Form/AccountList/Account">
                <xsl:call-template name="transactions" /> 
            </xsl:for-each>  
        </xsl:if> 
              
         
         <br />
            
    </xsl:template>


    <xsl:template name="payeeInformationSection">

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

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

        <xsl:call-template name="payeeInformation" />
        
        <br />
        
    </xsl:template>


	<xsl:template name="payeeInformation">
        <fieldset class="viewOnly">
            <legend>Payee Information</legend>
            <div class="column1">
                <table>
                    <tr>
                        <td class="leftCol">Publisher Name:</td>
                        <td><xsl:value-of select="Form/Publisher/PublisherName" /></td>
                    </tr>
                    <tr>
                        <td class="leftCol">Publisher Type:</td>
                        <td>
                            <xsl:choose>
                                <xsl:when test="Form/Publisher/IsAgency = 1">Agency</xsl:when>
                                <xsl:when test="Form/Publisher/IsAdmin = 1">Administrator</xsl:when>
                                <xsl:otherwise>Standard</xsl:otherwise>
                            </xsl:choose>
                        </td>
                    </tr>

		    <!-- Show CMRRA information if agency -->
                    <xsl:choose>
                       <xsl:when test="Form/Publisher/IsAgency = 1">
		          <tr>
                             <td class="leftCol">
                                <xsl:if test="$cmd = 'edit_direct'">
                                    <xsl:attribute name="style">padding-bottom: 4px</xsl:attribute>
                                </xsl:if>
                                CMRRA Manufacturer ID:
                             </td>
                             <td>
     		                <xsl:call-template name="manufacturerIDField" />
                             </td>
		          </tr>
		          <tr>
                             <td class="leftCol">
                                <xsl:if test="$cmd = 'edit_direct'">
                                    <xsl:attribute name="style">padding-bottom: 4px</xsl:attribute>
                                </xsl:if>
                                Manufacturer Name:
                             </td>
                             <td>
     		                <xsl:call-template name="manufacturerNameField" />
                             </td>

		          </tr>
		       </xsl:when>
                    </xsl:choose>

                    <xsl:if test="$cmd = 'edit_indirect'">
                        <tr>
                            <td class="leftCol">Admin:</td>
                            <td><xsl:value-of select="Form/Publisher/AdminName" /></td>
                        </tr>
                        <tr>
                            <td class="leftCol">Agent:</td>
                            <td><xsl:value-of select="Form/Publisher/AgentName" /></td>
                        </tr>
                    </xsl:if>   
                    <tr>
                        <td class="leftCol">Total # Licenses:</td>
                        <td>
 		                    <xsl:if test="$cmd = 'edit_indirect'">
                                <xsl:choose>
                                    <xsl:when test="$publisherDirect = 1">
                                        0
                                    </xsl:when>                
                                    <xsl:otherwise>
                                        <xsl:value-of select="Form/Payor/PublisherIndirectLicenseCount" />
                                    </xsl:otherwise>
                                </xsl:choose>
 		                    </xsl:if>
 		                    <xsl:if test="$cmd = 'edit_direct'">
                                <xsl:choose>
                                    <xsl:when test="$publisherDirect = 1">
                                        <xsl:value-of select="Form/Payor/PublisherTotalLicenseCount" />
                                    </xsl:when>                
                                    <xsl:otherwise>
                                        <xsl:value-of select="Form/Payor/PublisherDirectLicenseCount" />
                                    </xsl:otherwise>
                                </xsl:choose>
 		                    </xsl:if>
                        </td>
                    </tr>                                                                                                     
                </table>
            </div>
            <div class="column2">
                <table style="padding-top: 0; margin-top: 0;">
                    <tr>
                        <td class="leftCol">Payor Name:</td>
                        <td><xsl:value-of select="Form/Payor/Name" /></td>
                    </tr>
                    <tr>
                        <td class="leftCol">Client Payor #:</td>
                        <td><xsl:value-of select="Form/Payor/ClientPayorID" /></td>
                    </tr>
                    <tr>
                        <td class="leftCol">Status:</td>
                        <td>
    					    <xsl:if test="Form/Payor/Inactive = 1">Inactive</xsl:if>
    					    <xsl:if test="Form/Payor/Inactive = 0">Active</xsl:if>
    					</td>
                    </tr>
                    <tr>
                        <td class="leftCol">Default:</td>
                        <td>
    					    <xsl:if test="Form/Payor/IsDefault = 1">Yes</xsl:if>
    					    <xsl:if test="Form/Payor/IsDefault = 0">No</xsl:if>
    					</td>
                    </tr>       
                    <tr>
                        <td class="leftCol">
                            <xsl:if test="$cmd = 'edit_direct'">
                                <xsl:attribute name="style">padding-bottom: 4px</xsl:attribute>
                            </xsl:if>
                            Minimum Payment (<xsl:value-of select="$publisherCurrencyCode" />):</td>
                        <td>
     		                <xsl:choose>
     		                    <xsl:when test="$cmd = 'edit_indirect'">
                                    <xsl:choose>
                                        <xsl:when test="normalize-space($indirectPublisherType)">
                                            <i>See <xsl:value-of select="$indirectPublisherType" /></i>
                                        </xsl:when>
                                        <xsl:otherwise>N/A</xsl:otherwise>
                                    </xsl:choose>     		                    
     		                    </xsl:when>
     		                    <xsl:otherwise>
     		                        <xsl:call-template name="minPaymentField" />
     		                    </xsl:otherwise>
     		                </xsl:choose>                        
                        </td>  
                    </tr>                                        
                </table>
            </div>            
        </fieldset>
    </xsl:template>

    
    <xsl:template name="minPaymentField">
        <xsl:call-template name="inlineError">
            <xsl:with-param name="path">Form/AccountList/Account/MinPayment</xsl:with-param>
            <xsl:with-param name="displayName">Minimum Payment</xsl:with-param>
        </xsl:call-template>
        <input name="Form_AccountList_Account_0_MinPayment" class="dollarField" id="Form_AccountList_Account_0_MinPayment" type="text" value="{Form/AccountList/Account/MinPayment}">
            <xsl:if test="$openRuns &gt; 0">
                <xsl:attribute name="disabled">disabled</xsl:attribute>
            </xsl:if>
        </input>    
    </xsl:template>

    <!-- Template for CMRRA manufacturer ID -->
    <xsl:template name="manufacturerIDField">
        <xsl:call-template name="inlineError">
            <xsl:with-param name="path">Form/AccountList/Account/ManufacturerID</xsl:with-param>
            <xsl:with-param name="displayName">CMRRA Manufacturer ID:</xsl:with-param>
        </xsl:call-template>
        <input name="Form_AccountList_Account_0_ManufacturerID" id="Form_AccountList_Account_0_ManufacturerID" type="text" value="{Form/AccountList/Account/ManufacturerID}">
            <xsl:if test="$openRuns &gt; 0">
                <xsl:attribute name="disabled">disabled</xsl:attribute>
            </xsl:if>
        </input>    
    </xsl:template>

    <!-- Template for CMRRA manufacturer name -->
    <xsl:template name="manufacturerNameField">
        <xsl:call-template name="inlineError">
            <xsl:with-param name="path">Form/AccountList/Account/ManufacturerName</xsl:with-param>
            <xsl:with-param name="displayName">Manufacturer Name:</xsl:with-param>
        </xsl:call-template>
        <input name="Form_AccountList_Account_0_ManufacturerName" id="Form_AccountList_Account_0_ManufacturerName" type="text" value="{Form/AccountList/Account/ManufacturerName}">
            <xsl:if test="$openRuns &gt; 0">
                <xsl:attribute name="disabled">disabled</xsl:attribute>
            </xsl:if>
        </input>    
    </xsl:template>

    <xsl:template name="transactions">
        <xsl:variable name="transactionPubID" select="IndirectPublisherID" />
        <xsl:variable name="transactionPubName">
            <xsl:choose>
                <xsl:when test="/Root/Form/AccountList/PublisherList/Publisher/PublisherID = $transactionPubID">
                    <xsl:value-of select="/Root/Form/AccountList/PublisherList/Publisher[PublisherID = $transactionPubID]/PublisherName" /><xsl:if test="$transactionPubID != $indirectPublisherID"> (Historical)</xsl:if>
                </xsl:when>
                <xsl:otherwise><xsl:value-of select="$indirectPublisherName" /></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>    
        <xsl:call-template name="pendingTransactions">
            <xsl:with-param name="transactionPubName" select="$transactionPubName" />
        </xsl:call-template>
        <xsl:if test="FinanceAccount/TransactionList/Transaction">
            <xsl:call-template name="transactionHistory">
                <xsl:with-param name="transactionPubName" select="$transactionPubName" />
            </xsl:call-template>            
        </xsl:if>
    </xsl:template>


    <xsl:template name="pendingTransactions">
        <xsl:param name="transactionPubName" />
        <xsl:variable name="accountPosition" select="@i" />
        <xsl:variable name="pendingTransactionCount" select="count(FinanceAccount/PendingTransactionList/PendingTransaction)" />
        <xsl:variable name="pendingPrefix">Form_AccountList_Account_<xsl:value-of select="@i" />_FinanceAccount_PendingTransactionList_PendingTransaction_</xsl:variable>      
        <input type="hidden" name="TransactionCount{@i}" id="TransactionCount{@i}" value="{$pendingTransactionCount}" />  
        <fieldset>
            <legend>Pending Transactions<xsl:if test="$cmd = 'edit_indirect'"> - <xsl:value-of select="$transactionPubName" /></xsl:if></legend>
            <p class="formHeader">Enter transaction information here.</p>
            <hr />
            <table class="textChart pendingTransactions" id="transactionTable{@i}">
      	        <tbody>
        	        <tr>        	        
        		        <th class="typeCol">Type</th>
        		        <th class="amount">
        			        Amount (<xsl:value-of select="$publisherCurrencyCode" />) 
					        <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:with-param name="linkID">amountHelp</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="FinanceAccount/PendingTransactionList/PendingTransaction">
              	                <xsl:call-template name="pendingTransactionRow">
              	                    <xsl:with-param name="accountPosition" select="$accountPosition" />
              	                    <xsl:with-param name="pendingPrefix" select="$pendingPrefix" />
              	                    <xsl:with-param name="pendingTransactionCount" select="$pendingTransactionCount" />
              	                </xsl:call-template>
            	            </xsl:for-each>
          	            </xsl:when>
          	            <xsl:otherwise>
            	            <xsl:call-template name="pendingTransactionRow">
            	                <xsl:with-param name="accountPosition" select="$accountPosition" />
          	                    <xsl:with-param name="pendingPrefix" select="$pendingPrefix" />
          	                    <xsl:with-param name="pendingTransactionCount" select="$pendingTransactionCount" />
          	                </xsl:call-template>            	            
          	            </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:value-of select="$pendingPrefix" />',<xsl:value-of select="@i" />)</xsl:when>
    					<xsl:otherwise>javascript:addTransaction(<xsl:value-of select="$pendingTransactionCount" />, '<xsl:value-of select="$calendarDateFormat" />','<xsl:value-of select="$pendingPrefix" />',<xsl:value-of select="@i" />)</xsl:otherwise>
    				</xsl:choose>			
    			</xsl:variable>
                <div class="insideFieldset">
    		        <p class="addLink">
    		    	    <a href="{$addLink}" title="Add Another Transaction" id="addImageLink{@i}">
    		    		    <img src="/production/images/icons/office/Add-(16x16).gif" height="16" width="16" />
    		    	    </a> <a href="{$addLink}" id="addTextLink{@i}">Add Another Transaction</a>
        		    </p>
    			</div>	    			
			</xsl:if>
        </fieldset>
        <br />
    </xsl:template>
  
  
    <xsl:template name="pendingTransactionRow">
        <xsl:param name="accountPosition" />
        <xsl:param name="pendingPrefix" />
        <xsl:param name="pendingTransactionCount" />
        <xsl:variable name="transactionPosition" select="position() - 1" />
        <xsl:variable name="transactionAccountID" select="AccountID" />
        <tr class="row{position() mod 2}" id="transaction{position()}">
        
            <td>
                <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" id="calendarLink{$accountPosition}_{$transactionPosition}">
        				    <img src="/production/images/icons/office/Calendar-(16x16).gif" height="16" width="16" class="calendarIcon" id="calendarIcon{$accountPosition}_{$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="$accountPosition" />_<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="transactionHistory">  
        <xsl:param name="transactionPubName" />
        <fieldset>
            <legend>Transaction History<xsl:if test="$cmd = 'edit_indirect'"> - <xsl:value-of select="$transactionPubName" /></xsl:if></legend>
            <table class="textChart hiOn transactionHistory">
                <xsl:call-template name="historyHeaderRow" />
                <xsl:for-each select="FinanceAccount/TransactionList/Transaction">
                    <xsl:call-template name="historyTransactionRow" />          
                </xsl:for-each>
            </table>
        </fieldset>
        <br />
    </xsl:template>
    
    
    <xsl:template name="historyHeaderRow">
        <tr>                      
            <th class="rtypeCol">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:template>


    <xsl:template name="historyTransactionRow">
        <tr class="row{position() mod 2}">                  
            <td>
	            <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:choose>
                    <xsl:when test="Memo = 'See Admin' or Memo = 'See Agent'">
                        <i><xsl:value-of select = "Memo" /></i>
                    </xsl:when>
                    <xsl:otherwise>   
                        <xsl:call-template name="replaceLineBreak">
                            <xsl:with-param name="string" select="Memo"/>
                        </xsl:call-template>
                    </xsl:otherwise>
                </xsl:choose>
            </td>
            <td class="amount">
                <xsl:value-of select="Balance" />
            </td>
        </tr>      
    </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="transactionTypeArray">
        <script type="text/javascript">
    	    // setting up an arrays for adding new transactions
    	
    	    var types = new Object();

            types[2] = 'Adjustment';
            types[3] = 'Advance';
            types[4] = 'Payment';

        </script>
    </xsl:template>
      

    <xsl:template name="submitButton">
        <input name="submit" type="submit" id="submitButton" value="Update">
            <xsl:if test="$openRuns &gt; 0">
                <xsl:attribute name="disabled">disabled</xsl:attribute>
            </xsl:if>        
        </input>
    </xsl:template>


    <xsl:template name="cancelButton">
        <input type="button" value="Return to Publisher" onClick="location.href='?PublisherID={Form/Publisher/PublisherID}&amp;c=show'" />
    </xsl:template>


</xsl:stylesheet>


