//Font changer
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();

$(document).ready(function() {
  $("#textonly").click(function () { 
    $("#mainContainer img").css("display","none");
    $(".topBanner").css("display","none");
    $("#pageContent").css("width","auto");
  });
  $("#searchbox").focus(function() {if ($("#searchbox").val() == "Search") {$("#searchbox").val("");}});
  $("#searchbox").keypress(function(event) {
     if (event.which == '13') {
         event.preventDefault();
         window.location.href = '/Search?search=' + $("#searchbox").val();
      }});
  if (querySt("l") != null)
  {
    $("#" + querySt("l").toLowerCase()).addClass("selected");
  }
  
  $(".limited100").counter({type: 'word',goal: 100});
  $(".limited300").counter({type: 'word',goal: 300});
  $(".limited500").counter({type: 'word',goal: 500});
  $('.absoluteIframeDOMWindow').openDOMWindow({height:300, width:500,eventType:'click', windowSource:'iframe', windowPadding:0,  anchoredClassName:'partnerLB' }); 
  $(".date_input").datepicker({'dateFormat' : 'dd/mm/yy'});
 });

function init() {
    font = "";
    if (getFontCookie() != "") {
        if (getFontCookie() != "") {
            font = getFontCookie();
        }
        setActiveStyleSheet(font);
    }
}

function setCookie(name, fontsize, expires) {
    if (fontsize == '') {
        fontsize = 'Small';
    }

    font = fontsize;
    submit();
    location.reload();
}

function submit() {
    createFontCookie(font);
}

function addStylesheet(title) {
    document.write("<link type='text/css' rel='alternate stylesheet' href='/Css/Font" + 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 setNewCookie(name, value) {
    var expdate = new Date();
    expdate.setTime(expdate.getTime() + (365 * 24 * 60 * 60 * 1000)); // 1 year from now
    document.cookie = name + "=" + value + ";expires=" + expdate.toGMTString() + "; path=/";
}

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

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 querySt(ji) {
  hu = window.location.search.substring(1);
  gy = hu.split("&");
  for (i=0;i<gy.length;i++) {
    ft = gy[i].split("=");
    if (ft[0] == ji) {
      return ft[1];
    }
  }
} 
  
function checkCookieAccept() {

    createTestCookie();

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