<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:dyn="http://exslt.org/dynamic" extension-element-prefixes="dyn">
  <xsl:output method="html" indent="no" doctype-system="http://www.w3.org/TR/html4/strict.dtd" doctype-public="-//W3C//DTD HTML 4.01//EN" encoding="utf-8" />

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

  <xsl:template name="pickLayout">
    <xsl:call-template name="fileData" />
    <xsl:call-template name="saleData" />
    <xsl:call-template name="matchList" />
    <xsl:call-template name="matchSearch" />
  </xsl:template>
  

  <xsl:template name="fileData">
    <div class="section" id="manageImport">   
      <form action="/bookpub/matcher" name="lineSearch">
        <input type="hidden" name="c" value="sale_pos" />
        <input type="hidden" name="file" value="{File/FileID}" />
        <ul>
		  <li><strong><xsl:value-of select="File/OrigFileName" /></strong></li> 
		  <li>Service: <strong><xsl:value-of select="File/ServiceName" /></strong></li> 
          <li>Exceptions: <strong><xsl:value-of select="File/RemainingExceptions" /></strong></li>
          <li>Line #
            <input type="text" name="line" size="6" maxlength="5" onFocus="this.select();">
            <xsl:attribute name="value">
              <xsl:if test="Sale/LineNum"><xsl:value-of select="Sale/LineNum" /></xsl:if>
              <xsl:if test="not(Sale/LineNum) and Params/line"><xsl:value-of select="Params/line" /></xsl:if>
            </xsl:attribute>
            </input>
            &#160; <input type="submit" value="Go" />
          </li>
        </ul>
      </form>
    </div>  
  </xsl:template>


  <xsl:template name="saleData">
    <div class="section">
      <xsl:if test="not(Sale)">
    	<p class="error">Line #<xsl:value-of select="Params/line" /> does not exist in this file.</p>
    	<br />
      </xsl:if>
      <xsl:if test="Sale">
	    <table class="textChart">
          <tr>
            <th width="60">Type</th>
            <th>Author</th>
            <th>Title</th>
            <th>Subtitle</th>
            <th>Format</th>	          
            <th>ISBN13</th>	
            <xsl:if test="normalize-space(Sale/rISBN10)">          
            <th>ISBN10</th>	              
            </xsl:if>
            <xsl:if test="normalize-space(Sale/rClientProductID)">
            <th>Client&#160;Service&#160;ID</th>
            </xsl:if>	    
            <xsl:if test="normalize-space(Sale/ServiceProductID)">      
            <th>Vendor&#160;ID</th>
            </xsl:if>
          </tr>
          <tr class="row1">
            <td><xsl:value-of select="Sale/ProductTypeString" /></td>
            <td><xsl:value-of select="Sale/rAuthor" /></td>
            <td><xsl:value-of select="Sale/rTitle" /></td>
            <td><xsl:value-of select="Sale/rSubtitle" /></td>
            <td><xsl:value-of select="Sale/FormatTypeString" /></td>	   
            <td><xsl:value-of select="Sale/rISBN13" /></td>
            <xsl:if test="normalize-space(Sale/rISBN10)">	   
            <td><xsl:value-of select="Sale/rISBN10" /></td>	
            </xsl:if>   	       
            <xsl:if test="normalize-space(Sale/rClientProductID)">         
            <td><xsl:value-of select="Sale/rClientProductID" /></td>
            </xsl:if>
            <xsl:if test="normalize-space(Sale/ServiceProductID)">
            <td><xsl:value-of select="Sale/ServiceProductID" /></td>
            </xsl:if>
          </tr>
	    </table>
      </xsl:if>
    </div>
  </xsl:template>


  <xsl:template name="matchList">   
    <div class="section">
      <xsl:call-template name="navLinks" />
      <xsl:choose>
        <xsl:when test="normalize-space(Sale/ProductID) and Sale/ProductID != 0">
          <h3>Current Match</h3>
        </xsl:when>
        <xsl:otherwise>
          <h3>Suggested Book Matches</h3>
        </xsl:otherwise>
      </xsl:choose>
      <form action="/bookpub/matcher" name="match">
        <input type="hidden" name="sale" value="{Sale/SaleID}" />
        <input type="hidden" name="trackerPage" value="{Params/trackerPage}" />
        <xsl:if test="Sale">
      	  <xsl:call-template name="suggestedChart" />
        </xsl:if>
      <span id="ERRORproduct" class="error"></span>
      <xsl:if test="not(Params/search)">
        <xsl:call-template name="messages" />
      </xsl:if>
      <xsl:if test="normalize-space(Sale/ProductID) and Sale/ProductID != 0">
        <p><strong>This sale has been matched.</strong></p>
      </xsl:if>
      <xsl:if test="File/RemainingExceptions = 0">
        <p>All exceptions resolved for <b><xsl:value-of select="File/OrigFileNamePretty"/></b>.</p>
      </xsl:if>
      <br class="superBreak" />
      <xsl:call-template name="navLinks" />
      <table class="buttons">
        <tr>
          <td>
            <xsl:if test="normalize-space(Sale/ProductID) and Sale/ProductID != 0 and Sale/ImportStatus != 1">
              <xsl:attribute name="class">defaultButton</xsl:attribute>
              <input type="hidden" name="c" value="unmatch_pos" />
              <input type="submit" name="submit" value="Undo Match">
                <xsl:if test="Sale/ImportStatus != 2 and Sale/ImportStatus != 3">
                  <xsl:attribute name="disabled">true</xsl:attribute>
                </xsl:if>
              </input>
            </xsl:if>
            <xsl:if test="normalize-space(Products/Product/ProductID)">
              <xsl:if test="Sale/ProductID = '' or Sale/ProductID = 0">
              <xsl:attribute name="class">defaultButton</xsl:attribute>
                <input type="submit" name="submitMatch" value="Match to Catalog" disabled="true" />
                <input type="hidden" name="c" value="match_pos" />
              </xsl:if>
            </xsl:if>
          </td>
          <td><input type="button" value="Back to Summary" onClick="location.href='/support/bookpub?c=pos_feed&amp;page={Params/trackerPage}#file{File/FileID}'" /></td>
        </tr>
      </table>
    </form>
    </div>
  </xsl:template>


  <xsl:template name="matchSearch">    
    <xsl:if test="Sale/ProductID = '' or Sale/ProductID = 0">
      <div class="section">
        <form action="/bookpub/matcher" name="matcherSearch" class="matcherSearch">
        <input type="hidden" name="sale" value="{Sale/SaleID}" />
        <input type="hidden" name="c" value="sale_pos" />
        <input type="hidden" name="trackerPage" value="{Params/trackerPage}" />
        <script src="/production/javascript/validateSearch.js?v=1" type="text/javascript"></script>
        <xsl:if test="normalize-space(Params/search)">
          <input type="button" name="clear" value="Return To Suggestions" onClick="location.href='/bookpub/matcher?c=sale_pos&amp;sale={Sale/SaleID}&amp;trackerPage={Params/trackerPage}'" style="margin-bottom: 10px" />
        </xsl:if>
        <p><span id="ERRORsearch" class="error"></span></p>
        <p>
          <span id="searchText"></span> <select name="search_type">
            <option value="smart">
              <xsl:if test="Params/search_type = 'smart'">
                <xsl:attribute name="selected">selected</xsl:attribute>
              </xsl:if>
              Smart</option>
            <option value="all">
              <xsl:if test="Params/search_type = 'all'">
                <xsl:attribute name="selected">selected</xsl:attribute>
              </xsl:if>
              All</option>
            <option value="author">
              <xsl:if test="Params/search_type = 'author'">
                <xsl:attribute name="selected">selected</xsl:attribute>
              </xsl:if>
              Authors</option>
            <option value="title">
              <xsl:if test="Params/search_type = 'title'">
                <xsl:attribute name="selected">selected</xsl:attribute>
              </xsl:if>
              Titles</option>
            <option value="isbn10">
              <xsl:if test="Params/search_type = 'isbn10'">
                <xsl:attribute name="selected">selected</xsl:attribute>
              </xsl:if>
              ISBN10</option>
            <option value="isbn13">
              <xsl:if test="Params/search_type = 'isbn13'">
                <xsl:attribute name="selected">selected</xsl:attribute>
              </xsl:if>
              ISBN13</option>
          </select>
          <input type="text" name="search" maxlength="100" value="{Params/search}" onFocus="this.select();"/> 
          <input type="submit" name="Go" value="Search" />
          <input type="button" name="clear" value="Clear" onClick="document.forms['matcherSearch'].elements['search'].value = '';"/>
        </p>
        </form>
      </div>
    </xsl:if>
  </xsl:template>


  <xsl:template name="suggestedChart">
    <xsl:if test="Params/search">
      <p>
        <strong><xsl:value-of select="count(Products/Product)" /></strong>
        result<xsl:if test="count(Products/Product) != 1">s</xsl:if> found for <strong>'<xsl:value-of select="Params/search" />'</strong>
      </p>
    </xsl:if>
      
    <xsl:variable name="sortOrder">
      <xsl:choose>
        <xsl:when test="normalize-space(Params/sort_order)">descending</xsl:when>
        <xsl:otherwise>ascending</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    
    <div class="textChartHiMatcher">

    <table class="textChart hiOn">
    <xsl:if test="Products">
      <xsl:call-template name="productHeaderRow"/>
        
      <xsl:choose>
        
        <xsl:when test="Params/sort = 'format'">
            <xsl:for-each select="Products/Product">
              <xsl:sort select="translate(ProductFormat/BookFormatType/Description,'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')" data-type="text" order="{$sortOrder}" />
              <xsl:sort select="translate(ProductFormat/BookFormatSubtype/Description,'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"  data-type="text" order="{$sortOrder}" />
              <xsl:call-template name="productDataRow"/>
            </xsl:for-each>
        </xsl:when>
            
        <xsl:otherwise>

            <xsl:variable name="sortParam">
              <xsl:choose>
                <xsl:when test="Params/sort = 'author'">translate(Book/AuthorName,'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')</xsl:when>
                <xsl:when test="Params/sort = 'title'">translate(Book/Title,'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')</xsl:when>
                <xsl:when test="Params/sort = 'subtitle'">translate(Book/Subtitle,'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')</xsl:when>         
                <xsl:when test="Params/sort = 'isbn10'">translate(ISBN10,'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')</xsl:when>
                <xsl:when test="Params/sort = 'isbn13'">translate(ISBN13,'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')</xsl:when>
              </xsl:choose>
            </xsl:variable>
            
            <xsl:variable name="dataType">
              <xsl:choose>
                <xsl:when test="Params/sort = 'isbn10' or Params/sort = 'isbn13'">number</xsl:when>
                <xsl:otherwise>text</xsl:otherwise>
              </xsl:choose>
            </xsl:variable>                
        
            <xsl:for-each select="Products/Product">
              <xsl:sort select="dyn:evaluate($sortParam)" data-type="{$dataType}" order="{$sortOrder}" />
              <xsl:call-template name="productDataRow"/>
            </xsl:for-each>                
          </xsl:otherwise>
            
        </xsl:choose>
        
      </xsl:if>
    </table>
    
    </div>

  </xsl:template>


  <xsl:template name="productHeaderRow">

  	<xsl:variable name="sortLink">/bookpub/matcher?c=sale&amp;sale=<xsl:value-of select="Params/sale"/>&amp;<xsl:if test="Params/search_type">search_type=<xsl:value-of select="Params/search_type"/>&amp;</xsl:if><xsl:if test="Params/search">search=<xsl:value-of select="Params/search"/></xsl:if>&amp;trackerPage=<xsl:value-of select="/Root/Params/trackerPage" />&amp;</xsl:variable>
  	
    <tr>
      <xsl:if test="not(normalize-space(Sale/ProductID)) or Sale/ProductID = 0">
        <th width="60">Choose</th>
      </xsl:if>
            
      <th>
        <a>
          <xsl:attribute name="href">
            <xsl:value-of select="$sortLink" />sort=author<xsl:if test="Params/sort = 'author' and not(Params/sort_order)">&amp;sort_order=1</xsl:if>
          </xsl:attribute>Author</a>
      </th>
          
      <th>
        <a>
          <xsl:attribute name="href">
            <xsl:value-of select="$sortLink" />sort=title<xsl:if test="Params/sort = 'title' and not(Params/sort_order)">&amp;sort_order=1</xsl:if>
          </xsl:attribute>Title</a>
      </th>

      <th>
        <a>
          <xsl:attribute name="href">
            <xsl:value-of select="$sortLink" />sort=subtitle<xsl:if test="Params/sort = 'subtitle' and not(Params/sort_order)">&amp;sort_order=1</xsl:if>
          </xsl:attribute>Subtitle</a>
      </th>

      <th>
        <a>
          <xsl:attribute name="href">
            <xsl:value-of select="$sortLink" />sort=format<xsl:if test="Params/sort = 'format' and not(Params/sort_order)">&amp;sort_order=1</xsl:if>
          </xsl:attribute>Format</a>
      </th>
          
      <th>
        <a>
          <xsl:attribute name="href">
            <xsl:value-of select="$sortLink" />sort=isbn13<xsl:if test="Params/sort = 'isbn13' and not(Params/sort_order)">&amp;sort_order=1</xsl:if>
          </xsl:attribute>ISBN13</a>
      </th>

      <th>
        <a>
          <xsl:attribute name="href">
            <xsl:value-of select="$sortLink" />sort=isbn10<xsl:if test="Params/sort = 'isbn10' and not(Params/sort_order)">&amp;sort_order=1</xsl:if>
          </xsl:attribute>ISBN10</a>
      </th>
                        
    </tr>
        
  </xsl:template>


  <xsl:template name="productDataRow">
    <tr class="row{position() mod 2}">
      <xsl:choose>
        <xsl:when test="normalize-space(/Root/Sale/ProductID) and /Root/Sale/ProductID != 0">
          <td><xsl:value-of select="Book/AuthorName" /></td>
        </xsl:when>
        <xsl:otherwise>
          <td style="text-align: center"><input type="radio" name="product" value="{ProductID}" onClick="enableField('match','submitMatch');" /></td>
          <td><xsl:value-of select="Book/AuthorName" /></td>
        </xsl:otherwise>
      </xsl:choose>
      <td><xsl:value-of select="Book/Title" /></td>
      <td><xsl:value-of select="Book/Subtitle" /></td>
      <td>
        <xsl:value-of select="ProductFormat/BookFormatType/Description" />
        <xsl:if test="normalize-space(ProductFormat/BookFormatSubtype/Description)">
          <xsl:text> - </xsl:text><xsl:value-of select="ProductFormat/BookFormatSubtype/Description" />
        </xsl:if>
      </td>
      <td><xsl:value-of select="ISBN13" /></td>
      <td><xsl:value-of select="ISBN10" /></td>
      
      <!--
      <td>
        <xsl:choose>
          <xsl:when test="normalize-space(ClientTrackID)">
            <xsl:value-of select="ClientTrackID" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="ClientAlbumID" />
          </xsl:otherwise>
        </xsl:choose>
      </td>
      -->

    </tr>
  </xsl:template>


  <xsl:template name="navLinks">
    <div class="navPrev matcherNavPrev">
      <xsl:if test="normalize-space(Sale/PreviousErrorID)">
        <a href="?sale={Sale/PreviousErrorID}&amp;trackerPage={Params/trackerPage}&amp;c=sale_pos">&#171; Previous</a>
      </xsl:if>
      <xsl:if test="normalize-space(Sale/PreviousUnfixedID)">
        <a href="?sale={Sale/PreviousUnfixedID}&amp;trackerPage={Params/trackerPage}&amp;c=sale_pos">&#171; Previous Exception</a>
      </xsl:if>
      <xsl:if test="PreviousErrorID = '' and PreviousUnfixedID = ''">
        &#160;
      </xsl:if>
    </div>
    <div class="navNext matcherNavNext">
      <xsl:if test="normalize-space(Sale/NextUnfixedID)">
        <a href="?sale={Sale/NextUnfixedID}&amp;trackerPage={Params/trackerPage}&amp;c=sale_pos">Next Exception &#187;</a>
      </xsl:if>
      <xsl:if test="normalize-space(Sale/NextErrorID)">
        <a href="?sale={Sale/NextErrorID}&amp;trackerPage={Params/trackerPage}&amp;c=sale_pos">Next &#187;</a>
      </xsl:if>
      <xsl:if test="NextErrorID = '' and NextUnfixedID = ''">
        &#160;
      </xsl:if>
    </div>
  </xsl:template>

</xsl:stylesheet>


