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.bstyle="1px solid #DDDDDD";
bosh.error=false;
bosh.e=null;
bosh.tb=null;
bosh.tr=null;
bosh.tc=0;
bosh.cc=0;
bosh.cs=0;
bosh.lf="";
bosh.ftn=null;
bosh.ltn=null;
bosh.hist=new Array();
bosh.hidx=null;
bosh.hcmd=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.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.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.history=function(k)
{
if(bosh.hidx==null)
  return;
if(bosh.hidx>=bosh.hist.length)
  bosh.hcmd=bosh.iel.value;
if(k==0)
  {
  if(bosh.hidx>0)
    bosh.hidx--;
  else
    return;
  }
if(k==1)
  {
  if(bosh.hidx<bosh.hist.length)
    bosh.hidx++;
  else
    return;
  }
if(bosh.hidx>=bosh.hist.length)
  {
  if(bosh.hcmd!=null)
    bosh.iel.value=bosh.hcmd;
  return;
  }
bosh.iel.value=bosh.hist[bosh.hidx];
}

bosh.exec=function(str)
{
bosh.hist.push(str);
bosh.hidx=bosh.hist.length;
bosh.hcmd=null;
bosh.iel.blur();
bosh.cl.style.display="none";
bosh.br();
bosh.out("> "+str);
bosh.tid=setTimeout("bosh.timeout()",5000);
bosh.query("q="+str);
}

bosh.onkey=function(e)
{
if(e.keyCode==13)
  {
  var str=bosh.iel.value;
  bosh.iel.value="";
  if(str=="")
    return;
  bosh.exec(str);
  return;
  }
if(e.keyCode==38)
  {
  if(e.preventDefault)
    e.preventDefault();
  else
    e.returnValue=false;
  bosh.history(0);
  return;
  }
if(e.keyCode==40)
  {
  if(e.preventDefault)
    e.preventDefault();
  else
    e.returnValue=false;
  bosh.history(1);
  return;
  }
}

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

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

bosh.done=function()
{
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.br=function()
{
bosh.e=null;
bosh.oel.appendChild(document.createElement("br"));
}

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

bosh.img=function(s)
{
var img=document.createElement("img");
img.src=s;
img.onload=bosh.scroll
bosh.oel.appendChild(img);
}

bosh.pre=function(s,c)
{
var pre=document.createElement("pre");
if(c)
  pre.style.color=c;
if(s)
  pre.appendChild(document.createTextNode(s));
bosh.oel.appendChild(pre);
bosh.e=pre;
}

bosh.text=function(s)
{
if(!bosh.e)
  return;
bosh.e.appendChild(document.createTextNode(s));
}

bosh.span=function(s,c)
{
if(!bosh.e)
  return;
var span=document.createElement("span");
if(c)
  span.style.color=c;
span.appendChild(document.createTextNode(s));
bosh.e.appendChild(span);
}

bosh.a=function(s,l)
{
if(!bosh.e)
  return;
var link=document.createElement("a");
link.href=l
link.appendChild(document.createTextNode(s));
bosh.e.appendChild(link);
}

bosh.cmd=function(s,c)
{
if(!bosh.e)
  return;
var link=document.createElement("a");
link.className="cmd";
link.style.color=c;
link.href="javascript:bosh.exec('"+s+"')";
link.appendChild(document.createTextNode(s));
bosh.e.appendChild(link);
}

bosh.tab=function(s,c)
{
if(bosh.e)
  bosh.e=null;
bosh.e=document.createElement("table");
bosh.e.cellSpacing="0px";
bosh.tb=document.createElement("tbody");
var tr=document.createElement("tr");
var td=document.createElement("td");
td.colSpan=c;
td.className="hdr";
td.style.border=bosh.bstyle;
td.appendChild(document.createTextNode(s));
tr.appendChild(td);
bosh.tb.appendChild(tr);
bosh.e.appendChild(bosh.tb);
bosh.oel.appendChild(bosh.e);
bosh.tr=null;
bosh.tc=c;
bosh.cc=0;
bosh.cs=0;
}

bosh.fld=function(s)
{
if(!bosh.tr)
  bosh.tr=document.createElement("tr");
var td=document.createElement("td");
td.className="fld";
td.style.borderLeft=bosh.bstyle;
td.style.borderBottom=bosh.bstyle;
td.appendChild(document.createTextNode(s));
bosh.tr.appendChild(td);
bosh.cc++;
}

bosh.nr=function()
{
if(bosh.cc<bosh.tc)
  {
  if(bosh.cs>0)
    {
    for(var i=bosh.cc;i<bosh.tc;i++)
      {
      var td=bosh.tr.childNodes[bosh.cc];
      if(td.lastChild.nodeName!="BR")
        td.appendChild(document.createElement("br"));
      td.appendChild(document.createElement("br"));
      bosh.cc++;
      }
    bosh.tr=null;
    bosh.cc=0;
    bosh.cs=0;
    return;
    }
  for(var i=bosh.cc;i<bosh.tc;i++)
    {
    var td=document.createElement("td");
    td.style.borderLeft=bosh.bstyle;
    td.style.borderBottom=bosh.bstyle;
    td.appendChild(document.createElement("br"));
    bosh.tr.appendChild(td);
    }
  }
bosh.tr.lastChild.style.borderRight=bosh.bstyle;
bosh.tb.appendChild(bosh.tr);
bosh.tr=null;
bosh.cc=0;
bosh.cs=0;
}

bosh.ds=function(c)
{
if(!bosh.tr)
  bosh.tr=bosh.tb.lastChild
bosh.cs=c;
bosh.cc=c;
}

bosh.dt=function(s)
{
if(!bosh.tr)
  bosh.tr=document.createElement("tr");
if(bosh.cs>0)
  {
  var td=bosh.tr.childNodes[bosh.cc];
  if(td.lastChild.nodeName!="BR")
    td.appendChild(document.createElement("br"));
  td.appendChild(document.createTextNode(s));
  bosh.cc++;
  return;
  }
var td=document.createElement("td");
td.style.borderLeft=bosh.bstyle;
td.style.borderBottom=bosh.bstyle;
td.appendChild(document.createTextNode(s));
bosh.tr.appendChild(td);
bosh.cc++;
}

bosh.end=function()
{
if(bosh.tid)
  {
  clearTimeout(bosh.tid);
  bosh.tid=null;
  }
bosh.done();
}

bosh.pend=function()
{
bosh.end();
bosh.lf="";
}

