// JavaScript Document

// IE Exclusion variable.
// A value of '6' means that all IE versions 6 and earlier will receive a warning message. Later versions will be accepted.
// If site is available to all versions of IE, set to zero '0';
var IEexclusion = 6;

// Apple Portable Exlusion variable.
// A value of 'true' will display a warning message to users of Apple portable devices (iPad, iPod and iPhone).
var ApplePortableExclusion = false;

//Variable for loading functions to be triggered upon window.onload
var loadItems = new Array();

//Function for triggering functions upon window.onload
function initPage(){
	for(var i = 0;i < loadItems.length;i++){
    	eval(loadItems[i]);
	}
}

//Function for adding functions to be triggered upon window.onload
function initAdd(func){
  loadItems[loadItems.length] = func;
}

//Variable for loading functions to be triggered upon window.resize
var resizeItems = new Array();

//Function for triggering functions upon window.resize
function resizePage(){
	for(var i = 0;i < resizeItems.length;i++){
    	eval(resizeItems[i]);
	}
}

//Function for adding functions to be triggered upon window.resize
function resizeAdd(func){
  resizeItems[resizeItems.length] = func;
}

//Code to ascertain IE Version.
if (navigator.appName == 'Microsoft Internet Explorer') {
	var ua = navigator.userAgent;
	var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
	if (re.exec(ua) != null){
		iev = parseFloat(RegExp.$1);
		if(iev <= IEexclusion){
			alert("Unfortunately this website content requires Internet Explorer version "+(IEexclusion+1)+" or newer, and you are using version "+iev+".\n\nTo enjoy all that this website has to offer, please update your browser.");		
		}
	}
}

//Code to ascertain Apple Portable Device.
var device = false;
if (navigator.userAgent.match(/iPhone/i)) {
	device = "iPhone";
} else if (navigator.userAgent.match(/iPod/i)) {
	device = "iPod";
} else if (navigator.userAgent.match(/iPad/i)) {
	device = "iPad";
}
if(ApplePortableExclusion && device){
	alert("Unfortunately this website content will not display correctly on your "+device+". We apologise for any inconvenience.");
}

//Function to retrieve active window area.
function getWindowSize(){
	windowSize=new Object();
	if( typeof( window.innerWidth ) == 'number' ) {
		windowSize.height = window.innerHeight; windowSize.width = window.innerWidth;
		windowSize.scrollHeight = window.pageYOffset; windowSize.scrollWidth = window.pageXOffset;
	} else if( document.documentElement && document.documentElement.clientWidth ) {
		windowSize.height = document.documentElement.clientHeight; windowSize.width = document.documentElement.clientWidth;
		windowSize.scrollHeight = document.documentElement.scrollTop; windowSize.scrollWidth = document.documentElement.scrollLeft;
	} else if( document.body && document.body.clientWidth ) {
		windowSize.height = document.body.clientHeight; windowSize.width = document.body.clientWidth;
		windowSize.scrollHeight = document.body.scrollTop; windowSize.scrollWidth = document.body.scrollLeft;
	}
	if(document.getElementById("pageSize")){
		windowSize.bodyHeight = document.getElementById("pageSize").offsetHeight;
		windowSize.bodyWidth = document.getElementById("pageSize").offsetWidth;
	}
	
	return windowSize;
}

function rollOverThis(theItem){
	theItem.style.opacity = (0.7);
	theItem.style.filter  = "alpha(opacity=70)";
}
function rollOutThis(theItem){
	theItem.style.opacity = (1);
	theItem.style.filter  = "alpha(opacity=100)";
}
var rollInterval = new Array();
var currentSpeed = new Array();
var motionStatus = new Array();
var modulus = new Array();
var itemSpeed = new Array();
var slowDistance = new Array();
var currentCapsule = 0;
var blindOpen = false;
var capsuleOpen = 0;
var nextBlind = 0;
var nextCapsule = 0;
var nextCapsule = 0;
var dropSpeed = 15;
var lastChapter = 0;

