<!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">//<![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 _makeSAMPSuccessHandler(sampButton, tableSource) {
			// returns the callback for a successful hub connection
			return function(conn) {
				conn.declareMetadata([{
					"samp.description": "A form in a web interface at "+document.URL,
					"samp.icon.url": completeURL("/favicon.png")
				}]);

				// set the button up so clicks send again without reconnection.
				sampButton.unbind("click");
				sampButton.click(function(e) {
					sendSAMP(conn, tableSource);
				});

				// make sure we unregister when the user leaves the page
				$(window).on("unload", function() {
					conn.unregister();
				});

				// send the stuff once (since the connection has been established
				// in response to a click alread)
				sendSAMP(conn, tableSource);
			};
		}

		function connectAndSendSAMP(sampButton, tableSource) {
			samp.register("DaCHS Web",
				_makeSAMPSuccessHandler(sampButton, tableSource),
				function(err) {
					alert("Could not connect to SAMP hub: "+err);
				}
			);
		}

		function sendSAMP(conn, tableSource) {
			var tableURL = getFormQuery(tableSource, 
				{'_FORMAT': 1})+"&_FORMAT=VOTable&VERB=3";
			var msg = new samp.Message("table.load.votable", {
				"table-id": "DaCHSResult",
				"url": tableURL,
				"name": "DaCHS result"});
			conn.notifyAll([msg]);
		}

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

			var sampButton = $('#sendViaSAMP');
			var tableSource = document.getElementById('genForm');
			sampButton.click(function (e) {
				connectAndSendSAMP(sampButton, 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.
					This needs a fairly modern hub to work.">Send via SAMP</button>
			</n:invisible>
			<button n:render="flotplot">Quick Plot</button>
		</p>
	</script>

</body>
</html>

