<!--                                                                  -->
<!--  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:variable name="termCount" select="count(/Root/Form/DistributorContract/TermList/Term)" />

    <xsl:template name="contractEditLayout">
        <script type="text/javascript" src="/production/javascript/validate/distributorContract.js" />
        <form name="contractEdit" id="contractEdit" method="post" action="distributor_contract">
            <xsl:if test="$termCount &gt; 0">
                <input type="hidden" name="showDelete" id="showDelete" value="1" />
            </xsl:if>
            <xsl:call-template name="contractEdit" />
            <xsl:call-template name="buttons" />
        </form>
        <xsl:call-template name="termArraysAndObjects" />
    </xsl:template>

    <xsl:template name="contractEdit">
        <div class="section">
            <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="edit" />
            <xsl:if test="normalize-space(Form/DistributorContract/DistributorContractID)">
                <input type="hidden" name="DistributorContractID" value="{Form/DistributorContract/DistributorContractID}" />
            </xsl:if>
            <xsl:if test="//*[@e]">
                <xsl:call-template name="errorMessageGeneral" />
                <xsl:call-template name="contractEditTermErrors" />
            </xsl:if>
            <xsl:call-template name="mainContractEdit" />
            <br />
            <xsl:call-template name="contractTermEdit" />
            <br class="superBreak" />
            <br />
        </div>
    </xsl:template>

    <xsl:template name="mainContractEdit">
        <fieldset>
            <legend>Contract</legend>
            <p class="formHeader">Enter contract information here.</p>
            <hr />
            <div class="insideFieldset">
                <div class="column1">
                    <table>
                        <tr>
                            <td class="leftCol"><label for="Form_DistributorContract_Title">*Contract Title:</label></td>
                            <td>
                                <xsl:call-template name="inlineError">
                                    <xsl:with-param name="path">Form/DistributorContract/Title</xsl:with-param>
                                    <xsl:with-param name="displayName">Title</xsl:with-param>
                                </xsl:call-template>
                                <input type="text" class="contractTitleField" name="Form_DistributorContract_Title" id="Form_DistributorContract_Title" value="{Form/DistributorContract/Title}" />
                            </td>
                        </tr>
                        <tr>
                            <td class="leftCol"><label for="Form_DistributorContract_ClientContractID">Contract ID:</label></td>
                            <td>
                                <xsl:call-template name="inlineError">
                                    <xsl:with-param name="path">Form/DistributorContract/ClientContractID</xsl:with-param>
                                    <xsl:with-param name="displayName">Contract ID</xsl:with-param>
                                </xsl:call-template>
                                <input type="text" name="Form_DistributorContract_ClientContractID" id="Form_DistributorContract_ClientContractID" value="{Form/DistributorContract/ClientContractID}" />
                            </td>
                        </tr>
                        <tr>
                            <td class="leftCol"><label for="Form_DistributorContract_PayorID">Payor:</label></td>
                            <td>
                                <xsl:call-template name="inlineError">
                                    <xsl:with-param name="path">Form/DistributorContract/PayorID</xsl:with-param>
                                    <xsl:with-param name="displayName">Payor</xsl:with-param>
                                </xsl:call-template>
                                <select name="Form_DistributorContract_PayorID" id="Form_DistributorContract_PayorID">
                                    <xsl:for-each select="Form/PayorList/Payor">
                                        <option value="{PayorID}">
                                            <xsl:if test="PayorID = /Root/Form/DistributorContract/PayorID">
                                                <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 colspan="2">
                                <br />*Required field
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="column2">
                    <table>
                        <tr>
                            <td class="leftCol"><label for="PayeeName">*Payee:</label></td>
                            <td>
                                <xsl:call-template name="inlineError">
                                    <xsl:with-param name="path">Form/DistributorContract/DistributorPayeeID</xsl:with-param>
                                    <xsl:with-param name="displayName">Payee</xsl:with-param>
                                </xsl:call-template>
                                <xsl:call-template name="payeeField" />
                            </td>
                        </tr>
                        <tr>
                            <td class="leftCol"><label for="Form_DistributorContract_IssueDate">Issue Date:</label></td>
                            <td>
                                <xsl:call-template name="inlineError">
                                    <xsl:with-param name="path">Form/DistributorContract/IssueDate</xsl:with-param>
                                    <xsl:with-param name="displayName">Issue Date</xsl:with-param>
                                </xsl:call-template>
                                <span id="Form_DistributorContract_IssueDate_TD">
                                    <input type="text" class="dateField" name="Form_DistributorContract_IssueDate" id="Form_DistributorContract_IssueDate" value="{Form/DistributorContract/IssueDate}" />
                                </span>
                                <a title="Select Issue Date">
                                    <img src="/production/images/icons/office/Calendar-(16x16).gif" class="calendarIcon" id="calendarIconIssueDate"/>
                                </a>
                                <script type="text/javascript">
                                    Calendar.setup({
                                    inputField     :    "Form_DistributorContract_IssueDate",     // id of the input field
                                    ifFormat       :    "%m/%d/%Y",     // format of the input field (even if hidden, this format will be honored)
                                    daFormat       :    "%m/%d/%Y",// format of the displayed date
                                    button         :    "calendarIconIssueDate",  // trigger button (well, IMG in our case)
                                    align          :    "Tl",           // alignment (defaults to "Bl")
                                    singleClick    :    true
                                    });
                                </script>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </fieldset>
    </xsl:template>

    <xsl:template name="contractTermEdit">
        <a name="terms" />
        <fieldset class="termEdit">
            <legend>Contract Terms</legend>
            <p class="formHeader">Configure your contract terms here.</p>
            <hr />
            <table class="textChart" id="termTable">
                <tbody>
                    <tr>
                        <th>Label</th>
                        <th>Distribution Fee</th>
                        <xsl:if test="$termCount &gt; 0">
                            <th class="deleteCol">Delete</th>
                        </xsl:if>
                    </tr>
                    <xsl:choose>
                        <xsl:when test="$termCount &gt; 0">
                            <xsl:for-each select="Form/DistributorContract/TermList/Term">
                                <xsl:call-template name="termRowEdit" />
                            </xsl:for-each>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template name="termRowEdit" />
                        </xsl:otherwise>
                    </xsl:choose>
                </tbody>
            </table>

            <xsl:variable name="addLink">
                <xsl:choose>
                    <xsl:when test="$termCount = 0">javascript:addTerm(1)</xsl:when>
                    <xsl:otherwise>javascript:addTerm('<xsl:value-of select="Form/DistributorContract/TermList/Term/@k" />')</xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <div class="insideFieldset">
                <p class="addLink">
                    <a href="{$addLink}" title="Add Another Term" id="addImageLink">
                        <img src="/production/images/icons/office/Add-(16x16).gif" height="16" width="16" />
                    </a> <a href="{$addLink}" id="addTextLink">Add Another Term</a>
                </p>
            </div>

        </fieldset>
    </xsl:template>

    <xsl:template name="termRowEdit">
        <xsl:variable name="termPosition">
            <xsl:choose>
                <xsl:when test="@k"><xsl:value-of select="@k" /></xsl:when>
                <xsl:otherwise>NEW0</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="termRowID">contractTerm_<xsl:value-of select="$termPosition" /></xsl:variable>
        <xsl:variable name="termPrefix">Form_DistributorContract_TermList_Term_<xsl:value-of select="$termPosition" />_</xsl:variable>
        <tr class="row{position() mod 2}" id="{$termRowID}">
            <td>
                <xsl:if test="LabelID[@e]">
                    <xsl:attribute name="class">errorInput</xsl:attribute>
                </xsl:if>
                <xsl:variable name="labelID" select="LabelID" />
                <select name="{$termPrefix}LabelID" id="{$termPrefix}LabelID">
                    <option value="">Choose...</option>
                    <xsl:for-each select="/Root/Form/LabelList/Label">
                        <option value="{LabelID}">
                            <xsl:if test="LabelID = $labelID">
                                <xsl:attribute name="selected">selected</xsl:attribute>
                            </xsl:if>
                            <xsl:value-of select="LabelName" />
                        </option>
                    </xsl:for-each>
                </select>
            </td>
            <td>
                <xsl:if test="DistributionFee[@e]">
                    <xsl:attribute name="class">errorInput</xsl:attribute>
                </xsl:if>
                <input type="text" class="smallRateField" name="{$termPrefix}DistributionFee" id="{$termPrefix}DistributionFee" value="{DistributionFee}" />
            </td>
            <xsl:if test="$termCount &gt; 0">
                <td>
                    <xsl:attribute name="class">
                        <xsl:choose>
                            <xsl:when test="Delete[@e] and HasDependency = 1">errorInput</xsl:when>
                            <xsl:when test="Delete[@e]">errorInput deleteCheckbox</xsl:when>
                            <xsl:when test="HasDependency = 1"></xsl:when>
                            <xsl:otherwise>deleteCheckbox</xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                    <div class="deleteCheckboxWrapper">
                        <input type="checkbox" name="{$termPrefix}Delete" id="{$termPrefix}Delete" value="1" onBlur="this.setAttribute('checked', this.checked);">
                            <xsl:choose>
                                <xsl:when test="HasDependency = 1">
                                    <xsl:attribute name="disabled">disabled</xsl:attribute>
                                </xsl:when>
                                <xsl:when test="Delete = 1">
                                    <xsl:attribute name="checked">checked</xsl:attribute>
                                </xsl:when>
                            </xsl:choose>
                        </input>
                        <xsl:if test="HasDependency = 1">
                            <xsl:call-template name="helpIcon">
                                <xsl:with-param name="tip">This term has pending reserves associated with it and cannot be deleted.	</xsl:with-param>
                                <xsl:with-param name="id"><xsl:value-of select="$termRowID" />_Tip</xsl:with-param>
                            </xsl:call-template>
                        </xsl:if>
                    </div>
                </td>
            </xsl:if>
        </tr>
    </xsl:template>

    <xsl:template name="payeeField">
        <input type="hidden" name="Form_DistributorContract_DistributorPayeeID" id="Form_DistributorContract_DistributorPayeeID" value="{Form/DistributorContract/DistributorPayeeID}" />
        <a href="javascript:distributorPayeeLookup()" title="Payee Lookup" id="payeeLookup"><img src="/production/images/icons/office/Addressbook-(16x16).gif" height="16" width="16" class="lookupIcon" /></a>
        <div id="payeeNameWrapper" class="lookupText">
            <xsl:choose>
                <xsl:when test="normalize-space(Form/DistributorContract/DistributorPayeeID) and substring(Form/DistributorContract/DistributorPayee/Name,26,1)">
                    <abbr title="{Form/DistributorContract/DistributorPayee/Name}"><xsl:value-of select="substring(Form/DistributorContract/DistributorPayee/Name,1,25)" />...</abbr>
                </xsl:when>
                <xsl:when test="normalize-space(Form/DistributorContract/DistributorPayeeID)">
                    <xsl:value-of select="Form/DistributorContract/DistributorPayee/Name" />
                </xsl:when>
                <xsl:otherwise>
                    (none)
                </xsl:otherwise>
            </xsl:choose>
        </div>
    </xsl:template>

    <xsl:template name="contractEditTermErrors">
        <div class="error">
            <xsl:if test="//Label[@e]">Label must be selected.<br /></xsl:if>
            <xsl:if test="//DistributionFee/@e = 'field_blank'">Distribution Fee cannot be blank.<br /></xsl:if>
            <xsl:if test="//DistributionFee/@e = 'field_invalid'">Distribution Fee is invalid.<br /></xsl:if>
            <xsl:if test="//Delete[@e]">Terms with pending reserves cannot be deleted.<br /></xsl:if>
            <br />
        </div>
    </xsl:template>

    <xsl:template name="termArraysAndObjects">
        <script type="text/javascript">
            // setting up a label hash to populate the select box when adding new terms

            var labels = new Object();
            <xsl:for-each select="/Root/Form/LabelList/Label">
                labels[<xsl:value-of select="LabelID" />] = '<xsl:call-template name="escapeJavascript"><xsl:with-param name="string" select="LabelName" /></xsl:call-template>';
            </xsl:for-each>

        </script>
    </xsl:template>

</xsl:stylesheet>