function blindAction1(capsule){
	if(document.getElementById("staticBanner")){
		document.getElementById("staticBanner").style.display = "block";
	}
	if(document.getElementById("homeBanner")){
		document.getElementById("homeBanner").style.display = "none";
	}
	
	clearInterval( rollInterval[currentCapsule] );
	
	//closeVideo();
	
	if(capsuleOpen > 0){
		nextCapsule = capsule;
		blindAction1up(capsuleOpen);
	} else {
		
		capsuleOpen = capsule;
		nextCapsule = 0;

		document.getElementById("navigation").style.height = "260px";

		document.getElementById("navblind"+capsule).style.left = "-360px";

		if(lastChapter > 0){
			alert(lastChapter+" "+capsule);
			document.getElementById("navchapter"+capsule+"_"+lastChapter).style.display = "none";
		}

		document.getElementById("navBot"+capsule).href = "javascript:blindAction1up("+capsule+");";
		document.getElementById("closeArrow"+capsule).style.display = "block";

		blindOpen = false;
		currentCapsule++;

		var startLoc = -235;
		var endLoc = 0;
		var travelDistance = endLoc - startLoc;

		startTravel(travelDistance,currentCapsule);
		rollInterval[currentCapsule] = setInterval ( "rollDown(\"navCapsule"+capsule+"\","+currentCapsule+","+startLoc+","+endLoc+","+slowDistance[currentCapsule]+","+itemSpeed[currentCapsule]+","+capsule+",1)", 15 );
	}
}

function blindAction1up(capsule){
	document.getElementById("closeArrow"+capsule).style.display = "none";
	document.getElementById("navBot"+capsule).href = "javascript:blindAction1("+capsule+");";
	currentCapsule++;
	var startLoc = 0;
	var endLoc = -235;
	var travelDistance = startLoc - endLoc;
	
	startTravel(travelDistance,currentCapsule);
	rollInterval[currentCapsule] = setInterval ( "rollDown(\"navCapsule"+capsule+"\","+currentCapsule+","+startLoc+","+endLoc+","+slowDistance[currentCapsule]+","+itemSpeed[currentCapsule]+","+capsule+",-1)", 15 );
}

function blindAction2(capsule,chapter){
	
	currentCapsule++;
	
	document.getElementById("navCapsule"+capsule).style.width = "540px";
	
	if(blindOpen){
		nextBlind = chapter;
		nextCapsule = capsule;
		blindAction2up(capsule);
	} else {
		document.getElementById("navchapter"+capsule+"_"+chapter).style.display = "block";
		var startLoc = -360;
		var endLoc = 0;
		var travelDistance = endLoc - startLoc;
		
		startTravel(travelDistance,currentCapsule);
		blindOpen = true;
		rollInterval[currentCapsule] = setInterval ( "rollAcross(\"navblind"+capsule+"\","+currentCapsule+","+startLoc+","+endLoc+","+slowDistance[currentCapsule]+","+itemSpeed[currentCapsule]+","+chapter+","+capsule+",1)", 15 );
	}
}

function blindAction2up(capsule){
	currentCapsule++;
	var startLoc = 0;
	var endLoc = -360;
	var travelDistance = startLoc - endLoc;
	
	startTravel(travelDistance,currentCapsule);
	
	rollInterval[currentCapsule] = setInterval ( "rollAcross(\"navblind"+capsule+"\","+currentCapsule+","+startLoc+","+endLoc+","+slowDistance[currentCapsule]+","+itemSpeed[currentCapsule]+","+lastChapter+","+capsule+",-1)", 15 );
}


function startTravel(travelDistance,capsule){
	itemSpeed[capsule] = dropSpeed;
	currentSpeed[capsule] = 1;
	motionStatus[capsule] = 0;
	slowDistance[capsule] = 999999;
	while((slowDistance[capsule] * 2) + itemSpeed[capsule] > travelDistance){
		itemSpeed[capsule]--;
		slowDistance[capsule] = ((itemSpeed[capsule] * (itemSpeed[capsule] + 1)) / 2);
	}
	var fullSpeedDistance = travelDistance - (slowDistance[capsule] * 2);
	modulus[capsule] = fullSpeedDistance % itemSpeed[capsule];
}

