/*
Copyright Devis Lucato, 2008

Developed for Sclerosi.org

Please support Multiple Sclerosis Information and Support 

with a back link to http://www.sclerosi.org
*/

// prototype for old browsers
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

// get client cookie
function getCookie(name) {
	var index = document.cookie.indexOf(name + "=");
	if (index == -1) return null;
	index = document.cookie.indexOf("=", index) + 1;
	var endstr = document.cookie.indexOf(";", index);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(index, endstr));
}

// set client cookie
function setCookie(name, value) {
	if (value != null && value != "")
		document.cookie = name + "=" + escape(value) + "; expires=" + CSS_expiry.toGMTString() + "; path=/";
	if(document.cookie.indexOf(name,0) < 0) {
		alert(MsgCookies);
	}
}

var CSS_Initialized=false;
var CSS_InitStarted=false;
var CSS_today = new Date();
var CSS_expiry = new Date(CSS_today.getTime() + 120 * 24 * 60 * 60 * 1000);// 120 days 

function SetCSS(name,value,values) {
	if(!document.getElementsByTagName) return;
	var documentCsss = document.getElementsByTagName('link');
	
	// fix for first visit 
	var disableFirst=(value != null);
	
	// value check 
	var isvalid=false;
	for (var i=0 ; i < values.length ; i++ ) {
		if (value==values[i]) isvalid=true;
	}
	if (!isvalid) value=values[0];
	
	// save cookie, after initialization
	if (CSS_Initialized) {
		setCookie('CSS'+name,value);
	}
	
	// disable all stylesheets
	for (var i = 0 ; i < documentCsss.length ; i++ ){
		var LinkCss = documentCsss[i];
		var LinkTitle = LinkCss.getAttribute("title");
		if (values.indexOf(LinkTitle)>-1) {
			if (disableFirst || LinkTitle!=values[0]) {
				LinkCss.disabled = true;
			}
		}
	}
	// enable choosen stylesheet
	for (var i = 0 ; i < documentCsss.length ; i++ ){
		var LinkCss = documentCsss[i];
		var LinkTitle = LinkCss.getAttribute("title");
		if (LinkTitle == value) {
			LinkCss.disabled = false;
		}
	}
}

// enable configured stylesheets
function CSS_Init() {
	if (!CSS_InitStarted) {
		CSS_InitStarted=true;
		SetCSS("Width",getCookie('CSSWidth'),["Narrow","Wide"]);
		SetCSS("Font",getCookie('CSSFont'),['Font1','Font2','Font3','Font0']);
		CSS_Initialized=true;
	}
}

try
{
onload_functions.push('CSS_Init()');
}
catch(err)
{
//
}
