function dbselect(f) {
 f.action = "count.php";
 f.submit();
}

function dbsearch(f,action) {
// seems not to work anymore with format smarts, but if it is a smarts,
// it comes thru with smiles format code
// smarts = get_smarts("smarts");
// f.smarts.value = smarts;
 smiles = get_smarts("smiles:a");
 f.smiles.value = smiles;
 f.smarts.value = smiles;
 f.action = action + ".php";
 if (smiles == "" && smarts == "") {
  alert("please sketch in a structure");
 } else {
  top.detail.document.write('<IMG SRC="bar.gif" WIDTH=88 HEIGHT=13>');
  f.submit();
 }
}

function fragments(f) {
 smiles = get_smarts("smiles:a");
 f.smiles.value = smiles;
 f.action = "fragments.php";
 if (smiles == "") {
  alert("please sketch in a structure");
 } else {
  //detail.location = "bar.gif";
  f.submit();
 }
}

function get_smarts (format) {
 smarts = left.document.MSketch.getMol(format);
 return (smarts);
}

function sketcher(smilesPlus,row,opts) {
// smilesPlus is an array; smiles is [0], atomlist to color is [1...]
 //left.document.MSketch.setMol(smilesPlus[0],opts);
 left.document.MSketch.setMol(smilesPlus[0]);
 if (row > 0) {
  left.document.getElementById('sid').innerHTML = "#" + row;
 } else {
  left.document.getElementById('sid').innerHTML = "";
 }
// releaseLock();
// viewer(nextSmiles);
//  coloratom(left.document.MSketch,smilesPlus);
  viewer(smilesPlus);
 
}

var locked = 0;
var nextSmiles = [];
function releaseLock() {
// Marvin let us know that display of mol is done.
// Go on to next, if available.
 alert("released");
 if (locked > 1) {
//  alert(locked);
  locked = 0;
  viewer(nextSmiles);
 } else if (locked == 1) {
  coloratom(left.document.MView,nextSmiles);
  locked = 0;
//  nextSmiles = "";
 } else {
//  alert("lock ? " + locked);
//  nextSmiles = "";
  locked = 0;
 }
}

function viewer(smilesPlus) {
// smilesPlus is an array; smiles is [0], atomlist to color is [1...]
 if (locked > 0) {
// as long as current display is still working, just keep track
// of most recent mouseOver smiles for next display, or coloratom on release.
  nextSmiles = smilesPlus;
  locked += 1;
  return;
 }
 nextSmiles = smilesPlus;
 //locked = 1;
 left.document.MView.setM(0,smilesPlus[0]);
 coloratom(left.document.MView,smilesPlus);
}

function coloratom(d,smilesPlus) {
// smilesPlus is an array; smiles is [0], atomlist to color is [1...]
 if (d) {
  for (i=1; i<smilesPlus.length; i++) {
   d.setSetSeq(0, smilesPlus[i], 1);
  }
 } else {
  alert("no viewer");
 }
}

function delay(n) {
 then = new Date();
 while(1) {
  now = new Date();
  if ( (now-then) > n ) break;
 }
}