function rollDown(capsuleID,capsule,startLoc,endloc,slowDistance,dropSpeed,thisCapsule,direction){
	var capsuleObj = document.getElementById(capsuleID);
	if(capsuleObj.style.top == ""){
		var currentPos = startLoc;
	} else {
		var currentPos = parseInt(capsuleObj.style.top);
	}
	
	currentPos += (currentSpeed[capsule] * direction);
	
	if(motionStatus[capsule] == 0){
		currentSpeed[capsule]++;
	}
	if(currentSpeed[capsule] == dropSpeed){
		motionStatus[capsule] = 1;
	}
	if(direction > 0){
		if(currentPos >= endloc){
			currentPos = endloc;
			clearInterval( rollInterval[capsule] );
			/*
			if(currentChapter > 0){
				blindAction2(1,currentChapter);
			}
			*/
		} else if(currentPos >= endloc - slowDistance){
			if(modulus[capsule] > 0){
				currentPos++;
				modulus[capsule]--;
			}
			currentSpeed[capsule] = Math.max(currentSpeed[capsule]-1,1);
			//document.getElementById("output").innerHTML = document.getElementById("output").innerHTML + " " + currentSpeed[capsule];
		}
	} else {
		if(currentPos <= endloc){
			currentPos = endloc;
			if(lastChapter > 0){
				document.getElementById("navchapter"+thisCapsule+"_"+lastChapter).style.display = "none";
			}
			document.getElementById("navigation").style.height = "25px";		
			document.getElementById("navCapsule"+thisCapsule).style.width = "180px";
				
			clearInterval( rollInterval[capsule] );
			lastChapter = 0;
			capsuleOpen = 0;
			
			if(document.getElementById("staticBanner")){
				document.getElementById("staticBanner").style.display = "none";
			}
			if(document.getElementById("homeBanner")){
				document.getElementById("homeBanner").style.display = "block";
			}
			
			if(nextCapsule > 0){
				blindAction1(nextCapsule);
			}
		} else if(currentPos <= endloc + slowDistance){
			if(modulus[capsule] > 0){
				currentPos--;
				modulus[capsule]--;
			}
			currentSpeed[capsule] = Math.max(currentSpeed[capsule]-1,1);
			//document.getElementById("output").innerHTML = document.getElementById("output").innerHTML + " " + currentSpeed[capsule];
		}
	}
	capsuleObj.style.top = currentPos+"px";
}

function rollAcross(capsuleID,capsule,startLoc,endloc,slowDistance,dropSpeed,chapter,thisCapsule,direction){
	
	var capsuleObj = document.getElementById(capsuleID);
	
	if(capsuleObj.style.left == ""){
		var currentPos = startLoc;
	} else {
		var currentPos = parseInt(capsuleObj.style.left);
	}
	
	currentPos += (currentSpeed[capsule] * direction);
	
	if(motionStatus[capsule] == 0){
		currentSpeed[capsule]++;
	}
	if(currentSpeed[capsule] == dropSpeed){
		motionStatus[capsule] = 1;
	}
	if(direction > 0){
		if(currentPos >= endloc){
			currentPos = endloc;
			clearInterval( rollInterval[capsule] );
			nextCapsule = 0;
			nextBlind = 0;
			lastChapter = chapter;
			blindOpen = true;
		} else if(currentPos >= endloc - slowDistance){
			if(modulus[capsule] > 0){
				currentPos++;
				modulus[capsule]--;
			}
			currentSpeed[capsule] = Math.max(currentSpeed[capsule]-1,1);
			//document.getElementById("output").innerHTML = document.getElementById("output").innerHTML + " " + currentSpeed[capsule];
		}
	} else {
		if(currentPos <= endloc){
			currentPos = endloc;
			clearInterval( rollInterval[capsule] );
			blindOpen = false;
			document.getElementById("navchapter"+thisCapsule+"_"+chapter).style.display = "none";
			if(nextBlind > 0){
				document.getElementById("navchapter"+thisCapsule+"_"+nextBlind).style.display = "block";
				blindAction2(nextCapsule,nextBlind);
			}
		} else if(currentPos <= endloc + slowDistance){
			if(modulus[capsule] > 0){
				currentPos--;
				modulus[capsule]--;
			}
			currentSpeed[capsule] = Math.max(currentSpeed[capsule]-1,1);
			//document.getElementById("output").innerHTML = document.getElementById("output").innerHTML + " " + currentSpeed[capsule];
		}
	}
	capsuleObj.style.left = currentPos+"px";
}

