// course and qual category drop menus

// sets up drop menus as DOM structure.

function setUpMenus() {
	for (var i=0; i<NumberOfMenus; i++) {
		// create node for <table>
		menu[i].table = document.createElement("table");
		menu[i].table.className="menu";
		menu[i].table.id="table"+i;
		menu[i].table.style.left = menu[i].positionX+"px";
		if (document.all) {
			menu[i].table.style.top = (menu[i].positionY-1)+"px"; // IE 
		}
		else {
			menu[i].table.style.top = (menu[i].positionY)+"px"; // correct for error in IE interpretation 
		}
		menu[i].tbody = document.createElement("tbody"); // tbody needed by IE6
		menu[i].tbody.id = "tbody"+i; 

		// create nodes for table rows 
		for (var j=0; j<menu[i].numberOfMenuitems; j++) {
			menu[i].menuitem[j].tr = document.createElement("tr");
			menu[i].menuitem[j].tr.id = "tr_c"+i+"r"+j;
			menu[i].menuitem[j].td = document.createElement("td"); 
			menu[i].menuitem[j].td.className= "menuentry";
			menu[i].menuitem[j].td.id = "td_c"+i+"r"+j;
			menu[i].menuitem[j].td.name = "menuitemtd-"+j;

			menu[i].menuitem[j].a = document.createElement("a");
			menu[i].menuitem[j].a.setAttribute("href", menu[i].menuitem[j].url);
			menu[i].menuitem[j].a.id = "a_c"+i+"r"+j;
			menu[i].menuitem[j].a.name = "a-"+j;
 			menu[i].menuitem[j].td.onmouseover= menuItemSelected;
			menu[i].menuitem[j].td.onmouseout= menuItemDeSelected;

			// create text node for menuitem
			menu[i].menuitem[j].textnode  = document.createTextNode(menu[i].menuitem[j].text);

			// connect in tree
			menu[i].menuitem[j].a.appendChild(menu[i].menuitem[j].textnode);
			menu[i].menuitem[j].td.appendChild(menu[i].menuitem[j].a);

			menu[i].menuitem[j].tr.appendChild(menu[i].menuitem[j].td);
			menu[i].tbody.appendChild(menu[i].menuitem[j].tr);
			menu[i].table.appendChild(menu[i].tbody);
		}
	}
}

// menu constructor..
	function menuType(nitems, pos) {
		this.numberOfMenuitems = nitems;
		if (document.all) {
			this.positionX = pos; // IE 
		}
		else {
			this.positionX = pos + 1; // correct for error in IE interpretation 
		}
		this.positionY = 182;
	}

// menuitem constructor..
	function menuitemType(t, u) {
		this.text = t;
		this.url = u;
	}


// function to build a path to root
// returns -1 if not on path of baseroot, otherwise "../../" etc to get to baseroot
function buildPath(baseroot) {
  var lpathname = location.href;
  var	lroot="";
	var ct=-1;
	var pos;
	var i;

	// look for root dir. name
	if ( (pos=lpathname.indexOf(baseroot)) == -1) {    // -1 returned if not found, else index
		// not in root path
		lroot = -1;
	}
	else {
    // count the number times "/" occurs in the path
	  while (1) {
	  if ( (pos=lpathname.indexOf("/",pos)) == -1) { 
		  break;
 	    }
	    ct++;
	    pos++; // next chr
    }

	  for (i=0; i<ct; i++) {
		  lroot ="../" + lroot;
	  }
  }
  return (lroot);
}


// returns path to course overview page for links
function coursePageLink(id) {
  return basehref()+"course/"+ id+ "/overview.php";
}

// returns path to exam overview page for links
function examPageLink(id) {
  return basehref()+"exam/"+ id+ "/overview.php";
}


