function Jsr(el,s1,s2)
{
this.el=el;
this.s1=s1;
this.s2=s2;
}

var bosh=new Object();

bosh.init=function()
{
bosh.cl=document.getElementById("cmdline");
bosh.iel=document.getElementById("input");
bosh.oel=document.getElementById("output");
bosh.fel=document.getElementById("file");
bosh.lel=document.getElementById("link");
bosh.error=false;
bosh.lf="";
bosh.ftn=null;
bosh.ltn=null;
var i=window.location.href.indexOf("?");
if(i==-1)
  bosh.loc=window.location.href;
else
  bosh.loc=window.location.href.slice(0,i);
document.onclick=bosh.onclick;
bosh.data=new Array();
bosh.cl.style.display="none";
bosh.tid=setTimeout("bosh.timeout()",5000);
var q=window.location.search;
if(q=="")
  q="index";
else
  q=q.slice(1);
bosh.query("q="+q);
}

bosh.query=function(str)
{
var script=document.createElement("script");
var q=str;
if(bosh.lf!="")
  q+="&l="+bosh.lf;
script.src="cgi-bin/jsq.rb?"+q;
bosh.error=false;
document.body.appendChild(script);
}

bosh.scroll=function()
{
var cy=window.pageYOffset;
var oy=-1;
while(cy!=oy)
  {
  window.scrollBy(0,9999);
  oy=cy;
  cy=window.pageYOffset;
  }
}

bosh.br=function()
{
bosh.oel.appendChild(document.createElement("br"));
}

bosh.out=function(str)
{
var pre=document.createElement("pre");
pre.appendChild(document.createTextNode(str));
bosh.oel.appendChild(pre);
}

bosh.out2=function(str,col)
{
var pre=document.createElement("pre");
pre.style.color=col;
pre.appendChild(document.createTextNode(str));
bosh.oel.appendChild(pre);
}

bosh.onkey=function(e)
{
if(e.keyCode==13)
  {
  var str=bosh.iel.value;
  bosh.iel.value="";
  if(str=="")
    return;
  bosh.iel.blur();
  bosh.cl.style.display="none";
  bosh.br();
  bosh.out("> "+str);
  bosh.tid=setTimeout("bosh.timeout()",5000);
  bosh.query("q="+str);
  }
}

bosh.cmd=function(str)
{
bosh.iel.blur();
bosh.cl.style.display="none";
bosh.br();
bosh.out("> "+str);
bosh.tid=setTimeout("bosh.timeout()",5000);
bosh.query("q="+str);
}

bosh.selection=function()
{
if(document.selection)
  return document.selection.createRange().text.length;
if(document.getSelection)
  return document.getSelection().length;
return 0;
}

bosh.onclick=function()
{
if(bosh.selection()==0)
  {
  bosh.scroll();
  bosh.iel.focus();
  }
}

bosh.done=function()
{
bosh.data=new Array();

if(bosh.error)
  {
  var ft=document.createTextNode("<NULL>");
  var lt=document.createTextNode("<NULL>");
  bosh.fel.style.color="red";
  bosh.lel.style.color="red";
  }
else
  {
  var ft=document.createTextNode(bosh.lf);
  var lt=document.createTextNode(bosh.loc+"?"+bosh.lf);
  bosh.fel.style.color="#A9B9D5";
  bosh.lel.style.color="#A9B9D5";
  }
if(bosh.ftn)
  bosh.fel.replaceChild(ft,bosh.ftn);
else
  bosh.fel.appendChild(ft);
bosh.ftn=ft;
if(bosh.ltn)
  bosh.lel.replaceChild(lt,bosh.ltn);
else
  bosh.lel.appendChild(lt);
bosh.ltn=lt;

bosh.cl.style.display="block";
bosh.scroll();
bosh.iel.focus();
}

bosh.timeout=function()
{
bosh.tid=null;
bosh.error=true;
bosh.out2("Error: request timed out","red");
bosh.done();
}

bosh.err=function()
{
bosh.error=true;
}

bosh.file=function(lf)
{
bosh.lf=lf;
}

bosh.ln=function(el,s1,s2)
{
bosh.data.push(new Jsr(el,s1,s2));
}

bosh.end=function()
{
if(bosh.tid)
  {
  clearTimeout(bosh.tid);
  bosh.tid=null;
  }
var c=bosh.data.length;
if(bosh.error || c<1)
  {
  bosh.done();
  return;
  }
var pre=null;
var i=0;
while(i<c)
  {
  var d=bosh.data[i];
  if(d.el=="br")
    {
    bosh.oel.appendChild(document.createElement("br"));
    i++;
    continue;
    }
  if(d.el=="img")
    {
    var img=document.createElement("img");
    img.src=d.s1;
    bosh.oel.appendChild(img);
    i++;
    continue;
    }
  if(d.el=="pre" && d.s1)
    {
    pre=document.createElement("pre");
    if(d.s2)
      pre.style.color=d.s2;
    pre.appendChild(document.createTextNode(d.s1));
    bosh.oel.appendChild(pre);
    i++;
    continue;
    }
  pre=document.createElement("pre");
  i++;
  while(i<c)
    {
    var d=bosh.data[i];
    if(d.el==null)
      {
      pre.appendChild(document.createTextNode(d.s1));
      i++;
      continue;
      }
    if(d.el=="span")
      {
      var span=document.createElement("span");
      span.style.color=d.s2;
      span.appendChild(document.createTextNode(d.s1));
      pre.appendChild(span);
      i++;
      continue;
      }
    if(d.el=="a")
      {
      var link=document.createElement("a");
      link.href=d.s2;
      link.appendChild(document.createTextNode(d.s1));
      pre.appendChild(link);
      i++;
      continue;
      }
    if(d.el=="cmd")
      {
      var link=document.createElement("a");
      link.className="cmd";
      link.style.color=d.s2;
      link.href="javascript:bosh.cmd('"+d.s1+"')";
      link.appendChild(document.createTextNode(d.s1));
      pre.appendChild(link);
      i++;
      continue;
      }
    break;
    }
  bosh.oel.appendChild(pre);
  }
bosh.done();
}
