/*jslint browser:true */
/*globals CodeMirror: false */
(function () {
	var results,
		keys = {
			"Ctrl-Enter": function (cm) {
				cm.execCommand('xena_run');
			}
		};

	function escapeHTML(str) {
		return str.replace(/[&"<>]/g, function (m) {
			return escapeHTML.replacements[m];
		});
	}

	escapeHTML.replacements = {
		"&": "&amp;",
		'"': "&quot;",
		"<": "&lt;",
		">": "&gt;"
	};

	function makeRequest(url, success, error) {
		var httpRequest = new XMLHttpRequest();

		httpRequest.onreadystatechange = function () {
			if (httpRequest.readyState === 4) {
				if (httpRequest.status === 200) {
					if (success) {
						success(httpRequest.responseText);
					}
				} else {
					console.log(httpRequest);
					if (error) {
						error(httpRequest.responseText);
					}
				}
			}
		};

		httpRequest.open('GET', url);
		httpRequest.send();
	}

	function main() {
		var el = document.getElementById("editor"),
			cm = CodeMirror(el, {
					vimMode: true,
					matchBrackets: false,
					autoCloseBrackets: true
			});

		cm.addKeyMap(keys);
		results = document.getElementById("results");
	}

	function disp_whitespace(str) {
		return str.replace(/ /g, '&nbsp')
			.replace(/\n/g, '<br>');
	}

	CodeMirror.commands.xena_run = function (e) {
		var query = e.getValue();
		makeRequest("data/" + encodeURIComponent(query),
			function (r) {
				var pr;
				try {
					pr = disp_whitespace(JSON.stringify(JSON.parse(r), null, 4));
				} catch (e) {
					pr = "Unable to parse:<br>" + e + "<br>" + escapeHTML(r);
				}
				results.innerHTML = pr;
			},
			function (r) {
				console.log(results);
				results.innerHTML = r;
			}
	   );
	};

	document.onreadystatechange = function () {
		if (document.readyState === "complete") {
			main();
		}
	};
}());

@@ -410,16 +410,25 @@
 est(url,
+ content,
  success
@@ -434,24 +434,24 @@
 s, error) %7B%0A
-
 %09%09var httpRe
@@ -824,17 +824,86 @@
 en('
-GE
+POS
 T', url
+, true);%0A%09%09httpRequest.setRequestHeader(%22Content-type%22, %22text/plain%22
 );%0A%09
@@ -920,16 +920,23 @@
 st.send(
+content
 );%0A%09%7D%0A%0A%09
@@ -1383,36 +1383,15 @@
 ta/%22
+,
  
-+ encodeURIComponent(
 query
-)
 ,%0A%09%09

Use POST instead of GET in console.