function loadBanner()
{
	var pageToLoad;
	if (750<self.screen.height)
	{
		pageToLoad = "banner.jpg + width='100%' align='right'";
	}
	else
	{
		pageToLoad = "logo.jpg + width='40%' align='left'";
	}
	document.getElementById("banner").innerHTML = "<img alt='Cynllun Trosolwg Ltd - System and Software Engineers' src=" + pageToLoad + " vspace='0' hspace='0'  >";
}
function Relocate(page)
{
	BrowserDetect.init();
	if (
		("Explorer"==BrowserDetect.browser) ||
		("Mozilla"==BrowserDetect.browser) ||
		("Safari"==BrowserDetect.browser) ||
		("Camino"==BrowserDetect.browser) ||
		("Opera"==BrowserDetect.browser) ||
		("Konqueror"==BrowserDetect.browser) ||
		("Firefox"==BrowserDetect.browser) ||
		("Netscape"==BrowserDetect.browser) 
	)
	{
//		if (0!=page.length) page = "?"+page;
		// Replace next line with commented out line that follows before distributing!
		// location.href = "index.htm"+page;
		location.href = "http://www.trosolwg.co.uk/"+page;
	}
}
function OpenItems(listElement, level)
{
	var item;
	var subitem;
//	alert("Element count: "+ listElement.childNodes.length + ", level" +level);
	for (var i = 0; i < listElement.childNodes.length; i++) 
	{
		item = listElement.childNodes[i];
		if (item.nodeName == "LI") 
		{
			SetItem(item, level);
			for (var subi = 0; subi < item.childNodes.length; subi++) 
			{
				subitem = item.childNodes[subi];
				if (subitem.nodeName == "UL") 
				{
					OpenItems(subitem, level+1);
				}
			}
		}
	}
}
function SetItem(listElement, level)
{
	var indentation;
	switch (level)
	{
		case 0:
			listElement.style.fontSize = "medium";
			indentation = "-50px";
			listElement.style.marginLeft = "15px";
			break;
		case 1:
			indentation = "-80px;";
			// Indentation for the benefit of Firefox 3.0
			listElement.style.marginLeft = "-40px";
			break;
		case 2:
			indentation = "-20px";
			// and remove it for lower indentation
			listElement.style.marginLeft = "0px";
			break;
		case 3:
			indentation = "-30px";
			listElement.style.marginLeft = "0px";
			break;
		case 4:
		default:
			indentation = "0px";
			listElement.style.marginLeft = "0px";
			break;
	}
	listElement.style.textIndent = indentation;
}
function OpenContents(page)
{
	loadBanner();
	// Don't follow this path for IE as it screws up
	if(!document.all)
	{
		var listElement;
		var uls;
		uls = document.getElementById("contentsList").getElementsByTagName("ul");
		for (var i=0;i<uls.length;i++) 
		{
			listElement=uls[i];
			if ("UL"==listElement.nodeName && "mktree"==listElement.className) 
			{
				OpenItems(listElement, 0);
			}
		}
	}
	if (0!=page.length)
	{
		var ctsTable = document.getElementById("contentsList").getElementsByTagName("ul");
		for (var uli=0;uli<ctsTable.length;uli++) 
		{
			var ul=ctsTable[uli];
			if (ul.nodeName=="UL" && ul.className=="mktree") 
			{
				openListElement(ul, page);
			}
		}
	}
}
function getScreenWidth()
{
	var x;
	if (top.document.documentElement && top.document.documentElement.clientWidth)
	// Explorer 6 Strict Mode
	{
		x = top.document.documentElement.clientWidth;
	}
	else 
		if (top.document.body) // other Explorers
		{
			x = top.document.body.clientWidth;
		}
	return x;
}
function getScreenHeight()
{
	var y;
	if (top.document.documentElement && top.document.documentElement.clientHeight)
	{
		y = top.document.documentElement.clientHeight;
	}
	else 
		if (top.document.body) 
		{
			y = top.document.body.clientHeight;
		}
	return y;
}
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	]
};
function loadQuery()
{
	var query = (location.href.indexOf("?")+1);
	if (query)
	{
		top.main.location.href = location.href.substring(query);
	}
}
function openPopUp(doc)
{
	var targetDocument = "formbody.htm?" +doc;
	var theScreenHeight = Math.min(getScreenHeight(),800);
	var theScreenParams = "status=1,toolbar=0,menubar=0,location=0,resizable=1,scrollbars=1, width=500, height=";
	theScreenParams +=theScreenHeight;
	mailWindow = window.open(targetDocument,null, theScreenParams);
}
function openDownloadPopUp(doc)
{
	var targetDocument = "download_form.htm";
	var theScreenHeight = Math.min(getScreenHeight(),800);
	var theScreenParams = "status=1,toolbar=0,menubar=0,location=0,resizable=1,scrollbars=1, width=700, height=";
	theScreenParams +=theScreenHeight;
	mailWindow = window.open(targetDocument,null, theScreenParams);
}

