<!--                                                                  -->
<!--  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/ajax_request.xsl" />

    <xsl:variable name="licenseArea">
        <xsl:choose>
            <xsl:when test="$cmd = 'show_licenses'">license</xsl:when>
            <xsl:when test="$cmd = 'show_ca_licenses'">ca_license</xsl:when>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="licenseType">
        <xsl:choose>
            <xsl:when test="$cmd = 'show_licenses'">United States</xsl:when>
            <xsl:when test="$cmd = 'show_ca_licenses'">Canadian</xsl:when>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="licenseTab">
        <xsl:choose>
            <xsl:when test="$cmd = 'show_licenses'">licenseTab</xsl:when>
            <xsl:when test="$cmd = 'show_ca_licenses'">caLicenseTab</xsl:when>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="mechCommand">
        <xsl:choose>
            <xsl:when test="$cmd = 'show_licenses'">mech</xsl:when>
            <xsl:when test="$cmd = 'show_ca_licenses'">ca_mech</xsl:when>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="ccCommand">
        <xsl:choose>
            <xsl:when test="$cmd = 'show_licenses'">make_cc</xsl:when>
            <xsl:when test="$cmd = 'show_ca_licenses'">make_ca_cc</xsl:when>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="mechExempt">
        <xsl:choose>
            <xsl:when test="$cmd = 'show_licenses'">MechanicalExempt</xsl:when>
            <xsl:when test="$cmd = 'show_ca_licenses'">CAMechanicalExempt</xsl:when>
        </xsl:choose>
    </xsl:variable>


    <xsl:template name="pickLayout">
        <xsl:call-template name="helpLink">
            <xsl:with-param name="helpType">Tab</xsl:with-param>
        </xsl:call-template>
		<xsl:call-template name="openRunStatusMessage" />
        <fieldset id="trackLicenseViewTable">
            <legend><xsl:value-of select="$licenseType" /> Mechanical Licenses</legend>
            <table class="textChart" id="trackLicenseTable">
                <tbody>
                    <xsl:call-template name="trackLicenseFilter" />
                    <xsl:choose>
                        <xsl:when test="Form/TrackList/Track">
                            <xsl:call-template name="trackLicenseHeader" />
                            <xsl:for-each select="Form/TrackList/Track">
                                <xsl:call-template name="trackLicenseRowSwitch" />
                            </xsl:for-each>
                        </xsl:when>
                        <xsl:otherwise>
                	        <tr>
                		        <td colspan="12" class="emptyMessage">No tracks match this criteria.</td>
                            </tr>
                        </xsl:otherwise>
                    </xsl:choose>
                    <xsl:call-template name="ccompRow" />
                </tbody>
            </table>
        </fieldset>
    </xsl:template>


    <xsl:template name="trackLicenseRowSwitch">
        <xsl:variable name="rowClass">row<xsl:value-of select="position() mod 2" /></xsl:variable>
        <xsl:choose>
            <xsl:when test="dyn:evaluate($mechExempt) != 0">
                <xsl:call-template name="trackExemptRow">
                    <xsl:with-param name="rowClass" select="$rowClass" />
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="TrackLicenseList/TrackLicense">
                <xsl:choose>
                    <xsl:when test="/Root/Params/productFilter = 15">
          	            <xsl:for-each select="TrackLicenseList/TrackLicense[ProductTypeID = '15']">            
                            <xsl:call-template name="trackLicenseRow">
                                <xsl:with-param name="rowClass" select="$rowClass" />
                            </xsl:call-template>
                        </xsl:for-each>                    
                    </xsl:when>
                    <xsl:otherwise>
          	            <xsl:for-each select="TrackLicenseList/TrackLicense[ProductTypeID = '' or ProductTypeID = /Root/Params/productFilter or not(normalize-space(/Root/Params/productFilter))]">            
                            <xsl:call-template name="trackLicenseRow">
                                <xsl:with-param name="rowClass" select="$rowClass" />
                            </xsl:call-template>
                        </xsl:for-each>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="trackNoLicenseRow">
                    <xsl:with-param name="rowClass" select="$rowClass" />
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>


	<xsl:template name="trackLicenseFilter">
		<xsl:variable name="licenseFilter" select="/Root/Params/licenseFilter" />
		<xsl:variable name="productFilter" select="/Root/Params/productFilter" />
		<tr>
			<td colspan="12" class="right">

				<label for="productFilter">Show Product Type: </label>
				<select name="productFilter" id="productFilter">
		            <xsl:attribute name="onchange">filterCatalogLicenses( oTabUpdateObj, '<xsl:value-of select="$licenseTab" />', this, { licenseFilter: '<xsl:value-of select="$licenseFilter" />' } );</xsl:attribute>
                    
                    <option value="">All</option>                     
      
                    <xsl:for-each select="Form/TypeList/Type[Type = 1]">
                        <option value="{ProductTypeID}">
                            <xsl:if test="ProductTypeID = /Root/Params/productFilter">
                                <xsl:attribute name="selected">selected</xsl:attribute>
                            </xsl:if>
                            <xsl:value-of select="Description" />
                        </option>
                    </xsl:for-each>

                    <xsl:for-each select="Form/TypeList/Type[Type = 2 and ($cmd = 'show_licenses' or (ProductTypeID != 15 and $cmd = 'show_ca_licenses'))]">
                        <option value="{ProductTypeID}">
                            <xsl:if test="ProductTypeID = /Root/Params/productFilter">
                                <xsl:attribute name="selected">selected</xsl:attribute>
                            </xsl:if>
                            <xsl:value-of select="Description" />
                        </option>
                    </xsl:for-each>                     					
					
				</select>
				<label for="licensedFilter">Show: </label>
                <select name="licenseFilter" id="licenseFilter">
		            <xsl:attribute name="onchange">filterCatalogLicenses( oTabUpdateObj, '<xsl:value-of select="$licenseTab" />', this, { productFilter: '<xsl:value-of select="$productFilter" />' } );</xsl:attribute>
				    <option value="">
                        <xsl:if test="$licenseFilter = ''">
                            <xsl:attribute name="selected">selected</xsl:attribute>
                        </xsl:if>
                        All
                    </option>
					<option value="licensed">
                        <xsl:if test="$licenseFilter = 'licensed'">
                            <xsl:attribute name="selected">selected</xsl:attribute>
                        </xsl:if>
                        Licensed
                    </option>
					<option value="unlicensed">
                        <xsl:if test="$licenseFilter = 'unlicensed'">
                            <xsl:attribute name="selected">selected</xsl:attribute>
                        </xsl:if>
                        Unlicensed
                    </option>
				</select>

	   	    </td>
	    </tr>
	</xsl:template>


	<xsl:template name="trackLicenseHeader">
		<xsl:variable name="licenseFilter" select="/Root/Params/licenseFilter" />
		<xsl:variable name="productFilter" select="/Root/Params/productFilter" />
		<xsl:variable name="repaintCmd">oTabUpdateObj.ActivateTab( '<xsl:value-of select="$licenseTab" />', 'productFilter=<xsl:value-of select="$productFilter" />&amp;licenseFilter=<xsl:value-of select="$licenseFilter" />' );</xsl:variable>
		<tr>
			<th class="icon payCol">
				Pay
				<div class="toggleLinks">
          	        <xsl:variable name="url">/rps/track?c=<xsl:value-of select="$mechCommand" />&amp;licenseFilter=<xsl:value-of select="$licenseFilter" />&amp;productFilter=<xsl:value-of select="$productFilter" />&amp;AlbumID=<xsl:value-of select="/Root/Params/AlbumID" />&amp;value=</xsl:variable>
          	        <xsl:choose>
          	            <xsl:when test="$openRuns = 0">
				            <a href="javascript:ajaxToggleCheckbox( '{$url}0', null, function() {{ {$repaintCmd} }} )">All</a> /
				            <a href="javascript:ajaxToggleCheckbox( '{$url}1', null, function() {{ {$repaintCmd} }} )">None</a>
				        </xsl:when>
				        <xsl:otherwise>All / None</xsl:otherwise>
				    </xsl:choose>
				</div>
			</th>
			<th class="trackNumCol">Track</th>
			<th>Track Name</th>
			<th>Product</th>
			<th>Cross</th>
			<th>Region</th>
			<th class="amount">Share</th>
			<th>Publisher</th>
			<th>Admin</th>
			<th>Agent</th>
			<th class="status">Status</th>
			<th class="actionsCol">Actions</th>
		</tr>
	</xsl:template>


    <xsl:template name="trackLicenseRow">
  	    <xsl:param name="rowClass" />
  	    
        <tr class="{$rowClass}">
    	    <td class="icon">
    		    <xsl:if test="position() = 1">
					<xsl:call-template name="mechCheckBox" />
    		    </xsl:if>
    	    </td>
            <td>
      	        <xsl:if test="position() = 1">
        	        <xsl:if test="../../TrackOrder &lt; 10">0</xsl:if><xsl:value-of select="../../TrackOrder" />
                </xsl:if>
            </td>
            <td><a href="{$licenseArea}?c=show&amp;TrackLicenseID={TrackLicenseID}"><xsl:value-of select="../../Title" /></a></td>
            <td>
                <xsl:call-template name="productTypeName">
                    <xsl:with-param name="productTypeID" select="ProductTypeID" />
                    <xsl:with-param name="typeList" select="/Root/Form/TypeList" />
                </xsl:call-template>            
            </td>
            <td class="status">
      	        <xsl:choose>
      		        <xsl:when test="Type = 1">
      			        <xsl:choose>
      				        <xsl:when test="CrossCollateralized = 1">Yes</xsl:when>
      				        <xsl:otherwise>No</xsl:otherwise>
      			        </xsl:choose>
      		        </xsl:when>
      		        <xsl:otherwise>N/A</xsl:otherwise>
				</xsl:choose>
            </td>
            <td>
                <xsl:if test="normalize-space(RegionID) and RegionID != 0">
                    <xsl:variable name="regionID" select="RegionID" />
                    <xsl:value-of select="/Root/Form/RegionList/Region[RegionID = $regionID]/Name" />
                </xsl:if>
            </td>
            <td class="amount"><xsl:value-of select="Share" />%</td>
            <td>
                <xsl:choose>
                    <xsl:when test="Type = 4">Public Domain</xsl:when>
                    <xsl:otherwise><xsl:value-of select="Publisher/PublisherName" /></xsl:otherwise>
                </xsl:choose>
            </td>
            <td>
                <xsl:if test="Type != 4"><xsl:value-of select="Publisher/AdminName" /></xsl:if>
            </td>
            <td>
                <xsl:if test="Type != 4"><xsl:value-of select="Publisher/AgentName" /></xsl:if>
            </td>
            <td class="status">
                <xsl:if test="Inactive = 1">Inactive</xsl:if>
			    <xsl:if test="Inactive = 0">Active</xsl:if>
            </td>
            <td>
    	        <xsl:if test="position() = 1"><a href="{$licenseArea}?c=preedit&amp;TrackID={../../TrackID}" title="Add New License">Add</a>, </xsl:if>
    	        <xsl:if test="count(/Root/Form/TrackList/Track) &gt; 1"><a href="{$licenseArea}?c=copy&amp;TrackLicenseID={TrackLicenseID}" title="Copy License">Copy</a>, </xsl:if>
    	        <a href="{$licenseArea}?c=edit&amp;TrackLicenseID={TrackLicenseID}" title="edit Existing License">Edit</a>
            </td>
        </tr>
    </xsl:template>


	<xsl:template name="trackNoLicenseRow">
		<xsl:param name="rowClass" />
        <tr class="{$rowClass}">
    	    <td class="icon">
    		    <xsl:call-template name="mechCheckBox" />
    	    </td>
            <td>
                <xsl:if test="TrackOrder &lt; 10">0</xsl:if><xsl:value-of select="TrackOrder" />
            </td>
            <td><xsl:value-of select="Title" /></td>
            <td colspan="3"></td>
            <td class="error">UNLICENSED</td>
            <td colspan="4"></td>
            <td><a href="{$licenseArea}?c=preedit&amp;TrackID={TrackID}" title="Add New License">Add</a></td>
        </tr>
	</xsl:template>


	<xsl:template name="trackExemptRow">
		<xsl:param name="rowClass" />
        <tr class="{$rowClass}">
    	    <td class="icon">
    		    <xsl:call-template name="mechCheckBox" />
    	    </td>
            <td>
                <xsl:if test="TrackOrder &lt; 10">0</xsl:if><xsl:value-of select="TrackOrder" />
            </td>
            <td><xsl:value-of select="Title" /></td>
            <td colspan="3"></td>
            <td class="error">EXCLUDED</td>
            <td colspan="5"></td>
        </tr>
	</xsl:template>


	<xsl:template name="mechCheckBox">
		<xsl:variable name="licenseFilter" select="/Root/Params/licenseFilter" />
		<xsl:variable name="productFilter" select="/Root/Params/productFilter" />
	    <xsl:variable name="trackID"><xsl:value-of select="TrackID" /></xsl:variable>
        <xsl:variable name="url">/rps/track?c=<xsl:value-of select="$mechCommand" />&amp;licenseFilter=<xsl:value-of select="$licenseFilter" />&amp;productFilter=<xsl:value-of select="$productFilter" />&amp;TrackID=<xsl:value-of select="$trackID" /></xsl:variable>
		<input type="checkbox" name="payMech" id="payMech" >
		    <xsl:if test="$openRuns &gt; 0">
		        <xsl:attribute name="disabled">disabled</xsl:attribute>
		    </xsl:if>
		    <xsl:attribute name="onclick">ajaxToggleCheckbox( '<xsl:value-of select="$url" />', this, function() { oTabUpdateObj.ActivateTab( '<xsl:value-of select="$licenseTab" />', 'productFilter=<xsl:value-of select="$productFilter" />&amp;licenseFilter=<xsl:value-of select="$licenseFilter" />' );} );</xsl:attribute>
	      	<!-- a choose structure was needed because we want to catch the negation
                 but sometime is comes back as null and sometimes as '0' -->
            <xsl:choose>
                <xsl:when test="dyn:evaluate($mechExempt) = 1">
			    </xsl:when>
			    <xsl:otherwise>
				    <xsl:attribute name="checked">checked</xsl:attribute>
			    </xsl:otherwise>
	      	</xsl:choose>
		</input>
	</xsl:template>


	<xsl:template name="ccompRow">
		<xsl:variable name="licenseFilter" select="/Root/Params/licenseFilter" />
		<xsl:variable name="productFilter" select="/Root/Params/productFilter" />
	    <xsl:variable name="trackID"><xsl:value-of select="TrackID" /></xsl:variable>
        <xsl:variable name="url">/rps/catalog?c=<xsl:value-of select="$ccCommand" />&amp;AlbumID=<xsl:value-of select="/Root/Params/AlbumID" /></xsl:variable>
		<tr>
			<td colspan="12" class="right">
				<br />
				<label for="AlbumContractID">Contract for Controlled Composition: </label>
				<select onchange="ajaxToggleSelect( '{$url}', this, null, 'AlbumContractID' )" name="AlbumContractID" id="AlbumContractID">
				    <xsl:if test="$openRuns &gt; 0">
				        <xsl:attribute name="disabled">disabled</xsl:attribute>
				    </xsl:if>
				    <xsl:if test="not(//TrackLicense[Type = 2])">
					<option value="0">None</option>
					</xsl:if>
					<xsl:for-each select="Form/AlbumContractList/AlbumContract/Contract[HasCComp = 1]">
						<option value="{../AlbumContractID}">
							<xsl:if test="(../IsControlledComp = 1 and $cmd = 'show_licenses') or (../IsCAControlledComp = 1 and $cmd = 'show_ca_licenses')">
								<xsl:attribute name="selected">selected</xsl:attribute>
							</xsl:if>
							<xsl:value-of select="Title" />
						</option>
					</xsl:for-each>
				</select>
			</td>
		</tr>
	</xsl:template>


</xsl:stylesheet>
