/*
** musaul/lab : misc
** v0.0.1
** $Revision: 258 $
** $Date: 2008-02-11 01:25:34 -0800 (Mon, 11 Feb 2008) $
** Copyright 2008 Musaul Karim. All rights reserved
** http://musaul.net/
*/


function GetJS(id, src)
{
  var jscode = "";
  jscode += "function show" + id + "() {";
  jscode += "var container = document.getElementById('" + id + "');";
  jscode += "container.innerHTML = unescape('" + escape(src) + "');";
  jscode += "}";
  return jscode;
}

/*
** This wouldn't work as you can't dynamically add Javascript using innerHTML
function Generate()
{
  var id = document.getElementById("codeId").value;
  if (null == id || "" == id)
  {
    alert("Please enter a BlockID");
  }
  var src = document.getElementById("src").value;
  var jscode = "";
  jscode += "&lt;div id='" + id + "'&gt;";
  jscode += "&lt;script id='" + id + "_script' type='text/javascript'&gt;";
  jscode += GetJS(id, src);
  jscode += "&lt;/script&gt;";
  jscode += "&lt;button onclick='show" + id + "()'&gt;Show " + id + "&lt;/button&gt;";
  jscode += "&lt;/div&gt;";

  document.getElementById("targ").value = jscode;
  document.getElementById("sandbox").innerHTML = jscode;

  var newjs = document.getElementById(id+"_script");
}
*/


function Generate()
{
  var id = document.getElementById("codeId").value;
  if (null == id || "" == id)
  {
    alert("Please enter a BlockID");
	return;
  }
  var src = document.getElementById("src").value;

  var jscode = GetJS(id, src);

  var theJS = document.createTextNode(jscode);

  var jsEl = document.createElement('script');
  jsEl.id = id + "_script";
  jsEl.type = 'text/javascript';
  jsEl.appendChild(theJS);

  var btnLabel = document.createTextNode("Show " + id);
  var btn = document.createElement('button');
  btn.setAttribute("onclick", "show" + id + "()");
  btn.appendChild(btnLabel);

  var jsHolder = document.createElement('div');
  jsHolder.id = id;
  jsHolder.appendChild(jsEl);
  jsHolder.appendChild(btn);

  var sandbox = document.getElementById("sandbox");
  sandbox.innerHTML = "";
  sandbox.appendChild(jsHolder);
  document.getElementById("targ").value = sandbox.innerHTML;

  var newjs = document.getElementById(id+"_script");
  //alert( newjs.type );
}


