/*-------------------------------------------------------------------------------------
Style Switching JavaScript functions based on the code in the article located at 
- "http://www.alistapart.com/articles/alternate/" - 
Code is to be placed in the body of the document as follows:

In the Header

-A list of styleSheets (disabling them initially stops them from flashing, non-default sheets are labeled as alternate):

<link rel="stylesheet" href="/css/intranet_main_redesign_small.asp?step=0&menucolor=a00000" type="text/css" disabled="true" title="default"/>
<link rel="alternate stylesheet" href="/css/intranet_main_redesign_small.asp?step=2&menucolor=a00000" type="text/css" / disabled="true" title="plus2">

-A link to this document:

<script language="JavaScript" src="styleswitcher.js" type="text/javascript"></script>

In the Body:

-A link or button that calls the script and indicates which stylesheet to change to(by title):
<span class="cls_font_size_small"><a href="#" onclick="setActiveStyleSheet('default'); return false;">A</a></span> 
	

----------------------------------------------------------------------------------------*/


	//this function cycles through all the stylesheets and disables them, then enables the selected one
	function setActiveStyleSheet(title)
		{
   		var i, a, main;
		//cycles through all the "link" tags to pull out ones that are stylesheets
 	  	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
			{
			if(a.getAttribute("media") != "print"){
     		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))
				{
       			a.disabled = true;
       			//if the title matches the title of the selected one, re-enable it
				if(a.getAttribute("title") == title) a.disabled = false;
    	 		}
			}	
   			}
		}
	
	//this function returns the name of the active stylesheet for cookie-setting purposes	
	function getActiveStyleSheet()
		{
		var i, a;
		//cycles through all the "link" tags to pull out ones that are stylesheets
 		for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
			{
			//if they are stylesheets AND they are not disabled, then the title is returned
  			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  			}
  		return null;
		}
	
	//function to create a cookie	
	function createCookie(name,value,days)
		{
		//sets up a cookie with a name, value, and an expiration date in "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=/";
		}
	
	//function to read a given cookie with "name"
	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 retrieves the preferred stylesheet
	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;
		}
	
	//function for when the window loads
	window.onload = function(e)
		{
		//reads the style cookie
  		var cookie = readCookie("ADLstyle");
		//retrieves the preferred stylesheet from the cookie
  		var title = cookie ? cookie : getPreferredStyleSheet();
		//sets the active stylesheet to the preferred Style Sheet
  		setActiveStyleSheet(title);
		}
	//function for when the window closes
	window.onunload = function(e)
		{
		//retrieves the active stylesheet
		var title = getActiveStyleSheet();
		//creates the cookie for the preferred stylesheet
		createCookie("ADLstyle", title, 365);
		}
	