//Just a demo function. Can be deleted.
function showWidthAndHeight(){
	windowSize = getWindowSize();
	document.getElementById("showStatus").innerHTML = windowSize.width + " x " + windowSize.height + " pixels visible area";
}

function setShadowHeight(){
	if(document.getElementById("reportContent")){
		var reportContent = document.getElementById("reportContent");
		document.getElementById("centreShadow").style.height = (reportContent.offsetHeight+3)+"px";
	}
}

function getAllClass(theParent,className){
	var allDivs = theParent.getElementsByTagName('div');
	var currentItem;
	var i = 0;
	var allItems = new Array();
	while (currentItem = allDivs.item(i++)) {
		if(currentItem.className == className){
			allItems.push(currentItem);
		}
	}
	return allItems;
}

function hrefTargets(){
	var allDivs = document.getElementById("reportContent").getElementsByTagName('a');
	var currentItem;
	var i = 0;
	var allItems = new Array();
	while (currentItem = allDivs.item(i++)) {
		var checkJava = currentItem.href.indexOf("javascript");
		if(checkJava < 0 && currentItem.className != "footnote-link"){
			currentItem.target = "_blank";
		}
	}
}

function getAllImages(theParent){
	var allImages = theParent.getElementsByTagName('img');
	var currentItem;
	var i = 0;
	var allItems = new Array();
	while (currentItem = allImages.item(i++)) {
		allItems.push(currentItem);
	}
	return allItems;
}

function styleThumbnails(){
	var allThumbs = new Array();
	allThumbs = getAllClass(document,"thumbnail"); 
	for(var i = 0 ; i < allThumbs.length; i++){
		
		var thumbcontents = allThumbs[i].innerHTML;
		allThumbs[i].innerHTML = "";
		
		var myInner = document.createElement('div');
		allThumbs[i].appendChild(myInner);
		myInner.setAttribute("class", "thumbnailInner");
		myInner.setAttribute("className", "thumbnailInner");
		myInner.innerHTML = thumbcontents;
		
		var allImages = new Array();
		allImages = getAllImages(allThumbs[i]);
		
		var imageName = allImages[0].src.split("/");
		var hiResName = str_replace("_thumb","",imageName[imageName.length-1]);
		
		allImages[0].hiResName = hiResName;
		allImages[0].onmousedown = function(){ lightBox(this.hiResName); }
		
		var myCorner = document.createElement('a');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "thumbnailTopCorner thumbnailCorner");
		myCorner.setAttribute("className", "thumbnailTopCorner thumbnailCorner");
		myCorner.href="javascript:lightBox('"+hiResName+"');";
		
		var myCorner = document.createElement('div');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "thumbnailBotCorner thumbnailCorner");
		myCorner.setAttribute("className", "thumbnailBotCorner thumbnailCorner");	
		
		allThumbs[i].style.display = "block";
	}
	Cufon.refresh();
}

