/***********************************************************************/
/* Stylesheet Toggle
/***********************************************************************/

function setActiveStyleSheet(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.disabled = true;
if(a.getAttribute("title") == title) a.disabled = false;
}
}
}

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 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;
}

window.onunload = function(e) {
var title = getActiveStyleSheet();
createCookie("style", title, 365);
}

var stylev;
var href=window.location.href;
var query=href.indexOf("?")>=0?href.substring(href.indexOf("?")):"";
var mktpos=query.toLowerCase().indexOf("style="),mkt="";
if(mktpos>=0){
	mkt=query.indexOf("&",mktpos)>0?query.substring(mktpos+6,query.indexOf("&",mktpos)):query.substring(mktpos+6)
	stylev=mkt;
}else{
	stylev = readCookie("style");
}
var title = stylev ? stylev : getPreferredStyleSheet();
setActiveStyleSheet(title);

window.onload = function(e) {
	addLoadEvent(carryoverSize(title));
}

function applyResizeScript() {
    if(!document.getElementById) { return; }    
    var toolTextresizeNormal = document.getElementById("tool-textresize-normal");
	var toolTextresizeLarge = document.getElementById("tool-textresize-large");
	var toolTextresizeExtralarge = document.getElementById("tool-textresize-extralarge");

    if (!toolTextresizeNormal) { return; }
	if (!toolTextresizeLarge) { return; }
	if (!toolTextresizeExtralarge) { return; }

    toolTextresizeNormal.onclick = function()
        {setActiveStyleSheet('', 1); carryoverSize(''); return false;}
	toolTextresizeNormal.keypress = function()
        {setActiveStyleSheet('', 1); carryoverSize(''); return false;}
	toolTextresizeLarge.onclick = function()
        {setActiveStyleSheet('textsize-large'); carryoverSize('textsize-large'); return false;}
	toolTextresizeLarge.keypress = function()
        {setActiveStyleSheet('textsize-large'); carryoverSize('textsize-large'); return false;}
	toolTextresizeExtralarge.onclick = function()
        {setActiveStyleSheet('textsize-extralarge'); carryoverSize('textsize-extralarge'); return false;}
	toolTextresizeExtralarge.keypress = function()
        {setActiveStyleSheet('textsize-extralarge'); carryoverSize('textsize-extralarge'); return false;}
} 

function carryoverSize(theSize) {
	var anchors = document.getElementsByTagName("a"),thehref,thehost;
	var anchors = document.getElementsByTagName("a");
	var referrer=document.referrer;
	var href=window.location.href;
	for (var i = 0; i < anchors.length; i++) {
		//Environment+MKT
		thehref=anchors[i].getAttribute("href");
		theclass=anchors[i].getAttribute("class");
		if(!theclass) theclass="";
		
		//only for http link
		if (thehref != null) {
		    if (thehref.indexOf("http://")>=0 || theclass.indexOf("carry_style")>=0){
			    thehref=appendCode(thehref,"style="+theSize,"style=");
			    
			    if(anchors[i])anchors[i].setAttribute("href",thehref);
		    }
		}
	}
}

function appendCode(theref,theValue,theParam){
if(theValue=="" || theref.substring(0,1)=="#" || theref.toLowerCase().substring(0,10)=="javascript"){return theref;}
else{
if(theref.indexOf("covermelife")>0||theref.indexOf("assurance-viepourmeproteger")>0||theref.indexOf("mlilacc01")>0){theValue="Open&"+theValue;}
if(theref.indexOf("www.manulife.ca")>0) return theref;
if(theref.toLowerCase().indexOf(theParam.toLowerCase())>=0) {
mktposHref=theref.toLowerCase().indexOf(theParam.toLowerCase());
mktsize=theParam.length;
mktHref=theref.indexOf("&",mktposHref)>0?theref.substring(mktposHref+mktsize,theref.indexOf("&",mktposHref)):theref.substring(mktposHref+mktsize);
return theref.replace(theParam+mktHref,theValue).replace(theParam.toLowerCase()+mktHref,theValue);
}
else if(theref.indexOf("?")>=0) return theref+"&"+theValue;
else return theref+"?"+theValue;
}
}

/***********************************************************************/
/* On Load Events
/***********************************************************************/

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

// Apply text resize script
addLoadEvent(applyResizeScript); 

