
// window.onload functions --------------------------------------------------------------------------------------------------------
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') { window.onload = func; }
	else { window.onload = function() { oldonload(); func(); } }
}

// initialise XMLHTTPRequest object --------------------------------------------------------------------------------------------------------
function GetXmlHttpObject() {
	var xmlHttp;
	try { xmlHttp=new XMLHttpRequest(); } // Firefox, Opera 8.0+, Safari 
	 catch (e) { try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } // Internet Explorer
      catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	   catch (e) { alert("Your browser does not support AJAX!"); return false; }}}
	return xmlHttp;
}

// cookies --------------------------------------------------------------------------------------------------------------------------------
var cookieEnabled=(navigator.cookieEnabled)? true : false // This next script checks whether cookies are enabled (and workable via javascript).
if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ //if not IE4+ nor NS6+
	document.cookie="testcookie"
	cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
}
function createCookie(name,value,days) { // days - 0 = cookie expires when browser is closed, > 0 = cookie expires in X days time
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}

// wrap all children of a given parent tag with a new nested tag -----------------------------------------------------------------
function wrapChildrenWith(thisTag, thisParent) {
	while (thisParent.hasChildNodes) { 
		try { thisTag.appendChild(thisParent.firstChild); }
		catch(e) { break; }
	};
	thisParent.appendChild(thisTag);
}






















// Home page image

function setHomeMessage() {
//	if (!document.getElementByID) { return; }
//	if (!document.getElementByID("homeBoxes")) { return; }

	// If cookies are NOT enabled, choose a random image.
//	if (!cookieEnabled) {
		
//	}
}
//addLoadEvent(setHomeMessage);













// Sub menu

function subMenuPrep() { // this is called immediately after loading the navigation
	var subMenuId = 'mainNav'; // set this the ID of the main navigation. It must be the ID of the UL element.

	if (!document.getElementById) { return }
	if (!document.getElementById(subMenuId)) { return }
	if (!document.getElementsByTagName) { return }

	var mainNav = document.getElementById(subMenuId);
	var uls = mainNav.getElementsByTagName("ul"); // get all ULs in #mainNav
	var div1, div2, divparent, dvs, lnk, found, encLI;
	for (var i=0; i<uls.length; i++) {
		lnk = uls[i].parentNode.getElementsByTagName("a")[0];
		divparent = uls[i].parentNode;
		uls[i].style.display="block"; // overrule the CSS declaration that hides this for the benefit of those without Javascript
		uls[i].parentNode.className="LIwithSubMenu"; // to the enclosing LI, add the class="LIwithSubMenu"
		// add new enclosing DIVs
		div1 = document.createElement("div"); div1.className="sm1";
		div2 = document.createElement("div"); div2.className="sm2";
		div2.appendChild(uls[i]);
		div1.appendChild(div2);
		divparent.appendChild(div1);
		// add the event handlers
		lnk.onmouseover = function() { subMenu(this,"y"); };
		lnk.onmouseout =  function() { subMenu(this,"n"); };
		found=false; encLI=uls[i]; while (!found) { encLI=encLI.parentNode; if (encLI.nodeName.toLowerCase() == "li") { found=true; } } // get to the enclosing LI
		dvs = encLI.getElementsByTagName("div"); // get all the DIVs within the LI and add the event handlers
		for (var j=0; j<dvs.length; j++) {
			dvs[j].onmouseover = function() { subMenu(this,"y"); };
			dvs[j].onmouseout =  function() { subMenu(this,"n"); };
		}
	}
}

function subMenu(thisID,showMenu) {
	var found=false; while (!found) { thisID=thisID.parentNode; if (thisID.nodeName.toLowerCase()=="li") { found=true; } } // Get to the enclosing LI
	var dv = thisID.getElementsByTagName("div")[0]; // Get the first DIV (the one that's hidden)

	if (showMenu=="y") { dv.style.display="block"; } // show or hide depending on the instruction
	else { dv.style.display="none"; }
}










// Assessment popups

function assessPopupPrep() {
	if (!document.getElementById) { return; }
	if (!document.getElementById("assessmentPopups")) { return; }

	// manipulate all links
	var lnks = document.getElementById("assessmentPopups").getElementsByTagName("a");
	var href;
	for (var i=0; i<lnks.length; i++) {
		if (lnks[i].className == "pop") {
			lnks[i].theHREF = lnks[i].getAttribute("href");
			lnks[i].onclick = function() { openPopup(this, href); }
			lnks[i].setAttribute("href","javascript:;");
		}
	}
}
addLoadEvent(assessPopupPrep);


function openPopup(thisOne, href) {
	var lnk = thisOne;
	var li = lnk.parentNode;
	var dv = document.createElement("div");
	var h4 = document.createElement("h4");
	var h4txt = document.createTextNode(lnk.innerHTML);	
	var loadspan = document.createElement("span");
	var loading = document.createElement("img");
	loadspan.appendChild(loading);
	var cls = document.createElement("a");
	var clstxt = document.createTextNode("[close]");
	cls.setAttribute("href","javascript:;");
	cls.onclick = function() { closePopup(this); }
	cls.appendChild(clstxt);
	loading.setAttribute("src",pathToSiteRoot+"/img/loading_icon.gif");
	h4.appendChild(cls);
	h4.appendChild(h4txt);
	dv.appendChild(h4);
	dv.appendChild(loadspan);
	li.appendChild(dv);
	li.className = "opened";

	getPopupContent(loadspan, href, lnk);
}

function getPopupContent(thisDiv, href, lnk) {
	var popupOb=GetXmlHttpObject();
	var url=lnk.theHREF+"&contentOnly=true";
	url=url+"&randomid="+Math.random();
	popupOb.onreadystatechange= function() { getPCchanged(popupOb, thisDiv, href); } ;
	popupOb.open("GET",url,true);
	popupOb.send(null);
}
function getPCchanged(popupOb, thisDiv, href) {
	if (popupOb.readyState==4) {
		if (popupOb.status==200 || popupOb.status==304) {
			thisDiv.innerHTML = popupOb.responseText;
		}
		else { 
			window.location.href= href;
		}
	}
}

function closePopup(thisOne, href) {
	var lnk = thisOne.parentNode.parentNode;
	var li = lnk.parentNode;

	li.className = "";
	lnk.parentNode.removeChild(lnk);
}



function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_jumpMenuGo(selName,targ,restore){ //v3.0
  var selObj = MM_findObj(selName); if (selObj) MM_jumpMenu(targ,selObj,restore);
}