function stylePullOuts(){
	var allThumbs = new Array();
	allThumbs = getAllClass(document,"pull-out"); 
	for(var i = 0 ; i < allThumbs.length; i++){
		
		var thumbcontents = allThumbs[i].innerHTML;
		allThumbs[i].innerHTML = "";
		
		var myInner = document.createElement('div');
		allThumbs[i].appendChild(myInner);
		myInner.setAttribute("class", "pull-outInner");
		myInner.setAttribute("className", "pull-outInner");
		myInner.innerHTML = thumbcontents;
		
		var myCorner = document.createElement('div');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "pull-outTopCorner");
		myCorner.setAttribute("className", "pull-outTopCorner");
		
		var myCorner = document.createElement('div');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "pull-outBotCorner");
		myCorner.setAttribute("className", "pull-outBotCorner");	
		
		allThumbs[i].style.display = "block";
	}
	
	var allThumbs = new Array();
	allThumbs = getAllClass(document,"pull-out-box-out"); 
	for(var i = 0 ; i < allThumbs.length; i++){
		
		var thumbcontents = allThumbs[i].innerHTML;
		allThumbs[i].innerHTML = "";
		
		var myInner = document.createElement('div');
		allThumbs[i].appendChild(myInner);
		myInner.setAttribute("class", "pull-out-box-outInner");
		myInner.setAttribute("className", "pull-out-box-outInner");
		myInner.innerHTML = thumbcontents;
		
		var myCorner = document.createElement('div');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "imageTopLeftCorner");
		myCorner.setAttribute("className", "imageTopLeftCorner");
		
		var myCorner = document.createElement('div');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "imageTopRightCorner");
		myCorner.setAttribute("className", "imageTopRightCorner");
		
		var myCorner = document.createElement('div');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "pull-outBotLeftCorner");
		myCorner.setAttribute("className", "pull-outBotLeftCorner");
		
		var myCorner = document.createElement('div');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "pull-outBotRightCorner");
		myCorner.setAttribute("className", "pull-outBotRightCorner");
		
		allThumbs[i].style.display = "block";
	}
	
	Cufon.refresh();
}
function styleBoxOut(){
	var allThumbs = new Array();
	
	allThumbs = getAllClass(document,"box-out-2-column"); 
	allThumbs = allThumbs.concat(getAllClass(document,"box-out-1-column"));
	
	for(var i = 0 ; i < allThumbs.length; i++){
		
		var myInner = allThumbs[i];
		
		var myCorner = document.createElement('div');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "box-outTopLeftCorner box-outCorner");
		myCorner.setAttribute("className", "box-outTopLeftCorner box-outCorner");
		
		var myCorner = document.createElement('div');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "box-outTopRightCorner box-outCorner");
		myCorner.setAttribute("className", "box-outTopRightCorner box-outCorner");
		
		var myCorner = document.createElement('div');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "box-outBotLeftCorner box-outCorner");
		myCorner.setAttribute("className", "box-outBotLeftCorner box-outCorner");
		
		var myCorner = document.createElement('div');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "box-outBotRightCorner box-outCorner");
		myCorner.setAttribute("className", "box-outBotRightCorner box-outCorner");	
		
		allThumbs[i].style.display = "block";
	}
	
	
}

function styleImage(){
	var allThumbs = new Array();
	allThumbs = getAllClass(document,"image-2"); 
	
	for(var i = 0 ; i < allThumbs.length; i++){
		
		var myInner = allThumbs[i];
		
		var myCorner = document.createElement('div');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "imageTopLeftCorner");
		myCorner.setAttribute("className", "imageTopLeftCorner");
		
		var myCorner = document.createElement('div');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "imageTopRightCorner");
		myCorner.setAttribute("className", "imageTopRightCorner");
		
		var myCorner = document.createElement('div');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "imageBotLeftCorner");
		myCorner.setAttribute("className", "imageBotLeftCorner");
		
		var myCorner = document.createElement('div');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "imageBotRightCorner");
		myCorner.setAttribute("className", "imageBotRightCorner");	
		
		allThumbs[i].style.display = "block";
	}
	
	var allThumbs = new Array();
	allThumbs = getAllClass(document,"image-1"); 
	
	for(var i = 0 ; i < allThumbs.length; i++){
		
		var allImages = new Array();
		allImages = getAllImages(allThumbs[i]);
		
		var myInner = document.createElement('div');
		allThumbs[i].insertBefore(myInner,allImages[0]);
		myInner.setAttribute("class", "image-1Inner");
		myInner.setAttribute("className", "image-1Inner");
		
		myInner.appendChild(allImages[0]);
		
		var myCorner = document.createElement('div');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "imageTopRightCorner");
		myCorner.setAttribute("className", "imageTopRightCorner");

		var myCorner = document.createElement('div');
		myInner.appendChild(myCorner);
		myCorner.setAttribute("class", "imageBotRightCorner");
		myCorner.setAttribute("className", "imageBotRightCorner");	
		
		allThumbs[i].style.display = "block";
	}
}


function styleTables(){
	var allDivs = document.getElementsByTagName('td');
	var currentItem;
	var i = 0;
	var allItems = new Array();
	while (currentItem = allDivs.item(i++)) {
		var tdContent = currentItem.innerHTML;
		currentItem.innerHTML = "<div>"+tdContent+"</div>";
	}
	Cufon.refresh();
}

