//Here is the code for my style-sheet changer.  It will have cookies to keep things going from one page to the next
//
//We just deal with alternative style sheets that are tagged with a vis_ prefix.  
//That means they are to affect the visual styles, but not necessarily the layout
//
function setStyle(selectedStyle)
{
	var i, sheet;
	
	for (i = 0; (sheet = document.getElementsByTagName ("link")[i]); i++)
	{
		if ((sheet.getAttribute("rel").indexOf("style" ) != -1) &&
			sheet.getAttribute ("title"))
			{
				if (sheet.getAttribute("title").indexOf("vis") != -1)
				{
//	alert ("in setStyle(), sheet attribute is " + sheet.getAttribute("title"));
					sheet.disabled = true;
					if (sheet.getAttribute("title") == selectedStyle)
						sheet.disabled = false;
					
				}
			}
	}
}


//what style did we choose?  ----------------------------------------------
function getChosenStyle()
{
		var i, sheet;
		
		for (i = 0; (sheet = document.getElementsByTagName ("link")[i]); i++)
		{
			if ((sheet.getAttribute("rel").indexOf("style" ) != -1) &&
				sheet.getAttribute ("title"))
				{
					if (sheet.getAttribute("title").indexOf("vis") != -1)
					{
							//alert("Sheet is " + sheet.getAttribute("title") + "\n sheet.disabled is " + sheet.disabled);
						if (sheet.disabled == false)
						{
								//alert("Sheet is " + sheet.getAttribute("title") + "\n sheet.disabled is " + sheet.disabled);
							
							return sheet.getAttribute("title");
						}
					}
				}
		}
	return "normal";		//if nothing got returned, we just use 'normal' style
}


//****************************************************************
//Create and read cookies so style can remain consistent through out the site

//First we get cookies if any exist
function getCookies()
{
	if (document.cookie)
	{
		visitor = readCookie('visitorName');
//		alert ("visitorName: " + visitor);
		
		pageGreeting(visitor);
		
		setStyle(readCookie('chosenStyle'));
	}
	else
	{
		setStyle ('normal');
	}
		setRadio(getChosenStyle());
}


//Here we set the cookies
function setCookies()
{
	if(visitor)
		createCookie ('visitorName', visitor);
	
	createCookie ('chosenStyle', getChosenStyle());
	
//	alert ("Cookie " = document.cookie);
}

//generic function for creating a cookie -- expiration date fixed at a fortnight
function createCookie(name, value)
{
	var now = new Date();
	var expDate = new Date();
	expDate.setTime(now.getTime() + (14*24*60*60*1000)); 	//cookie expires in a fortnight

	var expString = "; " +expDate.toUTCString();

	document.cookie = name + "=" + value + "; expires=" + expDate.toUTCString() + "; path=/"; 
	
	//alert("cookie created: " + document.cookie);
}


//generic cookie reading function
function readCookie(name)
{
	var cookies = document.cookie.split("; ");
	
	for (var i = 0; i<cookies.length; i++)
		if (cookies[i].indexOf(name) == 0)
	{
//		alert("cookie... " + cookies[i].substring(cookies[i].indexOf('=')+1));
			return cookies[i].substring(cookies[i].indexOf('=') + 1);
	}
//	alert ("cookie reads: " + document.cookie);
			
	return null;		//if name not found, we return null
	
}


//OK, repeat after me: "the property 'checked' is a *boolean*
// Hence one deals with it as if it were one.  Why do I forget?
//
function setRadio(selectedStyle)
{
	if (document.getElementById (selectedStyle))
		document.getElementById(selectedStyle).checked = true;
}
