<%@ Import Namespace="edu.jhu.Cas.Util" %>
<%@ Import Namespace="edu.jhu.Cas.Plot" %>
<%@ Register TagPrefix="uc1" TagName="Trailer" Src="Trailer.ascx" %>
<%@ Import Namespace="System.IO" %>

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

<!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>MyDB Info</title>
    <style type="text/css">
    #grid td{
        vertical-align:top;
    }
    #schemadiv p{
        padding: 0 0 0 0;
        margin: 0 0 0 0;
        text-align:left;
    }
    #schemadiv text{
       white-space:nowrap;
       width:100%;
    }
    #schemadiv .hideMe{
        visibility:hidden;
        display:none;
        text-align:center;
    }
    #schemadiv .showMe{
        visibility:visible;
        display:block;
        text-align:center;
    }
    .rw{
        width:78;
    }
    #status{
        position:absolute;
        z-index:100;
        top:0;
        left:0;
    }
    #statusM{
        font-variant:small-caps;
        padding:2 5 2 5;
        color:gray;
        white-space:nowrap;
    }
    </style>

    <script type="text/javascript" src="scripts/urlcoder.js"></script>

    <script type="text/javascript" src="scripts/mydb.js"></script>

</head>
<body>
    <div class="button" style="width: 100%;" id="status">
        <table cellpadding="0" cellspacing="0">
            <tr>
                <td style="width: 100%">
                </td>
                <td id="statusM">
                </td>
            </tr>
        </table>
    </div>
    <table height="100%" cellpadding="0" cellspacing="0" style="width: 100%; height: 100%;
        vertical-align: top; padding: 0 0 0 0; margin: 0 0 0 0;">
        <tr style="padding: 0 0 0 0; margin: 0 0 0 0; height: 100%;">
            <td style="padding: 0 0 0 0; margin: 0 0 0 0; vertical-align: top; height: 100%;">
                <div style="height: 100%; vertical-align: text-top; padding: 0 0 0 0; margin: 0 0 0 0;">
                    <p class="message" id="message">
                        <%=lmessage%>
                    </p>
                    <form runat="server" target="_top" id="Form1" style="padding: 0 0 0 0; margin: 0 0 0 0;
                        height: 100%">
                        <%if (ObjName == null) {%>
                        <h2>
                            <%=user%>
                            's MyDB</h2>
                        <b>
                            <%=space[0]%>
                            of
                            <%=space[1]%>
                            used</b>
                        <p class="textWidth">
                            From this page you can get various information about the contents of both your MyDB
                            and shared tables within your groups. Click the left table links to get information
                            about a specific table, such as rows, columns or size. From the table pages you
                            can also perform various table-specific tasks, such as:
                        </p>
                        <ul>
                            <li>Download a table</li>
                            <li>Mangage your group tables</li>
                            <li>Rename a table</li>
                            <li>Drop a table</li>
                        </ul>
                        <p>
                        </p>
                        <p class="textWidth">
                            <i>Sizes are approximations only.</i><br />
                            <font color="red"><i>Row counts are approximations only. For exact value run a count.</i></font><br />
                            <i>There's always some overhead, even empty MyDB's take up space.</i><br />
                            <i>Group tables do not count towards your MyDB size limit.</i>
                        </p>
                        <uc1:Trailer ID="Trailer1" runat="server" cvsTag="$Name: v3_5_18 $" cvsRev="$Revision: 1.64 $">
                        </uc1:Trailer>
                        <%}%>
                        <%else if (ObjType == CasDBObjType.TABLE || ObjType == CasDBObjType.VIEW) { %>
                        <h2>
                            <%=ObjName%>
                        </h2>
                        <%if (group) {%>
                        <font class="group">Group</font> table published by <b>
                            <%=groupInfo%>
                        </b>
                        <div>
                            <b>Description:</b></div>
                        <div>
                            <i>
                                <%=groupTableDescription%>
                            </i>
                        </div>
                        <%}%>
                        <%if (published) {%>
                        <div>
                            <font class="published">Published</font> to
                            <%for (int i = 0; i < ptgroups.Length; i++) { %>
                            <span class="group">
                                <%=ptgroups[i].Name %>
                            </span>
                            <%if (i < ptgroups.Length - 1) {%>
                            ,
                            <%}%>
                            <%}%>
                        </div>
                        <p>
                            <%=lmessage%>
                        </p>
                        <%}%>
                        <%if (pending) {%>
                        <p>
                            <font class="pending"><i>The query that created this table is still in progress</i></font></p>
                        <p>
                            <i>Refresh this page to get latest info</i></p>
                        <%}%>
                        <%if (failed) {%>
                        <div>
                            <font color="red"><i>This table was created by a failed/cancelled query.</i></font></div>
                        <%}%>
                        <p>
                            <%try { %>
                            <b>Contains ~<%=tableSpace[0]%>
                            </b>(~<%=tableSpace[1]%>)</p>
                        <%}
                          catch { } %>
                        <div id="menuItems">
                            <!----------------------------------->
                            <table id="moMenu" cellspacing="1" cellpadding="0" border="0" style="height: 20;">
                                <tr>
                                    <td>
                                        <input type="button" onclick="toggleNotes();" value="Notes" /></td>
                                    <%if (ObjName != null && (ObjType == CasDBObjType.TABLE || ObjType == CasDBObjType.VIEW)) { %>
                                    <td style="white-space: nowrap;">
                                        <input type="button" value="Sample" onclick="togglePreview();" />
                                    </td>
                                    <%} %>
                                    <%if (ObjName != null && ObjType == CasDBObjType.VIEW) { %>
                                    <td style="white-space: nowrap;">
                                        <input type="button" value="Source" onclick="toggleSource();" />
                                    </td>
                                    <%} %>
                                    <%if (isMyDB) {%>
                                    <td>
                                        <input type="button" onclick="toggleJob();" value="Job" /></td>
                                    <%}%>
                                    <%if (published) {%>
                                    <td>
                                        <input type="button" onclick="toggleSdesc();" value="Description" /></td>
                                    <%}%>
                                    <%if (!pending && isMyDB) {%>
                                    <td>
                                        <input type="button" value="Plot" onclick="togglePlot();" /></td>
                                    <td>
                                        <input type="button" value="βPlot" onclick="window.open('bp/default.aspx?tableName=<%=ObjName %>', 'bplot');" /></td>
                                    <td>
                                        <input type="button" value="Download" onclick="toggleExtract();" />
                                    </td>
                                    <%if (!group && ObjType == CasDBObjType.TABLE) { %>
                                    <td>
                                        <input type="button" value="Publish" onclick="togglePublish();" /></td>
                                    <%} %>
                                    <%if (SYSTEM.ENABLE_ASTRO) { %>
                                    <td>
                                        <input type="button" value="Neighbors" onclick="toggleNeighbors();" /></td>
                                    <%}%>
                                    <%}%>
                                    <%if (published && !group && isMyDB && ObjType == CasDBObjType.TABLE) {%>
                                    <td>
                                        <input type="button" value="Unpublish" onclick="toggleUnpublish();" /></td>
                                    <%}%>
                                    <%if (!group && !published && !pending && isMyDB && ObjType == CasDBObjType.TABLE) {%>
                                    <td>
                                        <input type="button" value="Rename" onclick="toggleRename();" /></td>
                                    <td>
                                        <input type="button" style="color: Red;" onclick="toggleDrop();" value="Drop" /></td>
                                    <%}%>
                                </tr>
                            </table>
                            <%}
                              else if (ObjType == CasDBObjType.FUNCTION) {%>
                            <h2>
                                <%=ObjName %>
                            </h2>
                            <div>
                                <input type="button" onclick="toggleNotes();" value="Notes" />
                                <input type="button" onclick="toggleSource();" value="Source" />
                                <%if (isMyDB) {%>
                                <asp:Button ID="Button9" ToolTip="Click to alter this function" OnClick="Edit" Text="Edit"
                                    runat="server" />
                                <asp:Button ID="Button10" ToolTip="Click to delete this function" OnClick="Drop"
                                    Text="Drop" runat="server" />
                                <%}%>
                            </div>
                            <%}
                              else if (ObjType == CasDBObjType.PROCEDURE) {%>
                            <h2>
                                <%=ObjName%>
                            </h2>
                            <div>
                                <input type="button" onclick="toggleNotes();" value="Notes" />
                                <input type="button" onclick="toggleSource();" value="Source" />
                                <%if (isMyDB) {%>
                                <asp:Button ID="Button7" ToolTip="Click to alter this procedure" OnClick="Edit" Text="Edit"
                                    runat="server" />
                                <asp:Button ID="Button4" ToolTip="Click to delete this procedure" OnClick="Drop"
                                    Text="Drop" runat="server" />
                                <%}%>
                            </div>
                            <%}%>
                            <%if (ObjType == CasDBObjType.TABLE || ObjType == CasDBObjType.VIEW) {%>
                        </div>
                        <%} %>
                        <!----end menu items ----->
                        <%if ((ObjType == CasDBObjType.TABLE || ObjType == CasDBObjType.VIEW) && ObjName != null && ObjName != "") {%>
                        <div>
                            <h4>
                                Table Schema <span style="font-size:9;font-weight:normal;">type [size]</span></h4>
                            <div id="schemaDiv">
                                <table id="schemaTable">
                                    <tr id="schemaRow">
                                    </tr>
                                </table>
                            </div>
                        </div>
                        <%}%>
                        <div id="notesPane" style="visibility: hidden; display: none">
                            <h4>
                                Notes</h4>
                            <p>
                                <%=GetNotesHTML() %>
                            </p>
                            <h4 style="margin: 0 0 0 0;">
                                New Note</h4>
                            <div>
                                <asp:TextBox runat="server" TextMode="MultiLine" Rows="5" Width="100%" ID="NewNoteBox" />
                            </div>
                            <div>
                                <asp:Button runat="server" Text="Add Note" OnClick="NewNote" ID="NewNoteButton" />
                            </div>
                        </div>
                        <div id="sourcePane" style="visibility: hidden; display: none">
                            <%if (ObjType == CasDBObjType.VIEW) { %>
                            <textarea name="code" class="sql:nogutter:nocontrols"><%=objSource %></textarea>
                            <%}
                              else { %>
                            <table style="width:100%" cellpadding="0" cellspacing="0">
                                <tr style="padding: 0 0 0 0; margin: 0 0 0 0;">
                                    <td style="padding: 0 0 0 0;">
                                        <textarea name="code" class="sql:nogutter:nocontrols"><%=objSource %></textarea>
                                    </td>
                                </tr>
                            </table>
                            <%}%>
                        </div>
                        <div id="previewPane" style="visibility: hidden; display: none">
                        </div>
                        <div id="jobPane" style="visibility: hidden; display: none; vertical-align: top;">
                            <%if (jobid == -1) { %>
                            <p>
                                <i>Job not available</i></p>
                            <%}
                              else if (jobid > 0) { %>
                            <iframe width="100%" name="jobFrame" id="jobFrame" onload="document.getElementById('jobPane').style.display = 'block';document.getElementById('jobFrame').style.height = window.frames['jobFrame'].document.body.offsetHeight;document.getElementById('jobPane').style.display = 'none'"
                                src="jobdetails.aspx?id=<%=jobid %>&noheaders=true" style="border-style: none;
                                height: 500; overflow: hidden; width: 100%;" frameborder="0"></iframe>
                            <%} %>
                        </div>
                        <div id="sdescBox" style="visibility: hidden; display: none">
                            <h4>
                                Table Description</h4>
                            <p>
                                <i>You can use this form to set descriptions for tables that you have published. These
                                    descriptions can be viewed by anyone who can see your shared table, however only
                                    the owner of table may modify a description.</i></p>
                            <table>
                                <tr>
                                    <th>
                                        Description:</th>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:TextBox runat="server" TextMode="MultiLine" Height="70" Columns="90" ID="publishDescriptionBox" /></td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:Button runat="server" Text="Set Description" OnClick="SetPublishDescription"
                                            ID="publishDescriptionSetButton" /></td>
                                </tr>
                            </table>
                        </div>
                        <div id="extractPane" style="visibility: hidden; display: none">
                            <h4>
                                Table Download</h4>
                            <p>
                                <i>From here you may download your table in a particular format. First choose the file
                                    format you'd like, then click 'Go'.</i>
                            </p>
                            <p>
                                <asp:DropDownList ID="extractDDL" runat="server" /></p>
                            <br />
                            <div>
                                <asp:Button ToolTip="Click to Extract" ID="Button1" OnClick="Extract" Text="Go" runat="server" />
                            </div>
                        </div>
                        <div id="neighborsPane" style="visibility: hidden; display: none">
                            <h4>
                                Neighbors Search</h4>
                            <div id="neighborsYes" style="display: none">
                                <p>
                                    <i>This form will create a new table containing the neighbors for each individual object
                                        in this table. Clicking 'Go' will create and submit a new job to the context you've
                                        selected. More conditions may be added to the search by modifying the query.</i></p>
                                <p>
                                    Search for neighbors in this context:&nbsp;
                                    <asp:DropDownList ID="targetDDL" runat="server" AutoPostBack="false" />
                                </p>
                                <ul>
                                    <li>Find neighbors within&nbsp;&nbsp;
                                        <asp:TextBox ID="nradius" runat="server" Columns="3" />
                                        &nbsp;&nbsp;arcminute(s) </li>
                                    <li>Store the results in a table called&nbsp;&nbsp;
                                        <asp:TextBox ID="nresult" runat="server" Columns="20" />
                                    </li>
                                </ul>
                                <div>
                                    <asp:Button ToolTip="Click to find neighbors" ID="Button11" OnClick="NSearch" Text="Go"
                                        runat="server" /></div>
                            </div>
                            <div id="neighborsNo" style="display: block">
                                <p>
                                    <i>If your table contains both a column named 'ra' and 'dec', you can use this form
                                        to perform a neighbors search on the objects in that table. If you already have
                                        columns that contain these coordinates, but are named something else, you can rename
                                        your columns using the 'Rename' menu. </i>
                                </p>
                            </div>
                        </div>
                        <div id="unpublishPane" style="visibility: hidden; display: none;">
                            <h4>
                                Unpublish Table</h4>
                            <p>
                                <i>From here you may depublish the current table from groups of your choosing.</i></p>
                            <br />
                            <div>
                                <asp:ListBox ID="upgroupbox" Rows="10" SelectionMode="single" runat="server" />
                            </div>
                            <p>
                                <asp:Button ToolTip="Click to UnPublish" ID="Button2" OnClick="UnPublish" Text="UnPublish"
                                    runat="server" />
                            </p>
                        </div>
                        <div id="publishPane" style="visibility: hidden; display: none;">
                            <h4>
                                Publish Table</h4>
                            <p>
                                <i>From here you can 'publish' a table to various groups of your choosing. Published
                                    tables are visible to all members of groups to which they are published. </i>
                            </p>
                            <div>
                                <%if (groups != null && groups.Length > 0) { %>
                                <asp:ListBox ID="pgroupbox" Rows="10" SelectionMode="single" runat="server" />
                                <p>
                                    <asp:Button ToolTip="Click to Publish" ID="publishButton" OnClick="Publish" Text="Publish"
                                        runat="server" />
                                </p>
                                <%}
                                  else {%>
                                <p>
                                    You are not a member of any groups.</p>
                                <%}%>
                            </div>
                        </div>
                        <div id="renamePane" style="visibility: hidden; display: none">
                            <h4>
                                Rename</h4>
                            <p>
                                <i>From this form you can rename various columns in the current table, or rename the
                                    table itself. To rename columns, change the interested values in the column listing
                                    above, then click 'Apply Column Name Changes'. To rename the current table, enter
                                    a new name then click 'Rename Table' below. </i>
                            </p>
                            <div>
                                <input type="button" title="Apply column names" onclick="changecnames('<%=ObjName%>');"
                                    value="Apply Column Name Changes" /></div>
                            <p>
                                Rename current table as:&nbsp;
                                <asp:TextBox ID="renameBox" runat="server" Columns="20" />
                                <asp:Button ID="Button3" ToolTip="Click to Rename" OnClick="Rename" Text="Rename Table"
                                    runat="server" />
                            </p>
                        </div>
                        <div id="dropPane" style="visibility: hidden; display: none">
                            <h4>
                                Drop Table</h4>
                            <p>
                                <i>This will *permanently* delete the current table. This cannot be undone.</i></p>
                            <br />
                            <div>
                                <asp:Button ID="dropButton" ToolTip="Click to Drop" OnClick="Drop" Text="Drop" runat="server" /></div>
                        </div>
                        <div id="mconPane" style="visibility: hidden; display: none">
                            <h4>
                                Manual Plot Constraints</h4>
                            <p>
                                <i>Here you can enter the constraints to your plot as if they were in a 'where' clause.
                                    For example you could enter something like "ra > 3 and dec < 3" or "text like '%this%'
                                    and ra < 3". </i>
                            </p>
                            Enter conditions below:
                            <p>
                                <textarea id="mconta" rows="10" cols="10" style="width: 200; height: 100"></textarea></p>
                            <div>
                                <input type="button" onclick="plot('<%=ObjName%>',true);" value="Plot" /></div>
                        </div>
                        <div id="plotPane" style="visibility: hidden; display: none">
                            <h4>
                                Plot Table</h4>
                            <p>
                                <i>This produces non-interactive plots and is suitable for plotting a great numbers
                                    of objects. If you are only interested in plotting a few objects, you may want to
                                    try out the 'Quick' plot functionality, which can be reached by clicking 'Plot'
                                    on the query page after any 'Quick' query has completed. </i>
                            </p>
                            <p>
                                <i>To plot a table, pick columns from above to represent values on the X and Y axis,
                                    then click 'Plot'. Only one 'X' and one 'Y' may be selected. A plotting output job
                                    with the parameters you've specified will be created and placed in queue. </i>
                            </p>
                            <p>
                                <i>You can also specify constraints on your plot by entering values into the limit fields.
                                    Any blank fields are considered unbounded. Constraints entered for columns that
                                    are not actively being plotted will still affect the plot. </i>
                            </p>
                            <p>
                                <i>You may also manually specify your constraints by clicking 'Manually Enter Plot Constraints'.</i>
                            </p>
                            <br />
                            <div>
                                <input id="plotButton" type="button" onclick="plot('<%=ObjName%>',false);" value="Plot" />
                                <input id="plotQueryButton" type="button" onclick="toggleMConstraints();" value="Manually Enter Plot Constraints" />
                            </div>
                        </div>
                    </form>
                </div>
            </td>
        </tr>
    </table>
    <input type="hidden" id="xcol_a" value="<%=Plotter.X_COL%>" />
    <input type="hidden" id="ycol_a" value="<%=Plotter.Y_COL%>" />
    <script type="text/javascript" src="scripts/shCore.js"></script>  
    <script type="text/javascript" src="scripts/shBrushSQL.js"></script>  
    <script type="text/javascript" src="scripts/shBrushXml.js"></script>  
    <link type="text/css" rel="stylesheet" href="scripts/SyntaxHighlighter.css"></link>  
    <link href="cjobs.css" rel="stylesheet" />
    <script language="javascript">  
        dp.SyntaxHighlighter.HighlightAll('code');  
    </script> 
    <%if (ObjName != null && ObjName != "") {
          if (ObjType == CasDBObjType.TABLE || ObjType == CasDBObjType.VIEW) { %>
    <script type="text/javascript">loadSchema('<%=wsid%>','<%=context%>','<%=ObjName%>','<%=ObjType%>');</script>
    <%
        }
    } 
    %>
    <script type="text/javascript">
