<!--                                                                  -->
<!--  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="" encoding="utf-8" />

    <xsl:include href="/app/tools/rps/templates/ajax_request.xsl" />
    <xsl:include href="/app/tools/common/production/templates/components/index.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:variable name="productFilterID" select="/Root/Params/productFilter" />
    <xsl:variable name="licenseFilterID" select="/Root/Params/licenseFilter" />
    <xsl:variable name="statusFilterID"  select="/Root/Params/statusFilter" />

    <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" />
        <div id="trackLicenseViewTable">
            <h3><xsl:value-of select="$licenseType" /> Mechanical Licenses</h3>
            <div class="Album-tab-table">
                <xsl:call-template name="trackLicenseFilter" />
                <table class="Table Table-with-highlight" id="trackLicenseTable">
                    <xsl:choose>
                        <xsl:when test="Form/TrackList/Track">
                            <thead>
                                <xsl:call-template name="trackLicenseHeader" />
                            </thead>
                            <tbody>
                                <xsl:for-each select="Form/TrackList/Track">
                                    <xsl:call-template name="trackLicenseRowSwitch" />
                                </xsl:for-each>
                            </tbody>
                        </xsl:when>
                        <xsl:otherwise>
                            <tfoot>
                                <tr class="Table-row Table-row-data">
                                    <td class="Table-col" colspan="12">No tracks match this criteria.</td>
                                </tr>
                            </tfoot>
                        </xsl:otherwise>
                    </xsl:choose>
                </table>
                <xsl:call-template name="ccompRow" />
            </div>
        </div>
    </xsl:template>


    <xsl:template name="trackLicenseRowSwitch">
        <xsl:choose>
            <xsl:when test="dyn:evaluate($mechExempt) != 0">
                <xsl:call-template name="trackExemptRow" />
            </xsl:when>
            <xsl:when test="TrackLicenseList/TrackLicense">

                <xsl:for-each select="TrackLicenseList/TrackLicense">
                    <xsl:call-template name="trackLicenseRow" />
                </xsl:for-each>

            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="trackNoLicenseRow" />
            </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" />
        <xsl:variable name="statusFilter">
            <xsl:if test="$licenseFilter != 'unlicensed'">
                <xsl:value-of select="/Root/Params/statusFilter" />
            </xsl:if>
        </xsl:variable>
        <div class="Table-controls Table-controls-top">
            <div class="Form-group Form-group-horizontal Table-controls-item">
                <div class="Form-item">
                    <xsl:variable name="licensesProductTypes">
                        <map>
                            <mapEntry><name>All</name><value></value></mapEntry>
                            <xsl:for-each select="Form/TypeList/Type[Type = 1]">
                                <mapEntry>
                                    <name><xsl:value-of select="Description" /></name>
                                    <value><xsl:value-of select="ProductTypeID" /></value>
                                </mapEntry>
                            </xsl:for-each>

                            <xsl:for-each select="Form/TypeList/Type[Type = 2 and ($cmd = 'show_licenses' or (ProductTypeID != 15 and $cmd = 'show_ca_licenses'))]">
                                <mapEntry>
                                    <name><xsl:value-of select="Description" /></name>
                                    <value><xsl:value-of select="ProductTypeID" /></value>
                                </mapEntry>
                            </xsl:for-each>
                        </map>
                    </xsl:variable>

                    <label class="Form-label">
                        <span class="Form-label-caption">Show Product Type</span>
                        <div class="Form-input-wrapper">
                            <xsl:call-template name="uiDropdown">
                                <xsl:with-param name="name" select="'productFilter'" />
                                <xsl:with-param name="id" select="'productFilter'" />
                                <xsl:with-param name="active" select="/Root/Params/productFilter" />
                                <xsl:with-param name="items" select="$licensesProductTypes" />
                                <xsl:with-param name="className" select="'Album-license-type-dropdown'" />
                                <xsl:with-param name="onChange">
                                    filterCatalogLicenses( oTabUpdateObj, '<xsl:value-of select="$licenseTab" />', this, { licenseFilter: '<xsl:value-of select="$licenseFilter" />', statusFilter: '<xsl:value-of select="$statusFilter" />' } );
                                </xsl:with-param>
                            </xsl:call-template>
                        </div>
                    </label>
                </div>
                <div class="Form-item">

                    <xsl:variable name="licensesFilterItems">
                        <map>
                            <mapEntry>
                                <name>All</name>
                                <value></value>
                            </mapEntry>
                            <mapEntry>
                                <name>Licensed</name>
                                <value>licensed</value>
                            </mapEntry>
                            <mapEntry>
                                <name>Unlicensed</name>
                                <value>unlicensed</value>
                            </mapEntry>
                        </map>
                    </xsl:variable>

                    <label class="Form-label">
                        <span class="Form-label-caption">Show</span>
                        <div class="Form-inputWrapper">
                            <xsl:call-template name="uiDropdown">
                                <xsl:with-param name="name" select="'licenseFilter'" />
                                <xsl:with-param name="id" select="'licenseFilter'" />
                                <xsl:with-param name="active" select="$licenseFilter" />
                                <xsl:with-param name="items" select="$licensesFilterItems" />
                                <xsl:with-param name="className" select="'Album-license-filter-dropdown'" />
                                <xsl:with-param name="onChange">
                                    filterCatalogLicenses( oTabUpdateObj, '<xsl:value-of select="$licenseTab" />', this, { productFilter: '<xsl:value-of select="$productFilter" />', statusFilter: '<xsl:value-of select="$statusFilter" />' } );
                                </xsl:with-param>
                            </xsl:call-template>
                        </div>
                    </label>
                </div>

                <div class="Form-item">
                    <xsl:variable name="licensesStatusFilterItems">
                        <map>
                            <mapEntry>
                                <name>Any Status</name>
                                <value></value>
                            </mapEntry>
                            <mapEntry>
                                <name>Active</name>
                                <value>active</value>
                            </mapEntry>
                            <mapEntry>
                                <name>Inactive</name>
                                <value>inactive</value>
                            </mapEntry>
                        </map>
                    </xsl:variable>

                    <label class="Form-label">
                        <div class="Form-inputWrapper">
                            <xsl:call-template name="uiDropdown">
                                <xsl:with-param name="name" select="'statusFilter'" />
                                <xsl:with-param name="id" select="'statusFilter'" />
                                <xsl:with-param name="active">
                                    <xsl:if test="$licenseFilter != 'unlicensed'">
                                        <xsl:value-of select="$statusFilter" />
                                    </xsl:if>
                                </xsl:with-param>
                                <xsl:with-param name="disabled">
                                    <xsl:if test="$licenseFilter = 'unlicensed'">
                                        <xsl:text>true</xsl:text>
                                    </xsl:if>
                                </xsl:with-param>
                                <xsl:with-param name="items" select="$licensesStatusFilterItems" />
                                <xsl:with-param name="className" select="'Album-license-status-filter-dropdown'" />
                                <xsl:with-param name="onChange">
                                    filterCatalogLicenses( oTabUpdateObj, '<xsl:value-of select="$licenseTab" />', this, { licenseFilter: '<xsl:value-of select="$licenseFilter" />', productFilter: '<xsl:value-of select="$productFilter" />' } );
                                </xsl:with-param>
                            </xsl:call-template>
                        </div>
                    </label>
                </div>
            </div>
        </div>
    </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 class="Table-row Table-row-header">
            <th class="Table-col Table-col-icon Table-col-one-line">
                Pay
                <div>
                    <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="Table-col">Track</th>
            <th class="Table-col">Track Name</th>
            <th class="Table-col">Product</th>
            <th class="Table-col">Cross</th>
            <th class="Table-col">Region</th>
            <th class="Table-col">Share</th>
            <th class="Table-col">Publisher</th>
            <th class="Table-col">Admin</th>
            <th class="Table-col">Agent</th>
            <th class="Table-col">Status</th>
            <th class="Table-col Table-col-all-actions">Actions</th>
        </tr>
    </xsl:template>

    <xsl:template name="trackLicenseRow">
        <tr class="Table-row Table-row-data">
            <td class="Table-col Table-col-icon">
                <xsl:if test="position() = 1">
                    <xsl:call-template name="mechCheckBox" />
                </xsl:if>
            </td>
            <td class="Table-col">
                <xsl:if test="position() = 1">
                    <xsl:if test="../../TrackOrder &lt; 10">0</xsl:if><xsl:value-of select="../../TrackOrder" />
                </xsl:if>
            </td>
            <td class="Table-col"><a href="{$licenseArea}?c=show&amp;TrackLicenseID={TrackLicenseID}"><xsl:value-of select="../../Title" /></a></td>
            <td class="Table-col">
                <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="Table-col">
                <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 class="Table-col">
                <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="Table-col"><xsl:value-of select="Share" />%</td>
            <td class="Table-col">
                <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 class="Table-col">
                <xsl:if test="Type != 4"><xsl:value-of select="Publisher/AdminName" /></xsl:if>
            </td>
            <td class="Table-col">
                <xsl:if test="Type != 4"><xsl:value-of select="Publisher/AgentName" /></xsl:if>
            </td>
            <td class="Table-col">
                <xsl:if test="Inactive = 1">Inactive</xsl:if>
                <xsl:if test="Inactive = 0">Active</xsl:if>
            </td>
            <td class="Table-col Table-col-all-actions">
                <xsl:if test="count(/Root/Form/TrackList/Track) &gt; 1">
                    <a href="{$licenseArea}?c=copy&amp;TrackLicenseID={TrackLicenseID}" class="js-tooltip Table-action-item" title="Copy License">Copy</a>
                </xsl:if>
                    <a href="{$licenseArea}?c=edit&amp;TrackLicenseID={TrackLicenseID}" class="js-tooltip Table-action-item" title="Edit Existing License">
                        <xsl:call-template name="icon">
                            <xsl:with-param name="type" select="'edit'" />
                            <xsl:with-param name="role" select="'action'" />
                        </xsl:call-template>
                    </a>
                <xsl:if test="position() = 1">
                    <a href="{$licenseArea}?c=preedit&amp;TrackID={../../TrackID}" class="js-tooltip Table-action-item" title="Add New License">
                        <xsl:call-template name="icon">
                            <xsl:with-param name="type" select="'add'" />
                        </xsl:call-template>
                    </a>
                </xsl:if>
            </td>
        </tr>
    </xsl:template>

    <xsl:template name="trackNoLicenseRow">
        <tr class="Table-row Table-row-data">
            <td class="Table-col Table-col-icon">
                <xsl:call-template name="mechCheckBox" />
            </td>
            <td class="Table-col">
                <xsl:if test="TrackOrder &lt; 10">0</xsl:if><xsl:value-of select="TrackOrder" />
            </td>
            <td class="Table-col"><xsl:value-of select="Title" /></td>
            <td class="Table-col" colspan="3"></td>
            <td class="Table-col Table-col-error">Unlicensed</td>
            <td class="Table-col" colspan="4"></td>
            <td class="Table-col Table-col-all-actions">
                <a href="{$licenseArea}?c=preedit&amp;TrackID={TrackID}" class="js-tooltip Table-action-item" title="Add New License">
                    <xsl:call-template name="icon">
                        <xsl:with-param name="type" select="'add'" />
                    </xsl:call-template>
                </a>
            </td>
        </tr>
    </xsl:template>

    <xsl:template name="trackExemptRow">
        <tr class="Table-row Table-row-data">
            <td class="Table-col Table-col-icon">
                <xsl:call-template name="mechCheckBox" />
            </td>
            <td class="Table-col">
                <xsl:if test="TrackOrder &lt; 10">0</xsl:if><xsl:value-of select="TrackOrder" />
            </td>
            <td class="Table-col"><xsl:value-of select="Title" /></td>
            <td class="Table-col" colspan="3"></td>
            <td class="Table-col Table-col-error">EXCLUDED</td>
            <td class="Table-col" 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>

        <xsl:call-template name="uiCheckbox">
            <xsl:with-param name="name" select="'payMech'" />
            <xsl:with-param name="id" select="'payMech'" />
            <xsl:with-param name="disabled" >
                <xsl:if test="$openRuns &gt; 0">
                    <xsl:text>true</xsl:text>
                </xsl:if>
            </xsl:with-param>
            <xsl:with-param 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" />' );} );
                <!-- a choose structure was needed because we want to catch the negation
                but sometime is comes back as null and sometimes as '0' -->
            </xsl:with-param>
            <xsl:with-param name="checked">
                <xsl:choose>
                    <xsl:when test="dyn:evaluate($mechExempt) = 1">
                        <xsl:text>false</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:text>true</xsl:text>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:with-param>
        </xsl:call-template>
    </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>
        <div class="Table-controls Table-controls-bottom">
            <div class="Form-group Form-group-horizontal Table-controls-item">
                <div class="Form-item">

                    <xsl:variable name="licensesContractCompositionItems">
                        <map>
                            <xsl:if test="not(//TrackLicense[Type = 2])">
                                <mapEntry>
                                    <name>None</name>
                                    <value>0</value>
                                </mapEntry>
                            </xsl:if>

                            <xsl:for-each select="Form/AlbumContractList/AlbumContract/Contract[HasCComp = 1]">
                                <mapEntry>
                                    <name><xsl:value-of select="Title" /></name>
                                    <value><xsl:value-of select="../AlbumContractID" /></value>
                                    <selected>
                                        <xsl:if test="(../IsControlledComp = 1 and $cmd = 'show_licenses') or (../IsCAControlledComp = 1 and $cmd = 'show_ca_licenses')">
                                            <xsl:text>true</xsl:text>
                                        </xsl:if>
                                    </selected>
                                </mapEntry>
                            </xsl:for-each>
                        </map>
                    </xsl:variable>

                    <label class="Form-label">
                        <span class="Form-label-caption">Contract for Controlled Composition</span>
                        <div class="Form-input-wrapper">

                            <xsl:call-template name="uiDropdown">
                                <xsl:with-param name="name" select="'AlbumContractID'" />
                                <xsl:with-param name="id" select="'AlbumContractID'" />
                                <xsl:with-param name="items" select="$licensesContractCompositionItems" />
                                <xsl:with-param name="className" select="'Album-license-composition-dropdown'" />
                                <xsl:with-param name="disabled">
                                    <xsl:if test="$openRuns &gt; 0">
                                        <xsl:text>true</xsl:text>
                                    </xsl:if>
                                </xsl:with-param>
                                <xsl:with-param name="onChange">
                                    <xsl:text>ajaxToggleSelect( '</xsl:text>
                                    <xsl:value-of select="$url" />
                                    <xsl:text>', this, null, 'AlbumContractID' )</xsl:text>
                                </xsl:with-param>
                            </xsl:call-template>
                        </div>
                    </label>
                </div>
            </div>
        </div>
    </xsl:template>


</xsl:stylesheet>
