﻿/*
 * <p>Title: Style Managemet</p>
 * <p>Description: JavaScript for css and font-size management based on cookies. Tested on Win (IE, Moz, Opera) and Mac (Safari)
</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: http://www.mac4.it</p>
 * @author m.casati@mac4.it
 * @version 1.1
 */

  
/**************************************************************************\
	STRING PROTOTYPE
\**************************************************************************/

function replaceText(pattern, substitute)
{
	return this.split(pattern).join(substitute);
}
String.prototype.replaceText = replaceText;

function trim()
{
	var newstr = this + "";
	newstr = newstr.lTrim();
	newstr = newstr.rTrim();
	return newstr;
}
String.prototype.trim = trim;
function lTrim()
{
	var newstr = this + "";
	while(newstr.charAt(0) == " ") 
		newstr = newstr.substring(1, newstr.length);	
	return newstr;
}
String.prototype.lTrim = lTrim;
function rTrim()
{
	var newstr = this + "";
	while(newstr.charAt(newstr.length - 1) == " ") 
		newstr = newstr.substring(0, newstr.length - 1);	
	return newstr;
}
String.prototype.rTrim = rTrim;

/**************************************************************************\
	COOKIES
\**************************************************************************/
function setCookie(key, value, expirationdate)
{
	var c = key + "=" + escape(value) + ";";
	c += "path=/;";
	if(expirationdate)
		c += "expires=" + expirationdate.toUTCString() + ";";
	document.cookie = c;
}
function getCookieList()
{
	var cl = document.cookie.split(";");
	var c = new Array();
	for(var i = 0; i < cl.length; i++)
	{
		var a = cl[i].split("=");
		if(a.length == 2)
			c[a[0].trim()] = escape(a[1].trim());
	}
	return c;
}
function getCookie(key)
{
	return getCookieList()[key];
}

/**************************************************************************\
	CSS
\**************************************************************************/
var COOKIE_CSS_KEY = "style";
var CSS_LIST = 
{
	"home"			        : "../res/asm____0.css",
	"default"			: "../res/asm____1.css",
	"plain"				: "../res/asm____2.css",
	"contrast"			: "../res/asm____3.css"
};
var DEFAULT_CSS = "default";
function setCSS(css)
{
	setCookie(COOKIE_CSS_KEY, css, getCookieExpirationDate());
	document.location.reload();
}
function initCSS()
{
	if(!document.getElementsByTagName)
		return;
	var css = getCookie(COOKIE_CSS_KEY);
	if(css)
	{
		var links = document.getElementsByTagName("link");
		for(var i = 0; i < links.length; i++)
		{
			if(links[i].type == "text/css" ) 
		         {
			    if ( links[i].title != "home")
			     {
				links[i].href = CSS_LIST[css];
			     }
		            else
			     {
				links[i].href = CSS_LIST["home"];
			     }
	                 }
		}	
	}
}

/**************************************************************************\
	TEXT SIZE
\**************************************************************************/
var COOKIE_TEXT_SIZE_KEY = "textsize";
var TEXT_SIZE_LIST = 
{
	"m"				: "small",
	"l"				: "large",
        "x"                             : "xx-large"
};
var DEFAULT_TEXT_SIZE = "m";

function getkey(e)
{
	if (window.event)
   		return window.event.keyCode;
	else if (e)
   		return e.keyCode;
	else
   		return null;
}

function setTextSize(s)
{
	setCookie(COOKIE_TEXT_SIZE_KEY, s, getCookieExpirationDate());
	document.location.reload();
}

function initTextSize()
{
	if(!document.getElementsByTagName)
		return;
	var cs = getCookie(COOKIE_TEXT_SIZE_KEY);
	s = (cs) ? cs : DEFAULT_TEXT_SIZE;
	document.getElementsByTagName("body")[0].style.fontSize = TEXT_SIZE_LIST[s];
}

/**************************************************************************\
	COMMONS
\**************************************************************************/
function getCookieExpirationDate()
{
	var dt = new Date();
	dt.setFullYear(dt.getFullYear() + 1);	// cookie expires after 1 year
	return dt;
}

/**************************************************************************\
	EVENT HANDLER
\**************************************************************************/
function scriptError(msg, url, line)
{
	//window.status = "ERROR: " + msg + " @ line " + line;
	return true;
}
window.onerror = scriptError;

bW=checkBrowser();



function checkBrowser()
{
   this.ver = navigator.appVersion;
   this.dom = document.getElementById ? 1 : 0;
   this.ie6= (this.ver.indexOf("MSIE 6") > -1 && this.dom) ? 1 : 0;
   this.ie7= (this.ver.indexOf("MSIE 7") > -1 && this.dom) ? 1 : 0;
   this.ie5= (this.ver.indexOf("MSIE 5") > -1 && this.dom) ? 1 : 0;
   this.ie4=(document.all && !this.dom) ? 1 : 0;
   this.ns5=(this.dom && parseInt(this.ver) >= 5) ? 1 : 0;
   this.ns4=(document.layers && ! this.dom) ? 1 : 0;
   this.bw=(this.ie7 || this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns5);
   this.cookie=navigator.cookieEnabled;
   return this;
}

function init(e)
{
	initCSS();
	fixCSS();
	initTextSize();
	initMenu();
}

function fixCSS()
{
   if (bW.ie4 || bW.ie6 || bW.ie7)
    {
      var obj=document.getElementById("attachmentHeader");
      if (obj != null) 
        {
          obj.style.borderTop="none";
        }
      if (bW.ie5 || bW.ie6)
       {
        obj=document.getElementById("mainContent");
        if (obj != null) 
          {
            obj.style.height="900px";
            obj.style.marginTop="0";
            obj.style.marginRight="0";
            obj.style.marginBottom="0";
            obj.style.marginLeft="15%";
            obj.style.paddingTop="0";
            obj.style.paddingRight="1.5em";
            obj.style.paddingBottom="0";
            obj.style.paddingLeft="0";
          }
         obj=document.getElementById("logo");
         if (obj != null) 
	   {
             obj.src="../img/logo.gif";
           }
       }
   }
}

var COOKIE_TOGGLE_MENU_KEY;
function toggle(i)
{
   var view=document.getElementById("toggleList_" + i);
   s = (view.style.display == "") ? "block" : "none";
   view.style.display=s;
   if (s=="block")
     {
      setCookie(COOKIE_TOGGLE_MENU_KEY, i, getCookieExpirationDate());
     }
}


function initMenu()
{
     	var cs = getCookie(COOKIE_TOGGLE_MENU_KEY);
	s = (cs) ? cs : "0";
        if ( (s != "0"))
	  {
	   document.getElementById("toggleList_" + s).style.display="block";
	  }
        vS= (document.getElementById("homeContent") == null) ? "block" : "none";
        document.getElementById("toggleList_" + 1).style.display=vS;
        document.getElementById("toggleList_" + 2).style.display=vS;
		document.getElementById("toggleList_" + 3).style.display=vS;
		document.getElementById("toggleList_" + 4).style.display=vS;
		document.getElementById("toggleList_" + 5).style.display=vS;
		document.getElementById("toggleList_" + 6).style.display=vS;
		document.getElementById("toggleList_" + 7).style.display=vS;
}


if(navigator.platform.toLowerCase().indexOf("mac") != -1)	// MAC OS: use event handler
	window.onload = init();
