<%@ Import Namespace="System.Data" %>
<%@ Register TagPrefix="uc1" TagName="Trailer" Src="Trailer.ascx" %>
<%@ Register TagPrefix="uc2" TagName="Header" Src="top.ascx" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="edu.jhu.Cas.Jobs" %>
<%@ Import Namespace="edu.jhu.Cas.Web" %>
<%@ Import Namespace="edu.jhu.Cas.Util" %>
<%@ Import Namespace="System.Web" %>

<%@ Page Language="c#" ValidateRequest="false" AutoEventWireup="false" Inherits="edu.jhu.Cas.Web.SubmitJob"
    EnableViewState="true" EnableViewStateMac="true" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Pragma" content="no-cache" />
    <title>CasJobs</title>
    <link href="cjobs.css" rel="stylesheet" />
    <script type="text/javascript" src="scripts/submitjob.js"></script>
    <script type="text/javascript" src="scripts/shortcut.js"></script>
    <script type="text/javascript" language="JavaScript">
        var queryIsRunning = false;
        var editor;
        var samplePreview;
        var cm;

        function customMouseOut(element, event) {
          var currentTarget = null;
          if (event.toElement) {
            currentTarget = event.toElement;
          } else if (event.relatedTarget) {
            currentTarget = event.relatedTarget;
          }
          if (currentTarget == element) {
            hideqsnode();
          }
        }

        function completeQuery() {
            if (queryIsRunning) {
                document.getElementById('message').className = 'happy';
                document.getElementById('message').innerHTML = 'Query complete!';
                showExecButtons();
                setEditorMid();
                queryIsRunning = false;
            }
        }
        function trmenu() {
            if (window.frames['resultPane'].document.body.innerHTML.length < 15)
                document.getElementById('menuRow').style.display = 'none';
            else {
                document.getElementById('menuRow').style.display = '';
                setEditorMid();
            }
        }
        function onSubmitQueryLoad() {
            initEditor();
            initSamplePreview();
            resubmit = document.location.href.indexOf("resubmit") > -1 && document.getElementById('rsqry').innerHTML.length > 0; // || document.location.href.indexOf("sql") > -1;
            loadSubmitPage();
            window.onunload = saveSubmitPage;
            if (document.getElementById('samples').style.display == 'block')
                document.getElementById('sampCheck').checked = true;
            editor.focus();
            initHist();
            window.onresize = windowResize;
            setSamples();
            initShortCuts();
        }
        function initShortCuts() {
            shortcut.add("Ctrl+H", setEditorMid);
            shortcut.add("Ctrl+J", maxQuery);
            shortcut.add("Ctrl+K", maxResults);
            shortcut.add("Ctrl+S", checksyntax);
            shortcut.add("Ctrl+Q", submitQuickQuery);
            shortcut.add("Ctrl+U", submitQuery);
            shortcut.add("Ctrl+P", getplan);
            shortcut.add("Enter", reindent, {
                'propagate': true
            });           
        }
        function reindent() {
            editor.indentLine(editor.getCursor().line); 
        }
        function setSamples() {
            var tddl = document.getElementById('targetDDL');
            var context = tddl[tddl.selectedIndex].value;
            var tmp = document.getElementById(context.toLowerCase() + "_samples");          
            if (!tmp)
                tmp = document.getElementById("global_samples");
            document.getElementById('samples').innerHTML = tmp.innerHTML;
        }
        function setContext() {
          var tddl = document.getElementById('targetDDL');
          var context = tddl[tddl.selectedIndex].value;
          var e = document.getElementById('mydbLink');
          e.setAttribute('href', 'MyDB.aspx?context=' + context);
          e.innerHTML = context;
        }
        var lastQry;
        var rsThread = null;
        var cm;
        function windowResize() {
            if (rsThread) {
                clearTimeout(rsThread);
            } else {
                lastQry = editor.getValue();
                editor.setValue('');
                cm.style.height = '100%';
                cm.style.width = 'auto';
            }
            rsThread = setTimeout(completeResize, 500);
        }
        function completeResize() {
            var osh = cm.offsetHeight;
            var osw = cm.offsetWidth;
            cm.style.height = osh;
            cm.style.width = osw;
            editor.setValue(lastQry);
            lastQry = null;
            rsThread = null;
        }
        function initEditor() {
            var qc = document.getElementById('queryCell');
            editor = CodeMirror(qc, {
                lineNumbers: true,
                matchBrackets: true,
                onCursorActivity: hfloats,
                //tabMode: "shift",
                tabMode: "indent",
                mode: "text/x-tsql"
            });
            cm = qc.childNodes[0];
            if (!cm.style) cm = qc.childNodes[1];
            cm.style.height = '100%';
            cm.style.width = 'auto';
            cm.style.padding = '0 0 0 0';
            cm.style.margin = '0 0 0 0';
            cm.style.overflow = 'hidden';
            cm.style.border = 'none';
            windowResize();
            //cm.style.height = cm.offsetHeight - 5;
        }
        function hfloats() {
            hideFloats();
        }
        function initSamplePreview() {
            var qc = document.getElementById('samplePreviewPane');
            samplePreview = CodeMirror(qc, {
                lineNumbers: false,
                matchBrackets: false,
                readOnly: true,
                mode: "text/x-tsql"
            });
        }
    </script>
    <style type="text/css">
        table
        {
            padding: 0 0 0 0;
            margin: 0 0 0 0;
        }
    </style>
    <script src="scripts/codemirror.js"></script>
    <script src="scripts/tsql.js"></script>
