// 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();
    }
  }
}

// Opens Word Documents links in new windows

function doPopups2()
{
 if (!document.getElementsByTagName) return false;
 var links = document.getElementsByTagName("a");
 for (var i=0; i < links.length; i++) {
  if (links[i].href.indexOf('.doc') !== -1) {
   links[i].onclick =
    function() {
     window.open(this.href,'popper','resizable,scrollbars');
     return false;
    }
    links[i].title += "\n(opens in a new window)";

    var img = document.createElement("img");

    img.setAttribute("style", "vertical-align:middle"); 
    img.setAttribute("src", "../i/new-win-icon.gif");
    img.setAttribute("alt", " (opens in a new window)");
    links[i].appendChild(img);
  }
 }
}


// 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].onclick =
    function() {
     window.open(this.href,'popper','resizable,scrollbars');
     return false;
    }
    links[i].title += "\n(opens in a new window)";
    var img = document.createElement("img");

    img.setAttribute("style", "vertical-align:middle"); 
    img.setAttribute("src", "../i/new-win-icon.gif");
    img.setAttribute("alt", " (opens in a new window)");
    links[i].appendChild(img);
  }
 }
}



// 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; 
			// alert("switch off" + ss[x].title)
		}
		if(arguments.length == 1)
		{
			for( var y = 0; y < arguments.length; y++ ) 
			{ 
				if( ss[x].title == arguments[y] ) 
				{
					// alert("switch on" + ss[x].title)
					ss[x].disabled = false; 
				} 
			}
		}
	} 
	removeLink("fon1");
	backToRegularView()
}
function rememberStyle( cookieName, cookieLife ) {


	// Checks to see if a view has been used.

	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( 'changeStyleColour(' + funcStr + ');' );
			eval( 'changeStyleFont(' + funcStr + ');' );
           	break;
	   }
    }
    window.MWJss = []; for( var ss = getAllSheets(), x = 0; ss[x]; x++ ) {
        MWJss[x] = ss[x].disabled;
    }    	
}

function clearBackToRegularView()
{
	if(document.getElementById("returnnormalview"))
	{
		eleRemove = document.getElementById("returnnormalview");
		document.body.removeChild(eleRemove)
	}
}


function backToRegularView()
{
	var eleForm;
	var eleDiv;
	var eleInput;
	
	clearBackToRegularView()

	for( var x = 0, ss = getAllSheets(); ss[x]; x++ ) 
	{
		
		if (ss[x].rel == "alternate stylesheet")
		{
			
			if (ss[x].disabled == false )
			{
				if(ss[x].title == "col4")
				{
					var eleForm = document.createElement("form");
					var eleInput = document.createElement("input");
					var eleDiv = document.createElement("div");

					eleDiv.setAttribute("id", "returnnormalview");

					eleForm.setAttribute("action", "#");
					eleForm.setAttribute("class", "regview");
					eleForm.setAttribute("className", "regview");

					eleInput.setAttribute("type", "submit");
					eleInput.setAttribute("value", "Back to regular layout");
					eleInput.setAttribute("class", "button");
					eleInput.setAttribute("className", "button");

					eleForm.appendChild(eleInput);
					eleDiv.appendChild(eleForm);
					document.body.appendChild(eleDiv);

					eleDiv = document.getElementById("returnnormalview");
					eleInputs = eleDiv.getElementsByTagName("input");
					eleInputs[0].onclick = function () {changeStyle(''); return false}
				}
			}
		}
	}
}

