<!--                                                                  -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:include href="/app/tools/support/templates/layout.xsl" />

<xsl:template name="mainBody" >
    <style type="text/css" media="screen">
    	@import url(/production/css/rps_main.css?v=8);
        .smallInputField {width: 80px;}
    </style>
    <script type="text/javascript" src="/production/javascript/supportJobRule.js?v=2" />
    <xsl:call-template name="dynamicClientMenu" />
    <xsl:call-template name="bodyTitle" >
        <xsl:with-param name="title">Job Rules</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="displayRunRules" />
    <br />
    <xsl:call-template name="displaySkipRules" />
    <br />
    <!---
    <xsl:call-template name="displayHostNameFilter" />
    <br />
    -->
    <xsl:call-template name="displayHostClasses" />
</xsl:template>

<xsl:template name="dynamicClientMenu" >
    <script type="text/javascript">
        function createClientMenu() {
            var menu = document.createElement("select");
            menu.add(new Option("All", 0, true, true), null);
            <xsl:for-each select="/Root/Clients/Client">
            <xsl:sort select="ClientName" />
                <xsl:variable name="clientID" select="ClientID" />
            menu.add( new Option(
                "<xsl:value-of select="ClientName" />",
                <xsl:value-of select="ClientID" />,
                false,
                false), null
            );

            </xsl:for-each>
            return menu;
        }
    </script>
</xsl:template>

<xsl:template name="displaySkipRules" >
    <xsl:variable name="skipRuleCount" select="count(/Root/SkipRuleForm/SkipRules/SkipRule)" />
    <div class="section">
		<fieldset><legend>Skip Rules</legend>
		<form name="skipRules" id="skipRules" action="/support/job_queue" method="get">
            <input type="hidden" name="c" value="skip" />
            <table class="textChart" id="skipRuleTable">
                <tr>
                    <th>Client</th>
                    <th>Class</th>
                    <th>Subclass</th>
                    <th>Delete</th>
                </tr>
                <xsl:for-each select="/Root/SkipRuleForm/SkipRules/SkipRule">
                <xsl:sort select="JobSkipRuleID" />
                    <xsl:call-template name="skipRuleRow" />
                </xsl:for-each>
            </table>
            <xsl:variable name="addSkipRuleLink">
                <xsl:choose>
                    <xsl:when test="$skipRuleCount = 0">javascript:addSkipRule(1)</xsl:when>
                    <xsl:otherwise>javascript:addSkipRule('<xsl:value-of select="/Root/SkipRuleForm/SkipRules/SkipRule/@k" />')</xsl:otherwise>
                </xsl:choose>
            </xsl:variable>

            <div class="insideFieldset">
                <p class="addLink">
                    <a href="{$addSkipRuleLink}" title="Add Another Rule" id="addSkipImageLink">
                        <img src="/production/images/icons/office/Add-(16x16).gif" height="16" width="16" />
                    </a> <a href="{$addSkipRuleLink}" id="addSkipRuleLink">Add Another Rule</a>
                </p>
                <input type="submit" value="Save" />
            </div>
        </form>
        </fieldset>
    </div>
</xsl:template>

<xsl:template name="skipRuleRow">
    <xsl:variable name="rulePosition">
    	<xsl:choose>
    		<xsl:when test="@k"><xsl:value-of select="@k" /></xsl:when>
    		<xsl:otherwise>NEW0</xsl:otherwise>
    	</xsl:choose>
    </xsl:variable>
    <xsl:variable name="ruleRowID">skipRule_<xsl:value-of select="$rulePosition" /></xsl:variable>
    <xsl:variable name="rulePrefix">SkipRuleForm_SkipRules_SkipRule_<xsl:value-of select="$rulePosition" />_</xsl:variable>
    <tr class="row{position() mod 2}" id="{$ruleRowID}">
        <xsl:variable name="skipRuleID" select="JobSkipRuleID" />
        <xsl:variable name="host" select="/Root/Params/host" />
        <xsl:variable name="ruleClientID" select="ClientID" />
        <td>
            <select name="SkipRuleForm_SkipRules_SkipRule_{$skipRuleID}_ClientID" id="SkipRuleForm_SkipRules_SkipRule_{$skipRuleID}_ClientID">
                <option value="0">All</option>
                <xsl:for-each select="/Root/Clients/Client">
                    <xsl:sort select="ClientName" />
                    <xsl:variable name="clientID" select="ClientID" />
                    <option value="{$clientID}">
					    <xsl:if test="$ruleClientID = $clientID">
                            <xsl:attribute name="selected">selected</xsl:attribute>
                        </xsl:if>
                        <xsl:value-of select="ClientName" />
                    </option>
                </xsl:for-each>
             </select>
        </td>
		<td><input type="text" class="classField" name="SkipRuleForm_SkipRules_SkipRule_{$skipRuleID}_Class" id="SkipRuleForm_SkipRules_SkipRule_{$skipRuleID}_Class" value="{Class}" /></td>
		<td><input type="text" class="classField" name="SkipRuleForm_SkipRules_SkipRule_{$skipRuleID}_Subclass" id="SkipRuleForm_SkipRules_SkipRule_{$skipRuleID}_Subclass" value="{Subclass}" /></td>
        <td><input type="checkbox" name="SkipRuleForm_SkipRules_SkipRule_{$skipRuleID}_Delete" id="SkipRuleForm_SkipRules_SkipRule_{$skipRuleID}_Delete" /></td>
	</tr>
