<!--                                                                  -->
<!--  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: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/rps/templates/global.xsl" />

  <xsl:template name="pickLayout">
    <!-- <xsl:value-of select="/Root/Params/c"/>  -->
    <xsl:choose>
      <xsl:when test="/Root/Params/c = 'save'" >
        <xsl:call-template name="showCommit" />
      </xsl:when>
      <xsl:when test="/Root/Params/c = 'discard'">
        <xsl:call-template name="showCommit" />
      </xsl:when>
      <xsl:when test="/Root/Params/c = 'fail'">
        <xsl:call-template name="showCommit" />
      </xsl:when>
      <xsl:when test="Distribution/UploadError" >
        <xsl:call-template name="showCommit" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="showImport" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="showCommit">
    <div id="distributionMain">
  	  <div class="section">
  	  	<fieldset>
	        <xsl:choose>
	          <xsl:when test="Messages/Message/Type = 'success' ">
			     	 	<legend>Import Results</legend>
			     	 	<div class="insideFieldset step" id="step3">
		            <h2 class="success">Import Successful!</h2>

		            <xsl:variable name="importVerb">
		            	<xsl:choose>
		            		<xsl:when test="Distribution/ImportSummary/TotalAlbums = 1"> was</xsl:when>
		            		<xsl:otherwise> were</xsl:otherwise>
		            	</xsl:choose>
		            </xsl:variable>

		            <p><b><xsl:value-of select="Distribution/ImportSummary/TotalAlbums" /></b> album<xsl:if test="Distribution/ImportSummary/TotalAlbums != 1">s</xsl:if> with <b><xsl:value-of select="Distribution/ImportSummary/TotalTracks" /></b> track<xsl:if test="Distribution/ImportSummary/TotalTracks != 1">s</xsl:if><xsl:value-of select="$importVerb" /> added to your catalog.</p>
		            <P>You have successfully completed the import process.  Click the button below to go back to the Catalog landing page.</P>
		            <br />

	            </div>
	          </xsl:when>

	          <xsl:when test="Messages/Message/Type = 'fail'">
			     	 	<legend>Import Results</legend>
			     	 	<div class="insideFieldset step" id="stepCancel">
		            <h2 class="error">Import Timeout</h2>
		            <P>An issue has occurred with your import.  Please click the button below and upload your file again.</P>
	            </div>
	          </xsl:when>

	          <xsl:when test="Messages/Message/Type = 'error'">
			     	 	<legend>Import Results</legend>
			     	 	<div class="insideFieldset step" id="stepCancel">
		            <h2 class="error">Import Cancelled</h2>
		            <P>You have cancelled the import process.  No albums have been added to your catalog.  Click the button below to go back to the Catalog landing page.</P>
	            </div>
	          </xsl:when>

              <xsl:when test="Distribution/UploadError" >
			     	 	<legend>Import Results</legend>
			     	 	<div class="insideFieldset step" id="stepError">
		            <h2 class="error">Upload Error</h2>
		            <P>Your upload has failed because another import file has already been uploaded.  <a href="/rps/distribution">view the import</a></P>
	            </div>
	          </xsl:when>

	          <xsl:otherwise>
			     	 	<legend>Import Results</legend>
			     	 	<div class="insideFieldset step" id="stepError">
		            <h2 class="error">Import Error</h2>
		            <P>You've reached this page in error.  Click the button below to go back to the Catalog landing page.</P>
	            </div>
	          </xsl:otherwise>
	        </xsl:choose>

	        <input type="button" value="Back to Albums" onClick="location.href='catalog'" />
	        <br /><br />
	       </fieldset>
	       <br /><br />
      </div>
    </div>
  </xsl:template>

  <xsl:template name="showImport">
    <!-- Define a tooltip object for duplicates -->
    <script type="text/javascript">
      var oTooltipError     = new RSAjaxTooltip( { x_offset: 0, y_offset: 0, divid: "error_tooltip", delay: 300, classname: "rs_tooltip" } );
      var oTooltipDuplicate = new RSAjaxTooltip( { x_offset: -40, y_offset: -10, divid: "duplicate_tooltip", delay: 300, classname: "rs_tooltip" } );    </script>

    <div id="distributionMain">
    	<xsl:call-template name="showMetadata" />
    </div>

    <script type="text/javascript">
      var oAlbumListArray = new Array;

      oAlbumListArray[0] = new RSDistributionOnclickUpdate( document.getElementById("albumsGood"),       document.getElementById("album0Image") );
      oAlbumListArray[6] = new RSDistributionOnclickUpdate( document.getElementById("albumsWarnings"),   document.getElementById("album6Image") );
      oAlbumListArray[7] = new RSDistributionOnclickUpdate( document.getElementById("albumsDuplicates"), document.getElementById("album7Image") );
      oAlbumListArray[9] = new RSDistributionOnclickUpdate( document.getElementById("albumsErrors"),     document.getElementById("album9Image") );
    </script>

  </xsl:template>


  <xsl:template name="showMetadata">
    <xsl:choose>
      <xsl:when test="Distribution/ImportValidated &gt; 0">
        <xsl:call-template name="displayMetadata" />
      </xsl:when>
      <xsl:otherwise>
	        <xsl:call-template name="uploadMetadata" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <xsl:template name="uploadMetadata">
  	<div class="section">
     <fieldset>
     	 <legend>Prepare and Upload Your Data</legend>
     	 <div class="insideFieldset step" id="step1">
     	 	<p>
     	 		This feature makes it easy for you to add new albums to your catalog. Follow the instructions below to begin the process:
     	 	</p>
   	 		<ol>
   	 			<li>
   	 				<b>Download</b> and <b>save</b> the latest <a href="/rps/distribution?c=metadata_template">Catalog Template</a> to your computer by clicking the link provided below.<br />
          	<i>If you already have the latest template move on to step 2.</i>
          </li>
          <li>
          	<b>Locate</b> the template on your computer and <b>fill-in</b> the appropriate album data for albums you wish to add.<br />
          	<i>If you require additional assistance with the template, download the </i><a href="/rps/distribution?c=metadata_spec_pdf">Catalog Specification</a><i> for instructions.</i>
					</li>
					<li>
						Click the <b>Browse</b> button below to choose the filled-in template.
					</li>
					<li>
						Once you have chosen the proper file, click the <b>Upload</b> button. This will take you to the Review &amp; Import section.<br />
          	<i>It may take a few minutes to upload your file, depending on the file size and your connection speed.</i>
					</li>
				</ol>
				<p>
					<b>* We recommend that you upload 1 album before attempting to upload multiple albums in order to familiarize yourself with the process.</b>
				</p>

				<br />
				<br />

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

				<br />

				<p class="downloads">
   	 			<a href="/rps/distribution?c=metadata_template" title="Download and use the latest template to add albums to your catalog"><img src="/production/images/icons/office/Document-(16x16).gif" height="16" width="16" /></a>&#160;<a href="/rps/distribution?c=metadata_template" title="Download and use the latest template to add albums to your catalog">Catalog Template</a><i> (version 6.0 - last updated: Oct 2009)</i>

   	 			<br />

   	 			<a href="/rps/distribution?c=metadata_spec_pdf" title="Download the Catalog Specification for your reference"><img src="/production/images/icons/office/Document-(16x16).gif" height="16" width="16" /></a>&#160;<a href="/rps/distribution?c=metadata_spec_pdf" title="Download the Catalog Specification for your reference">Catalog Specification</a><i> (version 6.0 - last updated: Oct 2009)</i>
 	 			</p>

	     </div>
     </fieldset>
     <br /><br />
    </div>
  </xsl:template>


  <xsl:template name="fileUploadForm">
	<div id="fileUploadContainer" class="fileUploadWrapper">
      <form action="#" encoding="multipart/form-data" enctype="multipart/form-data" method="post" onsubmit="bar.startUpload();return false;" name="fileUpload" id="fileUpload">
      <xsl:choose>
        <xsl:when test="Distribution/ImportStaged &gt; 0 and not( Distribution/ImportValidated &gt; 0 )">
		  <input type="hidden" name="fileUpload" id="fileUpload" value="{ Distribution/FileName }" />
        </xsl:when>
        <xsl:otherwise>
		  <input type="hidden" name="c" value="upload_file"/>
		  <input type="file" name="fileUpload" id="fileUpload" size="50"  />
  		  <xsl:call-template name="buttonsWithoutBox" />
        </xsl:otherwise>
      </xsl:choose>
	  </form>
	</div>

    <script type="text/javascript">
      var style = {
          'loadedcolor'    : '#326694',
          'width'          : '350',
          'height'         : '15',
          'bordercolor'    : 'black',
          'labelwidth'     : 170
      }

	  var options = {
                      errorSpan:  "uploadError",
                      uploadForm: "fileUpload",
                      initurl:    "/rps/distribution",
                      statusurl:  "/rps/distribution?c=upload_status",
                      failurl:    "/rps/distribution?c=fail",
                      successurl:  "/rps/distribution"
                    };

      var bar = new RSUploadProgressBar( "fileUploadContainer", style, options );
	</script>

    <xsl:if test="Distribution/ImportStaged &gt; 0 and not( Distribution/ImportValidated &gt; 0 )">
      <script type="text/javascript">bar.init();bar.setupProgressMonitor();</script>
    </xsl:if>
  </xsl:template>


  <xsl:template name="statusMessage">
    <xsl:if test="count(Messages/Message)">
      <P>
      <xsl:for-each select="Messages/Message">
        <span>
          <xsl:attribute name="class"><xsl:value-of select="Type" /></xsl:attribute>
          <xsl:value-of select="Code" />
        </span>
      </xsl:for-each>
      </P>
    </xsl:if>
  </xsl:template>


  <xsl:template name="displayMetadata">
  	<div class="section" id="importActions">

  		<xsl:if test="not(Distribution/ImportSummary/Good &gt; 0 or Distribution/ImportSummary/Warnings &gt; 0)">
	      <p class="errorGeneral bigMessageBox">
	      	No albums will be added to your catalog as a result of this upload.
	      </p>
  		</xsl:if>

     <fieldset>
     	 <legend>Review and Import Your Data</legend>
     	 <div class="insideFieldset step" id="step2">

     	 	<p>
					<b><xsl:value-of select="Distribution/ImportSummary/TotalAlbums"/></b> album<xsl:if test="Distribution/ImportSummary/TotalAlbums != 1">s</xsl:if> with <b><xsl:value-of select="Distribution/ImportSummary/TotalTracks"/></b> track<xsl:if test="Distribution/ImportSummary/TotalTracks != 1">s</xsl:if> are ready for your review. (<a href="/rps/distribution?c=exceldump">view entire file</a>)
				</p>

				<br />

				<ol>
					<li>
						Review the results of the upload below to make sure your album data is what you would like to add to your catalog.<br />
						<i>(if you navigate away from this page, your upload will be saved but not imported until you click the Import button.)</i>
						<br /><br />
						- Click the arrow <img src="/production/images/icons/expand.gif" height="10" width="10" alt="Expand Icon" /> to expand and view the details for each section.
						<br />
						- Use the "&lt; prev" and "more &gt;" links on the left to scroll through album information.
						<br />
						- Mouse over the colored fields and icons <img src="/production/images/icons/office/Warning-(16x16).gif" height="16" width="16" alt="Warning Icon" class="iconExample" /> <img src="/production/images/icons/office/Stop-(Alt-2)-(16x16).gif" height="16" width="16" alt="Error Icon" class="iconExample" /> for additional information.
          </li>
          <li>
          	Some albums may require your attention before they can be added to your catalog. One of two actions can be taken at this time:
          	<br /><br />
            - <b>Discard</b> this upload and correct items in your original spreadsheet and start the process over again.
						<br /><br />
            &#160;&#160;<b>OR</b>
						<br /><br />
            - <b>Download</b> the spreadsheet of albums that will not be imported so that they can be corrected later and proceed with the Import.
						<br /><br />
            &#160;&#160;&#160;&#160;- At this point only the albums listed in the "Albums that will be imported" section will be added to your catalog.
						<br />
            &#160;&#160;&#160;&#160;- The spreadsheet of non-imported albums can be worked on and used to start the import process over again.
					</li>
					<li>
						Click the <b>Import</b> button to add the validated albums to your catalog or click the <b>Discard</b> button to cancel this import and begin again.
					</li>
				</ol>

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

	    	<xsl:variable name="importAlbumsCount" select="Distribution/ImportSummary/Good + Distribution/ImportSummary/Warnings" />


    		<p class="importAlbumsCount">
    			<b><xsl:value-of select="$importAlbumsCount" /></b> album<xsl:if test="$importAlbumsCount != 1">s</xsl:if> will be added to your catalog when you click the import button.
				</p>


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

	    	</div>
	   	</fieldset>
	   	<br /><br />
    </div>

  </xsl:template>

  <xsl:template name="displayImportSummary">

  	<p class="resultsSectionHeader">
  		Albums that will be imported
  	</p>

    <p>
      <xsl:call-template name="albumButton">
        <xsl:with-param name="albumCount" select="Distribution/ImportSummary/Good" />
        <xsl:with-param name="status">0</xsl:with-param>
      </xsl:call-template>
      <xsl:if test="Distribution/ImportSummary/Good != 1"> are</xsl:if><xsl:if test="Distribution/ImportSummary/Good = 1"> is</xsl:if> ready for import.
    </p>
    <DIV id="albumsGood"/>

    <xsl:if test="Distribution/ImportSummary/Warnings &gt; 0">
	    <P>
	      <xsl:call-template name="albumButton">
	        <xsl:with-param name="albumCount" select="Distribution/ImportSummary/Warnings" />
	        <xsl:with-param name="status">6</xsl:with-param>
	      </xsl:call-template>
	      <xsl:variable name="albumWarningVerb">
	      	<xsl:choose>
	      		<xsl:when test="Distribution/ImportSummary/Warnings = 1"> is</xsl:when>
	      		<xsl:otherwise> are</xsl:otherwise>
	      	</xsl:choose>
	      </xsl:variable>
	      <xsl:value-of select="$albumWarningVerb" /> ready for import but<xsl:value-of select="$albumWarningVerb" /><span><xsl:if test="/Root/ClientTypes/ClientType[ClientTypeID = 4]/IsClientTypeFlag = 1"><xsl:attribute name="class">error</xsl:attribute></xsl:if> not ready for distribution</span>.
	      <xsl:call-template name="helpIcon">
	        <xsl:with-param name="tip">Distribution requires additional album data before it can be delivered.</xsl:with-param>
	      </xsl:call-template>
	    </P>
	    <DIV id="albumsWarnings"/>
    </xsl:if>

    <xsl:if test="Distribution/ImportSummary/Duplicates &gt; 0 or Distribution/ImportSummary/Errors &gt; 0">
			<xsl:if test="Distribution/ImportSummary/Good &gt; 0 or Distribution/ImportSummary/Warnings &gt; 0">
				<br />
			</xsl:if>
    	<p class="resultsSectionHeader">
    		Albums that will <b>not</b> be imported
    	</p>
    </xsl:if>

		<xsl:if test="Distribution/ImportSummary/Duplicates &gt; 0">
	    <P>
	      <xsl:call-template name="albumButton">
	        <xsl:with-param name="albumCount" select="Distribution/ImportSummary/Duplicates" />
	        <xsl:with-param name="status">7</xsl:with-param>
	      </xsl:call-template>
	      <xsl:variable name="dupeDownloadText">
	      	<xsl:choose>
	      		<xsl:when test="Distribution/ImportSummary/Duplicates = 1">download this album</xsl:when>
	      		<xsl:otherwise>download these albums</xsl:otherwise>
	      	</xsl:choose>
	      </xsl:variable>
      	<xsl:choose>
      		<xsl:when test="Distribution/ImportSummary/Duplicates = 1"> appears to be a duplicate of an existing album</xsl:when>
      		<xsl:otherwise> appear to be duplicates of existing albums</xsl:otherwise>
      	</xsl:choose> in your catalog. (<A HREF="/rps/distribution?c=exceldump&amp;show=7&amp;show=8"><xsl:value-of select="$dupeDownloadText" />)</A>
	    </P>
	    <DIV id="albumsDuplicates"/>
    </xsl:if>

    <xsl:if test="Distribution/ImportSummary/Errors &gt; 0">
	    <P>
	      <xsl:call-template name="albumButton">
	        <xsl:with-param name="albumCount" select="Distribution/ImportSummary/Errors" />
	        <xsl:with-param name="status">9</xsl:with-param>
	      </xsl:call-template>
	      <xsl:variable name="errorDownloadText">
	      	<xsl:choose>
	      		<xsl:when test="Distribution/ImportSummary/Errors = 1">download this album</xsl:when>
	      		<xsl:otherwise>download these albums</xsl:otherwise>
	      	</xsl:choose>
	      </xsl:variable>
	      contain<xsl:if test="Distribution/ImportSummary/Errors = 1">s</xsl:if> errors and cannot be added to your catalog at this time. (<A HREF="/rps/distribution?c=exceldump&amp;show=9"><xsl:value-of select="$errorDownloadText" /></A>)
	    </P>
	    <DIV id="albumsErrors"/>
    </xsl:if>

		<xsl:if test="Distribution/ImportSummary/Good = 0 and Distribution/ImportSummary/Warnings = 0 and Distribution/ImportSummary/Duplicates = 0 and Distribution/ImportSummary/Errors = 0">
			<p class="emptyMessage error">
				No metadata was found.
			</p>
		</xsl:if>

  </xsl:template>


  <xsl:template name="albumButton">
    <xsl:param name="albumCount" />
    <xsl:param name="status" />
    <xsl:variable name="ajaxURL">/rps/distribution?c=albumdetail&amp;show=</xsl:variable>
		<xsl:choose>
			<xsl:when test="$albumCount &gt; 0">
    		<A HREF="javascript:void(0);" onclick='oAlbumListArray[{$status}].URLRequest( {{ url: "{$ajaxURL}{$status}", status: "{$status}", oAEL: oAlbumListArray }} )' >
      		<img src="/production/images/icons/expand.gif" id="album{$status}Image" />
    		</A>
    	</xsl:when>
    	<xsl:otherwise><xsl:attribute name="class">noExpandButton</xsl:attribute></xsl:otherwise>
    </xsl:choose>
    <xsl:text> </xsl:text>
    <b><xsl:value-of select="$albumCount" /></b> album<xsl:if test="$albumCount != 1">s</xsl:if>

  </xsl:template>


  <xsl:template name="submitButton">
    <xsl:choose>

      <xsl:when test="Distribution/ImportValidated &gt; 0">
	      <input type="button" value="Import" id="importButton">
	      	<xsl:attribute name="onclick">
		      	<xsl:choose>
		      		<xsl:when test="Distribution/ImportSummary/Duplicates &gt; 0 or Distribution/ImportSummary/Errors &gt; 0">
		      			javascript:confirmImport(<xsl:value-of select="Distribution/ImportSummary/Duplicates" />,<xsl:value-of select="Distribution/ImportSummary/Errors" />);
		      		</xsl:when>
		      		<xsl:otherwise>location.href='/rps/distribution?c=save'</xsl:otherwise>
		      	</xsl:choose>
	      	</xsl:attribute>
	      	<!--
	      	<xsl:if test="not(Distribution/ImportSummary/Good &gt; 0 or Distribution/ImportSummary/Warnings &gt; 0)">
	      		<xsl:attribute name="disabled">disabled</xsl:attribute>
	      	</xsl:if>
	      	-->
	      </input>
      </xsl:when>

      <xsl:otherwise>
  			<input type="submit" id="uploadSubmit" value="Upload"/>
  		</xsl:otherwise>

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

  <xsl:template name="cancelButton">
    <xsl:choose>
      <xsl:when test="Distribution/ImportValidated &gt; 0">
	      <input type="button" onclick="location.href='/rps/distribution?c=discard'" value="Discard" />
			</xsl:when>
			<xsl:otherwise>
  			<input type="button" value="Back to Albums" onClick="location.href='catalog'" />
  		</xsl:otherwise>
  	</xsl:choose>
  </xsl:template>

</xsl:stylesheet>