function createElements(fullDisplayOptions)
{
	var div = document.createElement("div");
	var h2 = document.createElement("h2");
	var ulist = document.createElement("ul");

	h2.appendChild(document.createTextNode("Font size"));

	if (fullDisplayOptions == false)
	{
		// ulist.setAttribute("id","fonts");
		var eleLiList1 = createElementLi("fon1", "Small font size", "A", "sma");
		var eleLiList2 = createElementLi("fon2", "Medium font size", "A", "med");
		var eleLiList3 = createElementLi("fon3", "large font size", "A", "lar");
		// var eleFont = document.getElementById("fontch"); 

		
		ulist.appendChild(eleLiList3);
		ulist.appendChild(eleLiList2);
		ulist.appendChild(eleLiList1);
		
		
		// div.appendChild(h2);
		// div.appendChild(ulist);
	}

	if (fullDisplayOptions == true)
	{
		
		div.setAttribute("class","split");
		div.setAttribute("className", "split");
		
		var divTop = document.createElement("div");
		divTop.setAttribute("class","top");
		divTop.setAttribute("className", "top");
		
		var divLeft = document.createElement("div");
		var divRight = document.createElement("div");
		divRight.setAttribute("class","half2");
		divRight.setAttribute("className", "half2");
				
		h2.setAttribute("class","second");
		h2.setAttribute("className", "second");
		
		var eleUlColour = document.createElement("ul");
		var eleUlLayout = document.createElement("ul");
		var h2Color = document.createElement("h2");
		var h2Layout = document.createElement("h2");

		var eleLiList1 = createElementLi("fon1", "", "Regular font size", "fon1");
		var eleLiList2 = createElementLi("fon2", "", "Medium font size", "fon2");
		var eleLiList3 = createElementLi("fon3", "", "Large font size", "fon3");

		eleUlColour.setAttribute("id","colour");
		eleUlLayout.setAttribute("id","layout");

		h2Color.appendChild(document.createTextNode("Display"));
		h2Color.setAttribute("class","second");
		h2Color.setAttribute("className", "second");
		h2Layout.appendChild(document.createTextNode("Layout"));
		h2Layout.setAttribute("class","second");
		h2Layout.setAttribute("className", "second");
		
		var eleLiList1col = createElementLi("col", "", "Regular display", "lay1");
		var eleLiList2col = createElementLi("col1", "", "High contrast", "lay2");
		var eleLiList3col = createElementLi("col2", "", "Dark blue on cream", "lay3");
		var eleLiList4col = createElementLi("col3", "", "Dark blue on light blue", "lay4");
		var eleLiList5col = createElementLi("col4", "", "PDA simulator", "lay5");

		ulist.appendChild(eleLiList1);
		ulist.appendChild(eleLiList2);
		ulist.appendChild(eleLiList3);
		
		eleUlColour.appendChild(eleLiList1col);
		eleUlColour.appendChild(eleLiList2col);
		eleUlColour.appendChild(eleLiList3col);
		eleUlColour.appendChild(eleLiList4col);
		eleUlColour.appendChild(eleLiList5col);
			
		divLeft.appendChild(h2Color);
		divLeft.appendChild(eleUlColour);
		
		divRight.appendChild(h2);
		divRight.appendChild(ulist);
		
		divTop.appendChild(divLeft);
		divTop.appendChild(divRight);
		
		
		div.appendChild(divTop);	
	} 
	
  	return ulist;
  
  }
  function fontDisplay() 
  {
  
  
  	/* This function check to see if the browser is recognised and calls the create elements function based on whether the divs are found. */
    	if (navigator.appName == 'Microsoft Internet Explorer' && navigator.platform == 'MacPPC' || !document.getElementById || !document.getElementById("accoptions")) return false;
    
    		var eleFontResizer = createElements(false)
    		
   		var eleAccOptions = document.getElementById("accoptions"); 
   		
   		var eleListItems = eleAccOptions.getElementsByTagName("ul");
   		
   		var eleLiListItems = eleAccOptions.getElementsByTagName("li");
   		
   		var listitem = document.createElement("li");
   		var eleSpan = document.createElement("span");
   		
   		listitem.setAttribute("class", "text");
		listitem.setAttribute("className", "text");  
   		
   		listitem.appendChild(document.createTextNode("Text bigger"));
   		
   		eleLiListItems[1].className = "jsborder";
   		
   		listitem.appendChild(eleFontResizer);
   		
   		// eleLiListItems[0].appendChild(eleFontResizer);
   		
   		insertAfter(listitem,eleLiListItems[1])
   		
		// alert(eleAccOptions.innerHTML)

		// ahref.appendChild(document.createTextNode("More viewing options"));

		// para.appendChild(ahref);	
		// eleFont.appendChild(para);
		
    		/* If a element is found with the id displayoptions then the full options will be included */
   	
   		if(document.getElementById("jsmorestyles"))
  		{
  			var strJavaScriptUn = document.getElementById("jsmorestyles");
  			var para = document.createElement("p");
  			
			strJavaScriptUn.innerHTML = "";
  			
  			strJavaScriptUn.appendChild(document.createTextNode("For your convenience, we've provided you with a number of different ways in which you can view this website."));
  			
  			
  			var displayElements = createElements(true)
  			var eledisplayOptions = document.getElementById("jsmorestyles");
  			eledisplayOptions.appendChild(displayElements);
  			
  			
  			var newString = "fon1";
  			var newLoad = 0;
  			for( var x = 0, ss = getAllSheets(); ss[x]; x++ ) 
			{
				if(ss[x].title) 
				{
					if (ss[x].title.substr(0,3) == "fon")
					{
						// alert("Current state " + ss[x].disabled)
						if (ss[x].disabled == false)
						{
							newLoad++; 
							newString = ss[x].title;
						}
					}
				}	
  			}
  			if (newLoad > 1)
  			{
  				// If no stylesheets are selected then set to 0;
  				newString = "fon1";
  			}
  			removeLink(newString);
  		 }
  		 
  }
  