// sets up course drop menus as DOM structure.
function initDropDownCourseMenus() {

	// ** Note: this may run before the document is completely loaded **
	// **       therefore most things are undefined                   **

	// create data structure representing the drop-down menus..
  NumberOfMenus = 5;	
  menu = new Array(NumberOfMenus);

  h=10; /* horizontal positioning adjustment of all menus */

  /* widths of main menus edge (copy from html) +1 allows for vlines */
  hpos = new Array(NumberOfMenus);
  hpos[0] = 21; /* left padding */
  hpos[1] = 119+1;
  hpos[2] = 140+1;
  hpos[3] = 104+1;
  hpos[4] = 128+1;

  x=0;
  h+=hpos[x];
	menu[x++] = new menuType(15, h);
  h+=hpos[x];
	menu[x++] = new menuType(14, h);
  h+=hpos[x];
	menu[x++] = new menuType(5, h);
  h+=hpos[x];
	menu[x++] = new menuType(5, h);
  h+=hpos[x];
	menu[x++] = new menuType(3, h);

	for (var mi=0; mi<NumberOfMenus; mi++) {
		menu[mi].menuitem = new Array(menu.numberOfMenuitems);
	}

	x=0;
	menu[0].menuitem[x++] = new menuitemType("Business Analysis Essentials", coursePageLink("business-analysis-essentials"));
	menu[0].menuitem[x++] = new menuitemType("Organisational Context", coursePageLink("organisational-context"));
	menu[0].menuitem[x++] = new menuitemType("Requirements Engineering", coursePageLink("requirements-engineering"));
	menu[0].menuitem[x++] = new menuitemType("Modelling Business Processes", coursePageLink("modelling-business-processes"));
	menu[0].menuitem[x++] = new menuitemType("Benefits Management and Business Acceptance", coursePageLink("benefits-management-and-business-acceptance"));
	menu[0].menuitem[x++] = new menuitemType("Systems Modelling Techniques with UML", coursePageLink("systems-modelling-techniques-with-uml"));
	menu[0].menuitem[x++] = new menuitemType("Systems Development Essentials with Agile", coursePageLink("systems-development-essentials-with-agile"));
	menu[0].menuitem[x++] = new menuitemType("Foundation in Business Analysis", coursePageLink("foundation-in-business-analysis"));
	menu[0].menuitem[x++] = new menuitemType("Business Analysis Diploma oral revision workshop", coursePageLink("business-analysis-diploma-oral-revision-workshop"));
	menu[0].menuitem[x++] = new menuitemType("Facilitation Skills for Business Analysts", coursePageLink("facilitation-skills-for-business-analysts"));
	menu[0].menuitem[x++] = new menuitemType("IS Consultancy Practice", coursePageLink("is-consultancy-practice"));
	menu[0].menuitem[x++] = new menuitemType("IT-enabled Business Change", coursePageLink("it-enabled-business-change"));
	menu[0].menuitem[x++] = new menuitemType("Making the Business Case", coursePageLink("making-the-business-case"));
	menu[0].menuitem[x++] = new menuitemType("UML for Business Analysts", coursePageLink("uml-for-business-analysts"));
	menu[0].menuitem[x++] = new menuitemType("Writing Effective, Testable Use Cases", coursePageLink("writing-effective-testable-use-cases"));

	x=0;
	menu[1].menuitem[x++] = new menuitemType("Systems Development Essentials with Agile", coursePageLink("systems-development-essentials-with-agile"));
	menu[1].menuitem[x++] = new menuitemType("Systems Modelling Techniques with UML", coursePageLink("systems-modelling-techniques-with-uml"));
	menu[1].menuitem[x++] = new menuitemType("Systems Design and Implementation Techniques", coursePageLink("systems-design-and-implementation-techniques"));
	menu[1].menuitem[x++] = new menuitemType("Software Testing Foundation", coursePageLink("iseb-software-testing-foundation"));
	menu[1].menuitem[x++] = new menuitemType("Business Analysis Essentials", coursePageLink("business-analysis-essentials"));
	menu[1].menuitem[x++] = new menuitemType("Integrating Off-the-shelf Software Solutions", coursePageLink("integrating-off-the-shelf-software-solutions"));
	menu[1].menuitem[x++] = new menuitemType("Foundation in Business Analysis", coursePageLink("foundation-in-business-analysis"));
	menu[1].menuitem[x++] = new menuitemType("Enterprise and Solution Architecture Intermediate", coursePageLink("enterprise-and-solution-architecture-intermediate"));
	menu[1].menuitem[x++] = new menuitemType("Systems Development Diploma oral revision workshop", coursePageLink("systems-development-diploma-oral-revision-workshop"));
	menu[1].menuitem[x++] = new menuitemType("Data Modelling", coursePageLink("data-modelling"));
	menu[1].menuitem[x++] = new menuitemType("DSDM Atern Awareness", coursePageLink("dsdm-atern-awareness"));
	menu[1].menuitem[x++] = new menuitemType("DSDM Atern Practitioner", coursePageLink("dsdm-atern-practitioner"));
	menu[1].menuitem[x++] = new menuitemType("Systems Analysis and Specification using UML", coursePageLink("systems-analysis-and-specification-using-uml"));
	menu[1].menuitem[x++] = new menuitemType("UML and UP Overview", coursePageLink("uml-and-up-overview"));

	x=0;
	menu[2].menuitem[x++] = new menuitemType("Software Testing Foundation", coursePageLink("iseb-software-testing-foundation"));
	menu[2].menuitem[x++] = new menuitemType("Software Testing Intermediate", coursePageLink("software-testing-intermediate"));
	menu[2].menuitem[x++] = new menuitemType("Software Test Analyst", coursePageLink("software-test-analyst"));
	menu[2].menuitem[x++] = new menuitemType("Software Test Management", coursePageLink("software-test-management"));
	menu[2].menuitem[x++] = new menuitemType("Business Acceptance Testing", coursePageLink("business-acceptance-testing"));

	
	x=0;
	menu[3].menuitem[x++] = new menuitemType("ISEB Foundation in IS Project Management", coursePageLink("iseb-foundation-in-is-project-management"));
	menu[3].menuitem[x++] = new menuitemType("Project Risk Management", coursePageLink("project-risk-management"));
	menu[3].menuitem[x++] = new menuitemType("Benefits Management and Business Acceptance", coursePageLink("benefits-management-and-business-acceptance"));
	menu[3].menuitem[x++] = new menuitemType("Making the Business Case", coursePageLink("making-the-business-case"));
  menu[3].menuitem[x++] = new menuitemType("Introduction to Supplier Management", coursePageLink("introduction-to-supplier-management"));

	x=0;
	menu[4].menuitem[x++] = new menuitemType("Enterprise and Solution Architecture Intermediate", coursePageLink("enterprise-and-solution-architecture-intermediate"));
	menu[4].menuitem[x++] = new menuitemType("Enterprise and Solution Architecture Intermediate and Practitioner", coursePageLink("enterprise-and-solution-architecture-intermediate-and-practitioner"));
	menu[4].menuitem[x++] = new menuitemType("Enterprise and Solution Architecture Practitioner", coursePageLink("enterprise-and-solution-architecture-practitioner"));

	
  setUpMenus();
}

