/* setActiveStyleSheet() disables all 3 of the font size stylesheets, then enables the one that the user selected.
   the parameter title is passed from the link and matches the title attribute of the css reference.	
   setActiveStyleSheet() also turns off all of the text size icons, and then turns on the current selection
*/
function setActiveStyleSheet(title) {
  var linkTags = document.getElementsByTagName("link");
  
  for(var i=0;i<linkTags.length;i++)
  {
  	if(linkTags[i].getAttribute("rel").indexOf("stylesheet") && linkTags[i].getAttribute("title"))
	{
		linkTags[i].disabled = true;
		var myTitle = linkTags[i].getAttribute('title');
		if(linkTags[i].getAttribute("title") == title)
		{
			linkTags[i].disabled = false;
		}
	}
  }
}

/* getActiveStyleSheet() is used to find the currently enabled stylesheet, the name of that stylesheet is stored using a cookie
   and retrieved the next time a page is loaded to maintain the correct font-size.
*/
function getActiveStyleSheet() {
  var linkTags = document.getElementsByTagName("link");
  
  for(var i=0; i<linkTags.length; i++) {
  	if(linkTags[i].getAttribute("rel").indexOf("stylesheet") != -1
		&& linkTags[i].getAttribute("title") && !linkTags[i].disabled)
		return linkTags[i].getAttribute("title");
  }
  return null;
}

/* getPreferredStyleSheet() is used when no cookie is found to load the default/standard stylesheet
*/
function getPreferredStyleSheet() {
  var linkTags = document.getElementsByTagName("link");
  
  for(var i=0; i<linkTags.length; i++) {
    if(linkTags[i].getAttribute("rel").indexOf("style") != -1
       && linkTags[i].getAttribute("rel").indexOf("alt") == -1
       && linkTags[i].getAttribute("title"))
	   return linkTags[i].getAttribute("title");
  }
  return null;
}

/* setCookie() creates a cookie of name "style" with the value matching the user's current stylesheet selection
*/
function setCookie(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=/";
}

/* getCookie() retrieves a cookie of name "style", parses the value and returns xsmall, small or medium based on the user's previous stylesheet selection
*/
function getCookie(name) {
  var cookieName = name + "=";
  var cookieSplit = document.cookie.split(';');
  for(var i=0;i < cookieSplit.length;i++) {
    var c = cookieSplit[i];
    while(c.charAt(0)==' ')
	{
		c = c.substring(1,c.length);
	}
    if(c.indexOf(cookieName) == 0)
	{
		return c.substring(cookieName.length,c.length);
	}
  }
  return null;
}


/* This onload event retrieves the style cookie when a page loads 
*/

function onLoadGetCookie() {
  var cookie = getCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

/* This unonload event sets the style cookie when a page unloads 
*/


function unLoadSetCookie() {
  var title = getActiveStyleSheet();
  setCookie("style", title, 365);
}