<!--                                                                  -->
<!--  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" xmlns:set="http://exslt.org/sets" extension-element-prefixes="dyn set">

  <xsl:variable name="licenseIncomeCount" select="count(Root/Form/ArtistContractLicenseIncomeList/ArtistContractLicenseIncome)" />

  <xsl:template name="licenseIncomeLayout">
    <script type="text/javascript" src="/production/javascript/validate/artistContractLicenseIncome.js?t={$now}" />
    <script src="/production/javascript/autocomplete/licenseIncome.js?t={$now}" type="text/javascript"></script>
    <form name="licenseIncomeSource" id="licenseIncomeSource" method="post" action="artist_contract">
      <xsl:call-template name="licenseIncomeEdit" />
      <xsl:call-template name="buttons" />
    </form>
  </xsl:template>

  <xsl:template name="licenseIncomeEdit">
    <div class="section">
      <xsl:call-template name="openRunStatusMessage" />
      <xsl:if test="Params/_inline">
        <input type="hidden" name="_inline" value="1" />
      </xsl:if>
      <input type="hidden" name="formSubmit" value="1" />
      <input type="hidden" name="c" value="license_income" />
      <input type="hidden" name="ArtistContractID" value="{Params/ArtistContractID}" />
      <input type="hidden" name="licenseIncomeCount" id="licenseIncomeCount" value="{$licenseIncomeCount}" />
      <p class="formHeader">Enter license income source information here.</p>
      <hr />
      <xsl:if test="//*[@e]">
        <xsl:call-template name="errorMessageGeneral" />
        <xsl:call-template name="licenseIncomeErrors" />
      </xsl:if>
      <xsl:call-template name="licenseIncomeSection" />
    </div>
  </xsl:template>

  <xsl:template name="licenseIncomeSection">
    <table class="textChart" id="licenseIncomeSourceTable">
      <tbody>
        <tr>
          <th>Income Type</th>
          <th>Net Revenue Rate</th>
          <xsl:if test="$licenseIncomeCount &gt; 0">
            <th class="deleteCol">Delete</th>
          </xsl:if>
        </tr>
        <xsl:choose>
          <xsl:when test="$licenseIncomeCount &gt; 0">
            <xsl:for-each select="Form/ArtistContractLicenseIncomeList/ArtistContractLicenseIncome">
              <xsl:call-template name="licenseIncomeRow">
              	<xsl:with-param name="id" select="@k" />
              </xsl:call-template>
            </xsl:for-each>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="licenseIncomeRow" />
          </xsl:otherwise>
        </xsl:choose>
      </tbody>
    </table>

		<xsl:variable name="addLink">
			<xsl:choose>
				<xsl:when test="$licenseIncomeCount = 0">javascript:addLicenseIncome(1)</xsl:when>
				<xsl:otherwise>javascript:addLicenseIncome(<xsl:value-of select="$licenseIncomeCount" />)</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

	<p class="addLink">
	    <xsl:choose>
          <xsl:when test="$openRuns &gt; 0">
          </xsl:when>
          <xsl:otherwise>
	   	    <a href="{$addLink}" title="Add Another License Income Source" id="addImageLink">
    	   		<img src="/production/images/icons/office/Add-(16x16).gif" height="16" width="16" />
	   	    </a> <a href="{$addLink}" id="addTextLink">Add Another License Income Source</a>
		  </xsl:otherwise>
		</xsl:choose>
   	</p>

  </xsl:template>

  <xsl:template name="licenseIncomeRow">
  	<xsl:param name="id" select="'NEW0'" />
    <xsl:variable name="licenseIncomePosition" select="position() - 1" />
    <xsl:variable name="licenseIncomeTypeID" select="LicenseIncomeTypeID" />
    <xsl:variable name="rowID">licenseIncome<xsl:value-of select="$licenseIncomePosition" /></xsl:variable>
    <xsl:variable name="fieldPrefix">Form_ArtistContractLicenseIncomeList_ArtistContractLicenseIncome_<xsl:value-of select="$licenseIncomePosition" />_</xsl:variable>

    <tr class="row{position() mod 2}" id="{$rowID}">
      <td>
        <xsl:if test="LicenseIncomeTypeID[@e]">
          <xsl:attribute name="class">errorInput</xsl:attribute>
        </xsl:if>

				<div id="{$rowID}_AutoComplete" style="z-index: {1000 - position()}">
					<input id="{$fieldPrefix}LicenseIncomeType" name="{$fieldPrefix}LicenseIncomeType" type="text" class="incomeTypeField" value="{LicenseIncomeType}">
                      <xsl:if test="$openRuns = 1">
                        <xsl:attribute name="disabled">disabled</xsl:attribute>
                      </xsl:if>
						<xsl:if test="Inactive = 1">
            	<xsl:attribute name="disabled">disabled</xsl:attribute>
          	</xsl:if>
					</input>&#160;<span id="noMatchLicenseIncome_{$fieldPrefix}" class="yui-ac-warning"></span>
					<input type="hidden" name="{$fieldPrefix}LicenseIncomeTypeID" id="{$fieldPrefix}LicenseIncomeTypeID" value="{LicenseIncomeTypeID}" />
					<div id="licenseIncomeContainer_{$fieldPrefix}"></div>
					<script type="text/javascript">
              var licenseIncomeComp_<xsl:value-of select="$fieldPrefix" /> = new YAHOO.widget.RSLicenseIncomeAutoComplete("licenseIncomeContainer_<xsl:value-of select="$fieldPrefix" />","<xsl:value-of select="$fieldPrefix" />LicenseIncomeType", "<xsl:value-of select="$fieldPrefix" />LicenseIncomeTypeID",
               {
                  "noMatchWarningID": "noMatchLicenseIncome_<xsl:value-of select="$fieldPrefix" />"
               }
              );
          </script>
				</div>

      </td>
      <td>
        <xsl:if test="Percent[@e]">
          <xsl:attribute name="class">errorInput</xsl:attribute>
        </xsl:if>
        <input type="text" class="percentField" name="{$fieldPrefix}Percent" id="{$fieldPrefix}Percent" value="{Percent}">
          <xsl:if test="$openRuns = 1">
            <xsl:attribute name="disabled">disabled</xsl:attribute>
          </xsl:if>
          <xsl:if test="Inactive = 1">
            <xsl:attribute name="disabled">disabled</xsl:attribute>
          </xsl:if>
        </input>%
      </td>
      <xsl:if test="$licenseIncomeCount &gt; 0">
        <td class="icon">
          <xsl:if test="normalize-space(ArtistContractLicenseIncomeID)">
            <input type="checkbox" name="{$fieldPrefix}Delete" id="{$fieldPrefix}Delete">
              <xsl:if test="$openRuns = 1">
                <xsl:attribute name="disabled">disabled</xsl:attribute>
              </xsl:if>
              <xsl:if test="Inactive = 1">
                <xsl:attribute name="disabled">disabled</xsl:attribute>
              </xsl:if>
            </input>
          </xsl:if>
        </td>
      </xsl:if>
    </tr>
  </xsl:template>

  <xsl:template name="licenseIncomeErrors">
    <div class="error">
      <xsl:if test="//ArtistContractLicenseIncomeList[@e = 'field_duplicate']">Each income type can only be used once.<br /></xsl:if>
      <xsl:if test="//LicenseIncomeTypeID[@e = 'field_blank']">Income Type cannot be blank.<br /></xsl:if>
      <xsl:if test="//Percent[@e = 'field_blank']">Net Revenue Rate cannot be blank.<br /></xsl:if>
      <xsl:if test="//Percent[@e = 'field_invalid']">Net Revenue Rate is invalid.<br /></xsl:if>
      <br />
    </div>
  </xsl:template>

</xsl:stylesheet>
