<!--                                                                  -->
<!--  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:template name="permissionShowLayout">
		<script type="text/javascript" src="/production/javascript/tablesort.js"></script>
    <xsl:call-template name="filterPermissions" />
    <form method="post" action="permission" onSubmit="modifyField('submitButton','disable')">
      <input type="hidden" name="formSubmit" value="1" />
      <input type="hidden" name="c" value="show" />
      <xsl:call-template name="permissionShow" />
      <xsl:call-template name="buttons" />
    </form>
  </xsl:template>

  <xsl:template name="filterPermissions">
    <form method="get" action="permission" id="filterPerm" class="section">
      <input type="button" value="Add New" onClick="location.href='?c=add'" id="addPerm" />
      <strong>Show: </strong>
      <select name="App">
        <option value="">All Apps</option>
        <xsl:for-each select="Form/ApplicationList/Application">
          <option value="{.}">
            <xsl:if test=". = /Root/Params/App">
              <xsl:attribute name="selected">selected</xsl:attribute>
            </xsl:if>
            <xsl:value-of select="." />
          </option>
        </xsl:for-each>
      </select>
      <select name="Area">
        <option value="">All Areas</option>
        <xsl:for-each select="Form/AreaList/Area">
          <option value="{.}">
            <xsl:if test=". = /Root/Params/Area">
              <xsl:attribute name="selected">selected</xsl:attribute>
            </xsl:if>
            <xsl:value-of select="." />
          </option>
        </xsl:for-each>
      </select>
      <select name="Command">
        <option value="">All Commands</option>
        <xsl:for-each select="Form/CommandList/Command">
          <option value="{.}">
            <xsl:if test=". = /Root/Params/Command">
              <xsl:attribute name="selected">selected</xsl:attribute>
            </xsl:if>
            <xsl:value-of select="." />
          </option>
        </xsl:for-each>
      </select>
      <select name="AccessLevel">
        <option value="">All Access Levels</option>
        <xsl:for-each select="Form/AccessLevelList/AccessLevel">
          <option value="{AccessLevelID}">
            <xsl:if test="AccessLevelID = /Root/Params/AccessLevel">
              <xsl:attribute name="selected">selected</xsl:attribute>
            </xsl:if>
            <xsl:value-of select="Name" />
          </option>
        </xsl:for-each>
      </select>
      <input type="submit" value="Apply Filter" />
      <input type="button" value="Clear" onClick="location.href='permission'" />
    </form>
  </xsl:template>

  <xsl:template name="permissionShow">
    <div class="section">
      <xsl:call-template name="successMessageGeneral">
        <xsl:with-param name="successMessage">Permissions successfully updated!</xsl:with-param>
      </xsl:call-template>
      <xsl:call-template name="permissionList" />
    </div>
  </xsl:template>

  <xsl:template name="permissionList">
    <table class="textChart rowstyle-row1">
    	<xsl:choose>
				<xsl:when test="Form/PermissionList/Permission">
					<xsl:variable name="filterParams">?c=show<xsl:for-each select="Params/*[name() != 'SortBy' and name() != 'c']">&amp;<xsl:value-of select="name()" />=<xsl:value-of select="." /></xsl:for-each>
					</xsl:variable>
      		<tr>
        		<th class="sortable-text">
        			<a href="{$filterParams}&amp;SortBy=0">Application</a>
        		</th>
        		<th class="sortable-text">
        			<a href="{$filterParams}&amp;SortBy=1">Area</a>
        		</th>
        		<th class="sortable-text">
        			<a href="{$filterParams}&amp;SortBy=2">Command</a>
        		</th>
        		<th class="sortable-text">
        			<a href="{$filterParams}&amp;SortBy=3">Access Level</a>
        		</th>
        		<th class="icon">
        			Enabled?
        		</th>
      		</tr>
      		<xsl:for-each select="Form/PermissionList/Permission">
        		<xsl:call-template name="permissionRow" />
      		</xsl:for-each>
      	</xsl:when>
      	<xsl:when test="not(Form/PermissionList/Permission) and Params/App">
        	<tr>
          	<td class="emptyMessage">No permissions meet this criteria.</td>
        	</tr>
      	</xsl:when>
      	<xsl:otherwise>
					<tr>
          	<td class="emptyMessage">Please select from the options above to view and edit permissions.</td>
        	</tr> 	     	
      	</xsl:otherwise>
      </xsl:choose>
    </table>
  </xsl:template>

  <xsl:template name="permissionRow">
    <tr class="row{position() mod 2}">
      <td><xsl:value-of select="Application" /></td>
      <td><xsl:value-of select="Area" /></td>
      <td><xsl:value-of select="Command" /></td>
      <td>
        <xsl:variable name="accessLevelID" select="AccessLevelID" />
        <xsl:value-of select="/Root/Form/AccessLevelList/AccessLevel[AccessLevelID = $accessLevelID]/Name" />
      </td>
      <td class="icon">
        <input type="checkbox" name="Trigger_Form_PermissionList_Permission_{@k}_Enabled" onClick="checkCheckBox(this,'Form_PermissionList_Permission_{@k}_Enabled')">
          <xsl:if test="Enabled = 1">
            <xsl:attribute name="checked">checked</xsl:attribute>
          </xsl:if>
        </input>
        <input type="hidden" name="Form_PermissionList_Permission_{@k}_Enabled" id="Form_PermissionList_Permission_{@k}_Enabled" value="{Enabled}" />
      </td>
    </tr>
  </xsl:template>  

</xsl:stylesheet>
