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

  <xsl:variable name="lockedRecord">
    <xsl:choose>
      <xsl:when test="/Root/Form/ArtistContract/Status = 2">1</xsl:when>
      <xsl:when test="/Root/Form/ArtistContract/Status = 3">1</xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose> 
  </xsl:variable>

  <xsl:template name="contractShowLayout">
		<script type="text/javascript" src="/production/javascript/contract.js" />
    <xsl:call-template name="contractShow" />
    <xsl:call-template name="buttons" />
  </xsl:template>

  <xsl:template name="contractShow">
    <div class="section" id="contractShow">
      <xsl:call-template name="successMessageGeneral">
        <xsl:with-param name="successMessage">Contract successfully updated!</xsl:with-param>
      </xsl:call-template>
      <xsl:call-template name="contractView" />
      <br />
      <xsl:call-template name="termView" />
      <br />
      <div class="twoSections">
        <xsl:call-template name="ccompView">
          <xsl:with-param name="ccomp" select="Form/ArtistContract/ControlledComposition" />
        </xsl:call-template>
        <xsl:call-template name="expenseList" />
      </div>
      <xsl:call-template name="albumList" />
      <br />
      <xsl:call-template name="trackList" />
      <br /><br />
    </div>
  </xsl:template>

  <xsl:template name="termView">
    <fieldset class="contractTerms">
      <legend>Contract Terms</legend>
    <table class="textChart" id="termTable">
      <tbody>
        <tr>
          <xsl:if test="not(Form/ArtistContract/TermList/Term/ArtistContractTermID)">
            <xsl:attribute name="class">textChartEmpty</xsl:attribute>
          </xsl:if>
          <th class="rowLeft">Priority</th>
          <th>Source</th>
          <th>Region</th>
          <th>Channel</th>
          <th>Price</th>
          <th>Rate Type</th>
          <th class="percent">Rate</th>
          <th class="percent">Rate Reduction</th>
          <th class="percent">% of Sales</th>
          <th class="percent">Packaging</th>
          <th class="percent">Free Goods</th>
        </tr>
        <xsl:for-each select="Form/ArtistContract/TermList/Term">
          <xsl:call-template name="termRowView" />
        </xsl:for-each>
	    <xsl:if test="normalize-space(Form/ArtistContract/DefaultTerm/Rate)">
          <xsl:call-template name="defaultTermRowView" />
	    </xsl:if>
      </tbody>
    </table>
	<br />
	<xsl:if test="not(Form/ArtistContract/TermList/Term/ArtistContractTermID) and not(normalize-space(Form/ArtistContract/DefaultTerm/ArtistContractTermID))">
      No terms have been set for this contract.<br /><br />
    </xsl:if>
    <div class="liquidationScheduleHolder">
      <xsl:call-template name="liquidationScheduleView">
        <xsl:with-param name="liquidationSchedule" select="Form/ArtistContract/ReserveLiquidationList/ReserveLiquidation" />
        <xsl:with-param name="reserveRate" select="Form/ArtistContract/ReserveRate" />
      </xsl:call-template>
      <xsl:variable name="unitBasedScheduleTest">
        <xsl:for-each select="Form/ArtistContract/ReserveLiquidationList/ReserveLiquidation">
          <xsl:if test="normalize-space(Percent)"><xsl:value-of select="Percent" /></xsl:if>
        </xsl:for-each>
      </xsl:variable>
      <xsl:if test="normalize-space($unitBasedScheduleTest)">
        <div class="reserveTypes">
            <xsl:choose>
              <xsl:when test="Form/ArtistContract/DigitalReservesEnabled = 1">Physical and Digital</xsl:when>
              <xsl:otherwise>Physical Only</xsl:otherwise>
            </xsl:choose>
          </div> 
      </xsl:if>
    </div>
      <div class="singleButton">
        <input type="button" name="editTerms" value="Edit Terms" onClick="location.href='?c=edit&amp;ContractID={Form/ArtistContract/ArtistContractID}#terms'" />
      </div>
    </fieldset>
  </xsl:template>

  <xsl:template name="termRowView">
    <xsl:variable name="termPosition" select="position() - 1" />
    <tr class="row{position() mod 2}" id="contractTerm{position()}">
      <td class="rowLeft"><xsl:value-of select="Priority"/></td>
      <td>
        <xsl:variable name="sourceID"><xsl:value-of select="IncomeSourceID" /></xsl:variable>
	    <xsl:value-of select="/Root/Form/IncomeSourceList/IncomeSource[IncomeSourceID = $sourceID]/Name" />
      </td>
      <td>
        <xsl:variable name="regionID"><xsl:value-of select="RegionID" /></xsl:variable>
	    <xsl:value-of select="/Root/Form/RegionList/Region[RegionID = $regionID]/Name" />
      </td>
      <td>
	    <xsl:if test="ChannelID > 0">
          <xsl:variable name="channelID"><xsl:value-of select="ChannelID" /></xsl:variable>
	      <xsl:value-of select="/Root/Form/ChannelList/Channel[ChannelID = $channelID]/Name" />
		</xsl:if>
	    <xsl:if test="ChannelID = 0">
	      All
		</xsl:if>
      </td>
      <td>
	    <xsl:if test="PriceLevelID > 0">
          <xsl:variable name="priceID"><xsl:value-of select="PriceLevelID" /></xsl:variable>
	      <xsl:value-of select="/Root/Form/PriceLevelList/PriceLevel[PriceLevelID = $priceID]/Name" />
		</xsl:if>
	    <xsl:if test="PriceLevelID = 0">
	      All
		</xsl:if>
      </td>
      <td>
        <xsl:variable name="rateTypeID"><xsl:value-of select="ContractRateTypeID" /></xsl:variable>
	    <xsl:value-of select="/Root/Form/ContractRateTypeList/ContractRateType[ContractRateTypeID = $rateTypeID]/Name" />
      </td>
      <td class="percent"><xsl:if test="ContractRateTypeID = 6">$</xsl:if><xsl:value-of select="Rate" /><xsl:if test="ContractRateTypeID != 6">%</xsl:if></td>
      <td class="percent"><xsl:if test="normalize-space(RateReduction)"><xsl:value-of select="RateReduction" />%</xsl:if></td>
      <td class="percent"><xsl:if test="normalize-space(PercentageOfSales)"><xsl:value-of select="PercentageOfSales" />%</xsl:if></td>
      <td class="percent"><xsl:if test="normalize-space(PackagingDeduction)"><xsl:value-of select="PackagingDeduction" />%</xsl:if></td>
      <td class="percent"><xsl:if test="normalize-space(FreeGoodsDeduction)"><xsl:value-of select="FreeGoodsDeduction" />%</xsl:if></td>
    </tr>
  </xsl:template>

  <xsl:template name="defaultTermRowView">
    <tr class="defaultRow{count(Form/ArtistContract/TermList/Term) mod 2}" id="contractTermDefault">
      <td class="rowLeft">Default</td>
      <td>All</td>
      <td>All</td>
      <td>All</td>
      <td>All</td>
      <td>% Net Revenue</td>
      <td class="percent"><xsl:value-of select="Form/ArtistContract/DefaultTerm/Rate" />%</td>
      <td class="percent">0.00%</td>
      <td class="percent">0.00%</td>
      <td class="percent">0.00%</td>
      <td class="percent">0.00%</td>
    </tr>
  </xsl:template>


  <xsl:template name="expenseList">
    <fieldset class="expenseList">
      <legend>Recoupable Expenses</legend>
      <table class="textChart hiOn">
        <tr>
          <xsl:if test="not(Form/ExpenseTypeList/ExpenseType/ExpenseTypeID)">
            <xsl:attribute name="class">textChartEmpty</xsl:attribute>
          </xsl:if>
          <th class="rowLeft">Expense Type</th>
          <th>Recoupable</th>
        </tr>
        <xsl:for-each select="Form/ExpenseTypeList/ExpenseType[Inactive = 0]">
          <tr class="row{position() mod 2}">
            <td class="rowLeft"><xsl:value-of select="ExpenseName" /></td>
            <td><xsl:value-of select="Percent" />%</td>
          </tr>
        </xsl:for-each>
      </table>
      <xsl:if test="not(//Inactive = 0)">
        <br />No active recoupable expenses are associated with this contract.
      </xsl:if>
      <div class="insideFieldset">
        <div class="singleButton">
          <input type="button" name="editExpenses" value="Edit" onClick="location.href='contract?c=expense&amp;ContractID={Form/ArtistContract/ArtistContractID}'" />
        </div>
      </div>
    </fieldset>
  </xsl:template>

  <xsl:template name="albumList">
    <fieldset>
      <legend>Albums</legend>
      <table class="textChart">
        <tr>
          <xsl:if test="not(Form/AlbumContractList/AlbumContract/Album/AlbumID)">
            <xsl:attribute name="class">textChartEmpty</xsl:attribute>
          </xsl:if>
          <th class="rowLeft">Title</th>
          <th>Catalog #</th>
          <xsl:if test="Form/LabelList/Label">
            <th>Label</th>
          </xsl:if>
          <th>Artist</th>
          <th class="activeCol percent">Status</th>
          <th class="expenseCol percent">Expenses</th>
          <th class="crossCol">XC
            <xsl:call-template name="helpIcon">
              <xsl:with-param name="tip">Cross Collateralize</xsl:with-param>
            </xsl:call-template>
					</th>
        </tr>
        <xsl:for-each select="Form/AlbumContractList/AlbumContract/Album">
          <xsl:sort select="translate(Title,$lcletters,$ucletters)" />
          <xsl:call-template name="albumListRow" />
        </xsl:for-each>
      </table>
      <xsl:if test="not(Form/AlbumContractList/AlbumContract/Album)">
        <br />No albums are currently associated with this contract.
      </xsl:if>
    </fieldset>
  </xsl:template>

  <xsl:template name="albumListRow">
    <tr class="row{position() mod 2}">
      <td class="rowLeft"><a href="catalog?AlbumID={AlbumID}&amp;c=show"><xsl:value-of select="Title" /><xsl:if test="TitleVersion != ''"> (<xsl:value-of select="TitleVersion" />)</xsl:if></a></td>
      <td><xsl:value-of select="CatalogNumber" /></td>
      <xsl:if test="/Root/Form/LabelList/Label">
        <xsl:variable name="albumLabelID"><xsl:value-of select="LabelID" /></xsl:variable>
        <td><xsl:value-of select="/Root/Form/LabelList/Label[LabelID = $albumLabelID]/LabelName" /></td>
      </xsl:if>
      <td><xsl:value-of select="ArtistName" /></td>
      <td class="percent">
				<xsl:call-template name="albumStatus">
					<xsl:with-param name="status" select="Status" />
				</xsl:call-template>
			</td>
      <td class="percent"><a href="/rps/catalog?c=expense&amp;AlbumContractID={../AlbumContractID}">View</a></td>
      <td>
        <a href="catalog?c=cc&amp;ContractID={../ContractID}&amp;AlbumContractID={../AlbumContractID}">
          <xsl:if test="../CrossCollateralized = 0">No</xsl:if>
          <xsl:if test="../CrossCollateralized = 1">Yes</xsl:if>
        </a>
      </td>
    </tr>
  </xsl:template>

  <xsl:template name="trackList">
    <fieldset>
      <legend>Tracks</legend>
        <table class="textChart hiOn">
          <tr>
            <xsl:if test="not(Form/TrackContractList/TrackContract/Track/TrackID)">
              <xsl:attribute name="class">textChartEmpty</xsl:attribute>
            </xsl:if>
            <th class="rowLeft">Title</th>
            <th>Artist</th>
            <th>Album</th>
            <th class="lengthCol">Length</th>
						<th class="percent">Proration</th>
            <th class="expenseCol percent">Expenses</th>
          	<th class="crossCol">XC
	            <xsl:call-template name="helpIcon">
  	            <xsl:with-param name="tip">Cross Collateralize</xsl:with-param>
    	        </xsl:call-template>
      	    </th>
          </tr>
          <xsl:for-each select="Form/TrackContractList/TrackContract/Track">
            <xsl:sort select="translate(Title,$lcletters,$ucletters)" />
            <xsl:call-template name="trackListRow" />
          </xsl:for-each>
        </table>
        <xsl:if test="not(Form/TrackContractList/TrackContract/Track)">
          <br />No tracks are currently associated with this contract.
        </xsl:if>
    </fieldset>
  </xsl:template>

  <xsl:template name="trackListRow">
    <tr class="row{position() mod 2}">
      <td class="rowLeft"><a href="track?TrackID={TrackID}&amp;c=show"><xsl:value-of select="Title" /></a></td>
      <td><xsl:value-of select="ArtistName" /></td>
      <td><xsl:value-of select="../AlbumTitle" /></td>
      <td><xsl:value-of select="Duration" /></td>
      <td class="percent"><a href="javascript:editProration({../ContractID},{../TrackContractID},{../ProrateTrackCount})"><xsl:value-of select="../ProrateTrackCount" /></a></td>
      <td class="percent"><a href="/rps/track?c=expense&amp;TrackContractID={../TrackContractID}">View</a></td>
      <td>
        <a href="track?c=cc&amp;ContractID={../ContractID}&amp;TrackContractID={../TrackContractID}">
          <xsl:if test="../CrossCollateralized = 0">No</xsl:if>
          <xsl:if test="../CrossCollateralized = 1">Yes</xsl:if>
        </a>
      </td>
    </tr>
  </xsl:template>

  <xsl:template name="termList">
    <div class="termList">
      <h3>Physical Terms:</h3>
      <hr />
      <div class="contractDetailList">
        <xsl:if test="not($terms/Term)">
          No terms have been created.<br />
        </xsl:if>
        <xsl:for-each select="$terms/*">
          <xsl:call-template name="termListRow">
            <xsl:with-param name="mode" select="$mode" />
          </xsl:call-template>
        </xsl:for-each>
      </div>
      <input type="button" name="editTerms" value="Edit" onClick="location.href='?c=term&amp;ContractID={Form/ArtistContract/ArtistContractID}&amp;Mode={$mode}'" class="editSectionButton" />
    </div>
  </xsl:template>

  <xsl:template name="productFormatOption">
    <option value="{ProductFormatID}">
      <xsl:if test="ParentID != 0">- </xsl:if><xsl:value-of select="Name" />
    </option>
  </xsl:template>

  <xsl:template name="termListRow">
    <xsl:variable name="channelID" select="ChannelID" />
    <xsl:variable name="priceLevelID" select="PriceLevelID" />
    <xsl:value-of select="Percent" />% of 
    <xsl:if test="$mode = 1">
      <xsl:if test="$priceLevelID != 0"><xsl:value-of select="/Root/Form/PriceLevelList/PriceLevel[PriceLevelID = $priceLevelID]/Name" /> Priced </xsl:if>
      <xsl:if test="$channelID != 0"><xsl:value-of select="/Root/Form/ChannelList/Channel[ChannelID = $channelID]/Name" /> Channel </xsl:if>
    </xsl:if>
    <xsl:if test="ProductFormat/Name != 'All Physical'"><xsl:value-of select="ProductFormat/Name" />&#160;</xsl:if>sales
    <br />
  </xsl:template>

  <xsl:template name="deductionList">
    <div class="deductionList">
      <h3><xsl:if test="$mode = 1">Physical</xsl:if><xsl:if test="$mode != 1">Digital</xsl:if> Deductions:</h3>
      <hr />
      <div class="contractDetailList">
        <xsl:if test="not($deductions/Deduction)">
          No deductions have been created.<br />
        </xsl:if>
        <xsl:for-each select="$deductions/Deduction">
          <xsl:value-of select="Percent" />% for <xsl:value-of select="DeductionType/Name" /><br />
        </xsl:for-each>
      </div>
      <input type="button" name="EditDeductions" value="Edit" onClick="location.href='?c=deduction&amp;ContractID={Form/ArtistContract/ArtistContractID}&amp;Mode={$mode}'" class="editSectionButton" />
    </div>
  </xsl:template>

  <xsl:template name="OLDregionList">
    <div class="regionList">
      <h3><xsl:if test="$mode = 1">Physical </xsl:if>Regions:</h3>
      <hr />
      <div class="contractDetailList">
        <xsl:if test="not($regions/Region)">
          No regions have been associated with this model.<br />
        </xsl:if>
        <xsl:for-each select="$regions/Region">
          <xsl:value-of select="Percent" />% in <xsl:value-of select="Region/Name" /><br />
        </xsl:for-each>
      </div>
      <input type="button" name="EditRegions" value="Edit" onClick="location.href='?c=region&amp;ContractID={Form/ArtistContract/ArtistContractID}&amp;Mode={$mode}'" class="editSectionButton" />
    </div>
  </xsl:template>

  <xsl:template name="escalationList">
    <div class="escalationList escalationMode{$mode}">
      <h3><xsl:if test="$mode = 1">Physical</xsl:if><xsl:if test="$mode = 3">Digital Album</xsl:if><xsl:if test="$mode = 4">Digital Single</xsl:if> Escalations:</h3>
      <hr />
      <div class="contractDetailList">
        <xsl:if test="not($escalations/UnitEscalation)">
          No escalations have been created.<br />
        </xsl:if>
        <xsl:for-each select="$escalations/UnitEscalation">
          <xsl:if test="PennyBased = 1">$</xsl:if><xsl:value-of select="Amount" /><xsl:if test="PennyBased = 0">%</xsl:if> at <xsl:value-of select="Units" /> units<br />
        </xsl:for-each>
      </div>
      <input type="button" name="EditEscalations" value="Edit" onClick="location.href='?c=unit_escalation&amp;ContractID={Form/ArtistContract/ArtistContractID}&amp;Mode={$mode}'" class="editSectionButton" />
    </div>
  </xsl:template>

</xsl:stylesheet>
