<!--                                                                  -->
<!--  Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved     -->
<!--                                                                  -->
<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="shared.xsl" />

    <xsl:variable name="allPublishers" select="/Root/Statement/MechanicalStatement/MechanicalStatementPublisherList/MechanicalStatementPublisher" />
    <xsl:variable name="publisherCount" select="count($allPublishers)" />

	<xsl:variable name="statementVersion" select="/Root/Statement/MechanicalStatement/Version" />

    <xsl:variable name="columns">
  	    <xsl:choose>
  		    <xsl:when test="$statementVersion = 1">24</xsl:when>
  		    <xsl:when test="$statementVersion = 2">21</xsl:when>
  		    <xsl:when test="$statementVersion = 3">20</xsl:when>
  	    </xsl:choose>
    </xsl:variable>
    
    <xsl:variable name="mechArea">
        <xsl:choose>
            <xsl:when test="$cmd = 'show_mechanical'">mechanical</xsl:when>
            <xsl:when test="$cmd = 'show_ca_mechanical'">ca_mechanical</xsl:when>
        </xsl:choose>
    </xsl:variable>
    
    <xsl:variable name="pubArea">
        <xsl:choose>
            <xsl:when test="$cmd = 'show_mechanical'">publisher</xsl:when>
            <xsl:when test="$cmd = 'show_ca_mechanical'">ca_publisher</xsl:when>
        </xsl:choose>
    </xsl:variable>    
 
    <xsl:variable name="licenseArea">
        <xsl:choose>
            <xsl:when test="$cmd = 'show_mechanical'">license</xsl:when>
            <xsl:when test="$cmd = 'show_ca_mechanical'">ca_license</xsl:when>
        </xsl:choose>
    </xsl:variable>     
    

    <xsl:template name="pickLayout">
        <div id="statement">
   
            <xsl:call-template name="summary" />
            <xsl:choose>
                <xsl:when test="Statement/MechanicalStatement/Publisher/IsAdmin != 1 and Statement/MechanicalStatement/Publisher/IsAgency != 1">
                    <xsl:call-template name="trackDetails" />
                </xsl:when>
                <xsl:when test="Params/focusPublisherID">
                    <xsl:call-template name="trackDetails" />
                </xsl:when>
            </xsl:choose>

        </div>
        <xsl:call-template name="buttons" />
    </xsl:template>


    <xsl:template name="summary">
   	    <div class="section" id="statementSummary">
   	        <xsl:call-template name="helpLink" />
            <div class="statementInfo">

       	        <strong><xsl:value-of select="Statement/MechanicalRun/Label" /></strong>
       	
       	        <xsl:if test="Statement/MechanicalRun/CanDownload = 1">
       		        <br /><br />
	                <a href="?c=pdf_{$mechArea}&amp;MechanicalStatementID={Statement/MechanicalStatement/MechanicalStatementID}" title="Download PDF">
						<img src="/production/images/pdficon_small.gif" height="17" width="17" class="pdfIcon" />
					</a>
					<a href="?c=pdf_{$mechArea}&amp;MechanicalStatementID={Statement/MechanicalStatement/MechanicalStatementID}">Publisher View</a>
       	        </xsl:if>
       	
       	        <xsl:if test="Statement/MechanicalRun/CanDownloadText = 1">
       		        <br /><br />
	                <a href="?c=text_{$mechArea}&amp;MechanicalStatementID={Statement/MechanicalStatement/MechanicalStatementID}" title="Download Electronic Format">
						<img src="/production/images/icons/office/Document-(16x16).gif" height="16" width="16" class="pdfIcon" />
					</a>
					<a href="?c=text_{$mechArea}&amp;MechanicalStatementID={Statement/MechanicalStatement/MechanicalStatementID}">Electronic Format</a>					
				</xsl:if>
				
       	        <xsl:if test="Statement/MechanicalRun/CanDownloadCMRRA = 1 and Statement/MechanicalStatement/Publisher/IsAgency = 1">
       		        <br /><br />
	                <a href="?c=cmrra_{$mechArea}&amp;MechanicalStatementID={Statement/MechanicalStatement/MechanicalStatementID}" title="Download CMRRA Format">
						<img src="/production/images/icons/office/Document-(16x16).gif" height="16" width="16" class="pdfIcon" />
					</a>
					<a href="?c=cmrra_{$mechArea}&amp;MechanicalStatementID={Statement/MechanicalStatement/MechanicalStatementID}">CMRRA Format</a>					
				</xsl:if>				
				
                <br /><br />
        
                <xsl:call-template name="publisherInfo">
                    <xsl:with-param name="publisher" select="Statement/MechanicalStatement/Publisher" />
                </xsl:call-template>
        
                <xsl:call-template name="transactionTable">
        	        <xsl:with-param name="statement" select="Statement/MechanicalStatement" />
        	        <xsl:with-param name="transactions" select="Statement/MechanicalStatement/MechanicalStatementTransactionList/MechanicalStatementTransaction" />
                </xsl:call-template>
        
                <xsl:if test="$allPublishers">
                    <xsl:variable name="publisherSplit1" select="ceiling($publisherCount div 3)" />
                    <xsl:variable name="publisherSplit2" select="$publisherSplit1 * 2" />
                    <a name="top" />

                    <xsl:choose>         
                        <xsl:when test="$publisherCount &lt; 6">
                            <xsl:call-template name="publisherListTable">
                                <xsl:with-param name="publisherSet" select="$allPublishers" />
                                <xsl:with-param name="firstColumn">1</xsl:with-param>
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template name="publisherListTable">
                                <xsl:with-param name="publisherSet" select="$allPublishers[position() &lt;= $publisherSplit1]" />
                                <xsl:with-param name="firstColumn">1</xsl:with-param>
                            </xsl:call-template>
                            <xsl:call-template name="publisherListTable">
                                <xsl:with-param name="publisherSet" select="$allPublishers[position() &gt; $publisherSplit1 and position() &lt;= $publisherSplit2]" />
                            </xsl:call-template>
                            <xsl:call-template name="publisherListTable">
                                <xsl:with-param name="publisherSet" select="$allPublishers[position() &gt; $publisherSplit2]" />
                            </xsl:call-template>
                        </xsl:otherwise>
                    </xsl:choose>
                    <br class="superBreak" /><br />
                </xsl:if>
            </div>
            <xsl:if test="not($allPublishers) or ($allPublishers and Params/focusPublisherID)">
      	        <h3>Current Period Details:</h3>
			</xsl:if>
        </div>
    </xsl:template>


    <xsl:template name="publisherListTable">
        <xsl:param name="firstColumn" />
		<xsl:param name="publisherSet" />
		<table>
            <tr>
                <xsl:call-template name="publisherListHeader">
                    <xsl:with-param name="firstColumn" select="$firstColumn" />
                </xsl:call-template>
            </tr>
            <xsl:for-each select="$publisherSet">
                <tr>
                    <xsl:call-template name="publisherListRow" />
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>
    
  
    <xsl:template name="publisherListHeader">
        <xsl:param name="firstColumn" />
        <th>Publisher <xsl:if test="$publisherCount &gt; 1 and $firstColumn = 1 and (Params/focusPublisherID != 'all' or not(Params/focusPublisherID))">(<a href="/rps/statement?c=show_{$mechArea}&amp;MechanicalStatementID={/Root/Params/MechanicalStatementID}&amp;focusPublisherID=all">View All</a>)</xsl:if></th>
        <th>Amount Due</th>
    </xsl:template>


    <xsl:template name="publisherListRow">
        <td>
			<xsl:choose>
				<xsl:when test="not(/Root/Params/printer) and Publisher/PublisherID != normalize-space(/Root/Params/focusPublisherID)">
                    <a href="/rps/statement?c=show_{$mechArea}&amp;MechanicalStatementID={/Root/Params/MechanicalStatementID}&amp;focusPublisherID={Publisher/PublisherID}#{Publisher/PublisherID}">
        		        <xsl:value-of select="Publisher/PublisherName" />
      		        </a>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="Publisher/PublisherName" />
				</xsl:otherwise>
			</xsl:choose>
        </td>
        <td class="amount">
            <xsl:choose>
                <xsl:when test="OnHold = 1">ON-HOLD</xsl:when>
                <xsl:otherwise>
			        <xsl:value-of select="AmountDue" />
			    </xsl:otherwise>
			</xsl:choose>
        </td>
    </xsl:template>
    
  
    <xsl:template name="publisherInfo">
  	    <xsl:param name="publisher" />
        <a href="/rps/{$pubArea}?PublisherID={$publisher/PublisherID}&amp;c=show">
            <xsl:value-of select="$publisher/PublisherName" />
        </a><xsl:if test="$publisher/IsAdmin = 1"> (Admin)</xsl:if>
        <xsl:if test="normalize-space($publisher/StreetAddress)">
            <br />
            <xsl:value-of select="$publisher/StreetAddress" />
        </xsl:if>
        <xsl:if test="normalize-space($publisher/StreetAddress2)">
            <br />
            <xsl:value-of select="$publisher/StreetAddress2" />
        </xsl:if>        
        <xsl:if test="normalize-space($publisher/StreetAddress3)">
            <br />
            <xsl:value-of select="$publisher/StreetAddress3" />
        </xsl:if>        
        <xsl:if test="normalize-space($publisher/City) or normalize-space($publisher/StateProvince)">
            <br />
            <xsl:if test="normalize-space($publisher/City)"><xsl:value-of select="$publisher/City" /><xsl:if test="normalize-space($publisher/StateProvince)">, </xsl:if></xsl:if><xsl:value-of select="$publisher/StateProvince" />&#160;<xsl:value-of select="$publisher/PostalCode" />
        </xsl:if>
        <xsl:if test="normalize-space($publisher/CountryCode)">
            <br />
            <xsl:value-of select="$publisher/CountryCode" />
        </xsl:if>
        <br /><br />
    </xsl:template>
  
  
    <xsl:template name="trackDetails">
        <table class="statementTable">
        <xsl:call-template name="trackDetailsHeader" />
        <xsl:choose>
            <xsl:when test="Statement/MechanicalStatement/Publisher/IsAdmin = 1 or Statement/MechanicalStatement/Publisher/IsAgency = 1">
                <xsl:call-template name="publisherDetailsRow" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:for-each select="Statement/MechanicalStatement/MechanicalStatementTrackList/MechanicalStatementTrack">
                    <xsl:call-template name="trackDetailsRow" />
                </xsl:for-each>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:call-template name="publisherTotal">
            <xsl:with-param name="statement" select="/Root/Statement/MechanicalStatement" />
        </xsl:call-template>
        </table>
    </xsl:template>  


    <xsl:template name="trackDetailsHeader">
        <tr class="statementHeader">
            <th>Track Title</th>
            <th>Album Title</th>
            <th class="center">UPC</th>
            <th class="center">Product Config</th>
            <th>Region</th>
            <th class="center">Rate Period</th>
            <th>Base Rate</th>
            <th>% of Rate</th>
            <th class="amount">Share</th>
			<th class="percent">Net Rate</th>
			
			<xsl:if test="$statementVersion = 1">
      	        <th>Sales</th>
            </xsl:if>
      
			<xsl:if test="$statementVersion &gt; 1">
      	        <th class="amount">Gross Units</th>
            </xsl:if>      
      
            <th>% of Sales</th>
            
            <xsl:if test="$statementVersion &lt; 3">
                <th>Packaging</th>
            </xsl:if>
            
            <th>Free Goods</th>
            <th>Misc.</th>
            <th>Reserves</th>
			<th>Liquidations</th>
			<th>Returns</th>
            <th>Carryover</th>    
      	
			<xsl:if test="$statementVersion = 1">      	
	            <th class="amount">Gross Units</th>
	            <th class="amount">Total</th>
	            <th class="amount">Accrual</th>
	        </xsl:if>  

            <th class="amount">Net Units</th>
            <th class="amount">Amount Due</th>    
        </tr>
    </xsl:template>
 
 
    <xsl:template name="trackDetailsRow">
  	    <xsl:if test="MechanicalStatementCrossedLicenseList/MechanicalStatementLicense or MechanicalStatementCrossedLicenseTransactionList/MechanicalStatementCrossedLicenseTransaction">
			<xsl:call-template name="crossedLicenses" />        
		</xsl:if>
        <xsl:for-each select="MechanicalStatementLicenseList/MechanicalStatementLicense">
			<xsl:call-template name="uncrossedLicenses" />      
        </xsl:for-each>    
    </xsl:template>


    <xsl:template name="crossedLicenses">
  	    <xsl:choose>
	  	    <xsl:when test="MechanicalStatementCrossedLicenseList/MechanicalStatementLicense">
	  
    		  	<xsl:for-each select="MechanicalStatementCrossedLicenseList/MechanicalStatementLicense">
    		  		<xsl:variable name="position" select="position()" />
    		  		
    			  	<xsl:choose>
    			  		<xsl:when test="MechanicalStatementItemList/MechanicalStatementItem or MechanicalStatementAdjustmentItemList/MechanicalStatementAdjustmentItem">
    			    		<xsl:for-each select="MechanicalStatementItemList/MechanicalStatementItem">
    				   	 		<xsl:call-template name="itemRow">
    				   	 			<xsl:with-param name="position" select="$position + position() - 1" />
    				   	 		</xsl:call-template>
    			    		</xsl:for-each>
        					<xsl:for-each select="MechanicalStatementAdjustmentItemList/MechanicalStatementAdjustmentItem">    
        			            <xsl:call-template name="adjustmentRow" />
        			        </xsl:for-each>    			    		
    					    <xsl:call-template name="currentPeriodSubtotalRow">
    					    	<xsl:with-param name="subtotal" select="Subtotal" />
    					    </xsl:call-template>
    			    	</xsl:when>
    			    	<xsl:otherwise>
    			    		<xsl:call-template name="noItemRow">
    			    			<xsl:with-param name="position" select="$position" />
    			    		</xsl:call-template>
    			    	</xsl:otherwise>
    			    </xsl:choose>  		
		 			    
    			    <xsl:variable name="parsedPreviousAdvanceBalance" select="translate(PreviousAdvanceBalance, translate(PreviousAdvanceBalance,'0123456789', ''), '')" />
    			    <xsl:variable name="parsedSubtotal" select="translate(Subtotal, translate(Subtotal,'0123456789', ''), '')" />
    			    <xsl:variable name="parsedAdvanceBalance" select="translate(AdvanceBalance, translate(AdvanceBalance,'0123456789', ''), '')" />
    			    <xsl:variable name="parsedAdjustedSubtotal" select="translate(AdjustedSubtotal, translate(AdjustedSubtotal,'0123456789', ''), '')" />
			
    			  	<xsl:for-each select="MechanicalStatementLicenseTransactionList/MechanicalStatementLicenseTransaction">
    			    	<xsl:call-template name="licenseTransactionRow" />
    			    </xsl:for-each>
    			
    			    <xsl:if test="$parsedPreviousAdvanceBalance != 0 or $parsedAdvanceBalance != 0">
    			    	<xsl:call-template name="previousBalanceRow">
    			    		<xsl:with-param name="previousAdvanceBalance" select="PreviousAdvanceBalance" />
    			    	</xsl:call-template>
    			    </xsl:if>
    			    
    			    <xsl:if test="$parsedPreviousAdvanceBalance != 0 or $parsedAdvanceBalance != 0">
    			    	<xsl:call-template name="licenseBalanceRow">
    			    		<xsl:with-param name="advanceBalance" select="AdvanceBalance" />
    			    	</xsl:call-template>    	 	
    			    </xsl:if>      
    			
    			    <xsl:if test="MechanicalStatementLicenseTransactionList/MechanicalStatementLicenseTransaction or $parsedPreviousAdvanceBalance != 0 or $parsedSubtotal != $parsedAdjustedSubtotal">
    			    	<xsl:call-template name="licenseAdjustedSubtotalRow">
    			    		<xsl:with-param name="adjustedSubtotal" select="AdjustedSubtotal" />
    			    	</xsl:call-template>
    			    </xsl:if>  
    			           	 				   	 	
		        </xsl:for-each>	
		        	    
				<tr>
		    	    <td colspan="{$columns}" style="height: 6px;"></td>
		        </tr>	
		        	    
			</xsl:when>		
				
			<xsl:otherwise>
    		    <tr>
    		     	<xsl:if test="position() != 1">
    		    		<xsl:attribute name="class">total</xsl:attribute>
    		    	</xsl:if>		    
    		        <td>
    		            <a href="track?c=show&amp;TrackID={TrackID}">
    		                <xsl:value-of select="SongTitle" />
    		            </a>
    		        </td>
    		        <td>
    		            <a href="catalog?c=show&amp;AlbumID={AlbumID}">
    		                <xsl:value-of select="substring(AlbumName,1,20)" /><xsl:if test="substring(AlbumName,21,1)">...</xsl:if>
    		            </a>
    		        </td>
    		        <td colspan="{$columns - 2}"></td>
    		    </tr>		
			</xsl:otherwise>		
		</xsl:choose>		    
    
		<xsl:for-each select="MechanicalStatementCrossedLicenseList/MechanicalStatementLicense/MechanicalStatementAdjustmentItemList/MechanicalStatementAdjustmentItem">    
            <xsl:call-template name="adjustmentRow" />
        </xsl:for-each>    
    
        <xsl:call-template name="currentPeriodSubtotalRow">
        	<xsl:with-param name="title">Current Period Crossed License Subtotal</xsl:with-param>
        	<xsl:with-param name="subtotal" select="CrossedSubtotal" />
        	<xsl:with-param name="noLine">1</xsl:with-param>
        </xsl:call-template>
    
        <xsl:variable name="parsedCrossedPreviousAdvanceBalance" select="translate(CrossedPreviousAdvanceBalance, translate(CrossedPreviousAdvanceBalance,'0123456789', ''), '')" />
        <xsl:variable name="parsedCrossedSubtotal" select="translate(CrossedSubtotal, translate(CrossedSubtotal,'0123456789', ''), '')" />
        <xsl:variable name="parsedCrossedAdvanceBalance" select="translate(CrossedAdvanceBalance, translate(CrossedAdvanceBalance,'0123456789', ''), '')" />
        <xsl:variable name="parsedCrossedAdjustedSubtotal" select="translate(CrossedAdjustedSubtotal, translate(CrossedAdjustedSubtotal,'0123456789', ''), '')" />

      	<xsl:for-each select="MechanicalStatementCrossedLicenseTransactionList/MechanicalStatementCrossedLicenseTransaction">
        	<xsl:call-template name="licenseTransactionRow" />
        </xsl:for-each>

        <xsl:if test="$parsedCrossedPreviousAdvanceBalance != 0 or $parsedCrossedAdvanceBalance != 0">
        	<xsl:call-template name="previousBalanceRow">
        		<xsl:with-param name="title">Previous Crossed License Balance</xsl:with-param>
        		<xsl:with-param name="previousAdvanceBalance" select="CrossedPreviousAdvanceBalance" />
        	</xsl:call-template>
        </xsl:if>
    
        <xsl:if test="$parsedCrossedPreviousAdvanceBalance != 0 or $parsedCrossedAdvanceBalance != 0">
        	<xsl:call-template name="licenseBalanceRow">
        		<xsl:with-param name="title">Crossed License Balance Forward</xsl:with-param>
        		<xsl:with-param name="advanceBalance" select="CrossedAdvanceBalance" />
        	</xsl:call-template>    	 	
        </xsl:if>      

        <xsl:if test="MechanicalStatementCrossedLicenseTransactionList/MechanicalStatementCrossedLicenseTransaction or $parsedCrossedPreviousAdvanceBalance != 0 or $parsedCrossedSubtotal != $parsedCrossedAdjustedSubtotal">
        	<xsl:call-template name="licenseAdjustedSubtotalRow">
        		<xsl:with-param name="title">Crossed License Adjusted Subtotal</xsl:with-param>
        		<xsl:with-param name="adjustedSubtotal" select="CrossedAdjustedSubtotal" />
        	</xsl:call-template>
        </xsl:if>        
    </xsl:template>
    
  
    <xsl:template name="uncrossedLicenses">
  
      	<xsl:choose>
      		<xsl:when test="MechanicalStatementItemList/MechanicalStatementItem or MechanicalStatementAdjustmentItemList/MechanicalStatementAdjustmentItem">
        		<xsl:for-each select="MechanicalStatementItemList/MechanicalStatementItem">
         	 		<xsl:call-template name="itemRow">
         	 			<xsl:with-param name="position" select="position()" />
         	 		</xsl:call-template>
        		</xsl:for-each>
                <xsl:for-each select="MechanicalStatementAdjustmentItemList/MechanicalStatementAdjustmentItem">
                    <xsl:call-template name="adjustmentRow" />
                </xsl:for-each>        		
    		    <xsl:call-template name="currentPeriodSubtotalRow">
    		    	<xsl:with-param name="subtotal" select="Subtotal" />
    		    </xsl:call-template>
        	</xsl:when>
        	<xsl:otherwise>
        		<xsl:call-template name="noItemRow">
        			<xsl:with-param name="position">1</xsl:with-param>
        		</xsl:call-template>
        		<xsl:call-template name="currentPeriodSubtotalRow">
        			<xsl:with-param name="subtotal" select="Subtotal" />
        			<xsl:with-param name="noLine" select="'1'" />
        		</xsl:call-template>
        	</xsl:otherwise>
        </xsl:choose>
    
        <xsl:variable name="parsedPreviousAdvanceBalance" select="translate(PreviousAdvanceBalance, translate(PreviousAdvanceBalance,'0123456789', ''), '')" />
        <xsl:variable name="parsedSubtotal" select="translate(Subtotal, translate(Subtotal,'0123456789', ''), '')" />
        <xsl:variable name="parsedAdvanceBalance" select="translate(AdvanceBalance, translate(AdvanceBalance,'0123456789', ''), '')" />
        <xsl:variable name="parsedAdjustedSubtotal" select="translate(AdjustedSubtotal, translate(AdjustedSubtotal,'0123456789', ''), '')" />
    
        <xsl:for-each select="MechanicalStatementLicenseTransactionList/MechanicalStatementLicenseTransaction">
            <xsl:call-template name="licenseTransactionRow" />
        </xsl:for-each>
    
        <xsl:if test="$parsedPreviousAdvanceBalance != 0 or $parsedAdvanceBalance != 0">
        	<xsl:call-template name="previousBalanceRow">
        		<xsl:with-param name="previousAdvanceBalance" select="PreviousAdvanceBalance" />
        	</xsl:call-template>
        </xsl:if>
    
        <xsl:if test="$parsedPreviousAdvanceBalance != 0 or $parsedAdvanceBalance != 0">
        	<xsl:call-template name="licenseBalanceRow">
        		<xsl:with-param name="advanceBalance" select="AdvanceBalance" />
        	</xsl:call-template>   
        </xsl:if>      

        <xsl:if test="MechanicalStatementLicenseTransactionList/MechanicalStatementLicenseTransaction or $parsedPreviousAdvanceBalance != 0 or $parsedSubtotal != $parsedAdjustedSubtotal">
        	<xsl:call-template name="licenseAdjustedSubtotalRow">
        		<xsl:with-param name="adjustedSubtotal" select="AdjustedSubtotal" />
        	</xsl:call-template>
        </xsl:if>        
    </xsl:template>


    <xsl:template name="publisherDetailsRow">
        <xsl:for-each select="$allPublishers">
            <xsl:if test="MechanicalStatementTrackList or MechanicalStatementTransactionList">
                <tr>
                    <td colspan="{$columns}">
                        <xsl:if test="position() != 1">
                            <xsl:attribute name="class">pubtotal</xsl:attribute>
                        </xsl:if>
                        <a name="{Publisher/PublisherID}" />
                        <strong>
                            <a>
                                <xsl:if test="not(/Root/Params/printer)">
                                    <xsl:attribute name="href">/rps/<xsl:value-of select="$pubArea" />?c=show&amp;PublisherID=<xsl:value-of select="Publisher/PublisherID" /></xsl:attribute>
                                </xsl:if>
                                <xsl:value-of select="Publisher/PublisherName" />
                            </a>
                        </strong>
                        &#160;<a href="#top" title="back to top"><img src="/production/images/icons/mini/arrow_up.gif" /></a>
                    </td>
                </tr>
                <tr>
                    <td colspan="{$columns}" class="emptyCell"></td>
                </tr>
                <xsl:for-each select="MechanicalStatementTrackList/MechanicalStatementTrack">
                    <xsl:call-template name="trackDetailsRow" />
                </xsl:for-each>
                <tr>
                    <td colspan="{$columns}" class="emptyCell"></td>
                </tr>
                
                
                <xsl:variable name="parsedPreviousBalance" select="translate(PreviousBalance, translate(PreviousBalance,'0123456789', ''), '')" />
                
                <xsl:if test="($parsedPreviousBalance != 0) or (MechanicalStatementPublisherTransactionList/MechanicalStatementPublisherTransaction)">
                
                    <xsl:call-template name="publisherSubtotal" />
                
                    <xsl:if test="$parsedPreviousBalance != 0">
                        <xsl:call-template name="publisherPreviousBalance" />
                    </xsl:if>		
			
        			<xsl:for-each select="MechanicalStatementPublisherTransactionList/MechanicalStatementPublisherTransaction">
                        <xsl:call-template name="publisherTransactionRow" />
        			</xsl:for-each>
      
                    <xsl:call-template name="publisherEndingBalance" />    
                
                </xsl:if> 

                <xsl:call-template name="publisherAmountDue" />
                
            </xsl:if>
        </xsl:for-each>
    </xsl:template>


    <xsl:template name="publisherSubtotal">
		<tr>
            <td colspan="{$columns - 4}" class="emptyCell"></td>
            <td colspan="3" class="amountLabel">Publisher Subtotal:</td>
            <td class="amount">
                <xsl:value-of select="PublisherSubtotal" />
            </td>
        </tr>
    </xsl:template>
    
    
    <xsl:template name="publisherPreviousBalance">
		<tr>
            <td colspan="{$columns - 4}" class="emptyCell"></td>
            <td colspan="3" class="amountLabel">Previous Balance:</td>
            <td class="amount">
                <xsl:value-of select="PreviousBalance" />
            </td>
        </tr>
    </xsl:template>  


    <xsl:template name="publisherAmountDue">
		<tr>
            <td colspan="{$columns - 4}" class="emptyCell"></td>
            <td colspan="3" class="amountLabel">Publisher Amount Due:</td>
            <td class="amount">
        	    <strong>  
        	        
					<xsl:choose>
						<xsl:when test="OnHold = 1">ON-HOLD</xsl:when>
                        <xsl:otherwise>
        			        <xsl:value-of select="AmountDue" />
        			    </xsl:otherwise>
					</xsl:choose>
					
                </strong>               
            </td>
        </tr>
    </xsl:template>


    <xsl:template name="publisherEndingBalance">
		<tr>
            <td colspan="{$columns - 4}" class="emptyCell"></td>
            <td colspan="3" class="amountLabel">Ending Balance:<br /><br /></td>
            <td class="amount total">
        	    <strong>  
            		<xsl:value-of select="StatementTotal" />    		
                </strong>  
                <br /><br />             
            </td>
        </tr>
    </xsl:template>


    <xsl:template name="publisherTotal">
        <xsl:param name="statement" />
        <tr>
            <td colspan="{$columns}" class="emptyCell pubtotal">
                <xsl:if test="not($allPublishers)">
                    <xsl:attribute name="class">emptyCell total</xsl:attribute>
                </xsl:if>
            </td>
        </tr>
        <tr>
            <td colspan="{$columns - 4}" class="emptyCell"></td>
            <td colspan="3" class="amountLabel">Total:</td>
            <td class="amount">
                <xsl:value-of select="$statement/Subtotal" />
            </td>
        </tr>
    </xsl:template>
    
    
    <xsl:template name="publisherTransactionRow">
    	<tr>
    	    <td colspan="{$columns - 4}" class="emptyCell"></td>
    		<td colspan="3" class="amountLabel">
    			<xsl:if test="TypeCode = 2">Adjustment:</xsl:if>	
    			<xsl:if test="TypeCode = 3">Advance:</xsl:if>
    			<xsl:if test="TypeCode = 4">Payment:</xsl:if>
    		</td>
    		<td class="amount">
                <xsl:value-of select="Amount" />
    		</td>
    	</tr>    
    </xsl:template>
    

    <xsl:template name="itemRow">
  	    <xsl:param name="position" />
        <tr>
     	    <xsl:if test="$position = 1">
    		    <xsl:attribute name="class">total</xsl:attribute>
    	    </xsl:if>
            <td>
                <a href="/rps/{$licenseArea}?c=show&amp;TrackLicenseID={TrackLicense/TrackLicenseID}" title="View License">
                    <img src="/production/images/icons/mini/briefcase.gif" width="10" height="10" />
                </a>
                <br />
                <a href="track?c=show&amp;TrackID={TrackLicense/TrackID}">
                    <xsl:value-of select="../../../../SongTitle" />
                </a>
            </td>
            <td>
                <a href="catalog?AlbumID={AlbumID}&amp;c=show">
                    <xsl:value-of select="substring(AlbumName,1,20)" /><xsl:if test="substring(AlbumName,21,1)">...</xsl:if>
                </a>
            </td>
            <td class="center"><xsl:value-of select="UPC" /></td>
            <td class="center">
                <xsl:variable name="productTypeID">
                    <xsl:choose>
                        <xsl:when test="TrackLicense/ProductTypeID = 15">15</xsl:when>
                        <xsl:otherwise><xsl:value-of select="ProductTypeID" /></xsl:otherwise>
                    </xsl:choose>
                 </xsl:variable>
                <xsl:value-of select="/Root/Statement/TypeList/Type[ProductTypeID = $productTypeID]/Description" />
            </td>
            <td>
      	        <xsl:if test="../../RegionID != 0">
	                <xsl:variable name="regionID" select="../../RegionID" />
	                <xsl:value-of select="/Root/Statement/RegionList/Region[RegionID = $regionID]/Name" />
                </xsl:if>
            </td>
            <td class="center"><xsl:value-of select="RatePeriod" /></td>
            <td><xsl:value-of select="BaseRate" /></td>
            <td class="amount">
                <xsl:value-of select="TrackLicense/RatePercentage" /><xsl:if test="normalize-space(TrackLicense/RatePercentage)">%</xsl:if>
            </td>
            <td class="amount">
                <xsl:value-of select="TrackLicense/Share" /><xsl:if test="normalize-space(TrackLicense/Share)">%</xsl:if>
            </td>
			<td class="percent">
				<xsl:value-of select="NetRate" />
			</td>
            <td class="amount">
                <xsl:value-of select="Sales" />
            </td>
      
            <td class="amount">
      	        <xsl:choose>
      		        <xsl:when test="normalize-space(TrackLicense/PercentageOfSales)">
        		        <xsl:value-of select="TrackLicense/PercentageOfSales" />%
        	        </xsl:when>
        	        <xsl:otherwise>N/A</xsl:otherwise>
                </xsl:choose>      
            </td>
            
            <xsl:if test="$statementVersion &lt; 3">
                <td class="amount">
          	        <xsl:choose>
          		        <xsl:when test="normalize-space(TrackLicense/PackagingDeduction)">
            		        <xsl:value-of select="TrackLicense/PackagingDeduction" />%
            	        </xsl:when>
            	        <xsl:otherwise>N/A</xsl:otherwise>
                    </xsl:choose>          
                </td>
            </xsl:if>
            
            <td class="amount">
      	        <xsl:choose>
      		        <xsl:when test="normalize-space(TrackLicense/FreeGoods)">
        		        <xsl:value-of select="TrackLicense/FreeGoods" />%
        	        </xsl:when>
        	        <xsl:otherwise>N/A</xsl:otherwise>
                </xsl:choose>
            </td>
            <td class="amount">
      	        <xsl:choose>
      		        <xsl:when test="normalize-space(TrackLicense/MiscDeduction)">
        		        <xsl:value-of select="TrackLicense/MiscDeduction" />%
        	        </xsl:when>
        	        <xsl:otherwise>N/A</xsl:otherwise>
                </xsl:choose>        
            </td>
      
            <td class="amount">
                <xsl:value-of select="Reserved" />
            </td>
            <td class="amount">
                <xsl:value-of select="Liquidated" />
            </td>
            <td class="amount">
                <xsl:value-of select="Returns" />
            </td> 
            <td class="amount">
                <xsl:value-of select="PreviousCarryover" />
            </td>  
            <td class="amount">
                <xsl:choose>
                    <xsl:when test="$statementVersion = 3">
                        <xsl:value-of select="NetUnits" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="GrossUnits" />
                    </xsl:otherwise>
                </xsl:choose>                    
            </td>
            <td class="amount">
                <xsl:value-of select="Total" />
            </td>    
      
            <xsl:if test="$statementVersion = 1">
	            <td class="amount">
	                <xsl:value-of select="Carryover" />
	            </td>
	            <td class="amount">
	                <xsl:value-of select="NetUnits" />
	            </td>
	            <td class="amount">
	                <xsl:value-of select="AmountPaid" />
	            </td>
            </xsl:if>
      
        </tr>
    </xsl:template>


    <xsl:template name="noItemRow">
  	    <xsl:param name="position" />
        <tr>
			<xsl:if test="$position = 1">
    		    <xsl:attribute name="class">total</xsl:attribute>
    	    </xsl:if>          
            <td>
          	    <xsl:if test="TrackLicenseID">
    	            <a href="/rps/{$licenseArea}?c=show&amp;TrackLicenseID={TrackLicenseID}" title="View License">
    	                <img src="/production/images/icons/mini/briefcase.gif" />
    	            </a>
    	            <br />
                </xsl:if>
                <a href="track?c=show&amp;TrackID={../../TrackID}">
                    <xsl:value-of select="../../SongTitle" />
                </a>
            </td>
            <td>
                <a href="catalog?c=show&amp;AlbumID={../../AlbumID}">
                    <xsl:value-of select="substring(../../AlbumName,1,20)" /><xsl:if test="substring(../../AlbumName,21,1)">...</xsl:if>
                </a>
            </td>
            <td class="center"><xsl:value-of select="UPC" /></td>
            <td class="center"></td>
            <td>
      	        <xsl:if test="RegionID != 0">
	                <xsl:variable name="regionID" select="RegionID" />
	                <xsl:value-of select="/Root/Statement/RegionList/Region[RegionID = $regionID]/Name" />
                </xsl:if>
            </td>
            <td colspan="{$columns - 5}"></td>
        </tr>
    </xsl:template>
    

    <xsl:template name="adjustmentRow">
        <tr>
            <td colspan="{$columns - 1}" class="right">
      	        <strong>
                    <xsl:call-template name="adjustmentName">
                        <xsl:with-param name="typeCode" select="AdjustmentTypeCode" />
                    </xsl:call-template>
				</strong>
            </td>
            <td class="right">
                <xsl:value-of select="Amount" />
            </td>
        </tr>
    </xsl:template>


    <xsl:template name="adjustmentName">
        <xsl:param name="typeCode" />
        <xsl:choose>
            <xsl:when test="$typeCode = 1">Controlled Comp Adjustment:</xsl:when>
            <xsl:otherwise>Adjustment:</xsl:otherwise>
        </xsl:choose>
    </xsl:template>
  
  
    <xsl:template name="licenseTransactionRow">
  	    <xsl:param name="noLine" />
        <tr>
            <td colspan="{$columns - 3}" class="amount">
      	        <xsl:if test="normalize-space(Memo)">
	      	        <strong>
						(<xsl:value-of select="Memo" />)
					</strong>
				</xsl:if>
            </td>
            <td colspan="2" class="amount">
          	    <strong>
          		    <xsl:choose>
          			    <xsl:when test="TypeCode = 3">Advance:</xsl:when>
          			    <xsl:otherwise>Adjustment:</xsl:otherwise>
          		    </xsl:choose>
          	    </strong>
            </td>
            <td class="amount">
          	    <xsl:if test="$noLine = 1">
          		    <xsl:attribute name="class">amount</xsl:attribute>
          	    </xsl:if>
          	    <xsl:call-template name="replaceDash">
            	    <xsl:with-param name="string" select="Amount" />
                </xsl:call-template>
            </td>
        </tr>
    </xsl:template>    
  
  
    <xsl:template name="currentPeriodSubtotalRow">
  	    <xsl:param name="noLine" />
  	    <xsl:param name="subtotal" />
  	    <xsl:param name="title" select="'Current Period License Subtotal'" />
        <tr>
            <td colspan="{$columns - 1}" class="amountLabel"><xsl:value-of select="$title" />:</td>
            <td class="amount total">
      	        <xsl:if test="$noLine = 1">
      		        <xsl:attribute name="class">amount</xsl:attribute>
      	        </xsl:if>
                <xsl:value-of select="$subtotal" />
            </td>
        </tr>
    </xsl:template>  


    <xsl:template name="previousBalanceRow">
  	    <xsl:param name="previousAdvanceBalance" />
  	    <xsl:param name="title" select="'Previous License Balance'" />
        <tr>
            <td colspan="{$columns - 1}" class="amountLabel"><xsl:value-of select="$title" />:</td>
            <td class="amount">
                <xsl:value-of select="$previousAdvanceBalance" />
            </td>
        </tr>
    </xsl:template>  


    <xsl:template name="licenseBalanceRow">
  	    <xsl:param name="advanceBalance" />
  	    <xsl:param name="title" select="'License Balance Forward'" />
        <tr>
            <td colspan="{$columns - 1}" class="amountLabel"><xsl:value-of select="$title" />:</td>
            <td class="amount total">
	            <xsl:value-of select="$advanceBalance" />      
            </td>
        </tr>
    </xsl:template>  


    <xsl:template name="licenseAdjustedSubtotalRow">
  	    <xsl:param name="adjustedSubtotal" />
  	    <xsl:param name="title" select="'License Adjusted Subtotal'" />
        <tr>
            <td colspan="{$columns - 1}" class="amountLabel"><xsl:value-of select="$title" />:</td>
            <td class="amount total">
	            <xsl:value-of select="$adjustedSubtotal" />      
            </td>
        </tr>
    </xsl:template>  
  
 
    <xsl:template name="submitButton">
        <xsl:attribute name="class" />
    </xsl:template>


    <xsl:template name="cancelButton">
        <input type="button" name="Back" value="Back to Statements" onClick="location.href='{$mechArea}?MechanicalRunID={Statement/MechanicalStatement/MechanicalRunID}&amp;c=show&amp;PayorID={Statement/MechanicalStatement/PayorID}'" />
    </xsl:template>
    

</xsl:stylesheet>