function changecnames(tname){
	var args = "?renamecols=true&ObjName="+tname;
	var cnt = 0;
	var current = window.document.getElementById("COL"+cnt);
	var old;
	var found = false;
	while(current != null){
		old = window.document.getElementById("COL"+cnt+"ORIG");
		if(old.value != current.value){
			args+="&COL"+cnt+"="+current.value;
			args+="&COL"+cnt+"OLD="+old.value;
			found = true;
			}
		++cnt;
		current = window.document.getElementById("COL"+cnt);
	}
	var redirect ;
	if(found){
		redirect = "mydbcontent.aspx"+args;
	}else{
		redirect = "mydb.aspx?lmessage="+escape('No columns changed');
	}
	window.open(redirect,'_top','');	
}
function togglePlot(){
	setPane('plotPane',0);
}
function plot(table,domanual){
	var xcol, ycol;
	var qry = '';
	var xcol_a = document.getElementById('xcol_a').value;
	var ycol_a = document.getElementById('ycol_a').value;
	var mconta = document.getElementById('mconta');
	
	if(domanual)qry = mconta.value;
	for(i=0;i<1000;i++){
		xcolr = document.getElementById('xcolr_'+i);
		if(xcolr == null)break;
		ycolr = document.getElementById('ycolr_'+i);
		colname = document.getElementById('COL'+i+'ORIG').value;
		if(xcolr.checked)xcol = colname;
		if(ycolr.checked)ycol = colname;
		if(domanual)continue;
		upper_lim = document.getElementById('upper_'+i).value;		
		lower_lim = document.getElementById('lower_'+i).value;
		if(upper_lim == '')upper_lim = null;
		if(lower_lim == '')lower_lim = null;
		if(upper_lim != null || lower_lim != null){
			if(qry != '')qry= qry+'AND';
			if(upper_lim != null && lower_lim != null)
				qry = qry+' '+colname+' between '+lower_lim+' and '+upper_lim+' ';
			else if(upper_lim != null)
				qry = qry+' '+colname+' < '+upper_lim+' ';
			else if(lower_lim != null)
				qry = qry+' '+colname+' > '+lower_lim+' ';
		}
	}
	if(xcol == null || ycol == null)return alert('Please select a column for both X and Y');
	if(qry != '')qry = ' where '+qry;
	qry = 'select '+xcol+' as '+xcol_a+', '+ycol+' as '+ycol_a+' from '+table+qry;
	window.open('mydbcontent.aspx?plot=true&xcol='+xcol+'&ycol='+ycol+'&ObjName='+table+'&qry='+escape(qry),'_top','');
}
function createindex(table, column){
	window.open('mydbcontent.aspx?createindex=true&ObjName='+table+'&columnName='+column,'_top','');
}
var lastPane;
function setPane(pane,dogrid){  
	var target = document.getElementById(pane);
	if(target == lastPane)return;
	target.style.visibility = 'visible';
	target.style.display = 'block';
	if(lastPane != null){
		lastPane.style.visibility = 'hidden';
		lastPane.style.display = 'none';
	}
	lastPane = target;
	tabToolsSet(dogrid);
}
function toggleExtract(){setPane('extractPane',-1);}
function toggleNeighbors(){setPane('neighborsPane',-1);}
function togglePreview(){
    setPane('previewPane',-1);
    loadSample('<%=wsid%>','<%=context%>','<%=ObjName%>','<%=ObjType%>');
}
function toggleRename(){setPane('renamePane',-1);}
function togglePublish(){setPane('publishPane',-1);}
function toggleUnpublish(){setPane('unpublishPane',-1);}
function toggleDrop(){setPane('dropPane',-1);}
function toggleJob(){setPane('jobPane',-1);}
function toggleSdesc(){setPane('sdescBox',-1);}
function toggleNotes(){setPane('notesPane',-1);}
function toggleSource(){setPane('sourcePane',-1);}
function toggleMConstraints(){setPane('mconPane',0);}

function editfunction(){
	alert(document.getElementById('FunctionBox').value);
}
    </script>
<%if (ObjName != null && (ObjType == CasDBObjType.VIEW || ObjType == CasDBObjType.PROCEDURE || ObjType == CasDBObjType.FUNCTION)) {  %>
        <script type="text/javascript">toggleSource();</script>
    <%} else if (ObjType == CasDBObjType.TABLE && ObjName != null) {%>
       <script type="text/javascript">toggleNotes();</script>
    <%} %>
</body>
</html>
