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

    <xsl:variable name="fieldPrefix">Form_McpsLicense_McpsLicenseRetentionList_McpsLicenseRetention_</xsl:variable>

    <xsl:variable name="priceTiersConfigured">
        <xsl:for-each select="/Root/Form/Product/PriceList/Price">
            <xsl:choose>
                <xsl:when test="normalize-space(PriceID)">
                    <xsl:value-of select="PriceID" />
                </xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:variable>

    <xsl:variable name="committed">
        <xsl:choose>
            <xsl:when test="/Root/Form/McpsLicense/Committed = 1">1</xsl:when>
            <xsl:otherwise>0</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>


    <xsl:template name="pickLayout">
        <script type="text/javascript" src="/production/javascript/validate/mcpsLicense.js?v=1" />
        <form name="mcpsLicenseEdit" id="mcpsLicenseEdit" method="post" action="uk_mech">
            <input type="hidden" name="formSubmit" value="1" />
            <input type="hidden" name="c" value="edit_mcps_license" />
            <input type="hidden" name="McpsLicenseID" value="{Form/McpsLicense/McpsLicenseID}" />
            <input type="hidden" name="ProductID" value="{Form/Product/ProductID}" />
            <input type="hidden" name="Form_McpsLicense_ProductID" value="{Form/Product/ProductID}" />
            <div class="section">
            <xsl:call-template name="helpLink">
                <xsl:with-param name="helpCommand" select="'show_mcps_license'" />
            </xsl:call-template>
                <xsl:call-template name="albumView" />
                <br />


     		    <xsl:call-template name="openRunStatusMessage" />
                <xsl:call-template name="errorMessageGeneral" />
                <xsl:call-template name="mcpsLicenseErrors" />


                <xsl:call-template name="mcpsLicenseEdit" />
                <br class="superBreak" />
                <br />
            </div>
            <xsl:call-template name="buttons" />
        </form>
    </xsl:template>

    <xsl:template name="submitButton">
        <input name="Submit" type="submit" id="submitButton" value="Update">
            <xsl:if test="$openRuns = 1">
                <xsl:attribute name="disabled" select="'disabled'" />
            </xsl:if>
        </input>
    </xsl:template>

    <xsl:template name="cancelButton">
        <input type="button" value="Return to Album" onClick="location.href='catalog?c=show&amp;AlbumID={Form/Album/AlbumID}&amp;tab=ukLicenseTab'" />
    </xsl:template>

    <xsl:template name="mcpsLicenseEdit">
        <fieldset>
            <legend>MCPS Data</legend>
            <div class="insideFieldset">
                <p class="formHeader">Enter MCPS product information here.</p>
                <hr />
                <xsl:call-template name="column1Edit" />
                <xsl:call-template name="column2Edit" />
            </div>
        </fieldset>
    </xsl:template>


    <xsl:template name="column1Edit">

        <xsl:variable name="productTypeID" select="Form/Product/ProductTypeID" />
        <xsl:variable name="productType" select="Form/TypeList/Type[ProductTypeID = $productTypeID]/Description" />

        <div class="column1">
            <table>
                <tr>
                    <td class="leftCol">Product Type:</td>
                    <td class="readOnlyCol">
                        <xsl:value-of select="$productType" />
                    </td>
                </tr>
                <tr>
                    <td class="leftCol">Product ID:</td>
                    <td class="readOnlyCol">
                        <xsl:value-of select="Form/Product/ProductCode" />
                    </td>
                </tr>
                <tr>
                  <td class="leftCol"><label for="Form_McpsLicense_McpsID">MCPS ID:</label></td>
                  <td>
                    <xsl:call-template name="inlineError">
                      <xsl:with-param name="path">Form/McpsLicense/McpsID</xsl:with-param>
                      <xsl:with-param name="displayName">MCPS ID</xsl:with-param>
                    </xsl:call-template>
                    <input type="text" class="mcpsLicenseIDField" name="Form_McpsLicense_McpsID" id="Form_McpsLicense_McpsID" value="{Form/McpsLicense/McpsID}">
                        <xsl:if test="$openRuns = 1">
                            <xsl:attribute name="disabled" select="'disabled'" />
                        </xsl:if>
                    </input>
                  </td>
                </tr>

                <xsl:if test="$productType = 'DVD'">
                    <tr>
                        <td class="leftCol"><label for="Form_McpsLicense_DvdCategory">DVD Category:</label></td>
                        <td>
                            <xsl:call-template name="inlineError">
                                <xsl:with-param name="path">Form/McpsLicense/DvdCategory</xsl:with-param>
                                <xsl:with-param name="displayName">DVD Category</xsl:with-param>
                            </xsl:call-template>
                            <select name="Form_McpsLicense_DvdCategory" id="Form_McpsLicense_DvdCategory">
                                <xsl:if test="$openRuns = 1">
                                    <xsl:attribute name="disabled" select="'disabled'" />
                                </xsl:if>
                                <option value="A">
                                    <xsl:if test="Form/McpsLicense/DvdCategory = 'A'">
                                        <xsl:attribute name="selected">selected</xsl:attribute>
                                    </xsl:if>
                                    A
                                </option>
                                <option value="B">
                                    <xsl:if test="Form/McpsLicense/DvdCategory = 'B'">
                                        <xsl:attribute name="selected">selected</xsl:attribute>
                                    </xsl:if>
                                    B
                                </option>
                                <option value="C">
                                    <xsl:if test="Form/McpsLicense/DvdCategory = 'C'">
                                        <xsl:attribute name="selected">selected</xsl:attribute>
                                    </xsl:if>
                                    C
                                </option>
                                <option value="AVP">
                                    <xsl:if test="Form/McpsLicense/DvdCategory = 'AVP'">
                                        <xsl:attribute name="selected">selected</xsl:attribute>
                                    </xsl:if>
                                    AVP
                                </option>
                            </select>
                        </td>
                    </tr>
                </xsl:if>

				<tr>
					<td class="leftCol">Product Status:</td>
					<td class="readOnlyCol">
					    <xsl:variable name="productStatusID" select="Form/Product/ProductStatusID" />
                        <xsl:value-of select="Form/StatusList/Status[ProductStatusID = $productStatusID]/Description" />
					</td>
				</tr>
                <tr>
                    <td class="leftCol">Release Date:</td>
                    <td class="readOnlyCol">
                        <xsl:value-of select="Form/Product/ReleaseDate" />
                    </td>
                </tr>
				<tr>
					<td class="leftCol">Current Default Price:</td>
					<td class="readOnlyCol">
					    <xsl:variable name="priceLevelID" select="Form/Product/DefaultPriceLevelID" />
                        <xsl:value-of select="Form/PriceLevelList/PriceLevel[PriceLevelID = $priceLevelID]/Name" />
					</td>
				</tr>

				<!-- The payor drop-down and supplier code below it need to start out (and stay)
				     in sync, so we'll figure out the selected payor ahead of time and reference
				     it for both rows.  We'll also create hash in javascript to store the payor id/
				     supplier code pairs so that we can update it when the drop-down changes -->

                <script type="text/javascript">
                    var supplierCodes = new Object();
                    <xsl:for-each select="Form/PayorList/Payor">
                        supplierCodes[<xsl:value-of select="PayorID" />] = '<xsl:value-of select="SupplierCode" />';
                    </xsl:for-each>
                </script>

		        <xsl:variable name="selectedPayorID">
                    <xsl:choose>
                        <xsl:when test="normalize-space(Form/McpsLicense/PayorID)">
                            <xsl:value-of select="Form/McpsLicense/PayorID" />
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="Form/PayorList/Payor[IsDefault = 1]/PayorID" />
                        </xsl:otherwise>
                    </xsl:choose>
                 </xsl:variable>

                <tr>
                    <td class="leftCol"><label for="Form_McpsLicense_PayorID">Payor:</label></td>
                    <td>
                        <xsl:call-template name="inlineError">
                            <xsl:with-param name="path">Form/McpsLicense/PayorID</xsl:with-param>
                            <xsl:with-param name="displayName">Payor</xsl:with-param>
                        </xsl:call-template>
                        <select name="Form_McpsLicense_PayorID" id="Form_McpsLicense_PayorID" onChange="updateSupplierCode(this.value)">
                            <xsl:if test="$openRuns = 1">
                                <xsl:attribute name="disabled" select="'disabled'" />
                            </xsl:if>
                            <xsl:for-each select="Form/PayorList/Payor">
                                <option value="{PayorID}">
                                    <xsl:if test="PayorID = $selectedPayorID">
                                        <xsl:attribute name="selected">selected</xsl:attribute>
                                    </xsl:if>
                                    <xsl:value-of select="substring(Name,1,25)" /><xsl:if test="substring(Name,26,1)">...</xsl:if>
                                </option>
                            </xsl:for-each>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td class="leftCol">Supplier Code:</td>
                    <td class="readOnlyCol" id="supplierCode">
                        <xsl:value-of select="Form/PayorList/Payor[PayorID = $selectedPayorID]/SupplierCode" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <br />*Required field
                    </td>
                </tr>
            </table>
        </div>

    </xsl:template>


    <xsl:template name="column2Edit">
        <div class="column2">
            <table>
                <tr>
                    <td class="leftCol">
                        <label for="Form_McpsLicense_RetentionCategory">
                            TV Advertised:
                        </label>
                    </td>
                    <td>
    				    <xsl:call-template name="inlineError">
    					    <xsl:with-param name="path">Form/McpsLicense/RetentionCategory</xsl:with-param>
    					    <xsl:with-param name="displayName">TV Advertised</xsl:with-param>
    				    </xsl:call-template>
    				    <select name="Form_McpsLicense_RetentionCategory" id="Form_McpsLicense_RetentionCategory">
                            <xsl:if test="$openRuns = 1">
                                <xsl:attribute name="disabled" select="'disabled'" />
                            </xsl:if>
    				        <option value="S">
    				            No
    				        </option>
                            <option value="T">
                                <xsl:if test="Form/McpsLicense/RetentionCategory = 'T'">
                                    <xsl:attribute name="selected">selected</xsl:attribute>
                                </xsl:if>
                                Yes
                            </option>
    				    </select>
    			    </td>
                </tr>
                <tr>
                    <td class="leftCol">
                        <label for="Form_McpsLicense_InitialPeriodsCompleted">
                            Initial Periods Completed:
                        </label>
                    </td>
                    <td>
    				    <xsl:call-template name="inlineError">
    					    <xsl:with-param name="path">Form/McpsLicense/InitialPeriodsCompleted</xsl:with-param>
    					    <xsl:with-param name="displayName">Initial Periods Completed</xsl:with-param>
    				    </xsl:call-template>
    				    <select name="Form_McpsLicense_InitialPeriodsCompleted" id="Form_McpsLicense_InitialPeriodsCompleted">
    				        <xsl:if test="$priceTiersConfigured != 0">
    				            <xsl:attribute name="onChange">historicalRetentions(this.value)</xsl:attribute>
    				        </xsl:if>
    				        <xsl:if test="$committed = 1 or $openRuns = 1">
    				            <xsl:attribute name="disabled">disabled</xsl:attribute>
    				        </xsl:if>
    				        <option value="0">0</option>
    				        <option value="1">
                                <xsl:if test="Form/McpsLicense/InitialPeriodsCompleted = 1">
                                    <xsl:attribute name="selected">selected</xsl:attribute>
                                </xsl:if>
    				            1
    				        </option>
                            <option value="2">
                                <xsl:if test="Form/McpsLicense/InitialPeriodsCompleted = 2">
                                    <xsl:attribute name="selected">selected</xsl:attribute>
                                </xsl:if>
                                2
                            </option>
                            <option value="3">
                                <xsl:if test="Form/McpsLicense/InitialPeriodsCompleted = 3">
                                    <xsl:attribute name="selected">selected</xsl:attribute>
                                </xsl:if>
                                3
                            </option>
                            <option value="4">
                                <xsl:if test="Form/McpsLicense/InitialPeriodsCompleted = 4">
                                    <xsl:attribute name="selected">selected</xsl:attribute>
                                </xsl:if>
                                4
                            </option>
                            <option value="5">
                                <xsl:if test="Form/McpsLicense/InitialPeriodsCompleted &gt; 4">
                                    <xsl:attribute name="selected">selected</xsl:attribute>
                                </xsl:if>
                                5+
                            </option>
    				    </select>
    			    </td>
                </tr>

                <xsl:variable name="initialPeriods">
                    <xsl:choose>
                        <xsl:when test="normalize-space(Form/McpsLicense/InitialPeriodsCompleted)">
                            <xsl:value-of select="Form/McpsLicense/InitialPeriodsCompleted" />
                        </xsl:when>
                        <xsl:otherwise>0</xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>

                <xsl:if test="$committed = 0 or ($committed = 1 and $initialPeriods &gt; 0)">

                    <tr>
                        <td class="leftCol">
                            Historical Retentions:
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td colspan="2">

                            <xsl:if test="$priceTiersConfigured = 0 and $committed = 0 and $openRuns = 0">
                                <span class="error">Price tiers must be configured for the associated product in order to enter historical retentions.</span>
                            </xsl:if>

                            <xsl:variable name="showPeriod2">
                                <xsl:choose>
                                    <xsl:when test="$committed = 0 or ($committed = 1 and $initialPeriods &gt; 1)">1</xsl:when>
                                    <xsl:otherwise>0</xsl:otherwise>
                                </xsl:choose>
                            </xsl:variable>

                            <xsl:variable name="showPeriod3">
                                <xsl:choose>
                                    <xsl:when test="$committed = 0 or ($committed = 1 and $initialPeriods &gt; 2)">1</xsl:when>
                                    <xsl:otherwise>0</xsl:otherwise>
                                </xsl:choose>
                            </xsl:variable>

                            <xsl:variable name="showPeriod4">
                                <xsl:choose>
                                    <xsl:when test="$committed = 0 or ($committed = 1 and $initialPeriods &gt; 3)">1</xsl:when>
                                    <xsl:otherwise>0</xsl:otherwise>
                                </xsl:choose>
                            </xsl:variable>

                            <table id="historicalRetentions">
                                <tr>
                                    <th class="leftColumn">Period</th>
                                    <th>1</th>
                                    <xsl:if test="$showPeriod2 = 1">
                                        <th>2</th>
                                    </xsl:if>
                                    <xsl:if test="$showPeriod3 = 1">
                                        <th>3</th>
                                    </xsl:if>
                                    <xsl:if test="$showPeriod4 = 1">
                                        <th>4</th>
                                    </xsl:if>
                                </tr>

                                <tr>
                                    <td class="leftColumn">#&#160;Held</td>
                                    <td>
                                        <xsl:if test="Form/McpsLicense/McpsLicenseRetentionList/McpsLicenseRetention[PeriodID = 1]/UnitsHeld[@e]">
                                            <xsl:attribute name="class">errorInput</xsl:attribute>
                                        </xsl:if>
                                        <input type="text" name="{$fieldPrefix}0_UnitsHeld" id="{$fieldPrefix}0_UnitsHeld" value="{Form/McpsLicense/McpsLicenseRetentionList/McpsLicenseRetention[PeriodID = 1]/UnitsHeld}">
                                            <xsl:if test="$committed = 1 or $openRuns = 1 or $priceTiersConfigured = 0">
        				                        <xsl:attribute name="disabled">disabled</xsl:attribute>
        				                    </xsl:if>
                                        </input>
                                        <input type="hidden" name="{$fieldPrefix}0_McpsLicenseID" value="{Form/McpsLicense/McpsLicenseID}" />
                                        <input type="hidden" name="{$fieldPrefix}0_PeriodID" value="1" />
                                        <input type="hidden" name="{$fieldPrefix}0_Delete" id="{$fieldPrefix}0_Delete" />
                                    </td>
                                    <xsl:if test="$showPeriod2 = 1">
                                        <td>
                                            <xsl:if test="Form/McpsLicense/McpsLicenseRetentionList/McpsLicenseRetention[PeriodID = 2]/UnitsHeld[@e]">
                                                <xsl:attribute name="class">errorInput</xsl:attribute>
                                            </xsl:if>
                                            <input type="text" name="{$fieldPrefix}1_UnitsHeld" id="{$fieldPrefix}1_UnitsHeld" value="{Form/McpsLicense/McpsLicenseRetentionList/McpsLicenseRetention[PeriodID = 2]/UnitsHeld}">
                                                <xsl:if test="$committed = 1 or $openRuns = 1 or $priceTiersConfigured = 0">
            				                        <xsl:attribute name="disabled">disabled</xsl:attribute>
            				                    </xsl:if>
                                            </input>
                                            <input type="hidden" name="{$fieldPrefix}1_McpsLicenseID" value="{Form/McpsLicense/McpsLicenseID}" />
                                            <input type="hidden" name="{$fieldPrefix}1_PeriodID" value="2" />
                                            <input type="hidden" name="{$fieldPrefix}1_Delete" id="{$fieldPrefix}1_Delete" />
                                        </td>
                                    </xsl:if>
                                    <xsl:if test="$showPeriod3 = 1">
                                        <td>
                                            <xsl:if test="Form/McpsLicense/McpsLicenseRetentionList/McpsLicenseRetention[PeriodID = 3]/UnitsHeld[@e]">
                                                <xsl:attribute name="class">errorInput</xsl:attribute>
                                            </xsl:if>
                                            <input type="text" name="{$fieldPrefix}2_UnitsHeld" id="{$fieldPrefix}2_UnitsHeld" value="{Form/McpsLicense/McpsLicenseRetentionList/McpsLicenseRetention[PeriodID = 3]/UnitsHeld}">
                                                <xsl:if test="$committed = 1 or $openRuns = 1 or $priceTiersConfigured = 0">
            				                        <xsl:attribute name="disabled">disabled</xsl:attribute>
            				                    </xsl:if>
                                            </input>
                                            <input type="hidden" name="{$fieldPrefix}2_McpsLicenseID" value="{Form/McpsLicense/McpsLicenseID}" />
                                            <input type="hidden" name="{$fieldPrefix}2_PeriodID" value="3" />               <input type="hidden" name="{$fieldPrefix}2_Delete" id="{$fieldPrefix}2_Delete" />
                                        </td>
                                    </xsl:if>
                                    <xsl:if test="$showPeriod4 = 1">
                                        <td>
                                            <xsl:if test="Form/McpsLicense/McpsLicenseRetentionList/McpsLicenseRetention[PeriodID = 4]/UnitsHeld[@e]">
                                                <xsl:attribute name="class">errorInput</xsl:attribute>
                                            </xsl:if>
                                            <input type="text" name="{$fieldPrefix}3_UnitsHeld" id="{$fieldPrefix}3_UnitsHeld" value="{Form/McpsLicense/McpsLicenseRetentionList/McpsLicenseRetention[PeriodID = 4]/UnitsHeld}">
                                                <xsl:if test="$committed = 1 or $openRuns = 1 or $priceTiersConfigured = 0">
            				                        <xsl:attribute name="disabled">disabled</xsl:attribute>
            				                    </xsl:if>
                                            </input>
                                            <input type="hidden" name="{$fieldPrefix}3_McpsLicenseID" value="{Form/McpsLicense/McpsLicenseID}" />
                                            <input type="hidden" name="{$fieldPrefix}3_PeriodID" value="4" />
                                            <input type="hidden" name="{$fieldPrefix}3_Delete" id="{$fieldPrefix}3_Delete" />
                                        </td>
                                    </xsl:if>
                                </tr>
                                <tr>
                                    <td class="leftColumn">Price&#160;Tier</td>
                                    <td>
                                        <xsl:call-template name="priceTierSelect">
                                            <xsl:with-param name="period" select="'1'" />
                                        </xsl:call-template>
                                    </td>
                                    <xsl:if test="$showPeriod2 = 1">
                                        <td>
                                            <xsl:call-template name="priceTierSelect">
                                                <xsl:with-param name="period" select="'2'" />
                                            </xsl:call-template>
                                        </td>
                                    </xsl:if>
                                    <xsl:if test="$showPeriod3 = 1">
                                        <td>
                                            <xsl:call-template name="priceTierSelect">
                                                <xsl:with-param name="period" select="'3'" />
                                            </xsl:call-template>
                                        </td>
                                    </xsl:if>
                                    <xsl:if test="$showPeriod4 = 1">
                                        <td>
                                            <xsl:call-template name="priceTierSelect">
                                                <xsl:with-param name="period" select="'4'" />
                                            </xsl:call-template>
                                        </td>
                                    </xsl:if>
                                </tr>
                                <xsl:if test="$committed = 0 and $openRuns = 0 and $priceTiersConfigured != 0">
                                    <script type="text/javascript">
                                        <xsl:variable name="periodsCompleted">
                                            <xsl:choose>
                                                <xsl:when test="normalize-space(Form/McpsLicense/PeriodsCompleted)">
                                                    <xsl:value-of select="Form/McpsLicense/PeriodsCompleted" />
                                                </xsl:when>
                                                <xsl:otherwise>0</xsl:otherwise>
                                            </xsl:choose>
                                        </xsl:variable>
                                        historicalRetentions(<xsl:value-of select="$periodsCompleted" />);
                                    </script>
                                </xsl:if>
                            </table>
                        </td>
                    </tr>

                </xsl:if>
            </table>
        </div>
    </xsl:template>


    <xsl:template name="priceTierSelect">
        <xsl:param name="period" />

        <select name="{$fieldPrefix}{$period - 1}_PriceLevelID" id="{$fieldPrefix}{$period - 1}_PriceLevelID">
            <xsl:if test="$committed = 1 or $openRuns = 1 or $priceTiersConfigured = 0">
                <xsl:attribute name="disabled">disabled</xsl:attribute>
            </xsl:if>
            <xsl:for-each select="Form/PriceLevelList/PriceLevel[Type = 1]">
                <xsl:variable name="priceLevelID" select="PriceLevelID" />
                <xsl:variable name="priceID" select="/Root/Form/Product/PriceList/Price[PriceLevelID = $priceLevelID]/PriceID" />
                <xsl:if test="normalize-space($priceID)">
                    <option value="{PriceLevelID}">
                        <xsl:if test="PriceLevelID = /Root/Form/McpsLicense/McpsLicenseRetentionList/McpsLicenseRetention[PeriodID = $period]/PriceLevelID">
                            <xsl:attribute name="selected">selected</xsl:attribute>
                        </xsl:if>

                        <xsl:variable name="priceTierPrice">
                            <xsl:choose>
                                <xsl:when test="$committed = 0">
                                    <xsl:value-of select="/Root/Form/PriceList/Price[PriceID = $priceID]/Wholesale" />
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="/Root/Form/McpsLicense/McpsLicenseRetentionList/McpsLicenseRetention[PeriodID = $period]/Price" />
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:variable>

                        <xsl:value-of select="Name" /> / <xsl:value-of select="$priceTierPrice" />
                    </option>
                </xsl:if>
            </xsl:for-each>
        </select>

    </xsl:template>


    <xsl:template name="mcpsLicenseErrors">
        <div class="error">
            <xsl:if test="//UnitsHeld[@e]"># Held is invalid.<br /></xsl:if>
            <br />
        </div>
    </xsl:template>


</xsl:stylesheet>