// sets up exam drop menus as DOM structure.
function initDropDownExamMenus() {

	// ** Note: this may run before the document is completely loaded **
	// **       therefore most things are undefined                   **

	// create data structure representing the drop-down menus..
  NumberOfMenus = 7;
	menu = new Array(NumberOfMenus);

  h=10; /* horizontal positioning adjustment of all menus */

  /* widths of main menus edge (copy from html) */
  hpos = new Array(NumberOfMenus);
  hpos[0] = 80; /* left padding */
  hpos[1] = 109+1;
  hpos[2] = 117+1;
  hpos[3] = 108+1;
  hpos[4] = 95+1;
  hpos[5] = 112+1;
  hpos[6] = 70+1;


  x=0;
  h+=hpos[x];
	menu[x++] = new menuType(4, h);
  h+=hpos[x];
	menu[x++] = new menuType(2, h);
  h+=hpos[x];
	menu[x++] = new menuType(13, h);
  h+=hpos[x];
	menu[x++] = new menuType(3, h);
  h+=hpos[x];
	menu[x++] = new menuType(1, h);
  h+=hpos[x];
	menu[x++] = new menuType(1, h);
  h+=hpos[x];
	menu[x++] = new menuType(1, h);

	for (var mi=0; mi<NumberOfMenus; mi++) {
		menu[mi].menuitem = new Array(menu.numberOfMenuitems);
	}

	x=0;
	menu[0].menuitem[x++] = new menuitemType("Foundation Certificate in Business Analysis", examPageLink("iseb-foundation-certificate-in-business-analysis"));
	menu[0].menuitem[x++] = new menuitemType("Foundation Certificate in IT Enabled Business Change", examPageLink("iseb-foundation-certificate-in-it-enabled-business-change"));
	menu[0].menuitem[x++] = new menuitemType("Foundation Certificate in Software Testing", examPageLink("iseb-foundation-certificate-in-software-testing"));
	menu[0].menuitem[x++] = new menuitemType("Foundation Certificate in IS Project Management", examPageLink("iseb-foundation-certificate-in-is-project-management"));

	x=0;
	menu[1].menuitem[x++] = new menuitemType("Intermediate Certificate in Software Testing", examPageLink("iseb-intermediate-certificate-in-software-testing"));
	menu[1].menuitem[x++] = new menuitemType("Intermediate Certificate in Enterprise and Solution Architecture", examPageLink("iseb-intermediate-certificate-in-enterprise-and-solution-architecture"));

	x=0;
	menu[2].menuitem[x++] = new menuitemType("Certificate in Business Analysis Essentials", examPageLink("iseb-certificate-in-business-analysis-essentials"));
	menu[2].menuitem[x++] = new menuitemType("Certificate in Requirements Engineering", examPageLink("iseb-certificate-in-requirements-engineering"));
	menu[2].menuitem[x++] = new menuitemType("Certificate in Organisational Context", examPageLink("iseb-certificate-in-organisational-context"));
	menu[2].menuitem[x++] = new menuitemType("Certificate in Modelling Business Processes", examPageLink("iseb-certificate-in-modelling-business-processes"));
	menu[2].menuitem[x++] = new menuitemType("Certificate in Benefits Management and Business Acceptance", examPageLink("iseb-certificate-in-benefits-management-and-business-acceptance"));
	menu[2].menuitem[x++] = new menuitemType("Certificate in Systems Development Essentials", examPageLink("iseb-certificate-in-systems-development-essentials"));
	menu[2].menuitem[x++] = new menuitemType("Certificate in Systems Modelling Techniques", examPageLink("iseb-certificate-in-systems-modelling-techniques"));
	menu[2].menuitem[x++] = new menuitemType("Certificate in Systems Design Techniques", examPageLink("iseb-certificate-in-systems-design-techniques"));
	menu[2].menuitem[x++] = new menuitemType("Certificate in Integrating Off-the-shelf Software Solutions", examPageLink("iseb-certificate-in-integrating-off-the-shelf-software-solutions"));
	menu[2].menuitem[x++] = new menuitemType("Practitioner Certificate in Test Management", examPageLink("iseb-practitioner-certificate-in-test-management"));
	menu[2].menuitem[x++] = new menuitemType("Practitioner Certificate in Test Analysis", examPageLink("iseb-practitioner-certificate-in-test-analysis"));
	menu[2].menuitem[x++] = new menuitemType("Practitioner Certificate in Enterprise and Solution Architecture", examPageLink("iseb-practitioner-certificate-in-enterprise-and-solution-architecture"));
	menu[2].menuitem[x++] = new menuitemType("Certificate in IS Consultancy Essentials", examPageLink("iseb-certificate-in-is-consultancy-essentials"));

	x=0;
	menu[3].menuitem[x++] = new menuitemType("International Diploma in Business Analysis", examPageLink("iseb-diploma-in-business-analysis"));
	menu[3].menuitem[x++] = new menuitemType("International Diploma in Solution Development", examPageLink("iseb-diploma-in-systems-development"));
	menu[3].menuitem[x++] = new menuitemType("Certificate in IS Consultancy Practice", examPageLink("iseb-certificate-in-is-consultancy-practice"));

	x=0;
	menu[4].menuitem[x++] = new menuitemType("ISEB Professional Award", basehref()+"iseb-professional-award.php");

	x=0;
	menu[5].menuitem[x++] = new menuitemType("IIBA CBAP", examPageLink("iiba-cbap"));

	x=0;
	menu[6].menuitem[x++] = new menuitemType("ACCA cpd", basehref()+"acca.php");



  setUpMenus();
}


