// JavaScript Document

function hideContent() {
	if (document.getElementById) {
		document.getElementById('content').className = 'hidden';
		document.getElementById('left').style.visibility = 'hidden';
		document.getElementById('right').style.visibility = 'visible';
	} else if (document.all) {
		document.all['content'].className = 'hidden';
		document.all['left'].style.visibility = 'hidden';
		document.all['right'].style.visibility = 'visible';
	}
	
	// test if we can set a cookie, and if so, save closed state of text window
	var cookiesOK = testAcceptance();
	if (cookiesOK) {
		setCookie('windowSize','min');
	}
}

function showContent() {
	if (document.getElementById) {
		document.getElementById('content').className = 'visible';
		document.getElementById('left').style.visibility = 'visible';
		document.getElementById('right').style.visibility = 'hidden';
	} else if (document.all) {
		document.all['content'].className = 'visible';
		document.all['left'].style.visibility = 'visible';
		document.all['right'].style.visibility = 'hidden';
	}

	// test if we can set a cookie, and if so, save closed state of text window
	var cookiesOK = testAcceptance();
	if (cookiesOK) {
		setCookie('windowSize','max');
	}
}

// function to check if user closed window on last page, if so, keep it closed
function checkWindow() {
	var windowSize = readCookie('windowSize');
	if (windowSize == 'min') {
		hideContent();
	} else if (windowSize == 'max')	{
		showContent();
	}
}

// function to test whether the user accepts cookies
function testAcceptance() {
	var acceptsCookies = false;
	
	// try to set a cookie
	if (document.cookie == '') {
		document.cookie = 'acceptsCookies=yes';
		// if it succeeds, set variable to true
		if (document.cookie.indexOf('acceptsCookies=yes') != -1) {
			acceptsCookies = true; 
			// delete test cookie straight away
			document.cookie = 'acceptsCookies=yes' + '; expires=Fri, 13-Apr-1970 00:00:00 GMT';
		}
	
	// if there was already a cookie, set variable to true
	} else {	
		acceptsCookies = true;
	}

	// if user doesn't accept cookies, show message
	if (!(acceptsCookies)) {
		var message = "Your browser is not set to accept cookies.";
		message += "\nThis site requires cookies to be accepted if you wish to keep the text window";
		message += "\nclosed when you navigate to a new page.";
		message += "\nHowever, you can still access the entire site without accepting cookies.";
		message += "\n\nTo enable cookies in IE 6, click Tools > Internet Options > Privacy.";
		message += "\nTo enable cookies in IE 5.5 or earlier, click Tools > Internet Options > Security > Custom Level.";
		message += "\n\nTo enable cookies in Netscape Navigator 7, click Edit > Preferences > Privacy & Security > Cookies.";
		message += "\nTo enable cookies in Netscape Navigator 6, click Edit > Preferences > Advanced > Cookies.";
		message += "\nTo enable cookies in Netscape Navigator 4.x or earlier, click Edit > Preferences > Advanced.";
		message += "\n\nTo enable cookies in Mozilla, click Tools > Options > Privacy > Cookies.";
		message += "\n\nFor other browsers, see the Help menu for further instructions.";
		alert(message);
	}
	return acceptsCookies;
}

// function to create a cookie with given name and value
function setCookie(name, value) {
	document.cookie = name + '=' + escape(value) + '; path=/;';
}

// function to extract and return the value from a cookie
function readCookie(cookiename) {
	// if there's no cookie, go no further
	if(document.cookie == '') {	
		return false;
	} else {
		var firstChar, lastChar;
		var theWholeCookie = document.cookie;
		// find the start of 'cookiename'
		firstChar = theWholeCookie.indexOf(cookiename);		
		// skip 'name' and '='
		firstChar += cookiename.length + 1;					
		// Find the end of the value string (i.e. the next ';')
		lastChar = theWholeCookie.indexOf(";",firstChar);			
		if(lastChar == -1) {
			lastChar = theWholeCookie.length;
		}
		return unescape(theWholeCookie.substring(firstChar, lastChar));
	}
}

// function to delete a cookie
function deleteCookie(name) {
	var theValue = readCookie(name);
	if(theValue) {
		document.cookie = name + "=" + '; expires=Fri, 13-Apr-1970 00:00:00 GMT'; 
	}
} 

// function to split a cookie and return parts as info[] array
function splitCookie(cookieName,delimeter) {	
	// load the cookie into a variable
	var the_cookie = readCookie(cookieName); 
	info = the_cookie.split(delimter);
	return info;
}