// $Id: eci_acc.js,v 1.10 2007-11-13 19:28:11 patrick Exp $

function ECI_acc(instanceName, ui, cookieName, arg, sep1, sep2, val1, skipIds)
{
	this.instanceName = instanceName;
	this.ui = ui;
	this.cookieName = (String(cookieName) != 'undefined') ? cookieName : 'ECI_acc';
	this.arg = (String(arg) != 'undefined') ? arg : 'vi';
	this.sep1 = (String(sep1) != 'undefined') ? sep1 : '=';
	this.sep2 = (String(sep2) != 'undefined') ? sep2 : '&';
	this.val1 = (String(val1) != 'undefined') ? val1 : '1';
	// skip contrast for printer version
	this.skipIds = (String(skipIds) != 'undefined') ? skipIds : [];
	this.isPrinterVersion = this.inPrinterVersion();
	var isIE5Mac = (!window.showModelessDialog && window.ActiveXObject && document.getElementById);
	this.browserSupported = !isIE5Mac && document.getElementsByTagName && document.getElementById && document.createElement;
	this.registerLoadEvents();
	this.disableAlternateStyleSheets();
	this.useStyleAgain(this.cookieName);
}

ECI_acc.prototype.idToSkip = function(id)
{
	for (var i = 0; i < this.skipIds.length; i++)
	{
		if (this.skipIds[i] == id) return true;
	}
	return false;
}

ECI_acc.prototype.inPrinterVersion = function()
{
	this.args = this.getGET(this.sep1, this.sep2);
	if (typeof this.args[this.arg] == "string" && this.args[this.arg] == this.val1)
	{
		return true;
	}
	return false;
}

ECI_acc.prototype.getAlternateStyleSheets = function()
{
	if (!this.browserSupported) { return; }
	var allSheets = [];
	for (var i = 0, a; a = document.getElementsByTagName('link')[i]; i++)
	{
		var rel = a.rel ? a.rel : (a.getAttribute ? a.getAttribute('rel') : '');
		if (typeof rel == 'string' && /^alternate stylesheet$/i.test(rel))
		{
			allSheets[allSheets.length] = a;
		}
	}
	return allSheets;
}


ECI_acc.prototype.disableAlternateStyleSheets = function()
{
	if (!this.browserSupported) { return; }
	var a = this.getAlternateStyleSheets();
	for (var i = 0; i < a.length; i++)
	{
		a[i].disabled = true;
		if (a[i].setAttribute) a[i].setAttribute('disabled','disabled');
	}
}

ECI_acc.prototype.changeStyle = function()
{
	if (!this.browserSupported) { return; }
	var allSheets = this.getAlternateStyleSheets();
	for (var i = 0; i < allSheets.length; i++)
	{
		for (var j = 0; j < arguments.length; j++)
		{
			// skip contrast for printer version
			if (this.isPrinterVersion && this.idToSkip(arguments[j]))
			{
				continue;
			}
			if (allSheets[i].id && document.getElementById(allSheets[i].id) && allSheets[i].id == arguments[j])
			{
				allSheets[i].disabled = false;
				this.changeProperties(this[allSheets[i].id], document.getElementById(allSheets[i].id));
			}
		}
	}
}

ECI_acc.prototype.changeSize = function(set, unset)
{
	if (!this.browserSupported) { return; }
	for (var i = 0; i < unset.length; i++)
	{
		document.getElementById(unset[i]).disabled = true;
	}
	document.getElementById(set).disabled = false;
	if (window.scrollBy) { window.scrollBy(0,63000); }
	this.changeProperties(this[set], document.getElementById(set));
	this.rememberStyle(this.cookieName, 10);
}

ECI_acc.prototype.changeContrast = function(id)
{
	if (!this.browserSupported) { return; }
	var a = document.getElementById(id);
	a.disabled = a.disabled ? false : true;
	this.changeProperties(this[id], a);
	this.rememberStyle(this.cookieName, 10);
}

ECI_acc.prototype.rememberStyle = function(cookieName, cookieLife)
{
	if (!this.browserSupported) { return; }
	var allSheets = this.getAlternateStyleSheets();
	var outLine = '';
	for (var i = 0, s; s = allSheets[i]; i++)
	{
		if (s.id && document.getElementById(s.id) && s.disabled == false)
		{
			outLine += (outLine ? ' ECI ' : '') + s.id;
		}
	}
	var session = '';
	if (cookieLife)
	{
		session =
			';expires=' +
			new Date((new Date()).getTime() +
			(cookieLife * 86400000)).toGMTString();
	}
	document.cookie = (cookieName + '=' + escape(outLine) + session + ';path=/');
}

ECI_acc.prototype.useStyleAgain = function(cookieName)
{
	if (!this.browserSupported) { return; }
	for (var i = 0; i < document.cookie.split("; ").length; i++)
	{
		var oneCookie = document.cookie.split("; ")[i].split("=");
		if (oneCookie[0] == cookieName)
		{
			var funcStr = '';
			var styleStrings = unescape(oneCookie[1]).split(" ECI ");
			for (var j = 0; j < styleStrings.length; j++)
			{
				funcStr += (j ? ',' : '') + 'styleStrings[' + j + ']';
			}
			eval('this.changeStyle(' + funcStr + ');');
			break;
		}
	}
}

ECI_acc.prototype.registerLoadEvents = function()
{
	if (!this.browserSupported) { return; }
	window.ECI_acc_instanceName = this.instanceName;
	this.old_onload = null;
	this.old_onunload = null;
	if (typeof window.onload == "function")
	{
		this.old_onload = window.onload;
	}
	window.onload = function()
	{
		var instance = eval(window.ECI_acc_instanceName);
		if (instance.old_onload)
		{
			instance.old_onload();
			instance.old_onload = null;
		}
		instance.useStyleAgain(instance.cookieName);
	}
	if (typeof window.onunload == "function")
	{
		this.old_onunload = window.onunload;
	}
	window.onunload = function()
	{
		var instance = eval(window.ECI_acc_instanceName);
		if (instance.old_onunload)
		{
			instance.old_onunload();
			instance.old_onunload = null;
		}
		if (!instance.isPrinterVersion) instance.rememberStyle(instance.cookieName,10);
	}
}

ECI_acc.prototype.changeProperties = function(arr, sheet)
{
	for (var a in arr)
	{
		for (var b in arr[a])
		{
			if (document.getElementById(a))
			{
				eval(
					"document.getElementById(a)."
					+ b + "=" +
					(typeof arr[a][b] == 'function' ? arr[a][b](sheet): arr[a][b])
				);
			}
		}
	}
}

ECI_acc.prototype.writeUI = function()
{
	if (this.browserSupported)
	{
		document.writeln(this.ui);
	}
}

ECI_acc.prototype.getGET = function(s1, s2)
{
	var args = {};
	if (location.search)
	{
		var query = unescape(location.search.substring(1));
		var pairs = query.split(s2);
		for (var i = 0; i < pairs.length; i++)
		{
			var pos = pairs[i].indexOf(s1);
			if (pos == -1) { continue; }
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			args[argname] = String(value);
		}
	}
	return args;
}