function debugWrite(x) {

  if (debugMode) {
	
	var d = document.getElementById('debugArea');
	if (d) {
		var textnode  = document.createTextNode(x);
		var p = document.createElement('p');
		p.style.fontSize="7pt";
		p.style.width="730px";
		p.style.paddingLeft="230px";
		p.style.marginTop=0;
		p.style.marginBottom="1px";
		p.appendChild(textnode);
		d.appendChild(p);
	}
  }
}

function tintMenuTrigger(n, color) {

  // changes bg color of the menuTrigger (the object that's hovered over to invoke the menu)
  var id = "courseCat" + n;
  var node = document.getElementById(id);
  if (node) {
    node.style.backgroundColor = color;
  }
//  debugWrite ("Tint: id: " + id + "\n" + node.outerHTML);
}



function showMenu(n) {

	// display the menu whose number is supplied as argument

	var node = document.getElementById('placeholder');
	var ddm = menu[n].table;
	node.appendChild(ddm);

//	debugWrite ("show menu " + n + "\n" + node.innerHTML);

  tintMenuTrigger(n, "rgb(146,92,116)");
}


function hideMenu(n) {

	// remove display of the menu whose number is supplied as argument

//	debugWrite ("hide menu " + n);
	var ddm = menu[n].table;
	var node = document.getElementById('placeholder');
	for (var i=0; i<node.childNodes.length; i++)
	{
		if (node.childNodes[i]==ddm) {
			node.removeChild(ddm);
			break;
		}
	}
  tintMenuTrigger(n, "rgb(165,167,173)");
}


