var no_stylesheet = 2;  /* This value needs to be set to the number of normal style sheets i.e. those not affecting the accessibility functions */
var size_ss_start = no_stylesheet;
var font_ss_start = no_stylesheet + 5;

function fontsizeup() {
  active = getActiveStyleSheet();
  switch (active) {
    case 'Small' : 
      setActiveStyleSheet('Default');
      break;
    case 'Default' : 
      setActiveStyleSheet('Large');
      break;
    case 'Large' : 
      setActiveStyleSheet('Larger');
      break;
    case 'Larger' : 
      setActiveStyleSheet('Largest');
      break;
    case 'Largest' :
      break;
    default :
      setActiveStyleSheet('Default');
      break;
  }
}

function fontsizedown() {
  active = getActiveStyleSheet();
  switch (active) {
    case 'Largest' : 
      setActiveStyleSheet('Larger');
      break;
    case 'Larger' : 
      setActiveStyleSheet('Large');
      break;
    case 'Large' : 
      setActiveStyleSheet('Default');
      break;
    case 'Default' : 
      setActiveStyleSheet('Small');
      break;
    case 'Small' : 
       break;
    default :
      setActiveStyleSheet('Default');
      break;
  }
}

/* Font size functions */

function setActiveStyleSheet(title) {
  var i, a, arraylen, main;
  arraylen = document.styleSheets.length -3;
  for(i=size_ss_start; i<arraylen; i++) {
	a = document.getElementsByTagName("link")[i];  
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && a.getAttribute("href").indexOf("style") != -1) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a, arraylen, main;
  arraylen = document.styleSheets.length -3;
  for(i=size_ss_start; i<arraylen; i++) {
	a = document.getElementsByTagName("link")[i];  
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && a.getAttribute("href").indexOf("style") != -1 && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a, arraylen, main;
  arraylen = document.styleSheets.length -3;
  for(i=size_ss_start; i<arraylen; i++) {
	a = document.getElementsByTagName("link")[i];  
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
	   && a.getAttribute("href").indexOf("style") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}


/* Font type functions */

function setActiveFont(id) {
  var i, b, main;
	for (i=font_ss_start; i<document.styleSheets.length; i++){
		b = document.getElementsByTagName("link")[i];
    	if(b.getAttribute("rel").indexOf("style") != -1 && b.getAttribute("title")) {
  	    b.disabled = true;
  	    if(b.getAttribute("title") == id) b.disabled = false;
    }
  }
}


function getActiveFont() {
  var i, b;
	for (i=font_ss_start; i<document.styleSheets.length; i++){
	b = document.getElementsByTagName("link")[i];
    if(b.getAttribute("rel").indexOf("style") != -1 && !b.disabled) return b.getAttribute("title");
  }
  return null;
}

function getPreferredFont() {
  var i, b;
	for (i=font_ss_start; i<document.styleSheets.length; i++){
	b = document.getElementsByTagName("link")[i];
    if(b.getAttribute("rel").indexOf("style") != -1
       && b.getAttribute("rel").indexOf("alt") == -1
	   && b.getAttribute("title")
       ) return b.getAttribute("title");
  }
  return null;
}


/* Background colour functions */

function getCurrentColour() {
	var CurrColour;
	CurrColour = document.bgColor;
	if(CurrColour != "") {return CurrColour;
  }
  CurrColour = '#ffffff';
  return CurrColour;
}

function setActiveColour(colour) {
document.bgColor=colour;
}


/* Cookie fucntions */

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 expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

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;
}

/* Accessibility functions initialisation */

function initFontScaler() {
  var cookie = readCookie("style");
  var colour = readCookie("colour");
  var font = readCookie("fonttype");
  var title = cookie ? cookie : getPreferredStyleSheet();
  var id = font ? font : getPreferredFont();
  setActiveFont(id);
  setActiveStyleSheet(title);
  setActiveColour(colour);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  var colorbg = getCurrentColour();
  var fontused = getActiveFont();
  createCookie("style", title, 365);
  createCookie("colour", colorbg,365);
  createCookie("fonttype", fontused,365);
}

var cookie = readCookie("style");
var colour = readCookie("colour");
var font = readCookie("fonttype");
var title = cookie ? cookie : getPreferredStyleSheet();
var id = font ? font : getPreferredFont();
if (title == 'null') {
  title = getPreferredStyleSheet();
}

setActiveStyleSheet(title);

if (colour == 'null') {
  colour = '#ffffff';
}

setActiveColour(colour);

if (id == 'null') {
  id = getPreferredFont();
}

setActiveFont(id);


/*Set up controls for accessibility items */

function createControls() {
	
if (document.getElementsByTagName) {
	var col1 = '#ffffff';
	var col1a = "'"+col1+"'";
	var col2 = '#ffffe0';
	var col2a = "'"+col2+"'";
	var col3 = '#ccffff';
	var col3a = "'"+col3+"'";
	var col4 = '#cfffe8';
	var col4a = "'"+col4+"'";
	var col5 = '#ffddee';
	var col5a = "'"+col5+"'";
	var col6 = '#ddddff';
	var col6a = "'"+col6+"'";
	var col7 = '#ccffcc';
	var col7a = "'"+col7+"'";
	var col8 = '#cceedd';
	var col8a = "'"+col8+"'";
	var col9 = '#ffddb3';
	var col9a = "'"+col9+"'";
	var col10 ='#c9dfff';
	var col10a = "'"+col10+"'";
	var col11 ='#f0f0f0';
	var col11a = "'"+col11+"'";
	var text1 = 'arial';
	var text1a = "'"+text1+"'";
	var text2 = 'tahoma';
	var text2a = "'"+text2+"'";
	var text3 = 'trebuchet';
	var text3a = "'"+text3+"'";
	document.write('<form action="" method="get" id="textSizer" name="accessibility bar">');
   	document.write('<div>');
	document.write('<span class="textsizer_text">Font Size Change:</span>');
    document.write('<input alt="Increase font size" style="width: 56px; height: 23px" accessKey=. onclick="javascript:fontsizeup(); return false;" src="images/text_plus.jpg" type="image"/>');
    document.write('<input alt="Decrease font size" style="width: 56px; height: 23px" accessKey=, onclick="javascript:fontsizedown(); return false;" src="images/text_minus.jpg" type="image"/>');
	document.write('<span class="textsizer_text"> Background Colour Change:</span>');
	document.write('<input alt="White" style="width: 23px; background-color:'+col1+'; height: 23px" onclick="javascript:setActiveColour('+col1a+'); return false;"  src="images/spacer.gif" type="image"/>');
    document.write('<input alt="Pastel Yellow" style="width: 23px; background-color:'+col2+'; height: 23px" onclick="javascript:setActiveColour('+col2a+'); return false;"  src="images/spacer.gif" type="image"/>');
   	document.write('<input alt="Pastel Cyan" style="width: 23px; background-color:'+col3+'; height: 23px" onclick="javascript:setActiveColour('+col3a+'); return false;"  src="images/spacer.gif" type="image"/>');
   	document.write('<input alt="Pastel Aqua" style="width: 23px; background-color:'+col4+'; height: 23px" onclick="javascript:setActiveColour('+col4a+'); return false;"  src="images/spacer.gif" type="image"/>');
    document.write('<input alt="Pastel Pink" style="width: 23px; background-color:'+col5+'; height: 23px" onclick="javascript:setActiveColour('+col5a+'); return false;"  src="images/spacer.gif" type="image"/>');
   	document.write('<input alt="Pastel Violet" style="width: 23px; background-color:'+col6+'; height: 23px" onclick="javascript:setActiveColour('+col6a+'); return false;"  src="images/spacer.gif" type="image"/>');
    document.write('<input alt="Pastel Lime Green" style="width: 23px; background-color:'+col7+'; height: 23px" onclick="javascript:setActiveColour('+col7a+'); return false;"  src="images/spacer.gif" type="image"/>');
   	document.write('<input alt="Pastel Forest Green" style="width: 23px; background-color:'+col8+'; height: 23px" onclick="javascript:setActiveColour('+col8a+'); return false;"  src="images/spacer.gif" type="image"/>');
   	document.write('<input alt="Pastel Orange" style="width: 23px; background-color:'+col9+'; height: 23px" onclick="javascript:setActiveColour('+col9a+'); return false;"  src="images/spacer.gif" type="image"/>');
    document.write('<input alt="Pastel Blue" style="width: 23px; background-color:'+col10+'; height: 23px" onclick="javascript:setActiveColour('+col10a+'); return false;"  src="images/spacer.gif" type="image"/>');
   	document.write('<input alt="Light Grey" style="width: 23px; background-color:'+col11+'; height: 23px" onclick="javascript:setActiveColour('+col11a+'); return false;"  src="images/spacer.gif" type="image"/>');
	document.write('<span class="textsizer_text"> Font Style Change:</span>');
	document.write('<input alt="Arial (access key 5)" style="width: 56px; height: 23px" accessKey=5 onclick="javascript:setActiveFont('+text1a+'); return false;" src="images/'+text1+'.jpg" type="image"/>');
   	document.write('<input alt="Tahoma  (access key 6)" style="width: 56px; height: 23px" accessKey=6 onclick="javascript:setActiveFont('+text2a+'); return false;" src="images/'+text2+'.jpg" type="image"/>');
   	document.write('<input alt="Comic (access key 7)" style="width: 56px; height: 23px" accessKey=7 onclick="javascript:setActiveFont('+text3a+'); return false;" src="images/'+text3+'.jpg" type="image"/>');
	document.write('</div>');
   	document.write('</form>');
    }
}


/* Date function */

function ShowDate() {
d = new Array(
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
);
m = new Array(
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
);
today = new Date();
day = today.getDate();
year = today.getYear();
if (year < 2000)    // Y2K Fix, Isaac Powell
year = year + 1900; // http://onyx.idbsu.edu/~ipowell
end = "th";
if (day==1 || day==21 || day==31) end="st";
if (day==2 || day==22) end="nd";
if (day==3 || day==23) end="rd";
day+=end;
document.write(d[today.getDay()]+", "+m[today.getMonth()]+" ");
document.write(day+", " + year);
}