<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
		"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
		xmlns:n="http://nevow.com/ns/nevow/0.1">
<head n:render="commonhead">
	<title n:render="meta">title</title>

	<script type="text/javascript" src="/static/js/samp.js"/>
	<script type="text/javascript">
		var sourceTable = '<n:invisible n:render="string" n:data="tableName"/>';

		//<![CDATA[
		function watchKey(ev) {
			if (ev.which==13 && ev.ctrlKey) {
				ev.stopPropagation();
				if (ev.cancelable) {
					ev.preventDefault();
				}
				document.getElementById("genForm-action-submit").click();
			}
		}
		if (document.addEventListener) {
			// the keydown lets us swallow mozilla's textbox events.  Other browsers?
			document.addEventListener("keydown", watchKey, true);
		}

		function sendSAMP(conn, tableSource) {
			var tableURL = getFormQuery(tableSource,
				{'_FORMAT': 1})+"&_FORMAT=VOTable&VERB=3";
			var msg = new samp.Message("table.load.votable", {
				"table-id": sourceTable+"-"+(Date.now()%86400).toString(),
				"url": tableURL,
				// produce halfway unique strings at least for a day,
				// or TOPCAT will refuse repeated transfers.
				"name": sourceTable});
			conn.notifyAll([msg]);
		}

		$(document).ready(function() {
			$("#plotbuttonsholder").append(
				renderTemplate("tmpl_plotbuttons", {}));

			var sampButton = $('#sendViaSAMP');
			var tableSource = document.getElementById('genForm');
			var connector = new samp.Connector("DaCHS Web", {
					"samp.name": "DaCHS Web",
					"samp.description": "A form in a web interface at "+document.URL,
					"samp.icon.url": completeURL("/favicon.png")
				});
			$(document).on("beforeunload", function() {
				connector.unregister();
			});
			connector.onHubAvailability(function(hubPresent) {
				sampButton[0].disabled = !hubPresent;
			}, 2000);

			sampButton.click(function (e) {
				connector.runWithConnection(
					function(conn) {sendSAMP(conn, tableSource);});
			});

		});
	//]]></script>
	<style type="text/css" n:render="servicestyle"/>
</head>


<body n:render="withsidebar">
	<h1 n:render="meta">title</h1>

	<div class="querypars" n:data="queryseq" n:render="ifdata">
		<h2 id="qf-parameters"><a href="#_queryForm">Parameters</a></h2>
		<ul n:render="sequence">
			<li n:pattern="item" n:render="parpair"/>
		</ul>
	</div>

	<div class="result" n:render="ifdata" n:data="result">
		<h2 id="qf-result">Result</h2>
		<div class="resmeta" n:data="resultmeta" n:render="mapping">
			<p>Matched: <n:slot name="itemsMatched"/></p>
		</div>
		<div id="plotbuttonsholder"/>
		<div class="result" n:render="ifresult">
			<n:invisible n:render="resulttable"/>
		</div>
		<div class="warning" n:render="ifnoresult">
			<p n:render="metahtml">_noresultwarning</p>
		</div>
		<h2 id="qf-qf"><a name="_queryForm">Query Form</a></h2>
	</div>
	
	<div id="intro" n:render="intro" class="intro"/>
	<n:invisible n:render="form genForm"/>
	<div id="bottominfo" n:render="metahtml">_bottominfo</div>
	<div class="rights" n:render="metahtml">copyright</div>

	<script type="text/html" id="tmpl_plotbuttons">
		<p class="plotbuttons">
			<n:invisible n:render="iflinkable">
				<button id="sendViaSAMP" title="Broadcasts this table to all
					SAMP clients on your desktop." disabled="True">Send via SAMP</button>
			</n:invisible>
			<button n:render="flotplot">Quick Plot</button>
		</p>
	</script>

</body>
</html>

