// NOTE: $("select").value broken in IE, so targets must be enumerated...

var backend_data = [ [ "Javascript", "js_instructions" ], 
                     [ "Common Lisp", "lisp_instructions" ] ];
var last_compiled_js;

function on_compile_result (r, instructions_id) {
  try {
  $("compiled").innerHTML = xml_escape (last_compiled_js = r.responseText);
  backend_data.each (
    function (data) { Element.hide ($(data [1])); });
  var see_instr_ids = [ "see_instr_non_js", "see_instr_js" ];
  var hidden_see_instr = (instructions_id == "js_instructions") ? 0 : 1;
  Element.hide ($ (see_instr_ids [hidden_see_instr]));
  Element.show ($ (see_instr_ids [1 - hidden_see_instr]));
  Element.show ($(instructions_id));
  Element.show ("output_row");
  $("compile_button").value = "Compile >>";
  $("compile_button").disabled = false;
  } catch (e) { alert (e); }
}

function on_compile () {
  var data = backend_data [$("target").selectedIndex];
  var opts = {
    method: "get",
    parameters: "scheme=" + encodeURIComponent ($("scheme").value)
      + "&target=" + encodeURIComponent (data [0]),
    onComplete: function (r) { on_compile_result (r, data [1]); }
  }
  $("compile_button").value = "Compiling...";
  $("compile_button").disabled = true;
  new Ajax.Request ("servlets/cpscm/", opts);
  return false;
}

function eval_last () {
  if (last_compiled_js !== undefined)
    window.frames ["eval_frame"].window.do_eval (last_compiled_js);
}

function on_run () {
  if (on_eval_iframe_loaded != null)
    window.frames ["eval_frame"].window.location.replace ("do-eval.html");
  else
    eval_last ();
  return false;
}

function on_eval_iframe_loaded () {
  on_eval_iframe_loaded = null;
  eval_last ();
}

function initialize () {
  $("src_form").onsubmit = on_compile;
/*
  $("select_output").onclick = function () {
    var elem = $("compiled");
    elem.select (); elem.focus ();
    return false;
  }
*/
  var form = $ ("example_form");
  form.onsubmit = function () {
    var examples = $A (form.getElementsByTagName ("input"));
    var to_load = examples [0];
    examples.each (function (ex) {
                     if (ex.type == "radio" && ex.checked) to_load = ex.value;
                   });
    new Ajax.Request (
      "scheme-examples/" + to_load + ".txt",
      { method: "get", parameters: "",
        onComplete: function (r) { $("scheme").value = r.responseText; }
      });
    return false;
  };
  $("run_js1").onclick = on_run;
  $("run_js2").onclick = on_run;
}

function cpscm__js_obj2str (o) {
  var str = [ "t: " + (typeof o) ];
  var x;
  for (x in o) {
    val = o [x];
    str.push (x + ": " + ((val != undefined && val instanceof Object
                           && !(val instanceof Function))
                          ? (val): val));
  }
  return "{ " + str.join (", ") + " }";
}

function xml_escape (str) {
  str = str.replace (/&/g, "&amp;");
  str = str.replace (/</g, "&lt;");
  str = str.replace (/>/g, "&gt;");
  str = str.replace (/ /g, "&nbsp;");
  str = str.replace (/\n/g, "<br>");
  return str;
}