</head>
<body style="padding: 0 0 0 0; margin: 0 0 0 0" onload="onSubmitQueryLoad();">
    
    <form name="SubmitJob" id="SubmitJob" action="SubmitJob.aspx" method="post" target="_top"
    style="padding: 0 0 0 0; margin: 0 0 0 0">
    <textarea id="rsqry" style="display:none"><%=GetQuery()%></textarea>
    <input type="hidden" id="queue" name="queue" value="1" style="display: none;" />
    <table cellspacing="0" cellpadding="0" id="mostEverything" style="width: 100%; height: 100%">
        <tr>
            <td>
                <uc2:Header ID="Header" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                <table style="width: 100%">
                    <tr>
                        <th class="mover">
                            <a href="#" onmouseover="showqsnode('contextHelpDiv');" onmouseout="hideqsnode();">Context</a>
                        </th>
                        <th class="mover" style="white-space: nowrap">
                            <a href="#" onmouseover="showqsnode('tableHelpDiv');" onmouseout="hideqsnode();">Table
                                <span style="font-weight: normal" size="-2">(optional)</span></a>
                        </th>
                        <th class="mover" style="width: 100%">
                            <a href="#" onmouseover="showqsnode('taskNameHelpDiv');" onmouseout="hideqsnode();">
                                Task Name</a>
                        </th>
                    </tr>
                    <tr>
                        <%if (SYSTEM.ENABLE_ALT_SOURCES) { %>
                        <td align="center">
                            <select style="width: 100%" name="type" id="typeDDL">
                                <option>Classic</option>
                                <option>Cube</option>
                            </select>
                        </td>
                        <%} %>
                        <td align="center">
                            <select name="targest" id="targetDDL" onchange="setSamples();" onmouseover="setContext();showqsnode('contextDescrHelpDiv');">
                                <%foreach (string s in sList) {%>
                                <option <%=targisselected(s)%>>
                                    <%=s%>
                                </option>
                                <%}%>
                            </select>
                        </td>
                        <td align="center" style="width: 100px">
                            <input type="text" style="width: 100%" id="table" name="table" value="<%=GetUniqueTableName()%>" />
                        </td>
                        <td align="center">
                            <input type="text" name="taskname" style="width: 100%" value="<%=getTaskName()%>" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <table id="querypane" cellspacing="1" cellpadding="0" class="bar" style="padding: 0 0 0 0;
                    width: 100%;">
                    <tr>
                        <td style="text-align: center;">
                            <input id="sampCheck" type="button" value="Samples" onclick="toggleSamples();" />
                        </td>
                        <td style="text-align: center;">
                            <input id="histCheck" type="button" value="Recent" onclick="toggleHist();" />
                        </td>
                        <td style="text-align: center;">
                            <input id="clearButton" type="button" onclick="clearQry();" value="Clear" />
                        </td>
                        <td>
                        </td>
                        <td style="padding: 0 5 0 5; width: 25;">
                            <div style="white-space: nowrap" id="timer">
                            </div>
                        </td>
                        <td width="100%">
                            <div class="message" style="text-align: right; padding-right: 5; height: 15px; overflow: hidden;"
                                onmouseover="cjshowqsM();" onmouseout="cjhideqsM();" id="message">
                                <%=message%>
                            </div>
                            <div style="visibility: hidden; z-index: 100; position: absolute;" id="mmessage">
                            </div>
                        </td>
                        <td style="text-align: center;">
                            <input type="hidden" id="syntax" name="syntax" value="false" />
                            <input id="syntaxButton" title="Check query syntax. (CTRL-S)" type="button" value="Syntax"
                                onclick="checksyntax()" />
                        </td>
                        <td style="text-align: center;">
                            <input id="planButton" title="Show estimated query plan. (CTRL-P)" type="button" value="Plan"
                                onclick="getplan();" />
                        </td>
                        <td style="text-align: center;">
                            <input id="quickButton" title="Runs the query instantaneously.  Short duration and data limit. (CTRL-Q)"
                                value="Quick" type="button" onclick="submitQuickQuery();" />
                        </td>
                        <td style="text-align: center">
                            <input id="submitButton" value="Submit" title="Submits your query.  Longest duration. (CTRL-U)"
                                type="button" onclick="submitQuery();" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr id="queryRow">
            <td id="queryCell" height="100%" style="padding: 0 1 1 0;">
            </td>
        </tr>
        <tr id="resultsRow" style="display: none;">
            <td id="resultsCell" width="100%" height="65%">
                <iframe name="resultPane" id="resultPane" height="100%" style="height: 100%; width: 100%"
                    src="output/vodataclean.aspx" frameborder="0" onload="trmenu();" scrolling="auto">
                </iframe>
            </td>
        </tr>
        <tr id="menuRow">
            <td>
                <table class="bar" id="windowMenu" cellpadding="0" cellspacing="1" border="0" style="width: 100%;">
                    <tr>
                        <td style="font-variant: small-caps; padding: 0 10 0 10">
                            Results
                        </td>
                        <td>
                            <input type="button" value="Plot" onclick="window.open('output/voplot/loadjvt.aspx','plot','');" />
                        </td>
                        <td>
                            <input type="button" value="Save As" onclick="save();" />
                        </td>
                        <td>
                            <select id="ddl">
                                <option>HTML</option>
                                <option>CSV</option>
                                <option>VOTable</option>
                                <option>DataSet</option>
                            </select>
                        </td>
                        <td width="100%">
                            &nbsp;
                        </td>
                        <td style="padding-right: 5;">
                        </td>
                        <td style="font-variant: small-caps; padding: 0 10 0 10">
                            Display
                        </td>
                        <td>
                            <input type="button" title="CTRL-J" value="Query" onclick="maxQuery();" />
                        </td>
                        <td>
                            <input type="button" title="CTRL-K" value="Results" onclick="maxResults();" />
                        </td>
                        <td>
                            <input type="button" title="CTRL-H" value="Both" onclick="setEditorMid();" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td style="">
                <uc1:Trailer ID="Trailer1" runat="server" cvsTag="$Name$" cvsRev="$Revision: 126024 $">
                </uc1:Trailer>
            </td>
        </tr>
    </table>
    </form>
    <form name="svqry" id="Form1" action="output/svqry.aspx" method="post" target="hidden"
    style="display: none;">
    <input type="hidden" name="USQL" id="USQL" value="" />
    <input type="hidden" name="UCONTEXT" id="UCONTEXT" value="" />
    </form>
    <iframe name="hidden" style="display: none"></iframe>
    <input type="hidden" id="wsid" value="<%=wsid%>" />
    <div id="samples" style="position: absolute; display: none; width: 200; vertical-align: text-top;
        overflow: auto; z-index: 100; padding: 2 2 2 2;" class="transparent" >
    </div>
    <div id="all_samples" style="display:none;">
    <%=LoadSamples()%>
    </div>
    <div id="hist" style="position: absolute; display: none; vertical-align: text-top;
        overflow: auto; z-index: 100; padding: 2 2 2 2;" class="transparent">
    </div>
    <div style="">
        <%=LoadSamplesInfo()%>
    </div>
    <!--<div style="width:auto; border:none;" class="qpop" id="samplePreviewPane"></div>-->
    <div style="width: auto; padding: 0 0 0 0;" class="qpop" id="samplePreviewPane">
    </div>
    <div id="tableHelpDiv" class="qpopH">
        Submitted queries are required to direct their output into a table in MyDB. You
        can do this either by manually specifying 'select into' clause within your query,
        or by letting CasJobs automatically add the table in this field. Default table names
        in this field are usually unique.;
    </div>
    <div id="contextHelpDiv" class="qpopH">
        A <i>context</i> is a unique catalog of data.  Use this field to determine where your queries should run. 
        To see context overview go to MyDB page and select a context from the dropdown list on the left. You can also move the mouse over the context selection below.
    </div>
    <div id="contextDescrHelpDiv" class="qpopH" onmouseout="customMouseOut(this,event);">
        Click here for context overview:<br />
        <a id="mydbLink" href="MyDB.aspx"></a>
    </div>
    <div id="taskNameHelpDiv" class="qpopH">
        You can search your job history by taskname, so using a unique value in this field
        can help when searching for a particular job.
    </div>
    <script language="JavaScript" type="text/javascript" src="scripts/cookieMonster.js"></script>
    <script type="text/javascript" src="scripts/qspop.js"></script>
    <script type="text/javascript" src="scripts/urlcoder.js"></script>
    <script type="text/javascript" src="scripts/ajcrp.js"></script>
</body>
</html>
