<!--                                                                  -->
<!--  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:template name="pickLayout">
      <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>
      <script type="text/javascript">
        validationSet = {
          'Group': {
            'regexp': /^.*$/,
            'errorBlank': 'Please select data to report.<br /> '
          },
          'Type': {
            'regexp': /^.*$/,
            'errorBlank': 'Please select the data subset or type.<br /> '
          }
        };      
      </script>
      
        <div class="section" id="reportList">
            <fieldset class="menu">
                <legend>Request Report</legend>
                <xsl:call-template name="errorMessageGeneral" />
                <xsl:call-template name="reportRequestSelect" />
            </fieldset>
            <br class="superBreak" />
            <xsl:call-template name="dynamicReportList" />
            <br class="superBreak" />
	    </div>
	    <xsl:call-template name="typeList" />
    </xsl:template>


    <xsl:template name="dynamicReportList" >
        <xsl:if test="Form/ReportList/Report">
            <xsl:call-template name="reportTable" />
        </xsl:if>
	    <div class="reportMessaging">
	        <xsl:if test="not(Form/ReportList/Report)">
	            No reports are available at this time.
	            <br /><br />
	        </xsl:if>
	        <em>Reports are available for 30 days after their request date.</em>
	    </div>
    </xsl:template>
  

    <xsl:template name="reportTable">
        <table id="generatedReports" class="textChart">
    	    <tr>
        	    <th class="dateCol">Request Date</th>
        	    <th class="requestedCol">Requested By</th>
        	    <th class="reportCol">Report</th>
        	    <th class="statusCol">Status</th>
        	    <th class="deleteCol">Delete</th>
    	    </tr>  
    	    <xsl:for-each select="Form/ReportList/Report" >
        	    <tr class="row{position() mod 2}">
        	        <xsl:if test="ReportID = /Root/Params/Highlight">
        	            <xsl:attribute name="class">highlight</xsl:attribute>
        	        </xsl:if>
        		    <td class="dateCol"><xsl:value-of select="substring(DateCreated,0,11)" /></td>
        	        <td class="requestedCol"><xsl:value-of select="CreatedBy" /></td>
        	        <td class="reportCol">
        			    <xsl:call-template name="reportFileDownload" />
        		    </td>
        	        <td class="statusCol">
        	            <xsl:choose>
                            <xsl:when test="Status = 'new'">New</xsl:when>
                            <xsl:when test="Status = 'waiting'"><em>Waiting to Run</em></xsl:when>
                            <xsl:when test="Status = 'running'"><em>Running</em></xsl:when>
                            <xsl:when test="Status = 'available'">Available</xsl:when>
                            <xsl:when test="Status = 'cancelled'">Canceled</xsl:when>
                            <xsl:when test="Status = 'deleted'">Deleted</xsl:when>
                            <xsl:when test="Status = 'error'">Error</xsl:when>
                            <xsl:otherwise><xsl:value-of select="Status" /></xsl:otherwise>   	            
        	            </xsl:choose>
        	        </td>
        	        <td class="deleteCol">
        			    <xsl:call-template name="reportCancelReport" />
        		    </td>
        		</tr>
    	    </xsl:for-each>
    	</table>    
    </xsl:template>  
  

    <xsl:template name="reportCancelReport" >
        <xsl:choose>
	        <xsl:when test="Status = 'new' or Status = 'waiting' or Status = 'running'">
	            <a href="/rps/report/cancel?ReportID={ReportID}" >Cancel</a>
	        </xsl:when>
	        <xsl:otherwise>
	            <a href="javascript:void(-1)" onClick="confirmDeleteReport({ReportID})" >Delete</a>
	        </xsl:otherwise>
	    </xsl:choose>
    </xsl:template>


    <xsl:template name="reportFileDownload" >
        <xsl:value-of select="FileName" />
	    <xsl:if test="Status = 'available'">
	        <xsl:for-each select="Downloads/Type" >
	            <xsl:sort select="." />
	            <xsl:text> </xsl:text>
	            <a href="/rps/report/download?ReportID={../../ReportID}&amp;Type={.}">.<xsl:value-of select="translate(.,$lcletters,$ucletters)" /></a>	            
	        </xsl:for-each>
	    </xsl:if>
    </xsl:template>


    <xsl:template name="reportRequestSelect" >
        <form action="/rps/report/show" method="get" id="reportRequest">
            <table>
                <tr>
                    <td class="groupCell">
                        <xsl:call-template name="inlineError">
                            <xsl:with-param name="path">Group</xsl:with-param>
                            <xsl:with-param name="displayName">Data</xsl:with-param>
                        </xsl:call-template>
                        Select data to report
                        <select name="Group" id="Group" onChange="updateSubSelect(this, Type, typeList)" style="margin-left: 10px">
                            <option value="">Choose...</option>
			                <xsl:for-each select="Form/GroupList/Group">
			                    <xsl:sort select="Name" />
                                <option value="{Name}"><xsl:value-of select="Name" /></option>
			                </xsl:for-each>
                        </select>
                    </td>
                    <td class="typeCell">
                        <xsl:call-template name="inlineError">
                            <xsl:with-param name="path">Type</xsl:with-param>
                            <xsl:with-param name="displayName">Data Subset or Type</xsl:with-param>
                        </xsl:call-template>
                        Select data subset or type
                        <select name="Type" id="Type" style="margin-left: 10px">
                            <option value="">Choose...</option>
                        </select>
                    </td>
                    <td>
                        <input type="submit" value="Configure Report" />
                    </td>
                </tr>
            </table>
        </form>
    </xsl:template>


    <xsl:template name="typeList">
        <script type="text/javascript">
            // setting up an array of types for each group
            var typeList = new Object();

            <xsl:for-each select="/Root/Form/GroupList/Group">
		        var type<xsl:value-of select="@i" /> = new Object();

		        <xsl:call-template name="reportJSArrayElements" >
		            <xsl:with-param name="arrayName">type<xsl:value-of select="@i" /></xsl:with-param>
		        </xsl:call-template>

		        typeList['<xsl:value-of select="Name" />'] = type<xsl:value-of select="@i" />;
	        </xsl:for-each>

        </script>
    </xsl:template>


    <xsl:template name="reportJSArrayElements" >
        <xsl:param name="arrayName" />
	    <xsl:for-each select="TypeList/Type">
            <xsl:sort select="Type" />
	        <xsl:value-of select="$arrayName" />['<xsl:value-of select="." />'] = '<xsl:value-of select="." />';
	    </xsl:for-each>
    </xsl:template>


</xsl:stylesheet>