</xsl:template>

<xsl:template name="displayHostNameFilter" >
    <xsl:variable name="host" select="/Root/Params/host" />
        <div class="section">
		<fieldset>
		<form name="hostFilter" id="hostFilter" action="/support/job_queue" method="get">
            <table>
                <tr>
                    <td>
                        Hostname filter:  <input type="text" name="host" value="{$host}" /> <input type="submit" value="Choose" /></td>
                </tr>
            </table>
            <input type="hidden" name="c" value="rules" />
        </form>
        </fieldset>
        </div>
</xsl:template>

<xsl:template name="displayRunRules" >
    <xsl:variable name="host" select="/Root/Params/host" />
    <xsl:variable name="runRuleCount" select="count(/Root/RunRuleForm/JobRules/JobRule)" />
    <div class="section">
		<fieldset><legend>Run Rules</legend>
		<form name="runRules" id="runRules" action="/support/job_queue" method="post">
            <input type="hidden" name="c" value="run" />
            <input type="hidden" name="host" value="{$host}" />
            <table class="textChart" id="runRuleTable">
                <tr>
                    <th>Host Class</th>
                    <th>Client</th>
                    <th>Class</th>
                    <th>Subclass</th>
                    <th>Client Max</th>
                    <th>Global Max</th>
                    <th>Local Max</th>
                    <th>Delete</th>
                </tr>
                <xsl:for-each select="/Root/RunRuleForm/JobRules/JobRule">
                    <xsl:sort select="Hostname" />
                    <xsl:sort select="Class" />
                    <xsl:sort select="Subclass" />
                    <xsl:call-template name="runRuleRow" />
                </xsl:for-each>
            </table>
            <xsl:variable name="addRunRuleLink">
                <xsl:choose>
                    <xsl:when test="$runRuleCount = 0">javascript:addRunRule(1)</xsl:when>
                    <xsl:otherwise>javascript:addRunRule('<xsl:value-of select="/Root/RunRuleForm/JobRules/JobRule/@k" />')</xsl:otherwise>
                </xsl:choose>
            </xsl:variable>

            <div class="insideFieldset">
                <p class="addLink">
                    <a href="{$addRunRuleLink}" title="Add Another Rule" id="addRunImageLink">
                        <img src="/production/images/icons/office/Add-(16x16).gif" height="16" width="16" />
                    </a> <a href="{$addRunRuleLink}" id="addRunRuleLink">Add Another Rule</a>
                </p>
                <input type="submit" value="Save" />
            </div>
        </form>
        </fieldset>
    </div>
</xsl:template>

<xsl:template name="runRuleRow">
    <xsl:variable name="rulePosition">
    	<xsl:choose>
    		<xsl:when test="@k"><xsl:value-of select="@k" /></xsl:when>
    		<xsl:otherwise>NEW0</xsl:otherwise>
    	</xsl:choose>
    </xsl:variable>
    <xsl:variable name="ruleRowID">runRule_<xsl:value-of select="$rulePosition" /></xsl:variable>
    <xsl:variable name="rulePrefix">RunRuleForm_JobRules_JobRule_<xsl:value-of select="$rulePosition" />_</xsl:variable>
    <tr class="row{position() mod 2}" id="{$ruleRowID}">
        <xsl:variable name="runRuleID" select="JobRunRuleID" />
        <xsl:variable name="host" select="/Root/Params/host" />
        <xsl:variable name="ruleClientID" select="ClientID" />
		<td><input type="text" class="classField smallInputField" name="RunRuleForm_JobRules_JobRule_{$runRuleID}_Hostname" id="RunRuleForm_JobRules_JobRule_{$runRuleID}_Hostname" value="{Hostname}" /></td>
        <td>
            <select name="RunRuleForm_JobRules_JobRule_{$runRuleID}_ClientID" id="RunRuleForm_JobRules_JobRule_{$runRuleID}_ClientID">
                <option value="0">All</option>
                <xsl:for-each select="/Root/Clients/Client">
                    <xsl:sort select="ClientName" />
                    <xsl:variable name="clientID" select="ClientID" />
                    <option value="{$clientID}">
					    <xsl:if test="$ruleClientID = $clientID">
                            <xsl:attribute name="selected">selected</xsl:attribute>
                        </xsl:if>
                        <xsl:value-of select="ClientName" />
                    </option>
                </xsl:for-each>
             </select>
        </td>
		<td><input type="text" class="classField" name="RunRuleForm_JobRules_JobRule_{$runRuleID}_Class" id="RunRuleForm_JobRules_JobRule_{$runRuleID}_Class" value="{Class}" /></td>
		<td><input type="text" class="classField" name="RunRuleForm_JobRules_JobRule_{$runRuleID}_Subclass" id="RunRuleForm_JobRules_JobRule_{$runRuleID}_Subclass" value="{Subclass}" /></td>
		<td><input type="text" class="sourceField smallInputField" name="RunRuleForm_JobRules_JobRule_{$runRuleID}_ClientMax" id="RunRuleForm_JobRules_JobRule_{$runRuleID}_ClientMax" value="{ClientMax}" /></td>
		<td><input type="text" class="sourceField smallInputField" name="RunRuleForm_JobRules_JobRule_{$runRuleID}_GlobalMax" id="RunRuleForm_JobRules_JobRule_{$runRuleID}_GlobalMax" value="{GlobalMax}" /></td>
		<td><input type="text" class="sourceField smallInputField" name="RunRuleForm_JobRules_JobRule_{$runRuleID}_LocalMax" id="RunRuleForm_JobRules_JobRule_{$runRuleID}_LocalMax" value="{LocalMax}" /></td>
        <td><input type="checkbox" name="RunRuleForm_JobRules_JobRule_{$runRuleID}_Delete" id="RunRuleForm_JobRules_JobRule_{$runRuleID}_Delete" /></td>
	</tr>