function openListElement(ul, page)
{
	if (!ul.childNodes || ul.childNodes.length==0) { return false; }
	var returnValue = false;
	for (var itemi=0;itemi<ul.childNodes.length;itemi++) 
	{
		var item = ul.childNodes[itemi];
		if (item.nodeName == "LI") 
		{
			if(item.className=="liOpen") item.className="liClosed";
			for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) 
			{
				var sitem = item.childNodes[sitemi];
				if (sitem.nodeName=="UL") 
				{
					if (openListElement(sitem, page))
					{
						if(item.className!="liBullet") item.className="liOpen";
						returnValue = true;
					}
				}
				if (sitem.nodeName=="A")
				{
					var filepath = new RegExp("/"+page, "i");
					var file = sitem.href.match(filepath);
					if (file && sitem.href[sitem.href.length-1]!='#')
					{
						if(item.className!="liBullet") item.className="liOpen";
						sitem.style.color = "#191970";
						return true;
					}
					else
					{
						if ((sitem.style.color == "#191970")||
							(sitem.style.color == "rgb(25, 25, 112)")) sitem.style.color = "gray";
					}
				}
			}
		}
	}
	return returnValue;
}
function swapImage() 
{ 
	var i,j=0,x,a=swapImage.arguments; 
	document.sr=new Array; 
	for(i=0;i<(a.length-2);i+=3)
	{
		if ((x=findObj(a[i]))!=null)
		{
			document.sr[j++]=x; 
			if(!x.oSrc) x.oSrc=x.src; 
			x.src=a[i+2];
		}
	}
}
function findObj(n, d) 
{ 
	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=findObj(n,d.layers[i].document);
	}
	if(!x && d.getElementById) x=d.getElementById(n); 
	return x;
}
function swapImgRestore() 
{ 
	var i,x,a=document.sr; 
	for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++)
	{
		x.src=x.oSrc;
	}
}
// Automatically attach a listener to the window onload, to convert the trees
addEvent(window,"load",convertTrees);

// Utility function to add an event listener
function addEvent(o,e,f){
	if (o.addEventListener){ o.addEventListener(e,f,true); return true; }
	else if (o.attachEvent){ return o.attachEvent("on"+e,f); }
	else { return false; }
}

// utility function to set a global variable if it is not already set
function setDefault(name,val) {
	if (typeof(window[name])=="undefined" || window[name]==null) {
		window[name]=val;
	}
}

// Full expands a tree with a given ID
function expandTree(treeId) {
	var ul = document.getElementById("contentsList").getElementById(treeId);
	if (ul == null) { return false; }
	expandCollapseList(ul,nodeOpenClass);
}

// Fully collapses a tree with a given ID
function collapseTree(treeId) {
	var ul = document.getElementById("contentsList").getElementById(treeId);
	if (ul == null) { return false; }
	expandCollapseList(ul,nodeClosedClass);
}

// Expands enough nodes to expose an LI with a given ID
function expandToItem(treeId,itemId) {
	var ul = document.getElementById("contentsList").getElementById(treeId);
	if (ul == null) { return false; }
	var ret = expandCollapseList(ul,nodeOpenClass,itemId);
	if (ret) {
		var o = document.getElementById(itemId);
		if (o.scrollIntoView) {
			o.scrollIntoView(false);
		}
	}
}

