//function setOpts() { window.location = "skp:pform@"+Form.serialize('opts') }
// function divide() { setOpts(); window.location  = "skp:divide_faces"; }
//function protrude() { setOpts(); window.location = "skp:protrude"; }
//function mredo() { window.location = "skp:redo"; }
// function do_over() { setOpts(); window.location = "skp:do_over"; }
//function reduce() { setOpts(); window.location = "skp:reduce"; }
//function undo() { window.location = "skp:undo"; }
//function setReasonableValues() { setOpts(); window.location = "skp:setReasonableValues"; }

function setOpts(callback) {
    //alert("setOpts");
    var form = document.forms['opts'];
    var ary = formToArray(form);
    //log(ary);
    window.location = "skp:set_opts@"+callback+'--'+ary;
}

function viewSM() { window.location = "skp:sm_inspect"; }
function keep() { window.location = "skp:keep"; }
function usePreset() { setOpts(); window.location = "skp:usePreset"; }
function addGreebles() { setOpts(); window.location = "skp:addGreebles"; }

function setById(id, val) {
  var e = document.getElementById(id);
  e.value=val;
}

function skp(name, par) {
  //alert("skp");
  setOpts(name);
  //if (par==undefined) { par = "nil"; }
  //window.location = "skp:ruby_callback@"+name+";"+par;
}

function formToArray(form) {
  var arr = new Array();
  var els = form.elements;
  for (i=0; i<els.length; i++) {
    var e = els[i];
    var t = e.type;
    switch (t) {
      case "text":
	if (e.value != ""){
	  var str = e.value.replace(/'/, "\\'");
	  //log(str);
	  arr.push(e.name+"="+str);
	}
      break;
      case "checkbox":
	if (e.checked) arr.push(e.name + "=" + e.value);
      break;
      case "select-one":
	arr.push(e.name+"="+e.value);
      break;
      default:
      //alert(e.type)
    }
  }
  var t = arr.join(";");
  //log(t);
  if (t) return t;
  else return undefined; 
}


function handleCheckbox(o, id) {
  var t = document.getElementById(id);
  if (o.checked) { t.disabled=false; }
  if (!o.checked) { t.disabled=true; }
}


// http://ajaxcookbook.org/javascript-debug-log/
function log(message) {
  if (!log.window_ || log.window_.closed) {
    var win = window.open("", null, "width=400,height=200," +
	"scrollbars=yes,resizable=yes,status=no," +
	"location=no,menubar=no,toolbar=no");
    if (!win) return;
    var doc = win.document;
    doc.write("<html><head><title>Debug Log</title></head>" +
	"<body style=\"font-family: monospace;\"></body></html>");
    doc.close();
    log.window_ = win;
  }
  var logLine = log.window_.document.createElement("div");
  logLine.style.fontSize = '8pt';
  logLine.appendChild(log.window_.document.createTextNode(">"+message));
  log.window_.document.body.appendChild(logLine);
  log.window_.scrollBy(0, 100);
}