</xsl:template>



<xsl:template name="displayHostClasses" >
    <xsl:variable name="host" select="/Root/Params/host" />
    <xsl:variable name="hostClassCount" select="count(/Root/HostClassForm/HostClasses/JobHostClass)" />
    <div class="section">
		<fieldset><legend>Host Classes</legend>
		<form name="hostClasses" id="hostClasses" action="/support/job_queue" method="get">
            <input type="hidden" name="c" value="host_class" />
            <input type="hidden" name="host" value="{$host}" />
            <table class="textChart" id="hostClassTable">
                <tr>
                    <th>Hostname</th>
                    <th>Class</th>
                    <th>Delete</th>
                </tr>
                <xsl:for-each select="/Root/HostClassForm/HostClasses/JobHostClass">
                    <xsl:call-template name="hostClassRow" />
                </xsl:for-each>
            </table>
            <xsl:variable name="addHostClassLink">
                <xsl:choose>
                    <xsl:when test="$hostClassCount = 0">javascript:addHostClass(1)</xsl:when>
                    <xsl:otherwise>javascript:addHostClass('<xsl:value-of select="/Root/HostClassForm/HostClasses/JobHostClass/@k" />')</xsl:otherwise>
                </xsl:choose>
            </xsl:variable>

            <div class="insideFieldset">
                <p class="addLink">
                    <a href="{$addHostClassLink}" title="Add Another Host Class" id="addHostClassImageLink">
                        <img src="/production/images/icons/office/Add-(16x16).gif" height="16" width="16" />
                    </a> <a href="{$addHostClassLink}" id="addHostClassLink">Add Another Host Class</a>
                </p>
                <input type="submit" value="Save" />
            </div>
        </form>
        </fieldset>
    </div>
</xsl:template>

<xsl:template name="hostClassRow">
    <xsl:variable name="hostClassPosition">
    	<xsl:choose>
    		<xsl:when test="@k"><xsl:value-of select="@k" /></xsl:when>
    		<xsl:otherwise>NEW0</xsl:otherwise>
    	</xsl:choose>
    </xsl:variable>
    <xsl:variable name="hostClassRowID">hostClass_<xsl:value-of select="$hostClassPosition" /></xsl:variable>
    <xsl:variable name="hostClassPrefix">HostClassForm_HostClasses_JobHostClass_<xsl:value-of select="$hostClassPosition" />_</xsl:variable>
    <tr class="row{position() mod 2}" id="{$hostClassRowID}">
        <xsl:variable name="hostClassID" select="JobHostClassID" />
        <xsl:variable name="host" select="/Root/Params/host" />
		<td><input type="text" class="classField" name="HostClassForm_HostClasses_JobHostClass_{$hostClassID}_Hostname" id="HostClassForm_HostClasses_JobHostClass_{$hostClassID}_Hostname" value="{Hostname}" /></td>
		<td><input type="text" class="classField" name="HostClassForm_HostClasses_JobHostClass_{$hostClassID}_Class" id="HostClassForm_HostClasses_JobHostClass_{$hostClassID}_Class" value="{Class}" /></td>
        <td><input type="checkbox" name="HostClassForm_HostClasses_JobHostClass_{$hostClassID}_Delete" id="HostClassForm_HostClasses_JobHostClass_{$hostClassID}_Delete" /></td>
	</tr>
</xsl:template>

	<xsl:template name="clientIDToName">
		<xsl:param name="clientID" />
        <xsl:value-of select="/Root/Clients/Client[ClientID = $clientID]/ClientName" />
	</xsl:template>
</xsl:stylesheet>