// Performs 3 functions:
// a) Expand all nodes
// b) Collapse all nodes
// c) Expand all nodes to reach a certain ID
function expandCollapseList(ul,cName,itemId) {
	if (!ul.childNodes || ul.childNodes.length==0) { return false; }
	// Iterate LIs
	for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
		var item = ul.childNodes[itemi];
		if (itemId!=null && item.id==itemId) { return true; }
		if (item.nodeName == "LI") {
			// Iterate things in this LI
			var subLists = false;
			for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
				var sitem = item.childNodes[sitemi];
				if (sitem.nodeName=="UL") {
					subLists = true;
					var ret = expandCollapseList(sitem,cName,itemId);
					if (itemId!=null && ret) {
						item.className=cName;
						return true;
					}
				}
			}
			if (subLists && itemId==null) {
				item.className = cName;
			}
		}
	}
}

// Search the document for UL elements with the correct CLASS name, then process them
function convertTrees() {
	setDefault("treeClass","mktree");
	setDefault("nodeClosedClass","liClosed");
	setDefault("nodeOpenClass","liOpen");
	setDefault("nodeBulletClass","liBullet");
	setDefault("nodeLinkClass","bullet");
	setDefault("preProcessTrees",true);
	if (preProcessTrees) {
		if (!document.createElement) { return; } // Without createElement, we can't do anything
		uls = document.getElementById("contentsList").getElementsByTagName("ul");
		for (var uli=0;uli<uls.length;uli++) {
			var ul=uls[uli];
			if (ul.nodeName=="UL" && ul.className==treeClass) {
				processList(ul);
			}
		}
	}
}

// Process a UL tag and all its children, to convert to a tree
function processList(ul) {
	if (!ul.childNodes || ul.childNodes.length==0) { return; }
	// Iterate LIs
	for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
		var item = ul.childNodes[itemi];
		if (item.nodeName == "LI") {
			// Iterate things in this LI
			var subLists = false;
			for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
				var sitem = item.childNodes[sitemi];
				if (sitem.nodeName=="UL") {
					subLists = true;
					processList(sitem);
				}
			}
			var s= document.createElement("SPAN");
			var t= '\u00A0'; // &nbsp;
			s.className = nodeLinkClass;
			if (subLists) {
				// This LI has UL's in it, so it's a +/- node
				if (item.className==null || item.className=="") {
					item.className = nodeClosedClass;
				}
				// If it's just text, make the text work as the link also
				if (item.firstChild.nodeName=="#text") {
					t = t+item.firstChild.nodeValue;
					item.removeChild(item.firstChild);
				}
				s.onclick = function () {
					this.parentNode.className = (this.parentNode.className==nodeOpenClass) ? nodeClosedClass : nodeOpenClass;
					return false;
				}
			}
			else {
				// No sublists, so it's just a bullet node
				item.className = nodeBulletClass;
				s.onclick = function () { return false; }
			}
			s.appendChild(document.createTextNode(t));
			item.insertBefore(s,item.firstChild);
		}
	}
}
// openImage is needed for enlarging pictures, such as Costs Graph (see cost_effective_development.htm)
// the file opened is costsgraph.htm which includes the title and close window option. 
// Both costsgraph .jpg and costsgraph.htm are needed.
function openImage(img)
{
	var theScreenHeight = Math.min(getScreenHeight(),550);
	var theScreenWidth = Math.min(getScreenWidth(),600);
	var theScreenParams = "status=1,toolbar=0,menubar=0,location=0,resizable=1,scrollbars=1, width=";
	theScreenParams +=theScreenWidth+", height="+theScreenHeight;
	mailWindow = window.open(img,null, theScreenParams);
}
// used on manage_download page
function openDownload()
{
	var targetDocument = "./downloads/download_options.php";
	var theScreenHeight = Math.min(getScreenHeight(),800);
	var theScreenParams = "status=1,toolbar=0,menubar=0,location=0,resizable=1,scrollbars=1, width=800, height=";
	theScreenParams +=theScreenHeight;
	mailWindow = window.open(targetDocument,null, theScreenParams);
}
