var expdate = new Date ();
expdate.setTime (expdate.getTime() + (365 * 24 * 60 * 60 * 1000)); // 1 year from now
var font;
var colour;
var domain = document.domain;

init();
	
function init()
{	
	font = "small";
	colour = "white";
	
	if(getFontCookie()!="" || getColourCookie()!="")
	{
	    if(getFontCookie()!="")
	    {
	       font = getFontCookie();
	    }
	    if(getColourCookie()!="")
	    {
	       colour = getColourCookie();
	    }
	    setActiveStyleSheet(colour+font);
	}
}

function setCookie(name, fontsize, expires)
{ 
   if(getColourCookie()!="")
   {
	    colour = getColourCookie();
   }
   else
   {
       colour = 'white';
   }
   
   if(fontsize == '')
   {
      fontsize = 'small';
   }
   
   font = fontsize;
   submit();
   location.reload();
}

function submit()
{
   createFontCookie(font);
   createColourCookie(colour);
}

function addStylesheet(title)
{
  document.write("<link type='text/css' rel='alternate stylesheet' href='/Templates/css08/accessibility/"+ title +".css' title='"+ title +"'/>");
}

function setActiveStyleSheet(title) {
   addStylesheet(title);
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
	   a.rel = "alternate stylesheet";
       if(a.getAttribute("title") == title)
		{
			a.disabled = false;
			a.rel = "stylesheet";
		}
     }
   }
}

function createFontCookie(fontvalue) {
	setNewCookie("fontsize", fontvalue);
}

function createColourCookie(colourvalue) {
	setNewCookie("fontcolor", colourvalue);
}

function setNewCookie(name, value)
{
   var expdate = new Date ();
   expdate.setTime (expdate.getTime() + (365 * 24 * 60 * 60 * 1000)); // 1 year from now
   if(domain == 'btplc.btested.com')
   {
     //Staging cookie string
     document.cookie = name + "=" +value+";expires=" +expdate.toGMTString() +"; path=/";
   }
   else
   {
     //Live cookie string
     document.cookie = name + "=" +value+";expires=" +expdate.toGMTString() +"; domain=.btplc.com; path=/";
   }
  
}

function getFontCookie() {
		return getCookie("fontsize");  		
}

function getColourCookie() {
		return getCookie("fontcolor");  		
}
function getCookie(cookieName) {
		// document.cookie gives us a list of all the cookies from this domain
		// we need to find just the cookie we are interested in
		// take a substring between "cookiename=" and ";" - this is the value of the cookie
		// this function will return either the value of the cookie, or a null value if it's not there
		var cookiename = cookieName;
  		var nameStr = cookiename + "=";
  		var maxLen = document.cookie.length
  		var i = 0
  		while (i < maxLen) {
    			var j = i + nameStr.length
    			if (document.cookie.substring(i,j) == nameStr) {
       				var cookieEnd = document.cookie.indexOf(";",j);
			
       				if (cookieEnd == -1) {
	          				cookieEnd = document.cookie.length;
       				}

					a=document.cookie.substring(j,cookieEnd);
				       return unescape(document.cookie.substring(j,cookieEnd));
    			}
    		i++
  		}
  		return "";
}
	
// do they accept cookies?
// simply drop a test cookie and try to retrieve it. if it's not there, they don't take cookies

function createTestCookie() {
	setNewCookie("test", '1');
}
	
function getTestCookie() {
		return getCookie("test");  		
}
	
function checkCookieAccept() { 
	
	createTestCookie();

	if (getTestCookie()=="test") { 
		return 1;
	}
	else { 
		return 0;
	}
}