function fontScaleUp() {
	switch(getActiveStyleSheet()) {
		case 'A--':
			setActiveStyleSheet('A-');
			break;
		case 'A-':
			setActiveStyleSheet('A');
			break;
		case 'A':
			setActiveStyleSheet('A+');
			break;
		case 'A+':
			setActiveStyleSheet('A++');
			break;
		case 'A++':
			break;
		default:
			setActiveStyleSheet('A-');
			break;
	}
}

function fontScaleDown() {
	switch(getActiveStyleSheet()) {
		case 'A++':
			setActiveStyleSheet('A+');
			break;
		case 'A+':
			setActiveStyleSheet('A');
			break;
		case 'A':
			setActiveStyleSheet('A-');
			break;
		case 'A-':
			setActiveStyleSheet('A--');
			break;
		case 'A--':
			break;
		default:
			setActiveStyleSheet('A-');
			break;
	}
}

function setActiveStyleSheet(sTitle) {
	var iLink, aoLink, oLink;

	/* Itterate over the link elements.
	 */
	var aoLinks = document.getElementsByTagName('link');
	for(iLink = 0; iLink < aoLinks.length; iLink++) {
		oLink = aoLinks[ iLink ];

		/* If it's a stylesheet with a title, set
		 * the status dependant on the title matching.
		 */

		if(oLink.getAttribute('rel').indexOf('alternate stylesheet') != -1 &&
		   oLink.getAttribute('title')) {
			oLink.disabled = true;
			if(oLink.getAttribute('title') == sTitle){
				oLink.disabled = false;
			}
		}
	}
}

function getActiveStyleSheet() {
	var aoLinks, iLink, oLink;
	/* Itterate over the link elements.
	 */
	aoLinks = document.getElementsByTagName('link');
	for(iLink = 0; iLink < aoLinks.length; iLink++) {
		oLink = aoLinks[ iLink ];

		/* If it's a stylesheet, with a title and
		 * isn't disabled (ie active), return the title.
		 */
		if( oLink.getAttribute('rel').indexOf('alternate stylesheet') != -1 &&
		    oLink.getAttribute('title') &&
		   !oLink.disabled) {
			return(oLink.getAttribute('title'));
		}
	}
	return("");
}

function createCookie(sName, mValue, iLifeTime) {
	var oDate, sExpires = '';
	if(iLifeTime) {
		/* Lifetime specified, so convert days to milliseconds
		 * and then to a date string.
		 */
		iLifeTime *= 24 * 60 * 60 * 1000;
		oDate = new Date();
		oDate.setTime(oDate.getTime() + iLifeTime);
		sExpires = '; expires='+ oDate.toGMTString();
	}
	/* Set the cookie.
	 */
	document.cookie = sName +'='+ mValue + sExpires +'; path=/';
}

function getCookie(sName) {
	var asCookies, iCookie, asNameValue;

	/* Split the document's cookie string into individual cookies
	 */
	asCookies = document.cookie.split('; ');
	for(iCookie = 0; iCookie < asCookies.length; iCookie++) {

		/* Split the cookie into two parts seperated by an equals.
		 */
		asNameValue = asCookies[ iCookie ].split('=', 2);


		/* If the names match, return the value.
		 */
		if(asNameValue[0] == sName && asNameValue[1] != 'null') {
			return(asNameValue[1]);
		}
	}
	return("");
}

function getStyleCookie() {
	var sCookie = getCookie('style');
	return( sCookie.indexOf('A') == 0 ? sCookie : 'A-' );
}

/* When the document has loaded, fetch the style
 * and apply it
 */
window.onload = function(e) {
	setActiveStyleSheet( getStyleCookie() );
}

/* Just before the window closes, set a cookie to store
 * the currently selected style sheet
 */
window.onunload = function(e) {
	var sStyleSheet = getActiveStyleSheet();
	if(sStyleSheet.indexOf('A') == 0) {
		createCookie('style', sStyleSheet, 365);
	}
}

/* Try and get the specified style sheet and apply it to
 * the document.
 */
setActiveStyleSheet( getStyleCookie() );