function hideCurrentMenu() {

// Hides the currently displayed menu - if there is one.

	if (mnuSelected != -1) {
//		debugWrite("hideCurrentMenu: mnuSelected= "+mnuSelected);
		hideMenu(mnuSelected);
		mnuSelected=-1;
	}
}


function switchMenu(newMenuNumber) {

// if the requested menu is already displayed, does nothing.
// otherwise hides the currently displayed menu (if one is displayed)
// and displays the specified one.

//    debugWrite("switchMenu " + newMenuNumber);
	if (mnuSelected!=newMenuNumber)	{
		hideCurrentMenu();
		showMenu(newMenuNumber);
		mnuSelected=newMenuNumber;
	}
}


//                ************             Timeout Stuff             ****************

// The "show" timer is intended to prevent "flicker" of a menu as the cursor passes over it en route
// to some other link by requiring a finite hover time.

function showTimeout() {
// The timer that delays showing the menu has expired, so now show it.
	if (pendingMenu!=-1) {
		switchMenu(pendingMenu);
//		debugWrite("showTimeout: "+pendingMenu);
	}
	else {
//		debugWrite("showTimeout: no menu to show");
	}
	pendingMenu= -1;
}

function showMenuOnTimeout(menu) {
	if (showTimeoutId != 0) clearTimeout(showTimeoutId);
	pendingMenu = menu;
	showTimeoutId = setTimeout("showTimeout()", showMenuTimeDelay);
}

