function setActiveStyleSheet(attribute,title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && a.getAttribute("title").indexOf(attribute) != -1) {
	  a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  if ((attribute == "Contrast") && (document.getElementById(title) != null)){
		document.getElementById(title).checked = true;
  }
}

function getActiveStyleSheet(attribute) {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") &&  a.getAttribute("title").indexOf(attribute) != -1 && !a.disabled) return a.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();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  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;
}

function reStyle(attribute,change) {
	if (change == null) { // If only style sheet name is provided, switch to that sheet 
		var newCSS = attribute;
		if (attribute.indexOf("FontSize") != -1) {
			setActiveStyleSheet("FontSize",newCSS);
		} else if (attribute.indexOf("Contrast") != -1) {
			setActiveStyleSheet("Contrast",newCSS);
		}
	} else { // If a change value is provided increment/decrement the current style sheet
		var curCSS, curState, newState;
		switch(attribute) {
			case "FontSize" :
				curCSS = getActiveStyleSheet(attribute);
				curState = 2; //set default in case no style sheet is active
				if ((curCSS != null) && (curCSS.lastIndexOf("_") > -1)) {
					curState = curCSS.substr(curCSS.lastIndexOf("_")+1);
				}
				newState = parseInt(curState) + parseInt(change);
				if ((newState > 0) && (newState < 5)) {
					setActiveStyleSheet(attribute,attribute + "_" + newState);
				}
				break;
			case "Contrast" :
				curCSS = getActiveStyleSheet(attribute);
				curState = 1; //set default in case no style sheet is active
				if ((curCSS != null) && (curCSS.lastIndexOf("_") > -1)) {
					curState = curCSS.substr(curCSS.lastIndexOf("_")+1);
				}
				newState = parseInt(curState) + parseInt(change);
				if ((newState > 0) && (newState < 3)) {
					setActiveStyleSheet(attribute,attribute + "_" + newState);
				}
				break;
		}
	}
}