/* Function that sets the text size when the browser loads the page. */

window.onload = function() {
  getCookieTextSize();
}

/* Function that creates (sets) the cookie. */

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 var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

/* Function that reads (gets) the cookie. */

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;
}

/* Function that erases (deletes) the cookie. */

function eraseCookie(name) {
  createCookie(name, "", -1);
} 

/* Function that gets the cookie for the text size. */

var defaultFontSize = 75;

function getCookieTextSize() {
  var cookieSize = readCookie('newFontSize');
  if (cookieSize) {
    defaultFontSize = parseInt(cookieSize);
    var obj = document.getElementById("body");
    obj.style.fontSize = defaultFontSize + "%";
  }
}

/* Function that changes the font size. */

function cssChangeFontSize(size) {
  if (size == "smaller") {
    if (defaultFontSize > 50) {
      defaultFontSize = defaultFontSize - 5;
      createCookie('newFontSize', defaultFontSize, 2);
    }
  }
  else {
    defaultFontSize = defaultFontSize + 5;
    createCookie('newFontSize', defaultFontSize, 2);
  }
  var obj = document.getElementById("body");
  obj.style.fontSize = defaultFontSize + "%";
}

/* Function that resets the font size. */

function cssResetFontSize(){
	defaultFontSize = 75;
	createCookie('newFontSize',defaultFontSize,2);
	var obj = document.getElementById("body");
	obj.style.fontSize = defaultFontSize + "%";
}

/* Function that disables the primary screen stylesheet and enables the alternate screen stylesheet. */

function changeStyles() {
  var style_dec;
  for (i=0; (style_dec = document.getElementsByTagName("link")[i]); i++) {
    if (style_dec.getAttribute("rel").indexOf("style") != -1 && style_dec.getAttribute("title")) {
	  style_dec.disabled = true;
	  if (style_dec.getAttribute("title") == 'print_preview')
	    style_dec.disabled = false;
    }
  }
}

/* Function that opens the send page window. */

function sendPage() {
  window.open('http://www.barnardos.org.uk/send_page.htm?url=' + document.URL, 'winName', 'height=750, width=550, scrollbars=yes, toolbar=no, status=no, resizable=yes');
}

/* Function that opens the print preview window. */

function printPage() {
  window.open(window.location.href + "?style=print", 'winName', 'height=425, width=650, scrollbars=yes, toolbar=yes, menubar=yes, status=yes, resizable=yes');
}
