
// 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);
}



























// 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"; }
}





function randomTopImage() {
	if (!document.getElementById) { return }
	if (!document.getElementById("topquoteimg")) { return }

	var noOfImages;

	if (gblSuffix=='dotau') {
		if ( typeof( window[ 'quotesFor' ] ) != "undefined" ) {
			if (quotesFor=='louise') {
				noOfImages = 2;
				var randomNumber = (Math.floor(Math.random()*noOfImages))+1;
				document.getElementById("topquoteimg").src = '/img_topbarsAU/quote_'+randomNumber+'.jpg';
			}
			else if (quotesFor=='peter') {
				noOfImages = 2;
				var randomNumber = (Math.floor(Math.random()*noOfImages))+1;
				randomNumber = randomNumber+2;
				document.getElementById("topquoteimg").src = '/img_topbarsAU/quote_'+randomNumber+'.jpg';
			}
			else if (quotesFor=='alun') {
				document.getElementById("topquoteimg").src = '/img_topbarsAU/quote_5.jpg';
			}
			else if (quotesFor=='matthew') {
				document.getElementById("topquoteimg").src = '/img_topbarsAU/quote_6.jpg';
			}
			else {
				noOfImages = 8;
				var randomNumber = (Math.floor(Math.random()*noOfImages))+1;
				document.getElementById("topquoteimg").src = '/img_topbarsAU/quote_'+randomNumber+'.jpg';
			}
		}
		else {
			noOfImages = 8;
			var randomNumber = (Math.floor(Math.random()*noOfImages))+1;
			document.getElementById("topquoteimg").src = '/img_topbarsAU/quote_'+randomNumber+'.jpg';
		}
	}
	else {
		noOfImages = 31;
		var randomNumber = (Math.floor(Math.random()*noOfImages))+1;
		document.getElementById("topquoteimg").src = '/img_topbars/quote'+randomNumber+'.jpg'; }
}


function fontResizePrep() {
	if (!document.getElementsByTagName) {return;}
//	if (cookieEnabled) {return;}

	if (readCookie("fontsize")=="larger") {
		document.getElementsByTagName("body")[0].className = "larger"; }
	else {
		document.getElementsByTagName("body")[0].className = "normal"; }
}
addLoadEvent(fontResizePrep);

function fontNormal() {
	if (!document.getElementsByTagName) {return;}
	document.getElementsByTagName("body")[0].className = "normal";
	createCookie("fontsize","normal",10)
}
function fontEnlarge() {
	if (!document.getElementsByTagName) {return;}
	document.getElementsByTagName("body")[0].className = "larger";
	createCookie("fontsize","larger",10)
}



