function getNews()
{
var req = mkRequestor();
req.open("POST","@{toMaster NewsR}",true);
req.setRequestHeader("Accept","application/json");
req.send();
window.setTimeout("getNews()",500);
}
getNews();
function postLine(line) {
var req = mkRequestor();
req.open("POST","@{toMaster PutR}",true);
req.setRequestHeader("Accept","application/json");
req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
req.send("line="+encodeURIComponent(line));
}
function mkRequestor() {
var req;
if (window.XMLHttpRequest)
{
req=new XMLHttpRequest();
}
else
{
req=new ActiveXObject("Microsoft.XMLHTTP");
}
req.onreadystatechange=function()
{
if (req.readyState==4 && req.status==200)
{
var res = eval("("+req.responseText+")");
if (res.news != "") {
var cons = document.getElementById("cons");
cons.innerHTML+=res.news;
cons.scrollTop = cons.scrollHeight;
}
document.getElementById("prom").textContent = res.prompt;
fixwidth();
}
}
return req;
}
var hist = new Array();
var histIdx = -1;
function keypress(e){
if (!e) e = window.event;
var keyCode = e.keyCode || e.which;
var inp = document.getElementById("inp");
if (keyCode == '13'){ // enter
postLine(inp.value);
if (inp.value != "" && (hist.length == 0 || hist[0] != inp.value))
hist.unshift(inp.value);
histIdx = -1;
inp.value = "";
} else if (keyCode == '38') { // up
if ((histIdx+1) < hist.length) {
histIdx++;
inp.value = hist[histIdx];
}
} else if (keyCode == '40') { // down
if (histIdx >= 0) {
histIdx--;
if (histIdx >= 0) inp.value = hist[histIdx];
else inp.value = "";
}
}
}
document.getElementById("inp").onkeypress = keypress;
function fixwidth() {
document.getElementById("inp").style.width = (window.innerWidth - document.getElementById("prom").offsetWidth - 12)+"px";
}
fixwidth();