function getQuery(variable) {	
	var i;
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	
	for (i = 0; i < vars.length; i++) {
		var pair = vars[i].split("=");
		
		if (pair[0] == variable) {
			return pair[1];
		}
	}
}

function setLocation(strToPass) {
	var linkToUpdate = location.href;
		
	linkToUpdate = linkToUpdate.substring(0,location.href.indexOf(location.search));
	
	var strQuery = location.search;
	
	if(strQuery.indexOf("&") != -1) {
		strQuery = strQuery.substring(0,strQuery.indexOf("&"));
	}
	
	if(location.href.indexOf("?") != -1 && location.href.indexOf("?block=") == -1) {
		location.href = linkToUpdate + strQuery + "&block=" + strToPass;
	}
	else {
		location.href = linkToUpdate + "?block=" + strToPass;
	}
}

function hide(id) {
	document.getElementById(id).style.display = "none";
}
	
function show(id) {
	document.getElementById(id).style.display = "block";
}

function hideArr(passArr) {
	var item;
	
	for (item in passArr) {
		hide(passArr[item]);
	}
}

function setBodyLink(passArr,passID) {
	var strLink;
	var bodyLinks = "<div id=\"bodyLink" + passID + "\" class=\"bodyLinkBlock\"><p>";
	var endBodyLinks = "<div id=\"bodyLinkEnd" + passID + "\" class=\"bodyLinkEndBlock\"><p><b>Page</b> ";
	var intCount = 0;
	
	var item;
	for (item in passArr) {
		if (passArr[item].indexOf("_DeadEnd") == -1) {
			strLink = document.getElementById("title" + passArr[item].substring(passArr[item].indexOf("_"))).innerHTML;
			intCount = intCount + 1;
			bodyLinks = bodyLinks + "<a href=\"?page=" + getQuery("page") + "&block=" + getQuery("block") + "&article=" + passArr[item].substring(passArr[item].indexOf("_") + 1) + "\" id=\"anchor_" + getQuery("block") + "_" + passArr[item].substring(passArr[item].indexOf("_") + 1) + "\">" + strLink + "</a><br />";
			endBodyLinks = endBodyLinks + "<a href=\"?page=" + getQuery("page") + "&block=" + getQuery("block") + "&article=" + passArr[item].substring(passArr[item].indexOf("_") + 1) + "\" id=\"anchorEndLink_" + getQuery("block") + "_" + passArr[item].substring(passArr[item].indexOf("_") + 1) + "\">" + intCount + "</a>&nbsp;&nbsp;";
		}
	}
	
	endBodyLinks = endBodyLinks + "</p></div>";
	bodyLinks = bodyLinks + "</p></div>";
	
	var currContent = document.getElementById(passID).innerHTML;
	
	currContent = bodyLinks + currContent + endBodyLinks;
	
	document.getElementById(passID).innerHTML = currContent;
}

function setDeadEndLink(passArr,passID) {
	var strLink;
	var bodyLinks = "<div class=\"sideBar\"><p>";
	
	var item;
	for (item in passArr) {
		if (passArr[item].indexOf("_Body") == -1) {
			strLink = document.getElementById("title" + passArr[item].substring(passArr[item].indexOf("_"))).innerHTML;
			bodyLinks = bodyLinks + "<a href=\"?page=" + getQuery("page") + "&block=" + getQuery("block") + "&article=" + passArr[item].substring(passArr[item].indexOf("_") + 1) + "\" id=\"anchor_" + getQuery("block") + "_" + passArr[item].substring(passArr[item].indexOf("_") + 1) + "\">" + strLink + "</a><br />";
		}
	}
	
	bodyLinks = bodyLinks + "</p></div>";
	
	var currContent = document.getElementById(passID).innerHTML;
	
	currContent = bodyLinks + currContent;
	
	document.getElementById(passID).innerHTML = currContent;
}

function setArticleLink(passID,passArr) {
	
	var bodyLinks = "<div class=\"sideBar\"><p><span class=\"subTitle\">Related Articles:</span><br />";
	
	var item;
	for (item in passArr) {
		bodyLinks = bodyLinks + "<a href=\"" + passArr[item][0] + "\" target=\"_blank\" onclick=\"window.open(this.href,'_blank','width=800,height=600,scrollbars,menubar,resizable'); return false;\">" + passArr[item][1] + "</a><br />";
	}
	
	bodyLinks = bodyLinks + "</p></div>";
	
	var currContent = document.getElementById(passID).innerHTML;
	
	currContent = currContent + bodyLinks;
	
	document.getElementById(passID).innerHTML = currContent;
}

if (typeof blockArr == "object") {
	hideArr(blockArr);
}

if (getQuery("block")) {
	
	//Set the display style of the DIV passed as "BLOCK"
	show("block" + getQuery("block"));
	
	//Create a List of ALL THE DIVs on the page
	var tempArr = document.getElementsByTagName("div");
	
	//Create SUB ARRAY
	var tempSubArr = new Array();
	var countSub = 0;
	
	//Loop through ALL THE DIVs on the page.
	for (var count = 0; count < tempArr.length; count++) {
		//Look for the ID of each div to contain the phrase "block" and the string passed in the URL.
		if(tempArr[count].id.indexOf("block" + getQuery("block")) > -1) {
			//Make sure the the ID we get IS NOT the ID for the main DIV
			if(tempArr[count].id != "block" + getQuery("block")) {
				tempSubArr[countSub++] = tempArr[count].id;
			}
		}
	}
	
	//Pass the SUB ARRAY to the hideArr function to Hide ALL THE SUB DIVs
	hideArr(tempSubArr);
	
	//Look to see if an article has been set in the URL. If it has NOT the show the first block.
	if (getQuery("article")) {
		show("block" + getQuery("block") + "_" + getQuery("article"));
		setBodyLink(tempSubArr,"block" + getQuery("block") + "_" + getQuery("article"));
		setDeadEndLink(tempSubArr,"relatedResources_" + getQuery("block"));
		getArticleLink(getQuery("block"),getQuery("article"));
		document.getElementById("anchor_" + getQuery("block") + "_" + getQuery("article")).className = "current";
		if (getQuery("article").indexOf("DeadEnd") < 0) {
			document.getElementById("anchorEndLink_" + getQuery("block") + "_" + getQuery("article")).className = "current";
		}
		else {
			hide("bodyLinkblock" + getQuery("block") + "_" + getQuery("article"));
			document.getElementById("bodyLinkEndblock" + getQuery("block") + "_" + getQuery("article")).innerHTML = "<p><a href=\"JavaScript: setLocation('" + getQuery("block") + "');\">Return to " + document.getElementById(getQuery("block")).innerHTML + "</a></p>";
		}
	}
	else {
		show(tempSubArr[0]);
		setBodyLink(tempSubArr,tempSubArr[0]);
		setDeadEndLink(tempSubArr,"relatedResources_" + getQuery("block"));
		getArticleLink(getQuery("block"),"");
		document.getElementById("anchor_" + getQuery("block") + "_" + tempSubArr[0].substring(tempSubArr[0].indexOf("_") + 1)).className = "current";
		document.getElementById("anchorEndLink_" + getQuery("block") + "_" + tempSubArr[0].substring(tempSubArr[0].indexOf("_") + 1)).className = "current";
	}
	
	document.getElementById(getQuery("block")).className = "current";
}
else
{
	show(blockArr[0])
}