function str_replace(searchFor,replaceWith,subject){
	return 	subject.split(searchFor).join(replaceWith);
}

function selectSubChapter(chapter,section){
	window.location = "report.php?c="+chapter+"&s="+section;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
  var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
  if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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 createCookie(name,value,days) {
	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=/";
}

// JavaScript Document
function convert(code_number){
	var num1;
	var num2;
	var currnum = 128;
	num1 = eval(code_number);
	if(num1 >= currnum){
		num2 = "1";
		num1 = num1 - currnum;
		currnum = currnum / 2;
	} else {
		num2 = "0";
		currnum = currnum / 2;
	}
	for (p = 1; p <= 7; p++) {
		if(num1 >= currnum) {
			num2 = num2 + "1";
			num1 = num1 - currnum;
			currnum = currnum / 2;
		} else {
			num2 = num2 + "0";
			currnum = currnum / 2;
		}
	}
	return(num2);
}

function convertback(code_number){
	//remove comma separators
	var num1 = code_number.replace(/,/g,"");
	
	//Check that given number is 8 digits long
	if(num1.length == 8){
		
		//Convert 8 digit binary into decimal value.
		var currnum = 128;
		var num2 = eval(num1.charAt(0)) * currnum;
		for (i = 1; i <= 7; i++){
			currnum = currnum / 2;
			num2 = num2 + (eval(num1.charAt(i)) * currnum);
		}
		
		//Return code value
		return(num2);
	} else {
		return(0);
	}
}


function addchapter(chapterNumber){
	if(chapterNumber > 1){
		var selections = readCookie("selections");
		var decoded = convert(selections);
		var switches = decoded.split("");
		switches[chapterNumber-2] = 1;
		switches[7] = 1;
		decoded = switches.join();
		var encoded = convertback(decoded);
		createCookie("selections",encoded,1);
	}
	var frame = document.getElementById("popup");
	
	document.getElementById("popup").style.display = "block";
	document.getElementById("darkBackgroundLayer").style.display = "block";
	
	displayChapters();
}

function closelightbox(){
	document.getElementById("popup").style.top = "-1000px";
	document.getElementById("darkBackgroundLayer").style.display = "none";
}

function closeBox(){
	document.getElementById("popup").style.display = "none";
	document.getElementById("lightbox").style.display = "none";
	document.getElementById("darkBackgroundLayer").style.display = "none";
}

function removeChapter(chapterNumber){
		var selections = readCookie("selections");
		var decoded = convert(selections);
		var switches = decoded.split("");
		switches[chapterNumber-2] = 0;
		decoded = switches.join();
		var encoded = convertback(decoded);
		createCookie("selections",encoded,1) ;
		displayChapters();
}

function displayChapters(){
	var selections = readCookie("selections");
	var decoded = convert(selections);
	var switches = decoded.split("");
	var chapterTally = 0;
	for(i=0;i<=6;i++){
		if(switches[i] == 1){
			if(document.getElementById("builder"+(i+2))){
				document.getElementById("builder"+(i+2)).style.display = "block";
			}
			chapterTally++;
		} else{
			if(document.getElementById("builder"+(i+2))){
				document.getElementById("builder"+(i+2)).style.display = "none";
			}
		}
	}
	if(document.getElementById("chapterTally")){
		if(chapterTally == 1){
			document.getElementById("chapterTally").innerHTML = chapterTally + " chapter";
		} else {
			document.getElementById("chapterTally").innerHTML = chapterTally + " chapters";
		}
	}
	if(document.getElementById("selections")){
		document.getElementById("selections").value = selections;
	}
}


function resizeBackground(){
	windowSize = getWindowSize();
	
	document.getElementById("darkBackgroundLayer").style.width = windowSize.bodyWidth+"px";
	//document.getElementById("darkBackgroundLayer").style.height = Math.max(windowSize.height,windowSize.bodyHeight)+"px";
	
	var leftGap = Math.floor((windowSize.width - 494)/2);
	var topGap = Math.floor((windowSize.height - 191)/2);
	
	document.getElementById("popup").style.left = leftGap+"px";
	document.getElementById("popup").style.top = topGap+"px";
	
	var leftGap = Math.floor((windowSize.width - lightboxWidth)/2);
	var topGap = Math.floor((windowSize.height - lightboxHeight)/2);
	
	document.getElementById("lightbox").style.left = leftGap+"px";
	
	if(topGap < 0){
		topGap = 20;
	}

	document.getElementById("lightbox").style.top = (topGap+windowSize.scrollHeight)+"px";
	document.getElementById("lightbox").style.width = lightboxWidth+"px";
	document.getElementById("lightbox").style.height = lightboxHeight+"px";
	
	var combinedHeight = topGap+windowSize.scrollHeight+lightboxHeight;
	
	document.getElementById("darkBackgroundLayer").style.height = Math.max(windowSize.height, Math.max(windowSize.bodyHeight,combinedHeight) )+"px";
	
	//alert(document.getElementById("popup").style.top);
}

var lightboxWidth = 440;
var lightboxHeight = 392;
var isLoaded = 0;
var myImage;

function lightBox(imageName){
	lightboxWidth = 440;
	lightboxHeight = 392;
	document.getElementById("lightboxImage").src = "images/loading.gif";
	
	resizeBackground();
	
	var frame = document.getElementById("lightbox");
	
	frame.style.display = "block";
	document.getElementById("darkBackgroundLayer").style.display = "block";
	
	myImage = new Image();
	myImage.src = "reportimages/"+imageName;

	//document.getElementById("lightboxImage").src = "reportimages/"+imageName;
	
	isLoaded = setInterval('checkload()',100);
}

function checkload(){
	if(myImage.complete) {
		lightboxWidth = myImage.width + 40;
		lightboxHeight = myImage.height + 40;
		
		resizeBackground();
		
		document.getElementById("lightboxImage").src = myImage.src;
		
		resizeBackground();
		
		//document.getElementById("largepic").src = myImage.src;
		clearInterval (isLoaded);
		//testFunction();
	}
}

function popUp(URL,width,height) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+width+",height="+height+"');");
}

