var termStatus = 0;
//1:idle
//2:waiting for/printing response
//3:|more
var request = false;
var theScreen;
var theInput;
var whoAmI;
var timeout = 40;
var response;
try {
  request = new XMLHttpRequest();
} catch (trymicrosoft) {
  try {
    request = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
    try {
      request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {
      request = false;
    }  
  }
}
if (!request)
  alert("Error initializing XMLHttpRequest!");


function pageStart(){
	document.getElementById("input").focus();
	document.onclick=function(){document.getElementById("input").focus();}
}
function sendCmd(){
	if (termStatus == 0){
		theScreen = document.getElementById("screen");
		theInput = document.getElementById("input");
		whoAmI = document.getElementById("whoami");
		if (theInput.value=="clear"){
			theScreen.innerHTML = "";
			theInput.value = "";
		} else {
			termStatus =1;
			request.open("GET", ("themachine.php?cmd="+theInput.value), true);
			document.body.style.cursor = "wait";
			request.onreadystatechange = updatePage;
			request.send(null);
			theScreen.innerHTML+=whoAmI.innerHTML+" "+theInput.value+"<br/>\n";
			theInput.value = "";
			theInput.style.opacity=0;
			whoAmI.style.opacity=0;
		}
	} else if (termStatus ==3){
		termStatus =1;
		spitOut (response);
	}
}
function updatePage() {
 if (request.readyState == 4) {
   if (request.status == 200) {
	response = request.responseText;
	response = response.split("\n");
	timerID = setTimeout("spitOut (response)", timeout);
   }
 }
}
function spitOut (response){
	if (response.length>1) {
		var thisLine = response.shift();
		theScreen.innerHTML+="<br/>"+thisLine+"\n";
		if (thisLine!="--More--"){
			timerID = setTimeout("spitOut (response)", timeout);
		} else {
			termStatus =3;
		}
	} else {
		termStatus = 0;
		theInput.style.opacity=1;
		whoAmI.style.opacity=1;
		document.body.style.cursor = "auto";
	}
}