/*
Online catalog pages can use 5 different style sheets defined in the <link> elements
in the header.  These can be changed by the drop down menu on the index page that 
calls this function.
*/
function setActiveStyleSheet(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;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  createCookie("style", title, 365);
}

/*
The font size of the catlog pages can be changed via the drop down menu on the index page
that calls this function.
*/
function setActiveSize(percent)
{
  document.body.style.fontSize = percent;
  createCookie("size", percent, 365);
}

/*
Returns the style sheet currently being used.
*/
function getActiveStyleSheet() 
{
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

/*
Returns the current font size.
*/
function getActiveSize() 
{  
  return document.body.style.fontSize;
}

/*
Returns the default style sheet.
*/
function getPreferredStyleSheet() 
{
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

/*
Creates a non-invasive cookie that is stored on the users computer which stores their 
font size and style sheet selections so that they can be appied to all catalog pages 
and be recalled the next time they log on.
*/
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=/";
}

/*
Recalls the font size and style sheet information stored in the cookie created
by the above funtion.
*/
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;
}

/*
When a catlog page loads this function checks to see if a cookie exits containing
the style sheet and font size selections and then applies the selection if so.
*/
window.onload = function(e) 
{
  if(readCookie("style"))
  {
  	var cookie01 = readCookie("style");
  	var title = cookie01 ? cookie01 : getPreferredStyleSheet();
  	setActiveStyleSheet(title);
  }
  
  if(readCookie("size"))
  {
    var cookie02 = readCookie("size");
    setActiveSize(cookie02);
  }
}