﻿function setActiveStyleSheet(title, option)
{
	var stylesheets = document.getElementsByTagName("link");
	var matchString = title + option;
	
	for (i = 0; i < stylesheets.length; i++)
	{
		if (stylesheets[i].getAttribute("rel").indexOf("style") != -1 && stylesheets[i].getAttribute("title") && stylesheets[i].getAttribute("title").indexOf(title) != -1)
		{
			stylesheets[i].disabled = true;
			if (stylesheets[i].getAttribute("title") == matchString)
			{
				stylesheets[i].disabled = false;
			}
		}
	}
}


function getActiveStyleSheet(title)
{
	var stylesheets = document.getElementsByTagName("link");
	for (i = 0; i < stylesheets.length; i++)
	{
		if (stylesheets[i].getAttribute("rel").indexOf("style") != -1
			&& stylesheets[i].getAttribute("title") && stylesheets[i].getAttribute("title").indexOf(title) != -1 
			&& !stylesheets[i].disabled)
		{ return stylesheets[i].getAttribute("title"); }
	}
	return null;
}


function getPreferredStyleSheet(title)
{
	var stylesheets = document.getElementsByTagName("link");
	for (i = 0; i < stylesheets.length; i++)
	{
		if (stylesheets[i].getAttribute("rel").indexOf("style") != -1 && stylesheets[i].getAttribute("rel").indexOf("alt") == -1
			&& stylesheets[i].getAttribute("title") && stylesheets[i].getAttribute("title").indexOf(title) != -1)
		{ return stylesheets[i].getAttribute("title"); }
	}
	return null;
}


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();
//		var expires = "Thu, 01-Jan-1970 00:00:01 GMT";

	}
	else expires = "";

	var cookieString = name + "=" + value + expires + "; path=/";
//	alert("Creating Cookie: \n\n" + cookieString);	
	document.cookie = cookieString;
}


function readCookie(name, title)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
//alert("All Cookies: \n\n" + document.cookie);

//	alert("Cookie Count: " + ca.length);

	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ')
		{
//			alert("Getting substring of cookie splits");
			c = c.substring(1,c.length); 
		}

		if (c.indexOf(nameEQ) == 0)
		{ 
//			alert("Match found for cookie name!");
			var cookieContents = c.substring(nameEQ.length,c.length);
			var Pairs = cookieContents.split('|');
//			alert("Cookie contents split into " + Pairs.length + " parts");
//			alert("Try to find setting for '" + title + "'");
			for (j = 0; j < Pairs.length; j++)
			{
//				alert(Pairs[j]);
				if (Pairs[j].indexOf(title) != -1)
				{
//					alert("Matched for '" + title + "', so splitting up string to get value");
					PairParts = Pairs[j].split(',');
//					alert(PairParts);
					value = PairParts[1].substring(title.length, PairParts[1].length);

					if (value.length > 0)
					{
//						alert(value);
						return value;
						break;
					}
				}
			}
		}

//			return c.substring(nameEQ.length,c.length);
	}

	return null;
}


//window.onload = function(e)
//{
	var cookie = readCookie("DBASizeStyle","size");
	var option = cookie ? cookie : getPreferredStyleSheet("size");
	setActiveStyleSheet("size", option);

	var cookie = readCookie("DBASizeStyle","contrast");
	var option = cookie ? cookie : getPreferredStyleSheet("contrast");
	setActiveStyleSheet("contrast", option);
//}

window.onunload = function(e)
{
	var sizetitle = getActiveStyleSheet("size");
	var contrasttitle = getActiveStyleSheet("contrast");
	cookietitle = "size," + sizetitle + "|contrast," + contrasttitle;
	createCookie("DBASizeStyle", cookietitle, 365);
}

