// Allows multiple functions to load





function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


// Stylesheet switcher

function getAllSheets() {
	if( !window.ScriptEngine && navigator.__ice_version ) {
        return document.styleSheets;
    }
	if( document.getElementsByTagName ) {
        var Lt = document.getElementsByTagName('link'), St = document.getElementsByTagName('style');
	} else if(
        document.styleSheets && document.all ) { var Lt = document.all.tags('LINK'), St = document.all.tags('STYLE');
	} else { return []; } for( var x = 0, os = []; Lt[x]; x++ ) {
		var rel = Lt[x].rel ? Lt[x].rel : Lt[x].getAttribute ? Lt[x].getAttribute('rel') : '';
		if( typeof( rel ) == 'string' && rel.toLowerCase().indexOf('style') + 1 ) { os[os.length] = Lt[x]; }
	}
    for(var x = 0; St[x]; x++ ) {
        os[os.length] = St[x];
    }
    return os;
}
function changeStyle() {
	window.userHasChosen = window.MWJss;
	for( var x = 0, ss = getAllSheets(); ss[x]; x++ ) {
		if( ss[x].title ) { ss[x].disabled = true; }
		for( var y = 0; y < arguments.length; y++ ) { if( ss[x].title == arguments[y] ) { ss[x].disabled = false; } }
} }
function rememberStyle( cookieName, cookieLife ) {
	for( var viewUsed = false, ss = getAllSheets(), x = 0; window.MWJss && ss[x]; x++ ) {
        if( ss[x].disabled != MWJss[x] ) {
            viewUsed = true;
            break;
        }
    }
	if( !window.userHasChosen && !viewUsed ) {
        return;
    }
	for( var x = 0, outLine = '', doneYet = []; ss[x]; x++ ) {
		if( ss[x].title && ss[x].disabled == false && !doneYet[ss[x].title] ) {
            doneYet[ss[x].title] = true;
            outLine += ( outLine ? ' MWJ ' : '' ) + escape( ss[x].title );
        }
    }
	if( ss.length ) {
        document.cookie = escape( cookieName ) + '=' + escape( outLine ) + ( cookieLife ? ';expires=' + new Date( ( new Date() ).getTime() + ( cookieLife * 86400000 ) ).toGMTString() : '' ) + ';path=/';
    }
}
function useStyleAgain( cookieName ) {
	for( var x = 0; x < document.cookie.split( "; " ).length; x++ ) {
		var oneCookie = document.cookie.split( "; " )[x].split( "=" );
		if( oneCookie[0] == escape( cookieName ) ) {
			var styleStrings = unescape( oneCookie[1] ).split( " MWJ " );
			for( var y = 0, funcStr = ''; styleStrings[y]; y++ ) { funcStr += ( y ? ',' : '' ) + 'unescape( styleStrings[' + y + '] )'; }
			eval( 'changeStyle(' + funcStr + ');' );
            break;
	   }
    }
    window.MWJss = []; for( var ss = getAllSheets(), x = 0; ss[x]; x++ ) {
        MWJss[x] = ss[x].disabled;
    }
}

window.onload = function(e) {
    useStyleAgain('styleTestStore');
}
window.onunload = function(e) {
    rememberStyle('styleTestStore',10);
}


function fontDisplay() {

  if (navigator.appName == 'Microsoft Internet Explorer' && navigator.platform == 'MacPPC' || !document.getElementById || !document.getElementById("helplinks")) return false;


  var h2 = document.createElement("h2");
  h2.appendChild(document.createTextNode("Font size"));
  var ulist = document.createElement("ul");
  ulist.setAttribute("class","textsize");
  ulist.setAttribute("className","textsize");

  var listitem1 = document.createElement("li");
  var ahref1 = document.createElement("a");
  var hide1 = document.createElement("span");

  listitem1.setAttribute("class", "sma");
  listitem1.setAttribute("className", "sma");
  ahref1.setAttribute("href","#");
  ahref1.setAttribute("title","Regular size text"); 
  hide1.setAttribute("class", "hide");
  hide1.setAttribute("className", "hide");

  hide1.appendChild(document.createTextNode("Regular font size "));
  ahref1.appendChild(hide1);
  ahref1.appendChild(document.createTextNode("A"));
  listitem1.appendChild(ahref1);


  var listitem2 = document.createElement("li");
  var ahref2 = document.createElement("a");
  var hide2 = document.createElement("span");

  listitem2.setAttribute("class", "med");
  listitem2.setAttribute("className", "med");
  ahref2.setAttribute("href","#");
  ahref2.setAttribute("title","Medium size text"); 
  hide2.setAttribute("class", "hide");
  hide2.setAttribute("className", "hide");

  hide2.appendChild(document.createTextNode("Medium font size "));
  ahref2.appendChild(hide2);
  ahref2.appendChild(document.createTextNode("A"));
  listitem2.appendChild(ahref2);


  var listitem3 = document.createElement("li");
  var ahref3 = document.createElement("a");
  var hide3 = document.createElement("span");

  listitem3.setAttribute("class", "lar");
  listitem3.setAttribute("className", "lar");
  ahref3.setAttribute("href","#");
  ahref3.setAttribute("title","Large size text"); 
  hide3.setAttribute("class", "hide");
  hide3.setAttribute("className", "hide");

  hide3.appendChild(document.createTextNode("Large font size "));
  ahref3.appendChild(hide3);
  ahref3.appendChild(document.createTextNode("A"));
  listitem3.appendChild(ahref3);


  ulist.appendChild(listitem1);
  ulist.appendChild(listitem2);
  ulist.appendChild(listitem3);

  var helplinks = document.getElementById("fontsize");
  helplinks.appendChild(h2);
  helplinks.appendChild(ulist);

  ahref1.onclick = function () 
  {
  	changeStyle('');
	return false;
  }
  ahref2.onclick = function () 
  {
  	changeStyle('med'); 
  	return false;
  }
  ahref3.onclick = function () {changeStyle('lar'); return false}

  ahref1.onmouseover = function () {window.status=''; return true}
  ahref2.onmouseover = function () {window.status=''; return true}
  ahref3.onmouseover = function () {window.status=''; return true}
}


function openInNewWindow() { 
	// Change "_blank" to something like "newWindow" to load all links in the same new window 
	var newWindow = window.open(this.getAttribute('href'), '_blank'); 
	newWindow.focus(); 
	return false; 
} 

// Opens PDF links in new windows

function doPopups()
{
 if (!document.getElementsByTagName) return false;
 var links = document.getElementsByTagName("a");
 for (var i=0; i < links.length; i++) {
  if (links[i].href.indexOf('.pdf') !== -1 || links[i].href.indexOf('.doc') !== -1) {
   links[i].onclick =
    function() {
     window.open(this.href,'popper','resizable,scrollbars,toolbar,menubar');
     return false;
    }
    links[i].title += "Opens in a new window";

  }
 }
}

function printPage() {
  if (window.print){
    window.print()
  }else{
    alert("Sorry, your browser doesn't support this feature, please use the browser's built in print button");
  }
}

function showPrintButton(){
	var printpagelink
	printpagelink=document.getElementById("printpagelink");
	if(printpagelink) printpagelink.innerHTML = "<a href='javascript:;' onclick='printPage(); return false'>Print page</a>"
}

addLoadEvent(fontDisplay);
addLoadEvent(doPopups);
addLoadEvent(showPrintButton);

