currentStyleID = 3;

window.onload = function(e) {
	if (navigator.cookieEnabled == true) {
		var cookie = readCookie("bavstyle");
		if (cookie) {
			//alert("cookie = " + cookie);
			setActiveStyleSheet(cookie);
			currentStyleID = parseInt(cookie.charAt(6));
			//alert ("active style = " + currentStyleID);
		} else {
			//alert("cookie not set");
			fontStandard();
		}
	} else {
		//alert("cookies not enabled");
		fontStandard();
	}
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("bavstyle", title, 30);
}

function fontPlus () {
	if (currentStyleID < 5) {
		currentStyleID++;
		setActiveStyleSheet('style_'+currentStyleID);
	}
}

function fontMinus() {
	if (currentStyleID > 1) {
		currentStyleID--;
		setActiveStyleSheet('style_'+currentStyleID);
	}
}

function fontStandard() {
	currentStyleID = 3;
	setActiveStyleSheet('style_'+currentStyleID);
}

function setActiveStyleSheet(title) {
	var i, a, main, currentStyleID;

	for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if (a.getAttribute("rel") != null){
			if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
				a.disabled = true;
				if (a.getAttribute("title") == title) {
					a.disabled = false;
					currentStyleID = parseInt(title.charAt(6));
				}
			}

		}
	}

	return currentStyleID;
}

function getActiveStyleSheet() {
  var i, a, activeStyle;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
    	if(a.disabled == false) {
     		activeStyle = a.getAttribute("title");
    		return activeStyle;
    	}
    }

  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {

  //alert("create cookie = " + value);
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {

	//alert("read cookie = " + document.cookie);
	var nameEQ = name + "=";
  	var ca = document.cookie.split(';');
  	for(var i=0;i < ca.length;i++) {
    	var c = ca[i];
    	while (c.charAt(0)==' ') c = c.substring(1,c.length);
    	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  	}
  	return null;
}
