var theExpiryDate=new Date();
theSeconds=theExpiryDate.getSeconds();
theSeconds+=2592000; //1 month
theExpiryDate.setSeconds(theSeconds);
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 submit()
{
   createFontCookie(font);
   createColourCookie(colour);
}

function addStylesheet(title)
{
  document.write("<link type='text/css' rel='alternate stylesheet' href='/Templates/css07/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) {
	setCookie("btplc_font_cookie", fontvalue);
}

function createColourCookie(colourvalue) {
	setCookie("btplc_colour_cookie", colourvalue);
}

function setCookie(name, value)
{
   var theExpiryDate=new Date();
   theSeconds=theExpiryDate.getSeconds();
   theSeconds+=2592000; //1 month
   theExpiryDate.setSeconds(theSeconds);
   if(domain=="btplc.btested.com")
   {
      document.cookie = name + "=" +value+";expires=" +theExpiryDate.toGMTString() +"; domain=btplc.btested.com; path=/";
   }
     else
   {
      document.cookie = name + "=" +value+";expires=" +theExpiryDate.toGMTString() +"; domain=.btplc.com; path=/";
   }
}

function getFontCookie() {
		return getCookie("btplc_font_cookie");  		
}

function getColourCookie() {
		return getCookie("btplc_colour_cookie");  		
}
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 checkCookieAccept() { 
	
	//createTestCookie();
	setCookie("test", "test");		

	if (getCookie("test")=="test") { 
		return 1;
	}
	else { 
		return 0;
	}
}