<!--                                                                  -->
<!--  Copyright (C) 2011 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="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:include href="/app/tools/rps/templates/report/dynamic/common.xsl" />
	<xsl:include href="/app/tools/rps/templates/report/dynamic/options/default.xsl" />
    <xsl:include href="/app/tools/rps/templates/report/dynamic/criteria/default.xsl" />
    <xsl:include href="/app/tools/rps/templates/report/dynamic/criteria/sales.xsl" />


  <xsl:template name="pickLayout">
    <div class="section" id="reportShow">
      <link href="/production/css/rps/report.css?v=4" media="screen" rel="stylesheet" type="text/css" />
      <script src="/production/javascript/subSelect.js?v=1"></script>
      <script src="/production/javascript/report.js?v=11"></script>
      <form name="reportForm" method="post" action="/rps/report/show">
        <xsl:if test="Form/Report = 'RPS::Report::Dynamic::Sales::DigitalAndPhysical' or Form/Report = 'RPS::Report::Dynamic::Sales::Digital' or Form/Report = 'RPS::Report::Dynamic::Sales::Physical'">
            <xsl:attribute name="id">salesReport</xsl:attribute>
        </xsl:if>
      
        <input type="hidden" name="FormSubmit" value="1" />
		<input type="hidden" name="ReportID" value="{/Root/Params/ReportID}" />
		<input type="hidden" name="Group"    value="{/Root/Params/Group}" />
		<input type="hidden" name="Type"     value="{/Root/Params/Type}" />
		
		<xsl:call-template name="errorMessageGeneral" />

        <xsl:if test="Form/Parameters/OptionalData/*">
            <fieldset class="menu">
              <legend>Options</legend>
                <xsl:call-template name="showOptions" />
            </fieldset>
            <br class="superBreak" />
        </xsl:if>

        <xsl:if test="Form/Parameters/Criteria/* ">
            <fieldset class="menu">
              <legend>Report Criteria</legend>
                <xsl:choose>
                  <xsl:when test="Params/Group='Sales'">
                    <xsl:call-template name="showCriteriaSales"/>
                  </xsl:when>
                </xsl:choose>
                <xsl:call-template name="showCriteria" />
            </fieldset>
            <br class="superBreak" />
        </xsl:if>

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

	  </form>
    </div>
    <xsl:call-template name="formSupport" />

  </xsl:template>


  <xsl:template name="submitButton">
    <xsl:choose>
	  <xsl:when test="/Root/Form/Report/ReportID">
	    <input name="submit" type="submit" id="submitButton" value="Regenerate Report" />
	  </xsl:when>
	  <xsl:otherwise>
	    <input name="submit" type="submit" id="submitButton" value="Generate Report" />
	  </xsl:otherwise>
	</xsl:choose>
  </xsl:template>


  <xsl:template name="cancelButton">
    <input type="button" name="Cancel" value="Cancel" onClick="location.href='/rps/report/list'" />
  </xsl:template>


  <xsl:template name="notifyCheckbox" >
    <input type="checkbox" name="Notify" >
      <xsl:if test="normalize-space(/Root/Params/Notify) or /Root/Form/Report/NotifyActiveUser = 1 or (not(normalize-space(/Root/Params/FormSubmit)) and not(/Root/Form/Report/ReportID))">
        <xsl:attribute name="checked">CHECKED</xsl:attribute>
      </xsl:if>
    </input>
	Send me an email when my report is ready
  </xsl:template>
  
  
  <xsl:template name="formSupport">
    <script type="text/javascript">
      // The idea is to add in the event handlers here based on the xml.
      // This should allow us to do some customization without muddying the waters up above.
      
      <xsl:if test="Form/Parameters/Criteria/DateType and Form/Parameters/Criteria/StartDate and Form/Parameters/Criteria/EndDate">
        <xsl:call-template name="dateTypeSupport" />
      </xsl:if>
      
      <xsl:if test="Form/Parameters/OptionalData/ReportType and Form/Parameters/OptionalData/ShowProcessedExpenses and Form/Parameters/OptionalData/ShowUnprocessedExpenses">
        <xsl:call-template name="reportTypeSupport" />
      </xsl:if>
      
      <xsl:if test="Form/Parameters/Criteria/RunType and Form/Parameters/Criteria/Payor and Form/Parameters/Criteria/RunName">
        <xsl:call-template name="runTypeSupport" />        
      </xsl:if>

      <xsl:if test="Form/Parameters/Criteria/ShowByType and Form/Parameters/Criteria/StartDate and Form/Parameters/Criteria/EndDate and Form/Parameters/Criteria/RunType and Form/Parameters/Criteria/Payor and Form/Parameters/Criteria/RunName">
        <xsl:call-template name="showByTypeSupport" />        
      </xsl:if>

      <xsl:if test="Params/Group='Sales'">
        <xsl:call-template name="reportNameSupport" />
      </xsl:if>
      
      
    </script>
  </xsl:template> 
  
  
  <xsl:template name="dateTypeSupport">
    // Enable/disable start and end date fields based on date type selection
    var dateType  = document.getElementById("Parameters_DateType");
    dateType.onchange = function(){checkDateType();};
    checkDateType();  
  </xsl:template>
  
  
  <xsl:template name="reportTypeSupport">
    var rads = document.getElementsByName("Parameters_ReportType");
    for (var i = 0; i &lt; rads.length; i++) {
      rads[i].onclick = function(){checkReportType();};
    }
    checkReportType();  
  </xsl:template>
  
  
  <xsl:template name="showByTypeSupport">  
    // Enable/disable date and run fields based on show by type selection
    var showByType  = document.getElementById("Parameters_ShowByType");
    showByType.onchange = function(){checkShowByType();};
    checkShowByType();  
  </xsl:template>  
  
  
  <xsl:template name="runTypeSupport">
  
    // Populate payor list based on run type
    <xsl:call-template name="payorList" />
    var runType  = document.getElementById("Parameters_RunType");
    var payors   = document.getElementById("Parameters_Payor");
    var runName  = document.getElementById("Parameters_RunName");
    
    runType.onchange = function(){updateSubSelect(runType, payors, payorList, 1); updateRunByPayorAndType();};
    updateSubSelect(runType, payors, payorList, 1);      


    function updateRunByPayorAndType(){
        var runTypeValue = runType.value;
        var payorID = payors.value;   
        var runNameList = runs[runTypeValue];
        updateSubSelect(payors, runName, runNameList, 1);

        if (runTypeValue == 'Choose...')
        {
            var payorsOption = new Option("Choose...", '');
	        payors.options[0] = payorsOption; 
        }
        
        if (!payorID)
        {
            var runNameOption = new Option("Choose...", '');
	        runName.options[0] = runNameOption; 
        }
        
    }

    // Populate run list based on payor      
    
    payors.onchange = function(){updateRunByPayorAndType();};
    updateRunByPayorAndType();        
  </xsl:template>
  
  
  <xsl:template name="reportNameSupport">
    <xsl:if test="Params/Group='Sales'">
      document.getElementById("Parameters_ReportName").value="Sales " +
      "<xsl:value-of select="Params/Type" />";
    </xsl:if>
  </xsl:template>
  
    <xsl:template name="payorList">
        // setting up an array of payors for each royalty run type
        var payorList = new Object();
        var runs = new Object();
        
        runs["Choose..."] = {};
        
        <xsl:for-each select="/Root/Form/Parameters/RunData/*">
            
            <xsl:variable name="runType" select="name(.)" />
	        <xsl:variable name="runTypeValue">
	            <xsl:if test="$runType = 'ArtistRoyalty'">Artist/Producer</xsl:if>
	            <xsl:if test="$runType = 'LabelRoyalty'">Label</xsl:if>
	            <xsl:if test="$runType = 'CAMechanicals'">CA Mechanicals</xsl:if>
	            <xsl:if test="$runType = 'USMechanicals'">US Mechanicals</xsl:if>
	            <xsl:if test="$runType = 'UKMechanicals'">UK Mechanicals</xsl:if>
	        </xsl:variable>
	        
	        runs["<xsl:value-of select="$runTypeValue" />"] = {};
	        
	        runs["<xsl:value-of select="$runTypeValue" />"]['0'] = {};
	        runs["<xsl:value-of select="$runTypeValue" />"]['0']['0'] = 'Choose...';
	                    
	        var payor<xsl:value-of select="$runType" /> = new Object();

            payor<xsl:value-of select="$runType" />['0'] = 'Choose...';

    	    <xsl:for-each select="Payor">
                <xsl:sort select="Name" />
                
                runs['<xsl:value-of select="$runTypeValue" />']['<xsl:value-of select="PayorID" />'] = {};
           
    	        payor<xsl:value-of select="$runType" />['<xsl:value-of select="PayorID" />'] = '<xsl:call-template name="escapeJavascript"><xsl:with-param name="string" select="Name" /></xsl:call-template>';
    	        
    	        runs['<xsl:value-of select="$runTypeValue" />']['<xsl:value-of select="PayorID" />']['0'] = 'Choose...';
    	        
                <xsl:for-each select="Run">	    
                    <xsl:sort select="Label" />
                    runs['<xsl:value-of select="$runTypeValue" />']['<xsl:value-of select="../PayorID" />']['<xsl:value-of select="RunID" />'] = '<xsl:call-template name="escapeJavascript"><xsl:with-param name="string" select="Label" /></xsl:call-template>';
                </xsl:for-each>
    	        
    	    </xsl:for-each>		        
	        
	        payorList['<xsl:value-of select="$runTypeValue" />'] = payor<xsl:value-of select="$runType" />;    
	        
	        
        </xsl:for-each>
    </xsl:template>


</xsl:stylesheet>
