/*
  Used to determine the value for the id attribute in the body tag. The body.id attribute is used by the style.css file
  to the appropriate section in the glogal nav.
*/
function getSectionId(rootValue){
    var rootCategories = new Array("Home", "Explore & Learn", "Books & Stories", "Fun & Toronto", "Grownups", "Preschoolers");
    for(var i =0; i < rootCategories.length; i++){
        if(rootValue.indexOf(rootCategories[i]) != -1){
            return "sec"+i;
        }
    }
    return "sec0";
}

// Pull-Down Menu Navigation Function.
function go(num) {
	box = document.dropnav["navi" + num];
	destination = box.options[box.selectedIndex].value;
	if (destination) location.href = destination;
}

/*
  This method will attempt to return the first portion of the url's pathname. It is used throughout the kidsSpace
  site by the various Flash Header files in order to locate various resources. Apparently, Action Script ,which
*/
function getContext(pathname) {
  pathname = pathname == undefined ? window.location.pathname : pathname;

  var regex = /\/[a-zA-Z0-9]+\//;
  var context = regex.exec(pathname);
  if(context == null){
	//if null, there may not be a context. It may be www.google.com/index.html or something.
    context = '/';
  }
  return context;
}

/*
    Parses the path and returns a relative path to the root. This method is set to assume that the
    first prortion of the pathname is in fact a weblogic context directory (and thus, always present).
    If kidspace will not reveal a webcontext in the url, then, the function can be adjusted by
    changing the 'start' variable.
*/
function getRelativeRootPath(pathname){
  pathname = pathname == undefined ? window.location.pathname : pathname;
  var tokens = pathname.split("/");
  var relativePath = "";
  var start = 3;
  for(; start <= tokens.length; start++){
  	relativePath = relativePath+"../";
  }
  return relativePath;
}

function openScrolling(windowName,w,h) {
    window.name = "mainWindow";
    var set1="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,";
    var newWin = window.open(windowName,"Pop",set1+"width="+w+",height="+h)
}

function openNoScrolling(windowName,w,h) {
    window.name = "mainWindow";
    var set2="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,";
    var newWin = window.open(windowName,"Pop", set2+"width="+w+",height="+h);
}


/* Client-side access to querystring name=value pairs
	Version 1.2.3
	22 Jun 2005
	Adam Vandenberg
*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=Querystring_get

	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &

// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name

		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;

	var value=this.params[key]
	if (value==null) value=default_;

	return value
}

function changeDiv(divName, the_change)
{
    var the_style = getStyleObject(divName);
    if (the_style != false)
    {
        the_style.display = the_change;
        var div = document.getElementById(divName)

        for (var i = 0; i < div.childNodes.length; i++) {
            var el = div.childNodes[i];
            try {
                if ((el != undefined) && ((el.tagName == 'INPUT' || el.tagName == 'TEXTAREA') && el.value != 'Submit')) {
                    el.value = '';
                }
            } catch(ex) {
            }

        }
    }
}

function getStyleObject(objectId) {
    if (document.getElementById && document.getElementById(objectId)) {
        return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
        return document.all(objectId).style;
    } else {
        return false;
    }
}

/*
  This can be useful for mapping a function to the elements of a list. Helpfull in the sense that it will
  make the calling funtion more readable if the there are a number for loops. It won't return a value though.
  For example;
  map(function (n) { n = n+1; }, inputList);
*/
function map(fun, list){
    for(var i=0;i<list.length;i++){
          eval(fun(list[i], i));
    }
}

/*
 Useful for debugging purposes.
*/
function debugToConsole (msg) {
    if (document.getElementById) {
        var n = document.getElementById("console").getElementsByTagName("pre")[0];
        n.appendChild(document.createTextNode(msg + "\n"));
    }
}