function cancelShowTimeout() {
	if (showTimeoutId != 0) clearTimeout(showTimeoutId);
	pendingMenu = -1;
}

// **** menu removal timeouts ****

function mainRemoveTimeout() {
// removal of drop menus.
//	debugWrite("menu hidden by timeout")
	hideCurrentMenu();
}


function cancelMainRemoveTimeout() {
//	debugWrite("cancelled main timeout")
	clearTimeout(mainRemoveTimeoutId);
	mainRemoveTimeoutId=0;
}


function mouseGone() {
	if (mainRemoveTimeoutId != 0) clearTimeout(mainRemoveTimeoutId);
	mainRemoveTimeoutId = setTimeout("mainRemoveTimeout()", 250);
}


//                 **************              events                 ***************


function getDropMenuNumber(x) {
// arg. is the menu number from the menu bar 
	var m=-1;

  if ((x < NumberOfMenus) && (x >= 0)) {
    m=x;
	}
  return m;
}

function menuTrigger(x) {

// Argument is the menuTrigger number
// If a menu is already present, acts immediately:
//      If trigger x has a drop menu, switches to it.
//      If x represents a menuTrigger without a drop menu, hides the currently displayed drop-down menu
// If no menu is currently displayed, it starts a timer. If the timer is already running, it restarts it
//		with a new (or same) pendingMenu.

//	debugWrite("event: menuTrigger "+x+", " + this.name);

	var m = getDropMenuNumber(x);
	if (mnuSelected != -1) {
		// There is a drop menu currently up
		if (m == -1) {
			hideCurrentMenu(); // if non-drop menu selected
		}
		else {
			switchMenu(m);
		}
	}
	else {
		// there is no drop menu present.. restart timer, even if same menu, on the grounds that
		// you don't get 2 hover events without leaving.
		cancelShowTimeout(); // cancel (e.g. in case moved to a non-drop menu)
		if (m != -1) {
			showMenuOnTimeout(m);
		}
	}	
	cancelMainRemoveTimeout();
}

// unHover menu trigger

function unMenuTrigger(x) {

// Argument is the menuTrigger number

//	debugWrite("event:unMenuTrigger "+x+", " + this.name);

	mouseGone();

	// if the show timer is running, cancel it
	cancelShowTimeout();
}

// hover menuitem 

function menuItemSelected(event){
	if (!event) {
		event=window.event; //IE
		this.style.backgroundColor='rgb(146,92,116)';
	}
	else {
		event.currentTarget.style.backgroundColor='rgb(146,92,116)';
	}
	var target = (window.event) ? event.srcElement : event.target;
	var thisnode = (event.currentTarget) ? event.currentTarget : this;
//	debugWrite("event:hover" + thisnode.name+". Target: " +target.name);

	cancelMainRemoveTimeout();
}

// un hover menuitem 

function menuItemDeSelected(event){
	if (!event) {
		event=window.event; //IE
		this.style.backgroundColor='';
	}
	else {
		event.currentTarget.style.backgroundColor='';
	}
	var target = (window.event) ? event.srcElement : event.target;
	var thisnode = (event.currentTarget) ? event.currentTarget : this;

//	debugWrite("event:unhover " + thisnode.name+". Target: " +target.name);

	mouseGone();
}


var mnuSelected = -1;
var menuDisplayOn = false;		// used to prevent the menu being removed by an onmouseout initiated timeout

var menu;				// array of drop menus

var mainRemoveTimeoutId=0;
var pendingMenu= -1;		// menu to be displayed when showTimer expires
var showTimeoutId=0;
var showMenuTimeDelay=100;	// slug time on drop menu appearance, ms.

var basephphref = basehref()+"php/";

var debugMode=true;	// controls debug printout

