<!--                                                                  -->
<!--  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="isCreatePage" select="$cmd = 'edit' and not(normalize-space(/Root/Params/LabelContractID))" />

    <xsl:variable name="countryCodeOptionsItems">
        <map>
            <mapEntry>
                <name>Choose Country...</name>
                <value></value>
            </mapEntry>
            <mapEntry>
                <name>All</name>
                <value>ALL</value>
            </mapEntry>
            <xsl:for-each select="/Root/Form/CountryList/Country">
                <mapEntry>
                    <name>
                        <xsl:value-of select="Name"/>
                    </name>
                    <value>
                        <xsl:value-of select="CountryCode"/>
                    </value>
                </mapEntry>
            </xsl:for-each>
        </map>
    </xsl:variable>

    <xsl:variable name="serviceNameOptionsItems">
        <map>
            <mapEntry>
                <name>Choose Service...</name>
                <value></value>
            </mapEntry>
            <mapEntry>
                <name>All</name>
                <value>ALL</value>
            </mapEntry>
            <xsl:for-each select="/Root/Form/ServiceList/Service">
                <mapEntry>
                    <name>
                        <xsl:value-of select="ServiceName"/>
                    </name>
                    <value>
                        <xsl:value-of select="ServiceID"/>
                    </value>
                </mapEntry>
            </xsl:for-each>
        </map>
    </xsl:variable>

    <xsl:template name="contractEditLayout">
        <script type="text/javascript" src="/production/javascript/validate/labelContract.js?v=12"/>

        <form name="contractEdit" id="contractEdit" method="post" action="label_contract">
            <xsl:call-template name="contractEdit"/>
            <xsl:call-template name="buttons"/>
        </form>

        <xsl:call-template name="templates"/>
    </xsl:template>

    <xsl:template name="contractEdit">
        <div class="Contract-label-block" id="contractForm">
            <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="edit"/>
            <xsl:if test="normalize-space(Form/LabelContract/LabelContractID) and not(Params/copy)">
                <input type="hidden" name="LabelContractID" value="{Form/LabelContract/LabelContractID}"/>
            </xsl:if>
            <xsl:if test="//*[@e]">
                <xsl:call-template name="contractEditTermErrors"/>
            </xsl:if>
            <xsl:call-template name="mainContractEdit"/>
            <xsl:for-each select="Form/LabelContract/GroupTermList/GroupTerm">
                <xsl:sort select="LabelContractGroupTermID" data-type="number"/>
                <xsl:call-template name="termGroupEdit">
                    <xsl:with-param name="term" select="."/>
                </xsl:call-template>
            </xsl:for-each>
            <xsl:if test="not(Form/LabelContract/GroupTermList/GroupTerm)">
                <xsl:call-template name="termGroupEdit"/>
            </xsl:if>
            <xsl:if test="$openRuns = 0">
                <div class="Table-add-row-button">
                    <xsl:call-template name="uiButton">
                        <xsl:with-param name="iconLeftType" select="'add'" />
                        <xsl:with-param name="content" select="'Add Another Set of Terms'" />
                        <xsl:with-param name="type" select="'text-inline'" />
                        <xsl:with-param name="size" select="'large'" />
                        <xsl:with-param name="onClick" select="'LabelContractPage.addGroupTerm(false)'" />
                    </xsl:call-template>
                </div>
            </xsl:if>
        </div>
    </xsl:template>

    <xsl:template name="mainContractEdit">
        <section class="PageSection Contract-label-section">
            <div class="Required-notice-wrapper"><span class="Required-notice"/></div>
            <h2>Contract</h2>
            <div class="Form-header">
                <p>Enter contract information here.</p>
            </div>
            <div class="Form-col-container">
                <div class="Form-col">
                    <fieldset class="Form-fieldset Form-fieldset-standart">
                        <div class="Form-item">
                            <xsl:attribute name="class">
                                <xsl:text>Form-item Form-item-required</xsl:text>
                                <xsl:if test="Form/LabelContract/Title[@e]">
                                    <xsl:text> Form-item-error</xsl:text>
                                </xsl:if>
                            </xsl:attribute>
                            <label class="Form-label">
                                <span class="Form-label-caption">Contract Title</span>
                                <div class="Form-input-wrapper">
                                    <input type="text" class="Input Form-input contractTitleField" name="Form_LabelContract_Title" id="Form_LabelContract_Title" value="{Form/LabelContract/Title}">
                                        <xsl:if test="$openRuns = 1">
                                            <xsl:attribute name="disabled">disabled</xsl:attribute>
                                        </xsl:if>
                                    </input>
                                </div>
                            </label>
                            <xsl:call-template name="inlineError">
                                <xsl:with-param name="path">Form/LabelContract/Title</xsl:with-param>
                                <xsl:with-param name="displayName">Title</xsl:with-param>
                            </xsl:call-template>
                        </div>

                        <div class="Form-item">
                            <xsl:attribute name="class">
                                <xsl:text>Form-item</xsl:text>
                                <xsl:if test="Form/LabelContract/ClientContractID[@e]">
                                    <xsl:text> Form-item-error</xsl:text>
                                </xsl:if>

                            </xsl:attribute>
                            <label class="Form-label">
                                <span class="Form-label-caption">Contract ID</span>
                                <div class="Form-input-wrapper">
                                    <input class="Input Form-input" type="text" name="Form_LabelContract_ClientContractID" id="Form_LabelContract_ClientContractID" value="{Form/LabelContract/ClientContractID}">
                                        <xsl:if test="$openRuns = 1">
                                            <xsl:attribute name="disabled">disabled</xsl:attribute>
                                        </xsl:if>
                                    </input>
                                </div>
                            </label>
                            <xsl:call-template name="inlineError">
                                <xsl:with-param name="path">Form/LabelContract/ClientContractID</xsl:with-param>
                                <xsl:with-param name="displayName">Contract ID</xsl:with-param>
                            </xsl:call-template>
                        </div>

                        <div class="Form-item">
                            <xsl:attribute name="class">
                                <xsl:text>Form-item</xsl:text>
                                <xsl:if test="Form/LabelContract/PayorID[@e]">
                                    <xsl:text> Form-item-error</xsl:text>
                                </xsl:if>

                            </xsl:attribute>
                            <label class="Form-label">
                                <span class="Form-label-caption">Payor</span>
                                <div class="Form-input-wrapper">

                                    <xsl:variable name="labelContractItems">
                                        <map>
                                            <xsl:for-each select="Form/PayorList/Payor">
                                                <mapEntry>
                                                    <name>
                                                        <xsl:value-of select="Name"/>
                                                    </name>
                                                    <value>
                                                        <xsl:value-of select="PayorID"/>
                                                    </value>
                                                </mapEntry>
                                            </xsl:for-each>
                                        </map>
                                    </xsl:variable>

                                    <xsl:call-template name="uiDropdown">
                                        <xsl:with-param name="items" select="$labelContractItems"/>
                                        <xsl:with-param name="id" select="'Form_LabelContract_PayorID'"/>
                                        <xsl:with-param name="name" select="'Form_LabelContract_PayorID'"/>
                                        <xsl:with-param name="disabled">
                                            <xsl:if test="$openRuns = 1">
                                                <xsl:text>true</xsl:text>
                                            </xsl:if>
                                        </xsl:with-param>
                                        <xsl:with-param name="active" select="/Root/Form/LabelContract/PayorID"/>
                                    </xsl:call-template>
                                </div>
                            </label>
                            <xsl:call-template name="inlineError">
                                <xsl:with-param name="path">Form/LabelContract/PayorID</xsl:with-param>
                                <xsl:with-param name="displayName">Payor</xsl:with-param>
                            </xsl:call-template>
                        </div>
                    </fieldset>
                </div>
                <div class="Form-col">
                    <fieldset class="Form-fieldset Form-fieldset-standart">
                        <div class="Form-item">
                            <xsl:attribute name="class">
                                <xsl:text>Form-item Form-item-required</xsl:text>
                                <xsl:if test="Form/LabelContract/LabelPayeeID[@e]">
                                    <xsl:text> Form-item-error</xsl:text>
                                </xsl:if>
                            </xsl:attribute>
                            <label class="Form-label">
                                <span class="Form-label-caption">Payee</span>
                                <div class="Form-input-wrapper">
                                    <xsl:call-template name="payeeField"/>
                                </div>
                            </label>
                            <xsl:call-template name="inlineError">
                                <xsl:with-param name="path">Form/LabelContract/LabelPayeeID</xsl:with-param>
                                <xsl:with-param name="displayName">Payee</xsl:with-param>
                            </xsl:call-template>
                        </div>

                        <div class="Form-item">
                            <xsl:attribute name="class">
                                <xsl:text>Form-item</xsl:text>
                                <xsl:if test="Form/LabelContract/IssueDate[@e]">
                                    <xsl:text> Form-item-error</xsl:text>
                                </xsl:if>
                            </xsl:attribute>
                            <label class="Form-label">
                                <span class="Form-label-caption">Issue Date</span>
                                <div class="Form-input-wrapper">
                                    <xsl:call-template name="uiDatepicker">
                                        <xsl:with-param name="id" select="'Form_LabelContract_IssueDate'"/>
                                        <xsl:with-param name="name" select="'Form_LabelContract_IssueDate'"/>
                                        <xsl:with-param name="value" select="Form/LabelContract/IssueDate"/>
                                        <xsl:with-param name="disabled">
                                            <xsl:if test="$openRuns = 1">
                                                <xsl:text>true</xsl:text>
                                            </xsl:if>
                                        </xsl:with-param>
                                        <xsl:with-param name="className" select="'dateField'"/>
                                    </xsl:call-template>
                                </div>
                            </label>
                            <xsl:call-template name="inlineError">
                                <xsl:with-param name="path">Form/LabelContract/IssueDate</xsl:with-param>
                                <xsl:with-param name="displayName">Issue Date</xsl:with-param>
                            </xsl:call-template>
                        </div>
                    </fieldset>
                </div>
            </div>
            <xsl:if test="$cmd = 'edit_contract_ajax'">
                <fieldset class="Form-fieldset Form-fieldset-buttons">
                    <xsl:call-template name="uiButton">
                        <xsl:with-param name="content" select="'Cancel'"/>
                        <xsl:with-param name="className" select="'Form-button'"/>
                        <xsl:with-param name="onClick" select="'LabelContractPage.showContract(this)'" />
                    </xsl:call-template>

                    <xsl:call-template name="uiButton">
                        <xsl:with-param name="type" select="'primary'"/>
                        <xsl:with-param name="className" select="'Form-button'"/>
                        <xsl:with-param name="elemType" select="'submit'"/>
                        <xsl:with-param name="name" select="'submit'"/>
                        <xsl:with-param name="id" select="'submitButton'"/>
                        <xsl:with-param name="content" select="'Save'"/>
                        <xsl:with-param name="onClick" select="'LabelContractPage.submitContract(this)'" />
                    </xsl:call-template>
                </fieldset>
            </xsl:if>
        </section>
    </xsl:template>

    <xsl:template name="termGroupEdit">
        <xsl:param name="term" select="Form/GroupTerm"/>

        <xsl:variable name="groupID">
            <xsl:choose>
                <xsl:when test="normalize-space($term/LabelContractGroupTermID)">
                    <xsl:value-of select="$term/LabelContractGroupTermID"/>
                </xsl:when>
                <xsl:when test="normalize-space(/Root/Params/GroupTermID)">
                    <xsl:value-of select="/Root/Params/GroupTermID"/>
                </xsl:when>
                <xsl:otherwise>NEW0</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <section class="Contract-terms-wrapper" data-id="{$groupID}">
            <xsl:attribute name="data-index">
                <xsl:choose>
                    <xsl:when test="$groupID = 'NEW0'">0</xsl:when>
                    <xsl:otherwise>position() - 1</xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
            <xsl:choose>
                <xsl:when test="$cmd = 'edit_term_ajax'">
                    <form name="term{$groupID}" id="term{$groupID}" class="Contract-terms-form">
                        <input type="hidden" name="c" value="edit_term_ajax"/>
                        <input type="hidden" name="formSubmit" value="1"/>
                        <xsl:choose>
                            <xsl:when test="normalize-space($term/LabelContractGroupTermID)">
                                <input type="hidden" name="LabelContractGroupTermID" id="LabelContractGroupTermID" value="{$term/LabelContractGroupTermID}"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <input type="hidden" name="GroupTermID" id="GroupTermID" value="{$groupID}"/>
                            </xsl:otherwise>
                        </xsl:choose>
                        <input type="hidden" name="LabelContractID" id="LabelContractID" value="{$term/LabelContractID}"/>

                        <xsl:call-template name="termGroupEditFieldset">
                            <xsl:with-param name="term" select="$term"/>
                            <xsl:with-param name="groupID" select="$groupID"/>
                        </xsl:call-template>
                    </form>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:call-template name="termGroupEditFieldset">
                        <xsl:with-param name="term" select="$term"/>
                        <xsl:with-param name="groupID" select="$groupID"/>
                    </xsl:call-template>
                </xsl:otherwise>
            </xsl:choose>
        </section>
    </xsl:template>

    <xsl:template name="termGroupEditFieldset">
        <xsl:param name="term"/>
        <xsl:param name="groupID"/>

        <xsl:variable name="groupPrefix">
            <xsl:choose>
                <xsl:when test="$cmd = 'edit_term_ajax'">Form_GroupTerm_</xsl:when>
                <xsl:otherwise>Form_LabelContract_GroupTermList_GroupTerm_<xsl:value-of select="$groupID"/>_</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <section class="PageSection Contract-terms-section">
            <h2>Contract Terms</h2>

            <xsl:if test="$cmd = 'edit_term_ajax' and //*[@e]">
                <xsl:call-template name="contractEditTermErrors"/>
            </xsl:if>

            <fieldset class="Form-fieldset">
                <div class="Form-header">
                    <p>Choose your labels here</p>
                </div>

                <table class="Table Table-with-highlight Contract-label-table" id="labelTable{$groupID}">
                    <tbody>
                        <xsl:for-each select="$term/GroupTermLabelList/GroupTermLabel[normalize-space(LabelID)]">
                            <xsl:sort select="LabelName"/>
                            <xsl:call-template name="labelRowEdit">
                                <xsl:with-param name="groupPrefix" select="$groupPrefix"/>
                                <xsl:with-param name="groupID" select="$groupID"/>
                            </xsl:call-template>
                        </xsl:for-each>
                    </tbody>
                </table>
                <xsl:call-template name="labelAdd" />
            </fieldset>

            <xsl:variable name="hasExceptions">
                <xsl:choose>
                    <xsl:when test="$term/GroupTermExceptionList/GroupTermException/*[.!= 0 and .!= 'All' and .!= '']">1</xsl:when>
                    <xsl:otherwise>0</xsl:otherwise>
                </xsl:choose>
            </xsl:variable>

            <fieldset class="Form-fieldset">
                <div class="Form-header">
                    <p>Configure your terms here</p>
                </div>

                <table class="Table Table-with-highlight Contract-label-term-table">
                    <thead>
                        <tr class="Table-row Table-row-header">
                            <th class="Table-col">Service</th>
                            <th class="Table-col">Country</th>
                            <th class="Table-col">Distribution Fee</th>
                            <th class="Table-col Contract-label-term-add-col"/>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="Table-row Table-row-data Table-row-inputs">
                            <td class="Table-col">All</td>
                            <td class="Table-col">All</td>
                            <td class="Table-col">
                                <div class="Form-item">
                                    <xsl:attribute name="class">
                                        <xsl:text>Form-item</xsl:text>
                                        <xsl:if test="$term/DistributionFee[@e]">
                                            <xsl:text> Form-item-error</xsl:text>
                                        </xsl:if>
                                    </xsl:attribute>
                                    <label class="Form-label">
                                        <div class="Form-input-wrapper">
                                            <input type="text" class="Input Form-input Distribution-fee" name="{$groupPrefix}DistributionFee" id="{$groupPrefix}DistributionFee" value="{$term/DistributionFee}">
                                                <xsl:if test="$openRuns = 1">
                                                    <xsl:attribute name="disabled">disabled</xsl:attribute>
                                                </xsl:if>
                                            </input>
                                        </div>
                                    </label>
                                </div>
                            </td>
                            <td class="Table-col Contract-label-term-add-col">
                                <xsl:if test="$hasExceptions = 0 and $openRuns = 0">
                                    <a href="javascript:void(0)" onClick="LabelContractPage.showExceptions(this)" id="addExceptions{$groupID}">Add Term Exceptions</a>
                                </xsl:if>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </fieldset>

            <div class="Add-exception-block">
                <xsl:if test="$hasExceptions = 0">
                    <xsl:attribute name="class">Add-exception-block hidden</xsl:attribute>
                </xsl:if>
                <div>Except</div>

                <table class="Table Table-with-highlight Contract-label-exception-table" id="exceptionTable{$groupID}">
                    <tbody>
                        <xsl:for-each select="$term/GroupTermExceptionList/GroupTermException[normalize-space(ServiceID) or normalize-space(CountryCode) or normalize-space(DistributionFee)]">
                            <xsl:call-template name="exceptionRowEdit">
                                <xsl:with-param name="groupPrefix" select="$groupPrefix"/>
                                <xsl:with-param name="groupID" select="$groupID"/>
                            </xsl:call-template>
                        </xsl:for-each>
                        <xsl:call-template name="exceptionRowAdd" />
                    </tbody>
                </table>
            </div>

            <xsl:if test="$cmd = 'edit_term_ajax'">
                <fieldset class="Form-fieldset Form-fieldset-buttons Form-fieldset-buttons-stick Contract-terms-buttons">
                    <xsl:call-template name="uiButton">
                        <xsl:with-param name="content" select="'Cancel'" />
                        <xsl:with-param name="className" select="'Form-button'" />
                        <xsl:with-param name="onClick">
                            <xsl:choose>
                                <xsl:when test="$term/InDB = 1">LabelContractPage.showTerm(this)</xsl:when>
                                <xsl:otherwise>LabelContractPage.removeGroupTerm(this)</xsl:otherwise>
                            </xsl:choose>
                        </xsl:with-param>
                    </xsl:call-template>

                    <xsl:call-template name="uiButton">
                        <xsl:with-param name="type" select="'primary'"/>
                        <xsl:with-param name="content" select="'Save'" />
                        <xsl:with-param name="className" select="'Form-button'" />
                        <xsl:with-param name="onClick" select="'LabelContractPage.submitTerm(this)'" />
                    </xsl:call-template>
                </fieldset>
            </xsl:if>
        </section>
    </xsl:template>

    <xsl:template name="termGroupEditInnerTemplate">
        <xsl:variable name="prefix">
            <xsl:choose>
                <xsl:when test="$isCreatePage">Form_LabelContract_GroupTermList_GroupTerm_{0}_</xsl:when>
                <xsl:otherwise>Form_GroupTerm_</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <h2>Contract Terms</h2>

        <fieldset class="Form-fieldset">
            <div class="Form-header">
                <p>Choose your labels here</p>
            </div>

            <table class="Table Table-with-highlight Contract-label-table" id="labelTable{{0}}">
                <tbody>
                </tbody>
            </table>
            <xsl:call-template name="labelAdd" />
        </fieldset>

        <fieldset class="Form-fieldset">
            <div class="Form-header">
                <p>Configure your terms here</p>
            </div>

            <table class="Table Table-with-highlight Contract-label-term-table">
                <thead>
                    <tr class="Table-row Table-row-header">
                        <th class="Table-col">Service</th>
                        <th class="Table-col">Country</th>
                        <th class="Table-col">Distribution Fee</th>
                        <th class="Table-col Contract-label-term-add-col"/>
                    </tr>
                </thead>
                <tbody>
                    <tr class="Table-row Table-row-data Table-row-inputs">
                        <td class="Table-col">All</td>
                        <td class="Table-col">All</td>
                        <td class="Table-col">
                            <div class="Form-item">
                                <label class="Form-label">
                                    <div class="Form-input-wrapper">
                                        <input type="text" class="Input Form-input Distribution-fee" name="{$prefix}DistributionFee" id="{$prefix}DistributionFee" />
                                    </div>
                                </label>
                            </div>
                        </td>
                        <td class="Table-col Contract-label-term-add-col">
                            <a href="javascript:void(0)" onClick="LabelContractPage.showExceptions(this)" id="addExceptions{{0}}">Add Term Exceptions</a>
                        </td>
                    </tr>
                </tbody>
            </table>
        </fieldset>

        <div class="Add-exception-block hidden">
            <div>Except</div>

            <table class="Table Table-with-highlight Contract-label-exception-table" id="exceptionTable{{0}}">
                <tbody>
                    <xsl:call-template name="exceptionRowAdd" />
                </tbody>
            </table>
        </div>

        <fieldset class="Form-fieldset Form-fieldset-buttons Form-fieldset-buttons-stick Contract-terms-buttons">
            <xsl:call-template name="uiButton">
                <xsl:with-param name="content" select="'Cancel'" />
                <xsl:with-param name="className" select="'Form-button'" />
                <xsl:with-param name="onClick" select="'LabelContractPage.removeGroupTerm(this)'" />
            </xsl:call-template>

            <xsl:call-template name="uiButton">
                <xsl:with-param name="type" select="'primary'"/>
                <xsl:with-param name="content" select="'Save'" />
                <xsl:with-param name="className" select="'Form-button'" />
                <xsl:with-param name="onClick" select="'LabelContractPage.submitTerm(this)'" />
            </xsl:call-template>
        </fieldset>
    </xsl:template>

    <xsl:template name="termGroupEditTemplate">
        <div id="termGroupEditTemplate">
            <section class="Contract-terms-wrapper">
                <section class="PageSection Contract-terms-section">
                    <xsl:choose>
                        <xsl:when test="$isCreatePage">
                            <xsl:call-template name="termGroupEditInnerTemplate"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <form class="Contract-terms-form">
                                <input type="hidden" name="c" value="edit_term_ajax"/>
                                <input type="hidden" name="formSubmit" value="1"/>
                                <input type="hidden" name="LabelContractID" id="LabelContractID" value="{Form/LabelContract/LabelContractID}" />
                                <input type="hidden" name="GroupTermID" id="GroupTermID" value="{{0}}" />

                                <xsl:call-template name="termGroupEditInnerTemplate"/>
                            </form>
                        </xsl:otherwise>
                    </xsl:choose>
                </section>
            </section>
        </div>
    </xsl:template>

    <xsl:template name="labelRowEdit">
        <xsl:param name="groupPrefix"/>
        <xsl:param name="groupID"/>

        <xsl:variable name="labelTermID">
            <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="labelRowID">groupTerm_<xsl:value-of select="$groupID"/>_label_<xsl:value-of select="$labelTermID"/></xsl:variable>
        <xsl:variable name="labelPrefix"><xsl:value-of select="$groupPrefix"/>GroupTermLabelList_GroupTermLabel_<xsl:value-of select="$labelTermID"/>_</xsl:variable>

        <tr class="Table-row Table-row-data Label-row" id="{$labelRowID}">
            <xsl:if test="Delete = 1">
                <xsl:attribute name="class">Table-row Table-row-data Label-row Table-row-deleted</xsl:attribute>
            </xsl:if>
            <td class="Table-col">
                <xsl:variable name="labelID" select="LabelID"/>

                <div class="Form-item">
                    <xsl:if test="LabelID[@e]">
                        <xsl:attribute name="class">Form-item Form-item-error</xsl:attribute>
                    </xsl:if>
                    <label class="Form-label">
                        <div class="Form-input-wrapper">
                            <span id="{$labelRowID}_LabelName" class="Label-name">
                                <xsl:value-of select="/Root/Form/LabelList/Label[LabelID = $labelID]/LabelName"/>
                            </span>
                        </div>
                    </label>
                </div>

                <input type="hidden" name="{$labelPrefix}LabelID" id="{$labelRowID}_LabelID" value="{LabelID}"/>
            </td>
            <td class="Table-col Table-col-all-actions">
                <xsl:if test="$openRuns = 0">
                    <div title="Delete" class="js-tooltip Table-action-item DeleteRow-trigger DeleteRow-trigger-custom" onClick="LabelContractPage.deleteLabel(this)">
                        <div class="DeleteRow-label">
                            <span class="DeleteRow-icon DeleteRow-icon-delete">
                                <xsl:call-template name="icon">
                                    <xsl:with-param name="type" select="'delete'" />
                                    <xsl:with-param name="role" select="'action'" />
                                </xsl:call-template>
                            </span>
                            <span class="DeleteRow-icon DeleteRow-icon-undo">
                                <xsl:call-template name="icon">
                                    <xsl:with-param name="type" select="'undo'" />
                                    <xsl:with-param name="role" select="'action'" />
                                </xsl:call-template>
                            </span>
                        </div>
                    </div>

                    <input type="hidden" class="Label-delete-flag" name="{$labelPrefix}Delete" id="{$labelRowID}_Delete" value="{Delete}"/>
                </xsl:if>
            </td>
        </tr>
    </xsl:template>

    <xsl:template name="labelRowTemplate">
        <xsl:variable name="prefix">
            <xsl:choose>
                <xsl:when test="$isCreatePage">Form_LabelContract_GroupTermList_GroupTerm_{termId}_GroupTermLabelList_GroupTermLabel_</xsl:when>
                <xsl:otherwise>Form_GroupTerm_GroupTermLabelList_GroupTermLabel_</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <table class="Table" id="labelRowTemplate">
            <tbody>
                <tr class="Table-row Table-row-data Label-row">
                    <td class="Table-col">
                        <span class="Label-name" />

                        <input type="hidden" name="{$prefix}{{itemId}}_LabelID" id="{$prefix}{{itemId}}_LabelID" class="Label-id" />
                    </td>
                    <td class="Table-col Table-col-all-actions">
                        <xsl:if test="$openRuns = 0">

                            <div title="Delete" class="Table-action-item js-tooltip DeleteRow-trigger DeleteRow-trigger-custom" onClick="LabelContractPage.deleteLabel(this)">
                                <div class="DeleteRow-label">
                                    <span class="DeleteRow-icon DeleteRow-icon-delete">
                                        <xsl:call-template name="icon">
                                            <xsl:with-param name="type" select="'delete'" />
                                            <xsl:with-param name="role" select="'action'" />
                                        </xsl:call-template>
                                    </span>
                                    <span class="DeleteRow-icon DeleteRow-icon-undo">
                                        <xsl:call-template name="icon">
                                            <xsl:with-param name="type" select="'undo'" />
                                            <xsl:with-param name="role" select="'action'" />
                                        </xsl:call-template>
                                    </span>
                                </div>
                            </div>

                            <input type="hidden" class="Label-delete-flag" name="{$prefix}{{itemId}}_Delete" id="{$prefix}{{itemId}}_Delete" value="0" />
                        </xsl:if>
                    </td>
                </tr>
            </tbody>
        </table>
    </xsl:template>

    <xsl:template name="labelAdd">
        <xsl:variable name="contractLabelOptionsItems">
            <map>
                <mapEntry>
                    <name>Choose Label...</name>
                    <value></value>
                </mapEntry>
                <xsl:for-each select="/Root/Form/LabelList/Label">
                    <mapEntry>
                        <name>
                            <xsl:value-of select="LabelName"/>
                        </name>
                        <value>
                            <xsl:value-of select="LabelID"/>
                        </value>
                    </mapEntry>
                </xsl:for-each>
            </map>
        </xsl:variable>

        <div>
            <xsl:attribute name="class">
                <xsl:text>Form-item Form-item-standart Table-add-row-button Label-add-select-wrapper</xsl:text>
                <xsl:if test="//GroupTermLabelList[@e]">
                    <xsl:text> Form-item-error</xsl:text>
                </xsl:if>
            </xsl:attribute>
            <label class="Form-label">
                <div class="Form-input-wrapper">
                    <xsl:call-template name="uiDropdown">
                        <xsl:with-param name="id" select="'label-add-select'" />
                        <xsl:with-param name="name" select="'label-add-select'" />
                        <xsl:with-param name="items" select="$contractLabelOptionsItems"/>
                        <xsl:with-param name="disabled">
                            <xsl:if test="$openRuns = 1">
                                <xsl:text>true</xsl:text>
                            </xsl:if>
                        </xsl:with-param>
                        <xsl:with-param name="className" select="'Contract-label-select'"/>
                        <xsl:with-param name="selectClassName" select="'labelOptions'"/>
                    </xsl:call-template>
                </div>
            </label>
            <xsl:call-template name="inlineError">
                <xsl:with-param name="path">//GroupTermLabelList</xsl:with-param>
                <xsl:with-param name="displayName">Label</xsl:with-param>
            </xsl:call-template>

        </div>

        <div class="Label-add-button-wrapper">
            <xsl:call-template name="uiButton">
                <xsl:with-param name="content" select="'Add Label'" />
                <xsl:with-param name="className" select="'Form-button'" />
                <xsl:with-param name="onClick" select="'LabelContractPage.addLabel(this)'" />
            </xsl:call-template>
        </div>

    </xsl:template>

    <xsl:template name="exceptionRowEdit">
        <xsl:param name="groupPrefix"/>
        <xsl:param name="groupID"/>
        <xsl:variable name="exceptionID">
            <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="exceptionRowID">groupTerm_<xsl:value-of select="$groupID"/>_exception_<xsl:value-of select="$exceptionID"/></xsl:variable>
        <xsl:variable name="exceptionPrefix"><xsl:value-of select="$groupPrefix"/>GroupTermExceptionList_GroupTermException_<xsl:value-of select="$exceptionID"/>_</xsl:variable>

        <tr class="Table-row Table-row-data Table-row-inputs Exception-row" id="{$exceptionRowID}">
            <xsl:if test="Delete = 1">
                <xsl:attribute name="class">Table-row Table-row-data Table-row-inputs Table-row-deleted</xsl:attribute>
            </xsl:if>

            <td class="Table-col Contract-label-table-col-name" id="{$exceptionRowID}ServiceCell">
                <xsl:choose>
                    <xsl:when test="ServiceID[@e]">
                        <span id="{$exceptionRowID}_ServiceName" class="Service-name Contract-label-plain-name" />

                        <div class="Form-item">
                            <xsl:attribute name="class">
                                <xsl:text>Form-item</xsl:text>
                                <xsl:if test="ServiceID[@e]">
                                    <xsl:text> Form-item-error</xsl:text>
                                </xsl:if>
                            </xsl:attribute>
                            <label class="Form-label">
                                <div class="Form-input-wrapper">
                                    <xsl:call-template name="uiDropdown">
                                        <xsl:with-param name="id">
                                            <xsl:value-of select="$exceptionRowID"/>
                                            <xsl:text>_ServiceSelect</xsl:text>
                                        </xsl:with-param>
                                        <xsl:with-param name="name">
                                            <xsl:value-of select="$exceptionRowID"/>
                                            <xsl:text>_ServiceSelect</xsl:text>
                                        </xsl:with-param>
                                        <xsl:with-param name="items" select="$serviceNameOptionsItems"/>
                                        <xsl:with-param name="active" select="ServiceID" />
                                        <xsl:with-param name="onChange" select="'LabelContractPage.disableSelect(this)'" />
                                        <xsl:with-param name="disabled">
                                            <xsl:if test="$openRuns = 1">
                                                <xsl:text>true</xsl:text>
                                            </xsl:if>
                                        </xsl:with-param>
                                        <xsl:with-param name="className" select="'Service-name-select'" />
                                    </xsl:call-template>
                                </div>
                            </label>
                            <xsl:call-template name="inlineError">
                                <xsl:with-param name="path">ServiceID</xsl:with-param>
                                <xsl:with-param name="displayName">Service</xsl:with-param>
                            </xsl:call-template>
                        </div>
                    </xsl:when>
                    <xsl:otherwise>
                        <span id="{$exceptionRowID}_ServiceName" class="Service-name">
                            <xsl:variable name="serviceID" select="ServiceID" />
                            <xsl:choose>
                                <xsl:when test="$serviceID = 0 or $serviceID = 'ALL'">All</xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="/Root/Form/ServiceList/Service[ServiceID = $serviceID]/ServiceName"/>
                                </xsl:otherwise>
                            </xsl:choose>
                        </span>
                    </xsl:otherwise>
                </xsl:choose>

                <input type="hidden" name="{$exceptionPrefix}ServiceID" id="{$exceptionRowID}_ServiceID" class="Plain-id">
                    <xsl:attribute name="value">
                        <xsl:choose>
                            <xsl:when test="ServiceID = 0">ALL</xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="ServiceID"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                </input>
            </td>

            <td class="Table-col Contract-label-table-col-name" id="{$exceptionRowID}CountryCell">
                <xsl:choose>
                    <xsl:when test="CountryCode[@e]">
                        <span id="{$exceptionRowID}_CountryName" class="Country-name Contract-label-plain-name" />

                        <div class="Form-item">
                            <xsl:attribute name="class">
                                <xsl:text>Form-item</xsl:text>
                                <xsl:if test="CountryCode[@e]">
                                    <xsl:text> Form-item-error</xsl:text>
                                </xsl:if>
                            </xsl:attribute>
                            <label class="Form-label">
                                <div class="Form-input-wrapper">
                                    <xsl:call-template name="uiDropdown">
                                        <xsl:with-param name="id">
                                            <xsl:value-of select="$exceptionRowID"/>
                                            <xsl:text>_CountrySelect</xsl:text>
                                        </xsl:with-param>
                                        <xsl:with-param name="name">
                                            <xsl:value-of select="$exceptionRowID"/>
                                            <xsl:text>_CountrySelect</xsl:text>
                                        </xsl:with-param>
                                        <xsl:with-param name="items" select="$countryCodeOptionsItems"/>
                                        <xsl:with-param name="active" select="CountryCode" />
                                        <xsl:with-param name="onChange" select="'LabelContractPage.disableSelect(this)'" />
                                        <xsl:with-param name="disabled">
                                            <xsl:if test="$openRuns = 1">
                                                <xsl:text>true</xsl:text>
                                            </xsl:if>
                                        </xsl:with-param>
                                        <xsl:with-param name="className" select="'Country-code-select'" />
                                    </xsl:call-template>
                                </div>
                            </label>
                            <xsl:call-template name="inlineError">
                                <xsl:with-param name="path">CountryCode</xsl:with-param>
                                <xsl:with-param name="displayName">Country</xsl:with-param>
                            </xsl:call-template>
                        </div>
                    </xsl:when>
                    <xsl:otherwise>
                        <span id="{$exceptionRowID}_CountryName" class="Country-name">
                            <xsl:variable name="countryCode" select="CountryCode"/>
                            <xsl:choose>
                                <xsl:when test="$countryCode = 0 or $countryCode = 'ALL'">All</xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="/Root/Form/CountryList/Country[CountryCode = $countryCode]/Name"/>
                                </xsl:otherwise>
                            </xsl:choose>
                        </span>
                    </xsl:otherwise>
                </xsl:choose>

                <input type="hidden" name="{$exceptionPrefix}CountryCode" id="{$exceptionRowID}_CountryCode" class="Plain-id">
                    <xsl:attribute name="value">
                        <xsl:choose>
                            <xsl:when test="CountryCode = 0">ALL</xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="CountryCode"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                </input>
            </td>

            <td class="Table-col">
                <div>
                    <xsl:attribute name="class">
                        <xsl:text>Form-item</xsl:text>
                        <xsl:if test="DistributionFee[@e]">
                            <xsl:text> Form-item-error</xsl:text>
                        </xsl:if>
                    </xsl:attribute>
                    <label class="Form-label">
                        <div class="Form-input-wrapper">
                            <input type="text" class="Input Form-input Distribution-fee" name="{$exceptionPrefix}DistributionFee" id="{$exceptionRowID}_DistributionFee" value="{DistributionFee}">
                                <xsl:if test="$openRuns = 1">
                                    <xsl:attribute name="disabled">disabled</xsl:attribute>
                                </xsl:if>
                            </input>
                        </div>
                    </label>
                    <xsl:call-template name="inlineError">
                        <xsl:with-param name="path">DistributionFee</xsl:with-param>
                        <xsl:with-param name="displayName">Distribution Fee</xsl:with-param>
                    </xsl:call-template>
                </div>
            </td>

            <td class="Table-col Table-col-all-actions">
                <xsl:if test="$openRuns = 0">
                    <div class="Delete-link js-tooltip Table-action-item" title="Delete" id="{$exceptionRowID}_DeleteLink">
                        <a href="javascript:void(0)">
                            <xsl:attribute name="onClick">LabelContractPage.deleteException(this)</xsl:attribute>

                            <xsl:call-template name="icon">
                                <xsl:with-param name="type" select="'delete'" />
                                <xsl:with-param name="role" select="'action'" />
                            </xsl:call-template>
                        </a>
                    </div>

                    <div class="Undo-delete-link js-tooltip Table-action-item" title="Undo Delete" id="{$exceptionRowID}_UndoDeleteLink">
                        <a href="javascript:void(0)">
                            <xsl:attribute name="onClick">LabelContractPage.deleteException(this)</xsl:attribute>

                            <xsl:call-template name="icon">
                                <xsl:with-param name="type" select="'undo'" />
                                <xsl:with-param name="role" select="'action'" />
                            </xsl:call-template>
                        </a>
                    </div>

                    <input type="hidden" class="Exception-delete-flag" name="{$exceptionPrefix}Delete" id="{$exceptionRowID}_Delete" value="{Delete}"/>
                </xsl:if>
            </td>
        </tr>
    </xsl:template>

    <xsl:template name="exceptionRowTemplate">
        <xsl:variable name="prefix">
            <xsl:choose>
                <xsl:when test="$isCreatePage">Form_LabelContract_GroupTermList_GroupTerm_{termId}_GroupTermExceptionList_GroupTermException_</xsl:when>
                <xsl:otherwise>Form_GroupTerm_GroupTermExceptionList_GroupTermException_</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <table class="Table" id="exceptionRowTemplate">
            <tbody>
                <tr class="Table-row Table-row-data Table-row-inputs Exception-row">
                    <td class="Table-col" id="{{itemId}}ServiceCell">
                        <span class="Service-name" />
                        <input type="hidden" class="Service-id" name="{$prefix}{{itemId}}_ServiceID" id="{$prefix}{{itemId}}_ServiceID" />
                    </td>
                    <td class="Table-col" id="{{itemId}}CountryCell">
                        <span class="Country-name" />

                        <input type="hidden" class="Country-code" name="{$prefix}{{itemId}}_CountryCode" id="{$prefix}{{itemId}}_CountryCode" />
                    </td>
                    <td class="Table-col">
                        <input type="text" class="Input Form-input Distribution-fee" name="{$prefix}{{itemId}}_DistributionFee" id="{$prefix}{{itemId}}_DistributionFee" />
                    </td>
                    <td class="Table-col Table-col-all-actions">
                        <div class="Delete-link js-tooltip Table-action-item" title="Delete">
                            <a href="javascript:void(0)" onClick="LabelContractPage.deleteException(this)">
                                <xsl:call-template name="icon">
                                    <xsl:with-param name="type" select="'delete'" />
                                    <xsl:with-param name="role" select="'action'" />
                                </xsl:call-template>
                            </a>
                        </div>

                        <div class="Undo-delete-link js-tooltip Table-action-item" title="Undo Delete">
                            <a href="javascript:void(0)" onClick="LabelContractPage.deleteException(this)">
                                <xsl:call-template name="icon">
                                    <xsl:with-param name="type" select="'undo'" />
                                    <xsl:with-param name="role" select="'action'" />
                                </xsl:call-template>
                            </a>
                        </div>

                        <input type="hidden" class="Exception-delete-flag" name="{$prefix}{{itemId}}_Delete" id="{$prefix}{{itemId}}_Delete" value="0" />
                    </td>
                </tr>
            </tbody>
        </table>
    </xsl:template>

    <xsl:template name="exceptionRowAdd">
        <tr class="Table-row Table-row-data Table-row-inputs Exception-row Exceptions-add-row">
            <td class="Table-col">
                <xsl:call-template name="uiDropdown">
                    <xsl:with-param name="id" select="'exception-add-name'" />
                    <xsl:with-param name="name" select="'exception-add-name'" />
                    <xsl:with-param name="items" select="$serviceNameOptionsItems"/>
                    <xsl:with-param name="onChange" select="'LabelContractPage.addException(this)'" />
                    <xsl:with-param name="disabled">
                        <xsl:if test="$openRuns = 1">
                            <xsl:text>true</xsl:text>
                        </xsl:if>
                    </xsl:with-param>
                    <xsl:with-param name="className" select="'Service-name-select'" />
                </xsl:call-template>
            </td>
            <td class="Table-col">
                <xsl:call-template name="uiDropdown">
                    <xsl:with-param name="id" select="'exception-add-code'" />
                    <xsl:with-param name="name" select="'exception-add-code'" />
                    <xsl:with-param name="items" select="$countryCodeOptionsItems"/>
                    <xsl:with-param name="onChange" select="'LabelContractPage.addException(this)'" />
                    <xsl:with-param name="disabled">
                        <xsl:if test="$openRuns = 1">
                            <xsl:text>true</xsl:text>
                        </xsl:if>
                    </xsl:with-param>
                    <xsl:with-param name="className" select="'Country-code-select'" />
                </xsl:call-template>
            </td>
            <td class="Table-col">
                <input type="text" class="Input Form-input Distribution-fee">
                    <xsl:if test="$openRuns = 1">
                        <xsl:attribute name="disabled">disabled</xsl:attribute>
                    </xsl:if>
                </input>
            </td>
            <td class="Table-col Table-col-all-actions" />
        </tr>
    </xsl:template>

    <xsl:template name="payeeField">
        <div class="Form-input-wrapper Form-input-wrapper-compound">
            <input type="hidden" name="Form_LabelContract_LabelPayeeID" id="Form_LabelContract_LabelPayeeID" value="{Form/LabelContract/LabelPayeeID}"/>

            <div id="payeeNameWrapper" class="Input Form-input Input-readonly lookupText">
                <xsl:choose>
                    <xsl:when test="normalize-space(Form/LabelContract/LabelPayeeID) and substring(Form/LabelContract/LabelPayeeName,26,1)">
                        <abbr title="{Form/LabelContract/LabelPayeeName}"><xsl:value-of select="substring(Form/LabelContract/LabelPayeeName,1,25)"/>...</abbr>
                    </xsl:when>
                    <xsl:when test="normalize-space(Form/LabelContract/LabelPayeeID)">
                        <xsl:value-of select="Form/LabelContract/LabelPayeeName"/>
                    </xsl:when>
                    <xsl:otherwise>
                        (none)
                    </xsl:otherwise>
                </xsl:choose>
            </div>
            <div class="Form-input-conpound-part">
                <xsl:call-template name="uiButton">
                    <xsl:with-param name="type" select="'text-inline'"/>
                    <xsl:with-param name="content" select="'Payee Lookup'"/>
                    <xsl:with-param name="iconLeftType" select="'add'"/>
                    <xsl:with-param name="id" select="'payeeLookup'"/>
                    <xsl:with-param name="onClick">
                        <xsl:text>LabelContractPage.labelPayeeLookup()</xsl:text>
                    </xsl:with-param>
                    <xsl:with-param name="disabled">
                        <xsl:if test="$openRuns &gt; 0">
                            <xsl:text>true</xsl:text>
                        </xsl:if>
                    </xsl:with-param>
                </xsl:call-template>
            </div>
        </div>
    </xsl:template>

    <xsl:template name="contractEditTermErrors">
        <xsl:call-template name="uiWarning">
            <xsl:with-param name="type" select="'error'" />
            <xsl:with-param name="description">
                <xsl:if test="//LabelID/@e = 'field_blank'"><div>Label must be selected.</div></xsl:if>
                <xsl:if test="//LabelID/@e = 'field_invalid'"><div>Label is already attached to the payee.</div></xsl:if>
                <xsl:if test="//LabelID/@e = 'field_duplicate'"><div>Label can only be used once per contract.</div></xsl:if>
                <xsl:if test="//DistributionFee/@e = 'field_blank'"><div>Distribution Fee cannot be blank.</div></xsl:if>
                <xsl:if test="//DistributionFee/@e = 'field_invalid' or //DistributionFee/@e = 'field_out_of_range'"><div>Distribution Fee must be between 0-100%.</div></xsl:if>
                <xsl:if test="//Delete[@e]"><div>Terms with pending reserves cannot be deleted.</div></xsl:if>
                <xsl:if test="//GroupTermException/ServiceID[@e = 'field_blank']"><div>Service must be selected.</div></xsl:if>
                <xsl:if test="//GroupTermException/CountryCode[@e = 'field_blank']"><div>Country must be selected.</div></xsl:if>
                <xsl:if test="//GroupTermException/ServiceID[@e = 'field_duplicate'] or //GroupTermException/CountryCode[@e = 'field_duplicate']"><div>Cannot have multiple terms with the same Service and Country.</div></xsl:if>
                <xsl:if test="//GroupTermLabelList[@e = 'field_blank']"><div>At least one label must be associated with a set of terms.</div></xsl:if>
            </xsl:with-param>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="templates">
        <div class="hidden">
            <xsl:call-template name="labelRowTemplate"/>
            <xsl:call-template name="exceptionRowTemplate"/>
            <xsl:call-template name="termGroupEditTemplate"/>
        </div>
    </xsl:template>
</xsl:stylesheet>
