<!--                                                                  -->
<!--  Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved     -->
<!--                                                                  -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:dyn="http://exslt.org/dynamic" extension-element-prefixes="dyn">
    <xsl:output method="html" indent="no" doctype-system="" encoding="utf-8" />

    <xsl:include href="/app/tools/bookpub/templates/global.xsl" />

    <xsl:template name="pickLayout">

        <link href="/production/css/bookpub/catalog.css?v=4" rel="stylesheet" type="text/css" />
        <link href="/production/css/bookpub/price.css?v=6" rel="stylesheet" type="text/css" />
        <link rel="stylesheet" href="/production/css/jquery-ui_1.13.0.css" />

        <script type="text/javascript" src="/production/javascript/bookpub/price.js?v=7"></script>
        <script type="text/javascript" src="/production/javascript/jquery/jquery.ie-select-style.min.js"></script>
        <script src="/production/javascript/jquery/jquery-ui.min.1.13.0.js"></script>

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

    </xsl:template>


    <xsl:template name="priceExceptions">

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

        <div class="Table-controls Table-controls-top">
            <div class="Table-controls-item">
                <xsl:call-template name="exceptionFilters" />
            </div>
            <div class="Table-controls-item Table-controls-item-additive">
                <xsl:call-template name="toggleApproved" />
            </div>
        </div>

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

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


    <xsl:template name="exceptionForm">
        <form method="post" action="/bookpub/price">
	        <input type="hidden" name="c" value="{/Root/Params/c}" />
	        <input type="hidden" name="File" value="{/Root/Params/File}" />
	        <input type="hidden" name="Service" value="{/Root/Params/Service}" />
	        <input type="hidden" name="Approved" value="{/Root/Preferences/Approved}" />
	        <input type="hidden" name="Page" value="{/Root/Params/Page}" />
	        <input type="hidden" name="trackerPage" value="{Params/trackerPage}" />

		    <xsl:if test="Exception">
                <xsl:call-template name="exceptionList" />
            </xsl:if>

            <div class="Table-controls Table-controls-bottom">
                <div class="Table-controls-item">

                    <xsl:call-template name="uiButton">
                        <xsl:with-param name="content" select="'Unapprove'" />
                        <xsl:with-param name="elemType" select="'submit'" />
                        <xsl:with-param name="type" select="'danger'" />
                        <xsl:with-param name="className" select="'Form-button'" />
                        <xsl:with-param name="id" select="'unapproveButton'" />
                        <xsl:with-param name="name" select="'submit'" />
                        <xsl:with-param name="disabled" select="'true'" />
                        <xsl:with-param name="onClick">
                            <xsl:text>window.onbeforeunload = null</xsl:text>
                        </xsl:with-param>
                    </xsl:call-template>

                    <xsl:call-template name="uiButton">
                        <xsl:with-param name="content" select="'Approve'" />
                        <xsl:with-param name="elemType" select="'submit'" />
                        <xsl:with-param name="className" select="'Form-button'" />
                        <xsl:with-param name="id" select="'approveButton'" />
                        <xsl:with-param name="name" select="'submit'" />
                        <xsl:with-param name="disabled" select="'true'" />
                        <xsl:with-param name="onClick">
                            <xsl:text>window.onbeforeunload = null</xsl:text>
                        </xsl:with-param>
                    </xsl:call-template>
                </div>
                <div class="Table-controls-item Table-controls-item-pagination">
                    <xsl:call-template name="searchNavRecords" />
                </div>
            </div>

            <fieldset class="Form-fieldset Form-fieldset-buttons">

                <xsl:variable name="fileID">
                    <xsl:choose>
                        <xsl:when test="normalize-space(Params/File)">
                            <xsl:value-of select="Params/File" />
                        </xsl:when>
                        <xsl:otherwise>0</xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>

                <xsl:variable name="fileName">
                    <xsl:choose>
                        <xsl:when test="normalize-space($fileID)">
                            <xsl:call-template name="escapeJavascript">
                                <xsl:with-param name="string" select="OpenFiles/File[FileID = $fileID]/OrigFileName" />
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>0</xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>

                <xsl:variable name="serviceID">
                    <xsl:value-of select="Params/Service" />
                </xsl:variable>

                <xsl:variable name="serviceName">
                    <xsl:call-template name="escapeJavascript">
                        <xsl:with-param name="string" select="ValidatedServices/Service[ServiceID = $serviceID]/ServiceName" />
                    </xsl:call-template>
                </xsl:variable>

                <xsl:call-template name="uiButton">
                    <xsl:with-param name="content" select="'Back to Summary'" />
                    <xsl:with-param name="className" select="'Form-button'" />
                    <xsl:with-param name="id" select="'backButton'" />
                    <xsl:with-param name="url">
                        <xsl:text>/bookpub/tracker?page=</xsl:text>
                        <xsl:value-of select="Params/trackerPage" />
                        <xsl:text>#file</xsl:text>
                        <xsl:value-of select="Params/File" />
                    </xsl:with-param>
                </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="content" select="'Approve All'" />
                    <xsl:with-param name="disabled">
                        <xsl:if test="/Root/Counts/Totals/TotalUnapproved = 0">
                            <xsl:text>true</xsl:text>
                        </xsl:if>
                    </xsl:with-param>
                    <xsl:with-param name="onClick">
                        <xsl:text>approveAll('</xsl:text>
                        <xsl:value-of select="$fileName" />
                        <xsl:text>',</xsl:text>
                        <xsl:value-of select="$fileID" />
                        <xsl:text>,'</xsl:text>
                        <xsl:value-of select="$serviceName" />
                        <xsl:text>',</xsl:text>
                        <xsl:value-of select="$serviceID" />
                        <xsl:text>,'</xsl:text>
                        <xsl:value-of select="Params/Page" />
                        <xsl:text>','</xsl:text>
                        <xsl:value-of select="Params/trackerPage" />
                        <xsl:text>'); return false;</xsl:text>
                    </xsl:with-param>
                </xsl:call-template>
    		</fieldset>
		</form>
    </xsl:template>

    <xsl:template name="successMessage">
        <xsl:if test="ExceptionsApproved &gt; 0 or ExceptionsUnapproved &gt; 0 or Params/ExceptionsApproved">
            <xsl:call-template name="uiWarning">
                <xsl:with-param name="type" select="'success'" />
                <xsl:with-param name="description">
                    <xsl:choose>
                        <xsl:when test="Params/ExceptionsApproved = 'ALL'">
                            All price exceptions successfully approved!
                        </xsl:when>
                        <xsl:when test="ExceptionsApproved &gt; 0">
                            <xsl:value-of select="ExceptionsApproved" /> price exception<xsl:if test="ExceptionsApproved &gt; 1">s</xsl:if> successfully approved!
                        </xsl:when>
                        <xsl:when test="ExceptionsUnapproved &gt; 0">
                            <xsl:value-of select="ExceptionsUnapproved" /> price exception<xsl:if test="ExceptionsUnapproved &gt; 1">s</xsl:if> successfully unapproved!
                        </xsl:when>
                    </xsl:choose>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>


    <xsl:template name="exceptionList">
        <table class="Table" id="exceptionList">
            <xsl:choose>
                <xsl:when test="Preferences/OrderBy = 'variance'">
                    <xsl:call-template name="varianceSortGroup">
                        <xsl:with-param name="exception" select="Exception[@i = 0]" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:when test="Preferences/OrderBy = 'units'">
                    <xsl:call-template name="unitsSortGroup">
                        <xsl:with-param name="exception" select="Exception[@i = 0]" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:when test="normalize-space(Preferences/OrderBy) and Preferences/OrderBy != 'default'">
                    <xsl:call-template name="sortGroup">
                        <xsl:with-param name="exception" select="Exception[@i = 0]" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <thead>
                        <xsl:call-template name="exceptionHeaderRow" />
                    </thead>
                    <tbody>
                        <xsl:for-each select="Exception">
                            <xsl:call-template name="exceptionRow" />
                        </xsl:for-each>
                    </tbody>
                </xsl:otherwise>
            </xsl:choose>
        </table>
    </xsl:template>


    <xsl:template name="exceptionHeaderRow">
        <xsl:param name="checkboxGroup" select="'0'" />
        <tr class="Table-row Table-row-header">
            <th class="Table-col Table-col-select">
                <xsl:call-template name="uiCheckbox">
                    <xsl:with-param name="onClick">
                        <xsl:text>checkAllGroup(this, 'checkboxGroup_</xsl:text>
                        <xsl:value-of select="$checkboxGroup" />
                        <xsl:text>','exceptionList')</xsl:text>
                    </xsl:with-param>
                    <xsl:with-param name="id">
                        <xsl:text>checkAll_</xsl:text>
                        <xsl:value-of select="$checkboxGroup" />
                    </xsl:with-param>
                </xsl:call-template>
            </th>

            <xsl:call-template name="exceptionHeaderCell">
                <xsl:with-param name="orderBy" select="'author'" />
                <xsl:with-param name="displayName" select="'Author'" />
            </xsl:call-template>

            <xsl:call-template name="exceptionHeaderCell">
                <xsl:with-param name="orderBy" select="'title'" />
                <xsl:with-param name="displayName" select="'Title'" />
            </xsl:call-template>

            <xsl:call-template name="exceptionHeaderCell">
                <xsl:with-param name="orderBy" select="'format'" />
                <xsl:with-param name="displayName" select="'Format'" />
            </xsl:call-template>

            <xsl:call-template name="exceptionHeaderCell">
                <xsl:with-param name="orderBy" select="'catalogPrice'" />
                <xsl:with-param name="colspan" select="'2'" />
                <xsl:with-param name="displayName" select="'Effective Price'" />
            </xsl:call-template>

            <xsl:call-template name="exceptionHeaderCell">
                <xsl:with-param name="orderBy" select="'salePrice'" />
                <xsl:with-param name="displayName" select="'Reported Price'" />
            </xsl:call-template>

            <xsl:call-template name="exceptionHeaderCell">
                <xsl:with-param name="orderBy" select="'variance'" />
                <xsl:with-param name="displayName" select="'Variance'" />
            </xsl:call-template>

            <xsl:call-template name="exceptionHeaderCell">
                <xsl:with-param name="orderBy" select="'units'" />
                <xsl:with-param name="class" select="'center'" />
                <xsl:with-param name="displayName" select="'Units'" />
            </xsl:call-template>

            <xsl:call-template name="exceptionHeaderCell">
                <xsl:with-param name="orderBy" select="'country'" />
                <xsl:with-param name="class" select="'center'" />
                <xsl:with-param name="displayName" select="'Sales Country'" />
            </xsl:call-template>

            <xsl:call-template name="exceptionHeaderCell">
                <xsl:with-param name="orderBy" select="'saleDate'" />
                <xsl:with-param name="class" select="'center'" />
                <xsl:with-param name="displayName" select="'Sales Dates'" />
            </xsl:call-template>
            <th class="Table-col"></th>
        </tr>
    </xsl:template>


    <xsl:template name="exceptionRow">
        <xsl:param name="checkboxGroup" select="'0'" />
        <tr class="Table-row Table-row-data">
            <td class="Table-col Table-col-select">
                <xsl:call-template name="uiCheckbox">
                    <xsl:with-param name="onClick">
                        <xsl:text>checkGroupStatus(this, '</xsl:text>
                        <xsl:value-of select="$checkboxGroup" />
                        <xsl:text>','exceptionList')</xsl:text>
                    </xsl:with-param>
                    <xsl:with-param name="name">
                        <xsl:text>Approved_</xsl:text>
                        <xsl:value-of select="SaleID" />
                    </xsl:with-param>
                    <xsl:with-param name="value" select="'1'" />
                    <xsl:with-param name="className">
                        <xsl:text>checkboxGroup_</xsl:text>
                        <xsl:value-of select="$checkboxGroup" />
                        <xsl:text> approved_</xsl:text>
                        <xsl:value-of select="Approved" />
                    </xsl:with-param>

                </xsl:call-template>
            </td>
            <xsl:if test="Modified">
                <xsl:attribute name="class">modified</xsl:attribute>
            </xsl:if>
            <td class="Table-col">
                <a name="{SaleID}" />
                <xsl:value-of select="Author" />
            </td>
            <td class="Table-col"><xsl:value-of select="BookTitle" /></td>
            <td class="Table-col"><xsl:value-of select="Format" /></td>
            <td class="Table-col">
                <xsl:choose>
                    <xsl:when test="PublishingStatus = '07'">
                        <span class="Form-item-error">Out of Print</span>
                    </xsl:when>
                    <xsl:when test="ProductMarketPriceID = ''">--</xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="Price/@currency_code" />
                        <xsl:text> </xsl:text>
                        <xsl:value-of select="Price" />
                    </xsl:otherwise>
                </xsl:choose>
            </td>
            <td class="Table-col">

                <span title="View Price History" class="js-tooltip Table-action-item">
                    <xsl:attribute name="title">
                        View Price History&lt;br&gt;
                        Price Type: <xsl:variable name="priceTypeID" select="PriceTypeID" /><xsl:value-of select="/Root/PriceTypes/PriceType[PriceTypeID = $priceTypeID]/Display" />&lt;br&gt;
                        <xsl:if test="normalize-space(PriceTypeQualifierID)">
                            Price Type Qualifier:
                            <xsl:call-template name="priceTypeQualifierName">
                                <xsl:with-param name="id" select="PriceTypeQualifierID" />
                            </xsl:call-template>
                        </xsl:if>
                    </xsl:attribute>
                    <a href="javascript:void(0);" id="history{SaleID}">
                        <xsl:call-template name="icon">
                            <xsl:with-param name="type" select="'note'" />
                            <xsl:with-param name="role" select="'action'" />
                        </xsl:call-template>
                    </a>

                    <div id="dialog{SaleID}" title="priceHistoryModal">
                        <xsl:call-template name="loader" />
                    </div>
                    <script type="text/javascript">
                        $('#dialog<xsl:value-of select="SaleID" />').dialog({
                            autoOpen : false,
                            minWidth: 600,
                            closeOnEscape: true,
                            modal : false,
                            title: 'Price History',
                            position: {
                                my: 'center top',
                                at: 'center bottom',
                                of: $('#history<xsl:value-of select="SaleID" />')
                            },
                            open: function () {
                                $(this).load('/bookpub/price?c=price_history&amp;SaleID=<xsl:value-of select="SaleID" />')
                            }
                        });
                        $('#history<xsl:value-of select="SaleID" />').on('click', function () {
                            $('#dialog<xsl:value-of select="SaleID" />').dialog('open');
                            return false;
                        });
                    </script>
                </span>
            </td>
            <td class="Table-col">
                <xsl:value-of select="ListPrice/@currency_code" />
                <xsl:text> </xsl:text>
                <xsl:value-of select="ListPrice" />
            </td>
            <td class="Table-col">
                <xsl:choose>
                    <xsl:when test="ProductMarketPriceID = ''">--</xsl:when>
                    <xsl:when test="Variance = 0">N/A</xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="Variance" />%
                    </xsl:otherwise>
                </xsl:choose>
            </td>
            <td class="Table-col"><xsl:value-of select="Units" /></td>
            <td class="Table-col"><xsl:value-of select="Country" /></td>
            <td class="Table-col">
                <xsl:value-of select="DateBegin" />
                <xsl:if test="DateBegin != DateEnd">
                    <xsl:text> - </xsl:text>
                    <br />
                    <xsl:value-of select="DateEnd" />
                </xsl:if>
            </td>
            <td class="Table-col">
				<xsl:if test="Approved &gt; 0">
                    <span class="PriceExeptions-approved-text">Approved</span>
                </xsl:if>
			</td>
        </tr>
    </xsl:template>

    <xsl:template name="exceptionHeaderCell">
        <xsl:param name="orderBy" />
        <xsl:param name="class" />
        <xsl:param name="colspan" select="'1'" />
        <xsl:param name="displayName" />

        <xsl:variable name="sortReverse">
            <xsl:choose>
                <xsl:when test="/Root/Preferences/Order = 'DESC'">1</xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="onClick">
            <xsl:text>location.href='?OrderBy=</xsl:text>
            <xsl:choose>
                <xsl:when test="(/Root/Preferences/Order != 'DESC') or ($orderBy != /Root/Preferences/OrderBy) or (not(/Root/Preferences/OrderBy))">
                    <xsl:value-of select="$orderBy" />
                </xsl:when>
                <xsl:otherwise>default</xsl:otherwise>
            </xsl:choose>
            <xsl:if test="normalize-space(/Root/Params/File)">
                <xsl:text>&amp;File=</xsl:text><xsl:value-of select="/Root/Params/File" />
            </xsl:if>
            <xsl:if test="normalize-space(/Root/Params/Service)">
                <xsl:text>&amp;Service=</xsl:text><xsl:value-of select="/Root/Params/Service" />
            </xsl:if>
            <xsl:choose>
                <xsl:when test="/Root/Preferences/OrderBy = $orderBy and /Root/Preferences/Order = 'ASC'">
                    <xsl:text>&amp;Order=DESC</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>&amp;Order=ASC</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:text>'</xsl:text>
        </xsl:variable>

        <th colspan="{$colspan}" class="Table-col {$class}">
            <xsl:call-template name="uiTableSorting">
                <xsl:with-param name="caption" select="$displayName" />
                <xsl:with-param name="sortBy" select="/Root/Preferences/OrderBy"/>
                <xsl:with-param name="name" select="$orderBy"/>
                <xsl:with-param name="sortReverse" select="$sortReverse"/>
                <xsl:with-param name="onClick" select="$onClick" />
            </xsl:call-template>
        </th>
    </xsl:template>


    <xsl:template name="exceptionFilters" >
        <xsl:variable name="serviceID" select="/Root/Params/Service" />

        <xsl:call-template name="helpLink" />
        <form method="get" action="/bookpub/price" class="Form Form-horizontal">
            <input type="hidden" name="c" value="{/Root/Params/c}" />
            <input type="hidden" name="trackerPage" value="{/Root/Params/trackerPage}" />

            <div class="Form-item">
                <label class="Form-label">
                    <span class="Form-label-caption">Now Viewing</span>
                    <div class="Form-input-wrapper">
                        <xsl:variable name="fileFilterItems">
                            <map>
                                <mapEntry><name>All Files</name><value></value></mapEntry>
                                <xsl:for-each select="OpenFiles/File[ServiceID = $serviceID and PriceExceptions/@raw &gt; 0]">
                                    <mapEntry>
                                        <name><xsl:value-of select="PrettyFileName" /></name>
                                        <value><xsl:value-of select="FileID" /></value>
                                    </mapEntry>
                                </xsl:for-each>
                            </map>
                        </xsl:variable>
                        <xsl:call-template name="uiDropdown">
                            <xsl:with-param name="items" select="$fileFilterItems"/>
                            <xsl:with-param name="id" select="'FileFilter'"/>
                            <xsl:with-param name="name" select="'File'"/>
                            <xsl:with-param name="active" select="/Root/Params/File"/>
                            <xsl:with-param name="className" select="'Tracker-now-view-select'"/>
                            <xsl:with-param name="renderIndependent" select="'true'"/>
                        </xsl:call-template>

                    </div>
                </label>
            </div>

            <div class="Form-item">
                <label class="Form-label">
                    <div class="Form-input-wrapper">
                        <xsl:variable name="serviceFilterItems">
                            <map>
                                <xsl:for-each select="/Root/ValidatedServices/Service">
                                    <mapEntry>
                                        <name><xsl:value-of select="ServiceName" /></name>
                                        <value><xsl:value-of select="ServiceID" /></value>
                                    </mapEntry>
                                </xsl:for-each>
                            </map>
                        </xsl:variable>
                        <xsl:call-template name="uiDropdown">
                            <xsl:with-param name="items" select="$serviceFilterItems"/>
                            <xsl:with-param name="id" select="'Service'"/>
                            <xsl:with-param name="name" select="'Service'"/>
                            <xsl:with-param name="active" select="$serviceID"/>
                            <xsl:with-param name="renderIndependent" select="'true'"/>
                            <xsl:with-param name="className" select="'Tracker-service-filter-select'"/>
                            <xsl:with-param name="onChange">
                                <xsl:text>updateFileList(this)</xsl:text>
                            </xsl:with-param>
                        </xsl:call-template>

                    </div>
                </label>
            </div>

            <div class="Form-item">
                <label class="Form-label">
                    <span class="Form-label-caption">Exceptions per page</span>
                    <div class="Form-input-wrapper">
                        <xsl:variable name="pageSizeFilterItems">
                            <map>
                                <xsl:if test="Pagination/PageSize != 25 and Pagination/PageSize != 50 and Pagination/PageSize != 100 and Pagination/PageSize != 250">
                                    <mapEntry>
                                        <name><xsl:value-of select="Pagination/PageSize" /></name>
                                        <value><xsl:value-of select="Pagination/PageSize" /></value>
                                    </mapEntry>
                                </xsl:if>
                                <mapEntry>
                                    <name><xsl:value-of select="25" /></name>
                                    <value><xsl:value-of select="25" /></value>
                                </mapEntry>
                                <mapEntry>
                                    <name><xsl:value-of select="50" /></name>
                                    <value><xsl:value-of select="50" /></value>
                                </mapEntry>
                                <mapEntry>
                                    <name><xsl:value-of select="100" /></name>
                                    <value><xsl:value-of select="100" /></value>
                                </mapEntry>
                                <mapEntry>
                                    <name><xsl:value-of select="250" /></name>
                                    <value><xsl:value-of select="250" /></value>
                                </mapEntry>
                            </map>
                        </xsl:variable>
                        <xsl:call-template name="uiDropdown">
                            <xsl:with-param name="items" select="$pageSizeFilterItems"/>
                            <xsl:with-param name="name" select="'PageSize'"/>
                            <xsl:with-param name="active" select="Pagination/PageSize"/>
                            <xsl:with-param name="className" select="'Tracker-files-count-select'"/>
                        </xsl:call-template>

                    </div>
                </label>
            </div>
            <div class="Form-item">
                <xsl:call-template name="uiButton">
                    <xsl:with-param name="elemType" select="'submit'" />
                    <xsl:with-param name="content" select="'Update'" />
                    <xsl:with-param name="className" select="'Form-button'" />
                </xsl:call-template>
            </div>
            <div class="Form-item">
                <xsl:call-template name="uiDownloadLink">
                    <xsl:with-param name="text" select="'Exceptions Report'" />
                    <xsl:with-param name="url">
                        <xsl:choose>
                            <xsl:when test="normalize-space(/Root/Params/File)">
                                <xsl:text>/bookpub/tracker?c=price_exceptions&amp;file=</xsl:text>
                                <xsl:value-of select="/Root/Params/File" />
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:text>/bookpub/tracker?c=service_price_exceptions&amp;service=</xsl:text>
                                <xsl:value-of select="/Root/Params/Service" />
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:with-param>
                </xsl:call-template>
            </div>
    	</form>
    </xsl:template>


    <xsl:template name="searchNavRecords">
        <xsl:variable name="searchNavPreferences">
            <xsl:if test="/Root/Params/Service">&amp;Service=<xsl:value-of select="/Root/Params/Service" /></xsl:if>
            <xsl:if test="/Root/Params/File">&amp;File=<xsl:value-of select="/Root/Params/File" /></xsl:if>
            <xsl:if test="/Root/Params/trackerPage">&amp;trackerPage=<xsl:value-of select="/Root/Params/trackerPage" /></xsl:if>
        </xsl:variable>
        <div class="PriceExeptions-navigation">
            <xsl:call-template name="searchText" />
            <xsl:call-template name="uiPagination">
                <xsl:with-param name="lastPage" select="Pagination/TotalPages"/>
                <xsl:with-param name="totalCount" select="''" />
                <xsl:with-param name="currentPage" select="Pagination/Current"/>
                <xsl:with-param name="type" select="'priceExeptions'" />
                <xsl:with-param name="searchParams" select="$searchNavPreferences" />
            </xsl:call-template>
        </div>
    </xsl:template>


    <xsl:template name="searchText">
        <xsl:choose>
            <xsl:when test="not(Pagination/Total &gt; 0) and not(/Root/Counts/Totals/TotalExceptions)">
                <div class="PriceExeptions-info">
                    No price exceptions were found
                </div>
            </xsl:when>

            <xsl:otherwise>
                <xsl:variable name="totalPages">
                    <xsl:choose>
                        <xsl:when test="Pagination/TotalPages &gt; 0">
                            <xsl:value-of select="Pagination/TotalPages" />
                        </xsl:when>
                        <xsl:otherwise>1</xsl:otherwise>
                     </xsl:choose>
                 </xsl:variable>
                <xsl:if test="/Root/Counts/Totals/TotalExceptions &gt; 0">
                    <div class="PriceExeptions-info">
                        <xsl:choose>
                            <xsl:when test="/Root/Counts/Totals/TotalUnapproved &gt; 0">
                                <xsl:value-of select="/Root/Counts/Totals/TotalUnapproved" />
                            </xsl:when>
                            <xsl:otherwise>0</xsl:otherwise>
                        </xsl:choose>
                        <xsl:text> of </xsl:text>
                        <xsl:choose>
                            <xsl:when test="/Root/Counts/Totals/TotalExceptions &gt; 0">
                                <xsl:value-of select="/Root/Counts/Totals/TotalExceptions" />
                            </xsl:when>
                            <xsl:otherwise>0</xsl:otherwise>
                        </xsl:choose>
                        <xsl:text> Price Exceptions are Unapproved</xsl:text>
                        <xsl:if test="Pagination/TotalPages &gt; 1">
                            <xsl:text>&#160;—&#160;</xsl:text>
                        </xsl:if>
                    </div>
                </xsl:if>
            </xsl:otherwise>

        </xsl:choose>
    </xsl:template>

    <xsl:template name="toggleApproved">
        <xsl:variable name="pageURL">
            <xsl:text>?c=exceptions</xsl:text>
            <xsl:if test="/Root/Params/Service">&amp;Service=<xsl:value-of select="/Root/Params/Service" /></xsl:if>
            <xsl:if test="/Root/Params/File">&amp;File=<xsl:value-of select="/Root/Params/File" /></xsl:if>
            <xsl:if test="/Root/Params/Page">&amp;Page=<xsl:value-of select="/Root/Params/Page" /></xsl:if>
        </xsl:variable>
        <div class="toggleApproved">
            <xsl:if test="/Root/Counts/Totals/TotalApproved">
                <xsl:choose>
                    <xsl:when test="/Root/Preferences/Approved = 1">
                        <a href="{$pageURL}&amp;Approved=default">Show Approved (<xsl:value-of select="/Root/Counts/Totals/TotalApproved" />)</a>
                    </xsl:when>
                    <xsl:otherwise>
                        <a href="{$pageURL}&amp;Approved=1">Hide Approved (<xsl:value-of select="/Root/Counts/Totals/TotalApproved" />)</a>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:if>
        </div>
        <div class="toggleApprovedBalance"></div>
    </xsl:template>


    <xsl:template name="sortGroup">
        <xsl:param name="exception" />
        <xsl:param name="checkboxGroup" select="'0'" />

        <xsl:variable name="sortType">
            <xsl:if test="/Root/Preferences/OrderBy = 'author'">Author</xsl:if>
            <xsl:if test="/Root/Preferences/OrderBy = 'title'">BookTitle</xsl:if>
            <xsl:if test="/Root/Preferences/OrderBy = 'format'">Format</xsl:if>
            <xsl:if test="/Root/Preferences/OrderBy = 'catalogPrice'">Price</xsl:if>
            <xsl:if test="/Root/Preferences/OrderBy = 'salePrice'">ListPrice</xsl:if>
            <xsl:if test="/Root/Preferences/OrderBy = 'units'">Units</xsl:if>
            <xsl:if test="/Root/Preferences/OrderBy = 'country'">Country</xsl:if>
            <xsl:if test="/Root/Preferences/OrderBy = 'saleDate'">DateBegin</xsl:if>
        </xsl:variable>

        <xsl:variable name="sortTypeName">
            <xsl:if test="/Root/Preferences/OrderBy = 'author'">Author</xsl:if>
            <xsl:if test="/Root/Preferences/OrderBy = 'title'">Title</xsl:if>
            <xsl:if test="/Root/Preferences/OrderBy = 'format'">Format</xsl:if>
            <xsl:if test="/Root/Preferences/OrderBy = 'catalogPrice'">Effective Price</xsl:if>
            <xsl:if test="/Root/Preferences/OrderBy = 'salePrice'">Reported Price</xsl:if>
            <xsl:if test="/Root/Preferences/OrderBy = 'units'">Units</xsl:if>
            <xsl:if test="/Root/Preferences/OrderBy = 'country'">Country</xsl:if>
            <xsl:if test="/Root/Preferences/OrderBy = 'saleDate'">Sales Dates</xsl:if>
        </xsl:variable>

        <xsl:variable name="sortPath">$exception/<xsl:value-of select="$sortType" /></xsl:variable>

        <xsl:variable name="sortPathValue">$exception/<xsl:value-of select="$sortType" />/@value</xsl:variable>

        <xsl:variable name="sortPathRaw">$exception/<xsl:value-of select="$sortType" />/@raw</xsl:variable>

        <xsl:variable name="sortPathCurrency">$exception/<xsl:value-of select="$sortType" />/@currency_code</xsl:variable>

        <xsl:variable name="sortGroupHeading" select="dyn:evaluate($sortPath)" />

        <xsl:variable name="sortGroupHeadingValue" select="dyn:evaluate($sortPathValue)" />

        <xsl:variable name="sortGroupHeadingRaw" select="dyn:evaluate($sortPathRaw)" />

        <xsl:variable name="sortGroupHeadingCurrency" select="dyn:evaluate($sortPathCurrency)" />

        <xsl:variable name="sortGroupHeadingDisplay">
            <xsl:choose>
                <xsl:when test="not(normalize-space(dyn:evaluate($sortGroupHeadingValue))) and (/Root/Preferences/OrderBy = 'catalogPrice' or /Root/Preferences/OrderBy = 'salePrice')">No product price</xsl:when>
                <xsl:otherwise>
                    <xsl:if test="$sortGroupHeadingCurrency">
                        <xsl:value-of select="$sortGroupHeadingCurrency" /><xsl:text> </xsl:text>
                    </xsl:if>
                    <xsl:value-of select="$sortGroupHeading" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="exceptionCount" select="count(/Root/Exception[dyn:evaluate($sortType) = dyn:evaluate($sortPath)])" />

        <xsl:variable name="sortGroupCount">
            <xsl:choose>
                <xsl:when test="(/Root/Preferences/OrderBy = 'catalogPrice' or /Root/Preferences/OrderBy = 'salePrice') and $sortGroupHeadingValue">
                    <xsl:value-of select="/Root/Counts/Groups/Group[Name = $sortGroupHeadingValue]/Count" />
                </xsl:when>
                <xsl:when test="/Root/Preferences/OrderBy = 'saleDate' and $sortGroupHeadingRaw">
                    <xsl:value-of select="/Root/Counts/Groups/Group[translate(Name,'-','') = $sortGroupHeadingRaw]/Count" />
                </xsl:when>
                <xsl:when test="/Root/Preferences/OrderBy = 'catalogPrice' or /Root/Preferences/OrderBy = 'salePrice'">
                    <xsl:value-of select="/Root/Counts/Groups/Group[Name = '']/Count" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="/Root/Counts/Groups/Group[Name = $sortGroupHeading]/Count" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <thead>
            <tr class="Table-row sortGroup">
                <td class="Table-col Table-col-select" colspan="12">
                    <strong><xsl:value-of select="$sortTypeName" />: </strong> <xsl:value-of select="$sortGroupHeadingDisplay" />
                    (<xsl:value-of select="$sortGroupCount" />)
                </td>
            </tr>

            <xsl:call-template name="exceptionHeaderRow">
                <xsl:with-param name="checkboxGroup" select="$checkboxGroup" />
            </xsl:call-template>
        </thead>
        <tbody>
            <xsl:for-each select="/Root/Exception[dyn:evaluate($sortType) = dyn:evaluate($sortPath)]">
                <xsl:call-template name="exceptionRow">
                    <xsl:with-param name="checkboxGroup" select="$checkboxGroup" />
                </xsl:call-template>
            </xsl:for-each>
        </tbody>
        <xsl:variable name="newExceptionPosition" select="$exception/@i + $exceptionCount" />
        <xsl:if test="/Root/Exception[@i = $newExceptionPosition]">
            <xsl:call-template name="sortGroup">
                <xsl:with-param name="exception" select="/Root/Exception[@i = $newExceptionPosition]" />
                <xsl:with-param name="checkboxGroup" select="$checkboxGroup + 1" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>


    <xsl:template name="varianceSortGroup">
        <xsl:param name="exception" />
        <xsl:param name="checkboxGroup" select="'0'" />

		<xsl:variable name="varianceGroupID" >
			<xsl:choose>
			    <xsl:when test="$exception/Variance = 0">0</xsl:when>
			    <xsl:otherwise><xsl:value-of select="floor($exception/Variance div /Root/Counts/Groups/GroupSize) + 1" /></xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

        <xsl:variable name="varianceRangeStart">
            <xsl:choose>
                <xsl:when test="$exception/Variance = 0">0</xsl:when>
                <xsl:when test="($exception/Variance mod /Root/Counts/Groups/GroupSize) = 0"><xsl:value-of select="$exception/Variance - /Root/Counts/Groups/GroupSize" />.01</xsl:when>
                <xsl:otherwise><xsl:value-of select="floor($exception/Variance div /Root/Counts/Groups/GroupSize) * /Root/Counts/Groups/GroupSize" />.01</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="varianceRangeEnd" select="$varianceRangeStart + /Root/Counts/Groups/GroupSize - .01" />

        <xsl:variable name="sortGroupHeadingDisplay">
            <xsl:choose>
                <xsl:when test="$exception/Variance = 0">Cannot be calculated</xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$varianceRangeStart" />% - <xsl:value-of select="$varianceRangeEnd" />%
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="varianceCriteria">
            <xsl:choose>
                <xsl:when test="$exception/Variance = 0">Variance = 0</xsl:when>
                <xsl:otherwise>Variance &gt;= $varianceRangeStart and Variance &lt;= $varianceRangeEnd</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="exceptionCount" select="count(/Root/Exception[dyn:evaluate($varianceCriteria)])" />

        <thead>
            <tr class="Table-row sortGroup">
                <td class="Table-col Table-col-select" colspan="12">
                    <strong>Variance: </strong> <xsl:value-of select="$sortGroupHeadingDisplay" />
                    (<xsl:value-of select="/Root/Counts/Groups/Group[Name = $varianceGroupID]/Count" />)
                </td>
            </tr>
            <xsl:call-template name="exceptionHeaderRow">
                <xsl:with-param name="checkboxGroup" select="$checkboxGroup" />
            </xsl:call-template>
        </thead>
        <tbody>
            <xsl:for-each select="/Root/Exception[dyn:evaluate($varianceCriteria)]">
                <xsl:call-template name="exceptionRow">
                    <xsl:with-param name="checkboxGroup" select="$checkboxGroup" />
                </xsl:call-template>
            </xsl:for-each>
        </tbody>

        <xsl:variable name="newExceptionPosition" select="$exception/@i + $exceptionCount" />
        <xsl:if test="/Root/Exception[@i = $newExceptionPosition]">
            <xsl:call-template name="varianceSortGroup">
                <xsl:with-param name="exception" select="/Root/Exception[@i = $newExceptionPosition]" />
                <xsl:with-param name="checkboxGroup" select="$checkboxGroup + 1" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>


    <xsl:template name="unitsSortGroup">
        <xsl:param name="exception" />
        <xsl:param name="checkboxGroup" select="'0'" />

		<xsl:variable name="unitsGroupID" >
			<xsl:choose>
			    <xsl:when test="$exception/Units = 0">0</xsl:when>
			    <xsl:otherwise><xsl:value-of select="floor(($exception/Units - 1) div /Root/Counts/Groups/GroupSize) + 1" /></xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

        <xsl:variable name="unitsRangeStart">
            <xsl:choose>
                <xsl:when test="$exception/Units = 0">0</xsl:when>
                <xsl:otherwise><xsl:value-of select="(floor(($exception/Units - 1) div /Root/Counts/Groups/GroupSize) * /Root/Counts/Groups/GroupSize) + 1" /></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:variable name="unitsRangeEnd" select="$unitsRangeStart + /Root/Counts/Groups/GroupSize - 1" />

        <xsl:variable name="sortGroupHeadingDisplay">
            <xsl:value-of select="$unitsRangeStart" /> - <xsl:value-of select="$unitsRangeEnd" />
        </xsl:variable>

        <xsl:variable name="unitsCriteria">
            <xsl:text>Units &gt;= $unitsRangeStart and Units &lt;= $unitsRangeEnd</xsl:text>
        </xsl:variable>

        <xsl:variable name="exceptionCount" select="count(/Root/Exception[dyn:evaluate($unitsCriteria)])" />
        <thead>
            <tr class="Table-row sortGroup">
                <td class="Table-col Table-col-select" colspan="12">
                    <strong>Units: </strong> <xsl:value-of select="$sortGroupHeadingDisplay" />
                    (<xsl:value-of select="/Root/Counts/Groups/Group[Name = $unitsGroupID]/Count" />)
                </td>
            </tr>

            <xsl:call-template name="exceptionHeaderRow">
                <xsl:with-param name="checkboxGroup" select="$checkboxGroup" />
            </xsl:call-template>
        </thead>
        <tbody>
            <xsl:for-each select="/Root/Exception[dyn:evaluate($unitsCriteria)]">
                <xsl:call-template name="exceptionRow">
                    <xsl:with-param name="checkboxGroup" select="$checkboxGroup" />
                </xsl:call-template>
            </xsl:for-each>
        </tbody>

        <xsl:variable name="newExceptionPosition" select="$exception/@i + $exceptionCount" />
        <xsl:if test="/Root/Exception[@i = $newExceptionPosition]">
            <xsl:call-template name="unitsSortGroup">
                <xsl:with-param name="exception" select="/Root/Exception[@i = $newExceptionPosition]" />
                <xsl:with-param name="checkboxGroup" select="$checkboxGroup + 1" />
            </xsl:call-template>
        </xsl:if>

    </xsl:template>


    <xsl:template name="fileServiceArrays">
        <script type="text/javascript">

            // setting up an array of files for each service
            var filesForService = new Object();
            <xsl:for-each select="/Root/ValidatedServices/Service">
                <xsl:variable name="serviceID" select="ServiceID" />
          	    var fileList<xsl:value-of select="$serviceID" /> = new Object();
          	    <xsl:for-each select="/Root/OpenFiles/File[ServiceID = $serviceID and PriceExceptions/@raw &gt; 0]">
            	    fileList<xsl:value-of select="$serviceID" />[<xsl:value-of select="FileID" />] = '<xsl:value-of select="PrettyFileName" />';
    	        </xsl:for-each>
    	        filesForService[<xsl:value-of select="$serviceID" />] = fileList<xsl:value-of select="$serviceID" />;
            </xsl:for-each>

        </script>
    </xsl:template>


</xsl:stylesheet>