function insertAfter(newElement,targetElement) {

  	var parent = targetElement.parentNode;

  	if(parent.lastchild == targetElement) {

  		parent.appendChild(newElement);

  	} else {
  	
  	parent.insertBefore(newElement, targetElement.nextSibling);

  	}

}
  function createElementLi(strType, strTextNode, strLinkText, strClass)
  {
  
  	var listitem = document.createElement("li");
  	var ahref = document.createElement("a");
	var off = document.createElement("span");
	
	ahref.setAttribute("class", strClass);
	ahref.setAttribute("className", strClass);  
	ahref.setAttribute("href","#");
	off.setAttribute("class", "off");
	off.setAttribute("className", "off");
	off.appendChild(document.createTextNode(strTextNode));
	ahref.appendChild(off);
	ahref.appendChild(document.createTextNode(strLinkText));
	listitem.appendChild(ahref);

	ahref.onclick = function () 
		{
		
		changeStyleFont(strType); 
				
		return false
		}
		ahref.onmouseover = function () {window.status=''; return true}
		return listitem;
  }

function changeStyleFont() 
{
	var strStyleSheetTitle;
	var strStyleForRemoval = "fon" + 1;
	// alert(strStyleForRemoval)
	// window.userHasChosen = window.MWJss;
	// alert(arguments.length);
	for( var x = 0, ss = getAllSheets(); ss[x]; x++ ) 
	{
		strStyleSheetTitle = ss[x].title
		if( ss[x].title ) 
		{
			if (strStyleSheetTitle.substr(0,3) == "fon")
			{
				ss[x].disabled = true;
				
			}
		}
		if(arguments.length < 3)
		{
			for( var y = 0; y < arguments.length; y++ ) 
			{ 
				if( ss[x].title == arguments[y] ) 
				{ 
					
					ss[x].disabled = false; 
					strStyleForRemoval = ss[x].title;
				} 
			}
		}
	}
	
	removeLink(strStyleForRemoval);
}

function changeStyleColour() 
{
	var strStyleSheetTitle;
	var strStyleForRemoval;
	// window.userHasChosen = window.MWJss;
	// alert(arguments.length);
	for( var x = 0, ss = getAllSheets(); ss[x]; x++ ) 
	{
		strStyleSheetTitle = ss[x].title
		if( ss[x].title ) 
		{
			if (strStyleSheetTitle.substr(0,3) == "col")
			{
				ss[x].disabled = true;
			}
		}
		if(arguments.length < 3)
		{
			for( var y = 0; y < arguments.length; y++ ) 
			{ 
				if( ss[x].title == arguments[y] ) 
				{ 			
					ss[x].disabled = false;	
				} 
			}
		}
	}
	backToRegularView();
}
function removeLink(strStyleForRemoval)
{
	if (document.getElementById("jsmorestyles"))
	{
		eleMoreStyleOptions = document.getElementById("jsmorestyles");
		// alert(eleMoreStyleOptions);
		eleLinks = eleMoreStyleOptions.getElementsByTagName('li');
		// alert("num of links " + eleLinks.length);
		removeAlllinks();
		 for( var numOfLinks = 0; numOfLinks < eleLinks.length; numOfLinks++ ) 
		 {6
		 	
		 	// alert(eleLinks[numOfLinks].className + " : " + numOfLinks);
			if(eleLinks[numOfLinks].className == strStyleForRemoval)
			{
				//alert("match " + eleLinks[numOfLinks].className + " : " + numOfLinks);
				eleLinks[numOfLinks].setAttribute("id", "selected");
			}
		 }
	}
	// alert(document.getElementById("jsmorestyles").innerHTML)
}
function removeAlllinks()
{
	// alert("remove");
	eleMoreStyleOptions = document.getElementById("jsmorestyles");
	eleLinks = eleMoreStyleOptions.getElementsByTagName('li');
	for( var numOfLinks = 0; numOfLinks < eleLinks.length; numOfLinks++ ) 
	{
		eleLinks[numOfLinks].setAttribute("id", ""); 
	}
}



window.onload = function(e) {

    useStyleAgain('styleStore');
    
}

window.onunload = function(e) {

    rememberStyle('styleStore',10);
}




addLoadEvent(doPopups);
addLoadEvent(doPopups2);
addLoadEvent(fontDisplay);