/*
 * Copyright (c) 2005-6, killersudokuonline.com
 * All Rights Reserverd.  
 * With the exception of the function getArgs, dhtmlLoadScript, 
 * defineNodeConstants.
 * Use of this code requires prior permission from webmaster@killersudokuonline.com
 * 
 */


/*
 * This function parses comma-separated name=value argument pairs from
 * the query string of the URL. It stores the name=value pairs in 
 * properties of an object and returns that object. - Borrowed from O'Reilly
 */
function getArgs() {
    var args = new Object();
    var query = location.search.substring(1);     // Get query string.
    var pairs = query.split(",");                 // Break at comma.
    for(var i = 0; i < pairs.length; i++) {
	var pos = pairs[i].indexOf('=');          // Look for "name=value".
	if (pos == -1) continue;                  // If not found, skip.
	var argname = pairs[i].substring(0,pos);  // Extract the name.
	var value = pairs[i].substring(pos+1);    // Extract the value.
	args[argname] = unescape(value);          // Store as a property.
	// In JavaScript 1.5, use decodeURIComponent() instead of escape()
    }
    return args;                                  // Return the object.
}

function dhtmlLoadScript(url)
{
   var e = document.createElement("script");
   e.src = url;
   e.type="text/javascript";
   document.getElementsByTagName("head")[0].appendChild(e);
}

// this function defines constants
function defineNodeConstants() {
  if (!window.Node) {
    window.Node = {
      ELEMENT_NODE: 1,
      ATTRIBUTE_NODE: 2,
      TEXT_NODE: 3,
      COMMENT_NODE: 8,
      DOCUMENT_NODE: 9,
      DOCUMENT_FRAGMENT_NODE: 11
    }
  }
}

function getById(name) {
  var element;
  if (document.getElementById) {
    element = document.getElementById(name);
  } else if (document.all) {
    element = document.all[name];    // Use the all[] array to find the element
  } else if (document.layers) {
    element = document.layers[name]; // Use the layers[] array to get the element
  } else {
    alert("Sorry, I can't play on this browser.");
  }
  return element;
}

function removeAllChildren(cell) {
  if (cell.hasChildNodes()) {
    var children = cell.childNodes;
    for (var i=children.length-1; i>=0; i--) {
      cell.removeChild(children[i]);
    }
  }
}

function replaceChildrenWithText(node, text) {
	removeAllChildren(node);
	node.appendChild(document.createTextNode(text));
}

function setupForm(name, targetURL) {
  parent = getById("extra");

  var ifv = document.createElement("IFRAME");
  ifv.style.width="0px";
  ifv.style.height="0px";
  ifv.style.border="0px";
  ifv.name = name;

  var f = document.createElement("FORM");
  f.action = targetURL;
  f.method = "post"; 
  f.target = name;
//f.target = "_self";
  f.name = (name+"form");

  parent.appendChild(ifv);
  parent.appendChild(f);

  return f;
}

function addInput2Form(f, type, name, val) {
  var id=document.createElement("INPUT");
  id.type = type;
  id.name=name;
  id.value=val;
  f.appendChild(id);
}

function checkref() {
    if (window.location.href.indexOf('?') == -1) {
	window.location = "play.html?puzzle="+puzzleid;
    }
}

