﻿// Cookies functions
// Read cookie from user's machine
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;
}
// Create cookie with name, value and days before expiry
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=/";
}
// Load cookies into variables and set default values if no cookies found.
function getCookies() {
 ck_contrast = readCookie("contrast");
 thisContrast = ck_contrast ? ck_contrast : 'false';
 if (thisContrast == 'null') {
   thisContrast = 'false';
  }
 ck_fontSize = readCookie("fontsize");
 ck_fontSize = parseInt(ck_fontSize);
 if (!arr_fontSizes['true'][ck_fontSize]) {
  thisFontSize = 1;
 } else {
  thisFontSize = ck_fontSize;
 }
}
// Sets the cookie value for the font prefs
function setFontCookie() {
 createCookie("fontsize", thisFontSize, 365);
}
// Sets the contrast cookie
function setContrastCookie() {
 createCookie('contrast', thisContrast, 365);
}
// Retrieve stylesheet references from document
function getStyleSheets() {
 var i, a;
 for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
  if (a.getAttribute("rel").indexOf("style")!=-1 && a.getAttribute("title") == 'Main') {
   mainStylesheet = a;
  }
  if (a.getAttribute("rel").indexOf("style")!=-1 && a.getAttribute("title") == 'Contrast') {
   contrastStylesheet = a;
  }
 }
}
// IE version detection
function getIEVersionNumber() {
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");
    if (MSIEOffset == -1) {
        return 0;
    } else {
        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }
}
// Set the base font size of the body
function setFontSize(size) {
 if (document.body) {
   document.body.style.fontSize = lookupFontSize(size);
 }
 // Table font resize for IE 5 and 5.5 
 if (navigator.appName == "Microsoft Internet Explorer") {
  if (getIEVersionNumber() < 6) {
   var alltd = document.getElementsByTagName('td');
   for (var i=0; i<alltd.length; i++) {
    alltd[i].style.fontSize = lookupFontSize(size);
   }
   var allth = document.getElementsByTagName('th');
   for (var i=0; i<allth.length; i++) {
    allth[i].style.fontSize = lookupFontSize(size);
   }
  }
 }
 thisFontSize = size;
 setFontCookie();
}
function lookupFontSize(size) {
 if (thisContrast) {
  return arr_fontSizes[thisContrast][size];
 } else {
  return arr_fontSizes['false'][size];
 }
}
// Set the high contrast stylesheet status 
function setContrast(cont) {
 if (cont == 'true') {
  disableStylesheet(mainStylesheet, true);
  disableStylesheet(contrastStylesheet, false);
  thisContrast = 'true';
  setFontSize(thisFontSize);
 } else {
  disableStylesheet(mainStylesheet, false);
  disableStylesheet(contrastStylesheet, true);
  thisContrast = 'false';
  setFontSize(thisFontSize);
 }
 setContrastCookie();
}
function showStyleLinks() {
 var linkList = document.getElementById('footercontent'); /* footercontent relates specifically to the SA design. Used to be fAccessBar*/
 // find ul list under the div
 var ulElem = findChild(linkList, 'ul');
 // add to list of links
 var addCode = '';
 addCode = '<li><a href="javascript:void(0)" id="txtSmaller">Smaller text</a> </li>';
 addCode += '<li><a href="javascript:void(0)" id="txtLarger">Larger text</a> </li>';
 addCode += '<li id="highContrastL"><a href="javascript:void(0)" id="highContrast">High contrast</a> </li>';
 addCode += '<li id="normalContrastL"><a href="javascript:void(0)" id="normalContrast">Normal contrast</a> </li>';
 ulElem.innerHTML += addCode;
}
function setTextFunctions() {
 var smallerLink = document.getElementById('txtSmaller');
 var largerLink = document.getElementById('txtLarger');
 smallerLink.onclick = txtDown;
 largerLink.onclick = txtUp;
 var highContLink = document.getElementById('highContrast');
 var normalContLink = document.getElementById('normalContrast');
 highContLink.onclick = switchContrast;
 normalContLink.onclick = switchContrast;
}
function findChild(obj, elem) {
 var returnObj;
 for (i=0; i<obj.childNodes.length; i++) {
  var thisChild = obj.childNodes[i];
  if (typeof thisChild == "string") {
   thisChild = document.getElementById(thisChild);
  }
  if (thisChild.tagName != null) {
   if (thisChild.tagName.toLowerCase() == elem || thisChild.tagName == elem) {
    returnObj = thisChild;
   }
  }
 }
 return returnObj;
}
function txtDown() {
 var sizeToSet = thisFontSize - 1;
 if (sizeToSet < 0) {
  sizeToSet = 0;
 }
 setFontSize(sizeToSet);
}
function txtUp() {
 var sizeToSet = thisFontSize + 1;
 if (sizeToSet > 3) {
  sizeToSet = 3;
 }
 setFontSize(sizeToSet);
}
function switchContrast() {
 if (thisContrast == 'true') {
  setContrast('false');
 } else {
  setContrast('true');
 }  
} 
function disableStylesheet(ss, state) {
 ss.disabled = state;
}
// Called from init function
function initStyleSwitcher() {
 getCookies();
 getStyleSheets();
 setContrast(thisContrast);
 setFontSize(thisFontSize);
 showStyleLinks();
 setTextFunctions(); 
}
// Init vars
var ck_fontSize, ck_contrast, thisFontSize, thisContrast, mainStylesheet, contrastStylesheet;
var arr_fontSizes = new Array();
arr_fontSizes['true'] = new Array('100%', '125%', '150%', '175%');
arr_fontSizes['false'] = new Array('67%', '76%', '100%', '120%');
getCookies();
getStyleSheets();
disableStylesheet(contrastStylesheet, true); // Fix problem in IE that alternative stylesheets do not start out as false
setContrast(thisContrast);
setFontSize(thisFontSize);