<!--                                                                  -->
<!--  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="ajaxFlag">
        <xsl:choose>
            <xsl:when test="$cmd = 'edit_term_ajax'">1</xsl:when>
            <xsl:otherwise>0</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>


    <xsl:template name="contractEditLayout">
        <script type="text/javascript" src="/production/javascript/validate/labelContract.js?v=11" />
        <link href="/production/css/rps/label_contract.css?v=1" media="screen" rel="stylesheet" type="text/css" />
        <form name="contractEdit" id="contractEdit" method="post" action="label_contract">
            <xsl:call-template name="hiddenFields" />
            <xsl:call-template name="contractEdit" />
            <xsl:call-template name="buttons" />
        </form>
	</xsl:template>


  <xsl:template name="contractEdit">
    <div class="section" 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="errorMessageGeneral" />
        <xsl:call-template name="contractEditTermErrors" />
      </xsl:if>
      <xsl:call-template name="mainContractEdit" />
      <br />
      <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>
        <br />
      </xsl:for-each>
      <xsl:if test="not(Form/LabelContract/GroupTermList/GroupTerm)">
        <xsl:call-template name="termGroupEdit" />
        <br />
      </xsl:if>
      <xsl:if test="$openRuns = 0">
          <div id="addGroupTerm">
            <a href="javascript:void(-1)" onClick="addGroupTerm()">Add Another Set of Terms...</a>
          </div>
      </xsl:if>
      <br class="superBreak" />
      <br />
    </div>
  </xsl:template>


  <xsl:template name="mainContractEdit">
    <fieldset>
      <legend>Contract</legend>
      <p class="formHeader">Enter contract information here.</p>
      <hr />
        <table class="textChart">
          <tr>
            <td class="titleCol"><label for="Form_LabelContract_Title">*Contract Title:</label></td>
            <td class="idCol"><label for="Form_LabelContract_ClientContractID">Contract ID:</label></td>
            <td class="payorCol"><label for="Form_LabelContract_PayorID">Payor:</label></td>
            <td class="payeeCol"><label for="PayeeName">*Payee:</label></td>
            <td class="dateCol"><label for="Form_LabelContract_IssueDate">Issue Date:</label></td>
          </tr>
          <tr class="row1">
            <td>
              <input type="text" class="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>
		      <br />
              <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>		      
            </td>       
            <td>
              <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>
		      <br />
              <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>		      
            </td>        
            <td>
              <select name="Form_LabelContract_PayorID" id="Form_LabelContract_PayorID">
                <xsl:if test="$openRuns = 1">
                  <xsl:attribute name="disabled">disabled</xsl:attribute>
                </xsl:if>
                <xsl:for-each select="Form/PayorList/Payor">
                  <option value="{PayorID}">
                    <xsl:if test="PayorID = /Root/Form/LabelContract/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>
              <br />
              <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>              
            </td>      
            <td>
              <xsl:call-template name="payeeField" />
              <br />
              <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>              
            </td>       
            <td>
              <span id="Form_LabelContract_IssueDate_TD">
                <input type="text" class="dateField" name="Form_LabelContract_IssueDate" id="Form_LabelContract_IssueDate" value="{Form/LabelContract/IssueDate}" >
                  <xsl:if test="$openRuns = 1">
                    <xsl:attribute name="disabled">disabled</xsl:attribute>
                  </xsl:if>
		        </input>
              </span>
              <a title="Select Issue Date">
                <img src="/production/images/icons/office/Calendar-(16x16).gif" class="calendarIcon" id="calendarIconIssueDate"/>
              </a>
			  <xsl:if test="$openRuns = 0">
				<xsl:call-template name="calendar">
					<xsl:with-param name="fieldID">Form_LabelContract_IssueDate</xsl:with-param>
					<xsl:with-param name="buttonID">calendarIconIssueDate</xsl:with-param>
				</xsl:call-template>
			  </xsl:if>
			  <br />
              <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>			  
            </td>                              
          </tr>
          <tr>
            <td colspan="5">
              <br />*Required field
            </td>
          </tr>
        </table>
        <xsl:if test="$cmd = 'edit_contract_ajax'">
            <table class="buttons">
                <tr>
                    <td class="defaultButton">
                        <input name="submit" type="submit" id="submitButton" value="Save" onClick="submitContractAjax({Params/LabelContractID},'mainContractSection'); return false;" />
                    </td>
                    <td>
                        <input type="button" value="Cancel" onClick="showContractAjax({Params/LabelContractID},'mainContractSection'); return false;" />
                    </td>
                </tr>
           </table>        
        </xsl:if>
    </fieldset>
  </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>  

        <xsl:choose>
            <xsl:when test="$cmd = 'edit_term_ajax'">
                <form name="term{$groupID}" id="term{$groupID}">   
                    <input type="hidden" name="c" value="edit_term_ajax" />  
                    <input type="hidden" name="formSubmit" value="1" />   
                    <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>
        
    </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>        
        
        <fieldset class="termEdit">
            <legend>Contract Terms</legend>
            
            <xsl:if test="$cmd = 'edit_term_ajax'">
                <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="contractEditTermErrors" />
            </xsl:if>
            
            <p class="formHeader">Choose your labels here.</p>
            <hr />

	        <table class="textChart labelTable" 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>
                    <xsl:call-template name="labelRowAdd">
                        <xsl:with-param name="groupPrefix" select="$groupPrefix" />
                        <xsl:with-param name="groupID" select="$groupID" />
                        <xsl:with-param name="term" select="$term" />
                    </xsl:call-template>                    
	            </tbody>
	        </table>
	        <br />
	        
	        <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>
            <p class="formHeader">Configure your terms here.</p>
            <hr />
	        <table class="textChart termTable">
	            <tbody>	    
	                <tr>
	                    <th>Service:</th>
	                    <th>Country:</th>
	                    <th>Distribution Fee:</th>
	                    <th></th>
	                </tr>
	                <tr class="row1">
	                    <td>All</td>
	                    <td>All</td>
	                    <td>
                            <xsl:if test="$term/DistributionFee[@e]">
                                <xsl:attribute name="class">errorInput</xsl:attribute>
                            </xsl:if>
                            <input type="text" class="smallRateField" name="{$groupPrefix}DistributionFee" id="{$groupPrefix}DistributionFee" value="{$term/DistributionFee}" >
                                <xsl:if test="$openRuns = 1">
                                    <xsl:attribute name="disabled">disabled</xsl:attribute>
                                </xsl:if>
            		        </input>	                    
	                    </td>
	                    <td style="text-align: right; padding-right: 10px">
	                        <xsl:if test="$hasExceptions = 0 and $openRuns = 0">
	                            <a href="javascript:void(-1)" onClick="showExceptions('{$groupID}')" id="addExceptions{$groupID}">Add Term Exceptions...</a>
	                        </xsl:if>
	                    </td>
	                </tr>	 
	            </tbody>
	        </table>               
	         
	        <div id="exceptions{$groupID}">
	            <xsl:if test="$hasExceptions = 0">
	                <xsl:attribute name="class">hidden</xsl:attribute>
	            </xsl:if>
    	        <div class="exceptionsHeader">Except...</div>
    	        <hr />
    	        
    	        <table class="textChart exceptionTable" 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">
                            <xsl:with-param name="groupPrefix" select="$groupPrefix" />
                            <xsl:with-param name="groupID" select="$groupID" />
                            <xsl:with-param name="term" select="$term" />
                        </xsl:call-template>                    
    	            </tbody>
    	        </table>
    	        <br />	        
	        </div>

            <xsl:if test="$cmd = 'edit_term_ajax'">
                <table class="buttons">
                    <tr>
                        <td class="defaultButton">
                            <input name="button" type="button" id="submitButton" value="Save" onClick="submitTermAjax('term{$groupID}','groupTerm{$groupID}Section'); return false;" />
                        </td>
                        <td>                          
                            <input type="button" name="button" value="Cancel">                      
                                <xsl:attribute name="onClick">
                                    <xsl:choose>
                                        <xsl:when test="$term/InDB = 1">showTermAjax('<xsl:value-of select="$groupID" />','groupTerm<xsl:value-of select="$groupID" />Section'); return false;</xsl:when>
                                        <xsl:otherwise>hideGroupTerm('groupTerm<xsl:value-of select="$groupID" />Section'); return false;</xsl:otherwise>
                                    </xsl:choose>
                                </xsl:attribute>
                            </input>                            
                            
                        </td>
                    </tr>
               </table>        
            </xsl:if>
	        
            <br />    
        </fieldset>
    </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="row{position() mod 2}" id="{$labelRowID}">
            <td>
                <xsl:if test="LabelID[@e]">
                    <xsl:attribute name="class">errorInput</xsl:attribute>
                </xsl:if>            
                <span id="{$labelRowID}_LabelName">
                    <xsl:variable name="labelID" select="LabelID" />
                    <xsl:value-of select="/Root/Form/LabelList/Label[LabelID = $labelID]/LabelName" />
                </span>
                <input type="hidden" name="{$labelPrefix}LabelID" id="{$labelRowID}_LabelID" value="{LabelID}" />
            </td>
            <td class="deleteCol">
                <xsl:if test="LabelID[@e]">
                    <xsl:attribute name="class">deleteCol errorInput</xsl:attribute>
                </xsl:if>               
                <xsl:if test="$openRuns = 0">
                    <a href="javascript:void(-1)" id="{$labelRowID}_DeleteLink" onClick="deleteLabel('{$labelTermID}','{$groupID}')" title="Delete">
                        <img src="/production/images/icons/office/Close (16x16).png" alt="Delete" />
                    </a>
                    <a href="javascript:void(-1)" id="{$labelRowID}_UndoDeleteLink" onClick="undoDeleteLabel('{$labelTermID}','{$groupID}')" title="Undo Delete">
                        <xsl:if test="Delete != 1">
                            <xsl:attribute name="class">hidden</xsl:attribute>
                        </xsl:if>
                        <img src="/production/images/icons/office/Undo-(16x16).png" alt="Undo Delete" />
                    </a>                      
                    <input type="hidden" name="{$labelPrefix}Delete" id="{$labelRowID}_Delete" value="{Delete}" /> 
                </xsl:if>           
            </td>
        </tr>        
        <xsl:if test="Delete = 1">
            <script type="text/javascript">
                deleteLabel('<xsl:value-of select="$labelTermID" />', '<xsl:value-of select="$groupID" />')
            </script>
        </xsl:if>
    </xsl:template>


    <xsl:template name="labelRowAdd">
        <xsl:param name="groupPrefix" />
        <xsl:param name="groupID" />    
        <xsl:param name="term" />
        
        <xsl:variable name="labelTermID">NEW<xsl:value-of select="count($term/GroupTermLabelList/GroupTermLabel)" /></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="row{(count($term/GroupTermLabelList/GroupTermLabel[normalize-space(LabelID)]) + 1) mod 2}" id="{$labelRowID}">
            <td>
				<xsl:if test="LabelID[@e] or $term/GroupTermLabelList[@e]">
                    <xsl:attribute name="class">errorInput</xsl:attribute>
                </xsl:if>
                
                <span id="{$labelRowID}_LabelName"></span>

                <select name="{$labelPrefix}LabelID" id="{$labelRowID}_LabelID" class="labelOptions" onChange="addLabel('{$labelTermID}', '{$groupID}', '{$ajaxFlag}')">
                    <xsl:if test="$openRuns = 1">
                        <xsl:attribute name="disabled">disabled</xsl:attribute>
                    </xsl:if>
                    <option value="">Choose Label...</option>
                    <xsl:for-each select="/Root/Form/LabelList/Label">
                        <option value="{LabelID}">
                            <xsl:value-of select="LabelName" />
                        </option>
                    </xsl:for-each>
                </select>
            </td>
            <td class="deleteCol">
				<xsl:if test="LabelID[@e] or $term/GroupTermLabelList[@e]">
                    <xsl:attribute name="class">deleteCol errorInput</xsl:attribute>
                </xsl:if>            
                <xsl:if test="$openRuns = 0">
                    <a href="javascript:void(-1)" id="{$labelRowID}_DeleteLink" class="hidden" onClick="deleteLabel('{$labelTermID}','{$groupID}')" title="Delete">
                        <img src="/production/images/icons/office/Close (16x16).png" alt="Delete" />
                    </a>                  
                    <a href="javascript:void(-1)" id="{$labelRowID}_UndoDeleteLink" onClick="undoDeleteLabel('{$labelTermID}','{$groupID}')" title="Undo Delete" class="hidden">
                        <img src="/production/images/icons/office/Undo-(16x16).png" alt="Undo Delete" />
                    </a>                        
                    <input type="hidden" name="{$labelPrefix}Delete" id="{$labelRowID}_Delete" value="0" />
                </xsl:if>
            </td>
        </tr>     
    </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="row{position() mod 2}" id="{$exceptionRowID}">
        
            <td id="{$exceptionRowID}ServiceCell">    
                <xsl:choose>
                    <xsl:when test="ServiceID[@e]">
                        <xsl:attribute name="class">errorInput</xsl:attribute>
                        <span id="{$exceptionRowID}_ServiceName"></span>
                        <select name="{$exceptionRowID}_ServiceSelect" id="{$exceptionRowID}_ServiceSelect" class="serviceOptions" onChange="makeSelectReadOnly('{$exceptionRowID}_ServiceSelect','{$exceptionRowID}_ServiceID','{$exceptionRowID}_ServiceName','{$exceptionRowID}ServiceCell')">
                            <xsl:if test="$openRuns = 1">
                                <xsl:attribute name="disabled">disabled</xsl:attribute>
                            </xsl:if>
                            <option value="">Choose Service...</option>
                            <option value="">-----------------</option>
                            <option value="ALL">
                                <xsl:if test="ServiceID = 'ALL'">
                                    <xsl:attribute name="selected">selected</xsl:attribute>
                                </xsl:if>
                                All
                            </option>
                            <option value="">-----------------</option>
                            <xsl:variable name="serviceID" select="ServiceID" />
                            <xsl:for-each select="/Root/Form/ServiceList/Service">
                                <option value="{ServiceID}">
                                    <xsl:if test="ServiceID = $serviceID">
                                        <xsl:attribute name="selected">selected</xsl:attribute>
                                    </xsl:if>                                
                                    <xsl:value-of select="ServiceName" />
                                </option>
                            </xsl:for-each>
                        </select>                    
                    </xsl:when>          
                    <xsl:otherwise>              
                        <span id="{$exceptionRowID}_ServiceName">
                            <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">
                    <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 id="{$exceptionRowID}CountryCell">
                <xsl:choose>
                    <xsl:when test="CountryCode[@e]">
                        <xsl:attribute name="class">errorInput</xsl:attribute>
                        <span id="{$exceptionRowID}_CountryName"></span>
                        <select name="{$exceptionRowID}_CountrySelect" id="{$exceptionRowID}_CountrySelect" class="countryOptions" onChange="makeSelectReadOnly('{$exceptionRowID}_CountrySelect','{$exceptionRowID}_CountryCode','{$exceptionRowID}_CountryName','{$exceptionRowID}CountryCell')">
                            <xsl:if test="$openRuns = 1">
                                <xsl:attribute name="disabled">disabled</xsl:attribute>
                            </xsl:if>
                            <option value="">Choose Country...</option>
                            <option value="">-----------------</option>
                            <option value="ALL">
                                <xsl:if test="CountryCode = 'ALL'">
                                    <xsl:attribute name="selected">selected</xsl:attribute>
                                </xsl:if>
                                All
                            </option>
                            <option value="">-----------------</option>
                            <xsl:variable name="countryCode" select="CountryCode" />
                            <xsl:for-each select="/Root/Form/CountryList/Country">
                                <option value="{CountryCode}">               
                                    <xsl:if test="CountryCode = $countryCode">
                                        <xsl:attribute name="selected">selected</xsl:attribute>
                                    </xsl:if>                                   
                                    <xsl:value-of select="Name" />
                                </option>
                            </xsl:for-each>
                        </select>                        
                    </xsl:when>
                    <xsl:otherwise>                        
                        <span id="{$exceptionRowID}_CountryName">                           
                            <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">
                    <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>
                <xsl:if test="DistributionFee[@e]">
                    <xsl:attribute name="class">errorInput</xsl:attribute>
                </xsl:if>            
                <input type="text" class="smallRateField" name="{$exceptionPrefix}DistributionFee" id="{$exceptionRowID}_DistributionFee" value="{DistributionFee}">
                    <xsl:if test="$openRuns = 1">
                        <xsl:attribute name="disabled">disabled</xsl:attribute>
                    </xsl:if>
		        </input>	                
            </td>                      
            <td class="deleteCol">
                <xsl:if test="$openRuns = 0">
                    <a href="javascript:void(-1)" id="{$exceptionRowID}_DeleteLink">
                        <xsl:attribute name="onClick">
                            <xsl:if test="ServiceID[@e]">makeSelectReadOnly('<xsl:value-of select="$exceptionRowID" />_ServiceSelect','<xsl:value-of select="$exceptionRowID" />_ServiceID','<xsl:value-of select="$exceptionRowID" />_ServiceName','<xsl:value-of select="$exceptionRowID" />ServiceCell'); </xsl:if>
                            <xsl:if test="CountryCode[@e]">makeSelectReadOnly('<xsl:value-of select="$exceptionRowID" />_CountrySelect','<xsl:value-of select="$exceptionRowID" />_CountryCode','<xsl:value-of select="$exceptionRowID" />_CountryName','<xsl:value-of select="$exceptionRowID" />CountryCell'); </xsl:if>deleteException('<xsl:value-of select="$exceptionID" />','<xsl:value-of select="$groupID" />')</xsl:attribute>
                        <img src="/production/images/icons/office/Close (16x16).png" alt="Delete" />
                    </a>
                    
                    <a href="javascript:void(-1)" id="{$exceptionRowID}_UndoDeleteLink" title="Undo Delete">
                        <xsl:if test="Delete != 1 or not(normalize-space(Delete))">
                            <xsl:attribute name="class">hidden</xsl:attribute>
                        </xsl:if>      
                        <xsl:attribute name="onClick">     
                            <xsl:if test="ServiceID[@e]">makeSelectActive('<xsl:value-of select="$exceptionRowID" />_ServiceSelect','<xsl:value-of select="$exceptionRowID" />_ServiceName'); </xsl:if>   
                            <xsl:if test="CountryCode[@e]">makeSelectActive('<xsl:value-of select="$exceptionRowID" />_CountrySelect','<xsl:value-of select="$exceptionRowID" />_CountryName'); </xsl:if>undoDeleteException('<xsl:value-of select="$exceptionID" />','<xsl:value-of select="$groupID" />'); 
                        </xsl:attribute>       
                        <img src="/production/images/icons/office/Undo-(16x16).png" alt="Undo Delete" />
                    </a>                        
                    <input type="hidden" name="{$exceptionPrefix}Delete" id="{$exceptionRowID}_Delete" value="{Delete}" />     
                </xsl:if>       
            </td>
        </tr>       
        <xsl:if test="Delete = 1">
            <script type="text/javascript">
                deleteException('<xsl:value-of select="$exceptionID" />', '<xsl:value-of select="$groupID" />')
            </script>
        </xsl:if>         
    </xsl:template>


    <xsl:template name="exceptionRowAdd">
        <xsl:param name="groupPrefix" />
        <xsl:param name="groupID" />  
        <xsl:param name="term" />
        
        <xsl:variable name="exceptionID">NEW<xsl:value-of select="count($term/GroupTermExceptionList/GroupTermException) + 1" /></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="row{(count($term/GroupTermExceptionList/GroupTermException[normalize-space(ServiceID) or normalize-space(CountryCode) or normalize-space(DistributionFee)]) + 1) mod 2}" id="{$exceptionRowID}">
            <td>
				<xsl:if test="ServiceID[@e]">
                    <xsl:attribute name="class">errorInput</xsl:attribute>
                </xsl:if>
                
                <span id="{$exceptionRowID}_ServiceName"></span>

                <select name="{$exceptionPrefix}ServiceID" id="{$exceptionRowID}_ServiceID" class="serviceOptions" onChange="addException('{$exceptionID}', '{$groupID},', '{$ajaxFlag},')">
                    <xsl:if test="$openRuns = 1">
                        <xsl:attribute name="disabled">disabled</xsl:attribute>
                    </xsl:if>
                    <option value="">Choose Service...</option>
                    <option value="">-----------------</option>
                    <option value="ALL">All</option>
                    <option value="">-----------------</option>
                    <xsl:for-each select="/Root/Form/ServiceList/Service">
                        <option value="{ServiceID}">
                            <xsl:value-of select="ServiceName" />
                        </option>
                    </xsl:for-each>
                </select>
            </td>
            <td>
				<xsl:if test="CountryCode[@e]">
                    <xsl:attribute name="class">errorInput</xsl:attribute>
                </xsl:if>
                
                <span id="{$exceptionRowID}_CountryName"></span>

                <select name="{$exceptionPrefix}CountryCode" id="{$exceptionRowID}_CountryCode" class="countryOptions" onChange="addException('{$exceptionID}', '{$groupID}', '{$ajaxFlag}')">
                    <xsl:if test="$openRuns = 1">
                        <xsl:attribute name="disabled">disabled</xsl:attribute>
                    </xsl:if>
                    <option value="">Choose Country...</option>
                    <option value="">-----------------</option>
                    <option value="ALL">All</option>
                    <option value="">-----------------</option>
                    <xsl:for-each select="/Root/Form/CountryList/Country">
                        <option value="{CountryCode}">
                            <xsl:value-of select="Name" />
                        </option>
                    </xsl:for-each>
                </select>
            </td>          
            <td>
                <input type="text" class="smallRateField" name="{$exceptionPrefix}DistributionFee" id="{$exceptionRowID}_DistributionFee">
                    <xsl:if test="$openRuns = 1">
                        <xsl:attribute name="disabled">disabled</xsl:attribute>
                    </xsl:if>
		        </input>	                
            </td>                 
            <td class="deleteCol">
                <xsl:if test="$openRuns = 0">
                    <a href="javascript:void(-1)" id="{$exceptionRowID}_DeleteLink" class="hidden" onClick="deleteException('{$exceptionID}','{$groupID}','{$ajaxFlag}')">
                        <img src="/production/images/icons/office/Close (16x16).png" alt="Delete" />
                    </a>
                    <a href="javascript:void(-1)" id="{$exceptionRowID}_UndoDeleteLink" onClick="undoDeleteException('{$exceptionID}','{$groupID}');" title="Undo Delete" class="hidden">     
                        <img src="/production/images/icons/office/Undo-(16x16).png" alt="Undo Delete" />
                    </a>                       
                    <input type="hidden" name="{$exceptionPrefix}Delete" id="{$exceptionRowID}_Delete" value="0" />
                </xsl:if>
            </td>
        </tr>     
    </xsl:template>


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

	<xsl:choose>
      <xsl:when test="$openRuns &gt; 0">
        <img src="/production/images/icons/office/Addressbook-Disabled-(16x16).gif" height="16" width="16" class="lookupIcon" />
      </xsl:when>
      <xsl:otherwise>
        <a href="javascript:labelPayeeLookup()" title="Payee Lookup" id="payeeLookup"><img src="/production/images/icons/office/Addressbook-(16x16).gif" height="16" width="16" class="lookupIcon" /></a>
      </xsl:otherwise>
    </xsl:choose>

  	<div id="payeeNameWrapper" class="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>
  </xsl:template>


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


    <xsl:template name="hiddenFields">
        <input type="hidden" name="groupTermCount" id="groupTermCount">
            <xsl:attribute name="value">
                <xsl:choose>
                    <xsl:when test="Form/LabelContract/GroupTermList/GroupTerm">
                        <xsl:value-of select="count(Form/LabelContract/GroupTermList/GroupTerm)" />
                    </xsl:when>
                    <xsl:otherwise>1</xsl:otherwise>
                 </xsl:choose>
            </xsl:attribute>
        </input>
        <select name="labelList" id="labelList" class="hidden">
            <option value="">Choose Label...</option>
            <xsl:for-each select="/Root/Form/LabelList/Label">
                <option value="{LabelID}">
                    <xsl:value-of select="LabelName" />
                </option>
            </xsl:for-each>
        </select>      
        <select name="servicelList" id="serviceList" class="hidden">
            <option value="">Choose Service...</option>
            <option value="">-----------------</option>
            <option value="ALL">All</option>
            <option value="">-----------------</option>                
            <xsl:for-each select="/Root/Form/ServiceList/Service">
                <option value="{ServiceID}">
                    <xsl:value-of select="ServiceName" />
                </option>
            </xsl:for-each>
        </select>     
        <select name="countryList" id="countryList" class="hidden">
            <option value="">Choose Country...</option>
            <option value="">-----------------</option>
            <option value="ALL">All</option>
            <option value="">-----------------</option>                
            <xsl:for-each select="/Root/Form/CountryList/Country">
                <option value="{CountryCode}">
                    <xsl:value-of select="Name" />
                </option>
            </xsl:for-each>
        </select>           
    </xsl:template>
    

</xsl:stylesheet>
