// terminal.js - Javascript for terminal at pksage.com
// Some code borrowed from "The Search for Fonzie's Treasure" at
// http://mrspeaker.webeisteddfod.com/2005/04/17/the-fonz-and-ajax/
//
// You won't find any help here!

// ========================= Variables

var v_input;

var v_sendverb;
var v_sendnoun;

var c_introtext = "Welcome to pksage.com! I'm Andy Sage, and you've found my professional<br />portfolio and online base of operations. This text terminal is a way<br />for those of you who were alive before 1986 to browse the site more<br />comfortably. (The links above work too, but where's the fun in that?)<br /><br />Type 'go &lt;pagename>' to jump to another part of the site. Or if<br />you think you're more text-adventure savvy, you might try your hand at<br />some other commands... Enjoy!<br /><br />Visible exits: blog, portfolio, bio, contact<br /><br /><span style='text-decoration: underline'>News</span><br /><b>01/09/10</b> - Updates across the board. Welcome to the future!<br /><b>03/29/09</b> - Résumé updated.<br /><b>03/12/09</b> - Bio updated.<br />";


// ========================= Basic functions

function loadTerminal() {
	cls();
	println(">intro");
	println(c_introtext);
	print(">");
	document.getElementById("cmd").focus();
}

function checkForEnter(vt_keydown) {
	var keyCode = document.layers ? vt_keydown.which : document.all ? vt_keydown.keyCode : vt_keydown.keyCode;
	if ( keyCode == 13 )
	{
		doCommand();
		return false;
	}
	else
	{
		return true;
	}
}

function doCommand() {
	v_input = document.getElementById("cmd").value;
	
	parseInput(v_input);
	
	sendCommand();
}

function parseInput(v_checkinput)
{
	var v_verb1 = "";
	var v_noun1 = "";
	var a_words = null;
	
	a_words = v_checkinput.split(" ");
	v_verb1 = a_words[0];
	if (a_words.length > 1)
		v_noun1 = a_words[1];
	
	v_sendverb = v_verb1;
	v_sendnoun = v_noun1;
}

// ========================= I/O functions

function clearInput() {
	document.getElementById("cmd").value = "";
	document.getElementById("cmd").focus();
}

function print(vt_input) {
	document.getElementById("terminal").innerHTML += vt_input;
}

function println(vt_input) {
	document.getElementById("terminal").innerHTML += vt_input + "<br />";
}

function cls() {
	document.getElementById("terminal").innerHTML = "";
}

function pause(x)
{
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); }
	while(curDate - date < x);
} 

// ========================= Ajax functions

var v_datareq = null;

function sendCommand() {
	if(location.hostname.indexOf("pksage.com") != -1)
	{
		v_datareq = (!window.XMLHttpRequest)?(new ActiveXObject("Microsoft.XMLHTTP")):(new XMLHttpRequest());
		v_datareq.onreadystatechange = readyChange;
		v_datareq.open("POST", "terminal.php", true);
		v_datareq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		v_datareq.send("verb=" + v_sendverb + "&noun=" + v_sendnoun);
	}
}

function readyChange()
{
	if(v_datareq.readyState == 4)
	{
		var v_gotdata = v_datareq.responseText;
		
		var v_checklink = v_gotdata.substring(0, 4);
		
		if (v_checklink == "http") // result is an HTTP redirect
		{
			window.location = v_gotdata;
		}
		else                       // result is text
		{
			cls();
			println(">");
			println(c_introtext);
			print(">");
			println(v_input);
			println(v_gotdata);
			clearInput();
		}
		
	}
}