
/*
 * Functions to handle the pump selector drop downs
 */

var MAX_LEVELS=4;

/* This is a constructor from the DropdownNode object */

function DropdownNode(strName, strTarget, arrKids)
{
  this.name = strName;
  this.target = strTarget;
  this.kids = arrKids;
}

// This holds the node selected at each level.
var arrSelectedNodes = new Array();

// var dropdownNav - defined in the dynamic javascript.

function initLevel(intLevel, strText){

  if (intLevel != 1){
	  document.write("<br />");
  }
  // this is a bit of a hack because there is an additional selector page
  // that only has three drop-downs instead of four. Therefore, we will expect 
  // the first option to only contain one item
  var blnDisplayTopLevel = true;
  if ((intLevel == 1) && (strText == ""))
  {
	// do nothing as this only has three levels...
	blnDisplayTopLevel = false;
  }
  else
  {
  document.write("<span class='formHeadings' id='navy'>" + strText + "</span>");
  document.write("<br /><img src='../images/blank.gif' width='1' height='5' border='0' /><br />");
  document.write("<select name=\"psLevel" + intLevel + "\" onchange=\"changeOption(" + intLevel + ",this)\" class='related'>");
  document.write("<option>A dummy option</option>");
  document.write("<option>This is a dummy option to solve the problem of the width of the dropdowns</option>");
  document.write("</select>");
  document.write("<br />");
  }
  if (intLevel ==1) { // Set the top level
	arrSelectedNodes[0] = dropdownNav;
  }
  initOptions(intLevel, blnDisplayTopLevel);
  
}

function initOptions(intLevel, blnDisplayTopLevel)
{
	var objParent = arrSelectedNodes[intLevel-1];
	// Scan the kids
	var kids = objParent.kids;
	if (! kids) return;
	if (kids.length == 0) return;
	// Wipe out the old options
	// only do this if we are diplsaying the select box for the current level
	if (blnDisplayTopLevel)
	{
		var objSelect = document.forms["criteria"]["psLevel" + intLevel];
		if (objSelect == null) {
			alert("Warning, can't find selector, level " + intLevel);
			return;
		}
		var i;
		for (i=0; i<kids.length; i++) {
			var strName;
			strName = kids[i].name;
			if (strName == "") strName = "Unnamed option index:" + i;
			var o = new Option(strName,"hello",false,false);
			objSelect.options[i] = o;
		}
		var intAfterLast = i;
		// Wipe any extras.
		for (i=0; i<30; i++) {
			if (! objSelect.options[intAfterLast]) break;
			objSelect.options[intAfterLast] = null;
		}
	}
	// Finally select the 1st one by default
	arrSelectedNodes[intLevel] = kids[0];
}

function changeOption(intLevel, objSelect)
{
	var index = objSelect.selectedIndex;
	var newObj = arrSelectedNodes[intLevel-1].kids[index];
	if (newObj == null)
	{
		alert("Problem selecting new node: level:" + intLevel + " index:" + index);
		return;
	}
	arrSelectedNodes[intLevel] = newObj;
	var i;
	for (i=(intLevel + 1); i<=MAX_LEVELS; i++) {
		// this passes true each time, as it is called once everything has been displayed
		// and so we can assume that if a select box is displayed, it will include 
		// information
		initOptions(i, true);
	}
}

function pumpGo()
{
	// Get the last thingy
	var objCat = arrSelectedNodes[MAX_LEVELS];
	if (objCat.target == "")
		return alert(alertMsg); // the message is set via the admin, and added tot he page content during translation
	window.location.href = objCat.target;
}

