function setActiveStyleSheet(title) {
   for(i=0; (a = document.getElementsByTagName("a")[i]); i++) 
   {
       if(a.id=='highContrast')
       {
           if(title=="")
           {
               title = a.getAttribute("action");
            }

            if(title=='default')
            {
               a.setAttribute("action", "highcontrast");
            }
            else
            {
               a.setAttribute("action", "default");
            }
       }
   }

  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  createCookie("style", title, 365);
  // var randomnumber=Math.random();
  // document.getElementById("ifrm").src+="&rand="+randomnumber;    
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  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 'default';
}

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 setFontSize(strSelectorText, strSize){
        document.body.style.fontSize = strSize;
}

function highlightLink(){

        var str = arguments[0];
        if(str == 1) str = "1.0";
        str += "";
        str = str.replace(/\./,"")
        _topbar = document.getElementById("ResizeBar");
        if(_topbar){
                for(i=1;i < _topbar.childNodes.length;i += 2){
                        if(_topbar.childNodes[i].id == "a" + str){
                                _topbar.childNodes[i].className += " activeResize";
                        }
                        else {
                                _topbar.childNodes[i].className = _topbar.childNodes[i].className.replace(/\ activeResize/,"");
                        }
                }
        }

        _footer = document.getElementById("FooterResizeBar");

        if(_footer){
                for(i=0;i < _footer.childNodes.length;i += 2){
                        if(_footer.childNodes[i].id == "a" + str){
                                _footer.childNodes[i].className += " activeResize";
                        }
                        else {
                                _footer.childNodes[i].className = _footer.childNodes[i].className.replace(/\ activeResize/,"");
                        }
                }
        }
}


function handleFontSize(_val){

	var strBaseFontSize = "1em";

	if(document.body.style.fontSize!="")
	{
		strBaseFontSize = document.body.style.fontSize;		
	}

	var intBaseFontSize = parseFloat(strBaseFontSize.substr(0, strBaseFontSize.length-2));
	
	switch(_val)
	{
		case 'smaller':
			intBaseFontSize = intBaseFontSize - 0.2;
			strBaseFontSize = intBaseFontSize + 'em';
			break;
        
        		case 'bigger':       
			intBaseFontSize = intBaseFontSize + 0.2;
			strBaseFontSize = intBaseFontSize + 'em';
        			break;
    
        		default:
			strBaseFontSize = _val;
			break;
    	}
        
         	setFontSize('#Content .FullWidth', strBaseFontSize);
        
         	createCookie("fontsize", strBaseFontSize,365);
}

function getDate(){
        var _now=new Date();
        _now.setTime(_now.getTime()+365*24*60*60*1000);
        return(_now);
}

function initTextSwitcher() {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);

  var _cookie = readCookie("fontsize");
  var _size = _cookie ? _cookie : "1em";
  handleFontSize(_size);
}

addOnLoadEvent(initTextSwitcher);

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
