<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:qdf="http://services.chronos.org/qdf/QDF" version="1.1">
    <xsl:output method="html" indent="yes"/>
    <!-- Pull in our configured base URL -->
    <xsl:variable name="baseURL">
        <xsl:value-of select="document('xslt/stylesheetConfig.xml')/config/baseURL"/>
    </xsl:variable>
    <xsl:variable name="typesDoc">
        <xsl:text>types.xml</xsl:text>
    </xsl:variable>
    <!-- Pulls in our stylesheets and scripts and then sets up our content div  -->
    <xsl:template match="/">
        <link media="screen" rel="stylesheet" href="{$baseURL}css/form.css" type="text/css"/>
        <script language="javascript" type="text/javascript" src="{$baseURL}scripts/dhtml.js"/>
        <script language="javascript" type="text/javascript" src="{$baseURL}scripts/form.js"/>
        <script type="text/javascript">
            addEvent(window, 'load', function() { 
                var input, textarea;
                var inputs = document.getElementsByTagName('input');
                for (var i = 0; (input = inputs[i]); i++) {
                    addEvent(input, 'focus', oninputfocus);
                    addEvent(input, 'blur', oninputblur);
                }
                
                var textareas = document.getElementsByTagName('textarea');
                for (var i = 0; (textarea = textareas[i]); i++) {
                    addEvent(textarea, 'focus', oninputfocus);
                    addEvent(textarea, 'blur', oninputblur);
                }
            });
        </script>
        <div id="qdf-forms">
            <xsl:apply-templates/>
            <!-- match any queries -->
            <iframe id="results" name="results"/>
        </div>
        <div id="qdf-tools" style="text-align: right">
            <span id="directLinkSpan" style="display: none">
                <a href="javascript:alert('No query performed yet!');"
                    id="directLink">Direct Link</a>
            </span>
        </div>
        <div id="legend">
            <b>Legend:</b> Required fields are marked with a <img
            src="{$baseURL}images/required.gif"/>. When a field is complete it
            will be marked with a <img src="{$baseURL}images/complete.gif"/>. </div>
        <script language="javascript" type="text/javascript" src="{$baseURL}scripts/tooltip.js"/>
    </xsl:template>
    <!-- Match any queries in the document so we can generate forms for them -->
    <xsl:template match="query">
        <form action="{$baseURL}query/{id}" method="POST" name="{id}" id="{id}" onsubmit="return validate(this);">
            <span class="title">
                <xsl:value-of select="title"/>
            </span>
            <p>
                <xsl:value-of select="normalize-space(description)"/>
            </p>
            <!-- Handle our inputs -->
            <xsl:choose>
                <xsl:when test="count(input/param) &gt; 0">
                    <div class="input-block">
                        <xsl:for-each select="input/param[@required = 'true']">
                            <xsl:call-template name="input-param"/>
                        </xsl:for-each>
                        <xsl:for-each select="input/param[@required != 'true']">
                            <xsl:call-template name="input-param"/>
                        </xsl:for-each>
                    </div>
                </xsl:when>
                <xsl:otherwise>
                    <p style="font-style: italic">This query takes no inputs.</p>
                </xsl:otherwise>
            </xsl:choose>
            <!-- handle the 'Get Results As' drop down list -->
            <label for="serializeAs">Get Results As </label>
            <select name="serializeAs" id="serializeAs">
                <xsl:for-each select="serializers/serializer">
                    <option name="{.}" value="{.}">
                        <xsl:value-of select="."/>
                    </option>
                </xsl:for-each>
            </select>
            <input type="submit" name="submit" id="submit"/>
            <!-- Handle our outputs -->
            <xsl:if test="count(output/param[@required != 'true']) &gt; 0">
                <span class="customize"
                    onclick="setPropertyById('{generate-id(.)}', 'display', 'block')">Customize outputs...</span>
                <div class="output-block" id="{generate-id(.)}">
                    <p>Use the checkboxes below to select which outputs you
                        would like.</p>
                    <xsl:for-each select="output/param[@required != 'true']">
                        <xsl:call-template name="output-param"/>
                    </xsl:for-each>
                </div>
            </xsl:if>
        </form>
    </xsl:template>
    <!-- 
            Supporting templates 
    -->
    <!-- Input parameters template -->
    <xsl:template name="input-param" match="param">
        <!-- some useful variables -->
        <xsl:variable name="check">
            <xsl:if test="@required = 'true'">checkMe(this)</xsl:if>
        </xsl:variable>
        <xsl:variable name="req">
            <xsl:choose>
                <xsl:when test="@required = 'true'">required</xsl:when>
                <xsl:otherwise>optional</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="type">
            <xsl:value-of select="normalize-space(@type)"/>
        </xsl:variable>
        <xsl:variable name="id">
            <xsl:value-of select="generate-id(.)"/>
        </xsl:variable>
        <!-- this builds up our tooltip -->
        <xsl:variable name="desc">
            <xsl:text>&lt;b&gt;Description:&lt;/b&gt;&lt;br/&gt;</xsl:text>
            <xsl:value-of select="normalize-space(.)"/>
            <xsl:text>&lt;br/&gt; &lt;br/&gt;
                &lt;b&gt;Type:&lt;/b&gt; </xsl:text>
            <xsl:value-of select="$type"/>
            <xsl:text>&lt;br/&gt;</xsl:text>
            <xsl:value-of select="normalize-space(document($typesDoc)/types/type[@name = $type]/description)"/>
        </xsl:variable>
        <!-- this gets kinda complex -->
        <div class="field">
            <span onmouseover="return escape('{normalize-space($desc)}');">
                <label for="{$id}" class="{$req}">
                    <xsl:value-of select="@label"/>
                </label>
                <xsl:choose>
                    <!-- want to choose based on what's in the types document -->
                    <xsl:when test="document($typesDoc)/types/type[@name = $type]/set/value">
                        <!-- the types defined a set with values so pull in all the values for a drop down -->
                        <select name="{@name}" id="{$id}" onchange="setLabelClass('{$id}', 'completed')">
                            <option name="" value="">Select...</option>
                            <xsl:for-each select="document($typesDoc)/types/type[@name = $type]/set/value">
                                <option name="{.}" value="{.}">
                                    <xsl:value-of select="."/>
                                </option>
                            </xsl:for-each>
                            <xsl:if test="not(@required = 'true')">
                                <option name="" value=""/>
                            </xsl:if>
                        </select>
                    </xsl:when>
                    <xsl:when test="document($typesDoc)/types/type[@name = $type]/set[not(@url = '')]">
                        <!-- the types defined a set with a url so pull in all the values that match nodeName for a drop down -->
                        <xsl:variable name="url">
                            <xsl:value-of select="document($typesDoc)/types/type[@name = $type]/set/@url"/>
                        </xsl:variable>
                        <xsl:variable name="xpath">
                            <xsl:value-of select="document($typesDoc)/types/type[@name = $type]/set/@nodeName"/>
                        </xsl:variable>
                        <select name="{@name}" id="{$id}" onchange="setLabelClass('{$id}', 'completed');">
                            <option>Select...</option>
                            <xsl:for-each select="document($url)//*[local-name() = $xpath]">
                                <option name="{.}" value="{.}">
                                    <xsl:value-of select="."/>
                                </option>
                            </xsl:for-each>
                            <xsl:if test="not(@required = 'true')">
                                <option name="" value=""/>
                            </xsl:if>
                        </select>
                    </xsl:when>
                    <xsl:when test="document($typesDoc)/types/type[@name = $type]/examples/value">
                        <!-- the types defined some examples so pull in all the values for a drop down -->
                        <input type="text" name="{@name}" id="{$id}"
                            class="{$req}" onblur="{$check}"
                            onchange="setLabelClass('{$id}', 'completed');" style="margin-right: 5px;"/>
                        <select name="{@name}-example" id="{$id}-example" onchange="document.getElementById('{$id}').value = this.options[this.selectedIndex].value; setLabelClass('{$id}', 'completed');">
                            <option>Examples...</option>
                            <xsl:for-each select="document($typesDoc)/types/type[@name = $type]/examples/value">
                                <option name="{.}" value="{.}">
                                    <xsl:value-of select="."/>
                                </option>
                            </xsl:for-each>
                            <xsl:if test="not(@required = 'true')">
                                <option name="" value=""/>
                            </xsl:if>
                        </select>
                    </xsl:when>
                    <xsl:when test="$type = 'QDFOutputParams'">
                        <!-- the types defined a set with values so pull in all the values for a drop down -->
                        <select name="{@name}" id="{$id}" onchange="setLabelClass('{$id}', 'completed')">
                            <option name="" value="">Select...</option>
                            <xsl:for-each select="../../output/param">
                                <option name="{@name}" value="{@name}">
                                    <xsl:value-of select="@label"/>
                                </option>
                            </xsl:for-each>
                        </select>
                    </xsl:when>
                    <xsl:otherwise>
                        <!-- nothing defined in the types so the user is on his own -->
                        <input type="text" name="{@name}" id="{@name}"
                            class="{$req}" onblur="{$check}" onchange="setLabelClass('{$id}', 'completed');"/>
                    </xsl:otherwise>
                </xsl:choose>
            </span>
        </div>
    </xsl:template>
    <!-- Output parameters template -->
    <xsl:template name="output-param" match="param">
        <!-- some useful variables -->
        <xsl:variable name="check">
            <xsl:if test="@required='true'">this.checked ='true'</xsl:if>
        </xsl:variable>
        <xsl:variable name="req">
            <xsl:choose>
                <xsl:when test="@required = 'true'">required</xsl:when>
                <xsl:otherwise>optional</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="type">
            <xsl:value-of select="normalize-space(@type)"/>
        </xsl:variable>
        <!-- this builds up our tooltip -->
        <xsl:variable name="desc">
            <xsl:text>&lt;b&gt;Description:&lt;/b&gt;&lt;br/&gt;</xsl:text>
            <xsl:value-of select="normalize-space(.)"/>
            <xsl:text>&lt;br/&gt; &lt;br/&gt;
                &lt;b&gt;Type:&lt;/b&gt; </xsl:text>
            <xsl:value-of select="$type"/>
            <xsl:text>&lt;br/&gt;</xsl:text>
            <xsl:value-of select="normalize-space(document($typesDoc)/types/type[@name = $type]/description)"/>
        </xsl:variable>
        <div class="field">
            <span onmouseover="return escape('{normalize-space($desc)}');">
                <label for="{@name}">
                    <xsl:value-of select="@label"/>
                </label>
                <input type="checkbox" checked="checked" name="{@name}"
                    id="{@name}" value="{@name}" class="{$req}" onclick="{$check}"/>
            </span>
        </div>
    </xsl:template>
</xsl:stylesheet>
