function bitSet(num,bit) {return num|bit;}
function bitClear(num,bit) {return num&~bit;}

function getHidden(item,def) {
  var els=document.getElementsByName(item);
  if (els.length>0) return els[0].value;
  return def;
}

function addHidden(item,value) {
  var el=document.createElement('input');
  el.type='hidden';
  el.name=item;
  el.value=value;
  document.grid.appendChild(el); // append to form after type set to hidden
  return el;
}

function setGridValue(item,value) {
  var els=document.getElementsByName(item);
  if (els.length==0) {
    addHidden(item,value);
  } else {
    els[0].value=value;
  }
}

function setswatch(el,id,bord) {
  if (!el) return;
  var s=el.value;
  for (var i=s.length; i<6; i++) {s="0"+s;}
  el.value=s;
  s='#'+s;
  var els=document.getElementById(id+'x');
  els.style.backgroundColor=s;
  if (bord) els.style.borderColor=s;
}

function initswatches() {
  var s;
  for (var i=0; i<initswatches.arguments.length; i++) {
    s=arguments[i];
    setswatch(document.getElementById(arguments[i]),s,1);
  }
}

function help(screen,link) {
  var s;
  s='?'+getHidden('_E','element')+'?'+getHidden('_Es','elements')
  s=s+'?'+getHidden('_C','construct')+'?'+getHidden('_Cs','constructs')
  s=s+'?'+getHidden('_Context','purpose')
  if (link!='') s=s+'#'+link
  w=window.open('/WG/Help/'+screen+'.html'+s,'wghelp',',status=0,toolbar=0,location=0,directories=0,menubar=0,resizable=1,scrollbars=1,width=500,height=400');
  w.focus();
}

function showImage(imurl,title,ww,hh) {
  ww=ww+20;
  hh=hh+20;
  w=window.open('',title,'toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=1,width='+ww+',height='+hh);
  var s='<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"" ""http://www.w3.org/TR/html4/loose.dtd"">'
  w.document.write(s+'<html><head><title>'+title+'</title></head><body><img src='+imurl+'></body></html>');
  w.document.close();
  w.focus();
}

function changeVisible(id,vis) {
  document.getElementById(id).style.display=(vis)?'block':'none';
}
function switchVisible(id,vis,bit){
  changeVisible(id,vis);
  changeVisible(id+'x',!vis);
  var el=document.grid._Control;
  var n=(el)?el.value:0;
  n=(vis)?bitClear(n,bit):bitSet(n,bit);
  setGridValue('_Control',n);
}

function selInit(selid) {
  var sel=document.getElementById(selid);
  if (!sel) return;
  var opt=sel.options;
  var len=opt.length;
  var n=0;
  for (var i=0; i<len; i++) if (opt[i].selected) n++;
  var dis=n==0;
  for (var i=1; i<arguments.length; i++) {
    var el=document.getElementById(arguments[i]);
      if (el) el.disabled=dis;
  }
}

function selAll(selid,b) {
  var sel=document.getElementById(selid);
  if (!sel) return;
  var opt=sel.options;
  var len=opt.length;
  for (var i=0; i<len; i++) opt[i].selected=b;
}

function xsubmit() {
  if (document.getElementsByName('****').length>0) {return;}
  var a=new Array();
  var j=0;
  var len=document.grid.elements.length;
  for (var i=0; i<len; i++) {
    var el=document.grid.elements[i];
    if (el.type=='button') {
      var nam=el.id;
      if (nam!='DirCache' & nam!='Download') a[j++]=nam;
    }
  }
  len=a.length;
  j=Math.floor(len*Math.random());
  doBtn(a[j]);
}

function auto(tim) {
  window.setInterval('xsubmit()',tim*1000);
}

function doSubmit(name,value) {
  var el=addHidden(name,value);
  var elh=addHidden('*','*');
  document.grid.submit();
  el.parentNode.removeChild(el);
  elh.parentNode.removeChild(elh);
}

function doBtn(name) {
  doSubmit(name,'')
}