function formvalidation(){
	var textFields = new Array('firstname','surname','organisation');
	var emailFields = new Array('email');
	
	var formFailed = false;
	var emailFailed = false;
	
	for(var i = 0; i < textFields.length ; i++){
		if(document.getElementById(textFields[i]).value == ""){
			formFailed = true;
		}
	}
	
	var regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	for(var i = 0; i < emailFields.length ; i++){
		if(!regex.test(document.getElementById(emailFields[i]).value)){
			emailFailed = true;
		}
	}
	
	var message = "Please ";
	
	if(formFailed){
		message += "complete all fields and ";
	}
	
	if(emailFailed){  //New line
		message += "provide a valid email address and ";
	}
	
	if(formFailed || emailFailed){
		message = message.substr(0,message.length-5)+"."
		alert(message);
	} else {
		document.form1.submit();
	}
}

/*
*********************************************************
*                                                       *
*                FONT RESIZING FUNCTIONS                *
*                                                       *
*           COPYRIGHT 2011 BRANCHUS CREATIONS           *
*                                                       *
*       VERSION : 1.0                                   *              
*       DATE    : 30 JULY 2011                          *
*       AUTHOR  : BRUCE RAYNE                           *
*                                                       *
*********************************************************
*/

var baseFontSize = 13;
var minPointSize = 11;
var maxPointSize = 17;
var fontIncrement = 2;
var currentFontSize = baseFontSize;
var contentID = "reportContent";

function smallerFont(){
	currentFontSize = Math.max(minPointSize,currentFontSize-fontIncrement);
	document.getElementById(contentID).style.fontSize = currentFontSize+"px";
	Cufon.refresh();
	setShadowHeight();
	resizeBackground();
}

function largerFont(){
	currentFontSize = Math.min(maxPointSize,currentFontSize+fontIncrement);
	document.getElementById(contentID).style.fontSize = currentFontSize+"px";
	Cufon.refresh();
	setShadowHeight();
	resizeBackground();
}

/*
***********************************
*   END FONT RESIZING FUNCTIONS   *
***********************************
*/



window.onload=initPage;
window.onresize=resizePage;



//Just a demo. These can be deleted.
//initAdd("showLoad()");


//
