// JavaScript Document
window.onload = function (e){
	document.is = new Is();
	if (document.is.supported) {
			initializecss();
			initializeTree();			
			if (document.forms) { initializeOnFocusRules(); cssInputSelector (true) }			
			hidenoscript("hide");
					
			if (window.loadadvanced) window.loadadvanced();		
	}
}
window.onunload = function(e) {	persistcss (); }

	

/*ON FOCUS RULES*/
function initializeOnFocusRules (){ iOfr ("INPUT", function (a) {return a.type=="text"}); iOfr ("TEXTAREA", function (a) {return true})}
function iOfr (a, f){var k,i = gbn (a); for (var j=0; j<i.length, k = i[j]; j++) {if (f(k) && k.className.indexOf('noclear')==-1){k.old = k.value;k.onfocus = function (e){ this.value = (this.value == this.old)?"":this.value }}}}

/*CSS INPUT SELECTOR*/
function cssInputSelector (override){ var k,i = gbn ("INPUT"); for (var j=0; j<i.length, k = i[j]; j++) {if (k.form && k.form.className.length>0 && (k.className.length==0 || override)){k.className+=" "+k.form.className+k.type; }}}

/*NAVIGATION*/
function initializeTree() {
  var leftcol = gbi("colleft")
  if (leftcol) { var at = highlight (leftcol, 'selected', function (h) {return h.parentNode}); var tseed = Math.ceil(Math.random()*1000);
  treeX (leftcol, false, tseed); reveal (at); liveNode (at); if (at) {marknav(gbi('colmiddle'),at.href); if (gbi('colright'))marknav(gbi('colright'),at.href);}}
}

function treeX(c, nested, idSd) {	var tmp = c.firstChild;	
while (tmp) { if (tmp.tagName == "UL") { if (nested) {tmp.id = tmp.id?tmp.id:'t'+idSd; tmp.tctrl = 'c'+idSd; wlinkc (tmp.tctrl, tmp.id); colX (tmp);} idSd++; treeXb (tmp, idSd++); } tmp = tmp.nextSibling; }
}
function wlink (aTa, listTa, hrefStr, css, alt) { 
	var linktext  = "<img src='/images/spacer.gif' width='16' height='16' border=0 hspace=0 vspace=0 alt='"+alt+"' />"; 
	var aTag = gbi (aTa); if (!aTag){ aTag = makeLink (linktext, true, hrefStr, aTa); var listTag = gbi (listTa);
	if (listTag){listTag.parentNode.insertBefore(aTag,listTag.parentNode.firstChild);} else {alert ("couldn't find the list!"); }
	} else { aTag.href = hrefStr; aTag.innerHTML = linktext;}
	aTag.parentNode.className = css;
	return aTag;
}
document.cbpClass = "selectCollapsed";
document.ebpClass = "selectExpanded";
function treeXb(c,ids) {var tmp = c.firstChild; while (tmp) {if (tmp.tagName == "LI") { treeX (tmp,true,ids++); } tmp = tmp.nextSibling;	}}
function wlinke (aTa, listTa) {var expandStr = "javascript:cbr('"+aTa+"','"+listTa+"');"; return wlink (aTa, listTa, expandStr, document.ebpClass, "click to close");}
function wlinkc (aTa, listTa) {var collapseStr = "javascript:ebr('"+aTa+"','"+listTa+"');"; return wlink (aTa, listTa, collapseStr, document.cbpClass, "click to expand");}
function cbr (aTa, listTa){var l = gbi (listTa); col1 (l); wlinkc (aTa, listTa);}	
function ebr (aTa, listTa){var l = gbi (listTa); exp1(l); wlinke (aTa, listTa);}	
function findParent (s) {while (s && !(s.tctrl)) {s = s.parentNode;} return s; }
function colX (p){var tmp = p.getElementsByTagName ("LI");	for (var i=0; i<tmp.length; i++){tmp[i].style.display = 'none';	}}function exp1 (p){var tmp = p.firstChild; while (tmp){cstyle (tmp,'block'); tmp = tmp.nextSibling; }}
function col1 (p){var tmp = p.firstChild; while (tmp){cstyle (tmp,'none'); tmp = tmp.nextSibling;}}
function liveNode (tmp) { if (tmp && tmp.parentNode){var c = tmp.parentNode.firstChild; while (c){ if (c.tctrl){ebr (c.tctrl, c.id);} c = c.nextSibling; }}}
function reveal (s){var tmp = findParent (s); if (tmp) {ebr (tmp.tctrl, tmp.id); reveal (tmp.parentNode); }}
function cstyle (o,s){ if (o.style){ if (o.style.display){ o.style.display = s; }}}
function highlight (obj, nc, fn){var li = highlightLink (obj, nc, fn, cleanURL (document.location.href)); if (!li) {var bk = getBK(); if (bk){ li = highlightLink (obj, nc, fn, bk); if (li){li = addLink(fn,li)}}} if (!li) {li = highlightLink(obj, nc, fn, decode64(querystring('hseid')))}; if (!li) {li = highlightLink(obj, nc, fn, document.referrer) }; return li;}
function highlightLink (obj, nc, fn, href) {var l = obj.getElementsByTagName ("A");
for (var i=0; i<l.length; i++) { if (cleanURL (l[i].href) == href) { fn (l[i]).className = nc; return l[i];}} return false;}
function marknav (obj, href){var ehref = 'hseid='+encode64(href); var cl=cleanURL (document.location.href); var l = obj.getElementsByTagName ('A'); 
	for (var i=0; i<l.length; i++) {if (chooseToAppend (l[i].href,Array ('^http://[^\.]*?\.hse\.gov\.uk','^file:///','^http://intranet'),Array(cl,'mailto:','.*\.(pdf|PDF)$',l[i].innerText,l[i].innerHTML),Array ( ))) l[i].href = querystringappend (l[i].href,ehref);}
}
function chooseToAppend (testLink, mustmatchArr, dontmatchArr, testArr){
	var mustmatchresult = false;
	for (var h=0; h<mustmatchArr.length; h++){if (testLink.match (mustmatchArr[h])) mustmatchresult = true;} 	
	if (mustmatchresult){
		for (var i=0; i<dontmatchArr.length; i++){if (testLink.match (dontmatchArr[i])) return false;} 
		for (var j=0; j<testArr.length; j++) {if (testLink == testArr[j]) return false;} 
	} else {
		return false;
	}	
	return true;
}
function getBK (){var r;for(var a,i=0; (a = gbn("link")[i]); i++) {if(a.getAttribute("rel").indexOf("bookmark") != -1 && a.getAttribute("title")=='navigation') {r=a.getAttribute("href");}};return r;}
function addLink (fn, al) {var el = fn (al);el.innerHTML+="<ul><li class='selected'><a href='#skip'>"+gbi("colmiddle").getElementsByTagName ('h1')[0].innerText+"</a></li></ul>"; return el.getElementsByTagName ('A')[1];}
/* CSS */
function initializecss (){ var c = readCookie("style"); var t = c?c:getPreferredcss();	setActivecss(t, readCookie("href")); }
function persistcss (){ var c = readCookie("style"); if (c) {var title = getActivecss(); writeActivecss (title);}}
function writeActivecss (t, h) { createCookie("style", t, 365); if (h) { createCookie("href", h, 365); } }
function clearAlternatecss (){ setActivecss("",""); clearCookie (["style","href"]); window.unload = ""; }
function setActivecss(title, href) { if (!document.is.supported) { return notSupported(); }
  var i, a, main, setSheet = false;
  for(i=0; (a = gbn("link")[i]); i++) {
	if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) { a.disabled = true;
	   	if(a.getAttribute("title") == title) { a.disabled = false; writeActivecss (title); setSheet = true; } 
	}
	if (!setSheet && href && title){
		var hd = gbn("HEAD") [0], l = document.createElement ("LINK"); l.setAttribute ("rel","alternate stylesheet"); l.setAttribute ("href",href); l.setAttribute ("title",title);
		hd.appendChild(l); writeActivecss (title, href);
	}
  }
  return void 0;
}

function getActivecss() {
  var i, a; for(i=0; (a = gbn("link")[i]); i++) {
  if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");}
  return null;
}

function getPreferredcss() {
  var i, a; for(i=0; (a = gbn("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;
}


/********** UTILS *************/
function hidenoscript (aClass) {var al = gbn ("*"); for (var i=0; i<al.length; i++) {if (al[i].className){ if(al[i].className.indexOf ("noscript")>-1) al[i].className = aClass;}}}
function createCookie(n,v,d) {if (d) {var date = new Date(); date.setTime(date.getTime()+(d*24*60*60*1000)); var e = "; expires="+date.toGMTString();} else e = "";document.cookie = n+"="+v+e+"; path=/";}
function clearCookie (nameArr) { for (var i=0; i<nameArr.length; i++){ createCookie (nameArr [i], "", -1);}	return null; }
function querystring (id) { var m = document.location.href.match ("[^?]*?\\?(.*)"); if (m && m.length == 2) {var arg=m[1].match ("(.*?&)?"+id+"=([^(&#)]*)"); var aler=id+"\n"; if (arg) {for (var k=0; k<arg.length; k++){aler+=arg[k]+"\n";}} return (arg[2]);} else {return "";}}
function querystringappend (url, q) {var u = url.match("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?");var r = ""; if (u[2])r+=u[2]+":"; if (u[4])r+="//"+u[4]; r+=u[5]; if (u[7]){r+="?"+u[7]+"&"+q;} else {r+="?"+q;} if (u[9])r+="#"+u[9];return r;}
function stringInArray (aString, anArray){ for (var i=0; i<anArray.length; i++){if (aString == anArray [i]) return true;} return false;}
function readCookie(name) {var n = 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(n) == 0) return c.substring(n.length,c.length); }  return null; }
function cleanURL (dirtyurl){ return cleantext(cleantext (dirtyurl, '?'), '#'); }
function cleantext (i, a){ if (i.indexOf (a) >= 0) { return i.substring (0, i.indexOf (a));} else { return i; } }
function makeLink(ltext, ishtml, href, id){var l = document.createElement("A"); l.setAttribute("href", href); if (id){l.setAttribute("id",id);}
if (ishtml){ l.innerHTML = ltext; } else { ltext = document.createTextNode(ltext); l.appendChild(lText); } return l;}
function notSupported (){alert ("Unfortunately your browser is not supported for this functionality"); return void 0; }
function gbn (a){return document.getElementsByTagName (a);}
function gbi (a){return document.getElementById (a);}
/*********** SNIFFER ***********/
function Is() {
var a = navigator.userAgent.toLowerCase();
this.m = parseInt(navigator.appVersion);
this.mi = parseFloat(navigator.appVersion);
this.ns = ((a.indexOf('mozilla') != -1) && ((a.indexOf('spoofer') == -1) && (a.indexOf('compatible') == -1)));
this.ns4 = (this.ns && (this.m == 4));
this.ns6 = (this.ns && (this.m >= 5));
this.ie = (a.indexOf("msie") != -1);
this.ie3 = (this.ie && (this.m < 4));
this.ie4 = (this.ie && (this.m == 4) && (a.indexOf("msie 5.0") == -1));
this.ie5 = (this.ie && (this.m == 4) && (a.indexOf("msie 5.0") != -1));
this.ie55 = (this.ie && (this.m == 4) && (a.indexOf("msie 5.5") != -1));
this.ie6 = (this.ie && (a.indexOf("msie 6.0")!=-1) );
this.opera = (a.indexOf("opera") != -1);
this.opera6 = (a.indexOf("opera 6") != -1);
this.opera7 = (a.indexOf("opera 7") != -1);
this.supported = this.ie6 || this.ie5 || this.ie55 || this.ns6 || this.opera6 || this.opera7;
}

/* encoding */
   var kStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
   function encode64(inp) {
      var o="";
	  var c1, c2, c3, e1, e2, e3, e4 = "";
      var i = 0;
      do {
         c1 = inp.charCodeAt(i++); c2 = inp.charCodeAt(i++); c3 = inp.charCodeAt(i++);
         e1 = c1 >> 2; e2 = ((c1 & 3) << 4) | (c2 >> 4); e3 = ((c2 & 15) << 2) | (c3 >> 6); e4 = c3 & 63;
         if (isNaN(c2)) { e3 = e4 = 64; } else if (isNaN(c3)) { e4 = 64; }
         o += kStr.charAt(e1) + kStr.charAt(e2) + kStr.charAt(e3) + kStr.charAt(e4);
		 c1 = c2 = c3 = e1 = e2 = e3 = e4 = "";
      } while (i < inp.length);
      return o;
   }

   function decode64(inp) {
      var o = ""
	  var c1, c2, c3, e1, e2, e3, e4 = "";
      var i = 0;
      // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
      inp = inp.replace(/[^A-Za-z0-9\+\/\=]/g, "");

      do {
         e1 = kStr.indexOf(inp.charAt(i++)); e2 = kStr.indexOf(inp.charAt(i++)); e3 = kStr.indexOf(inp.charAt(i++)); e4 = kStr.indexOf(inp.charAt(i++));
         c1 = (e1 << 2) | (e2 >> 4); c2 = ((e2 & 15) << 4) | (e3 >> 2); c3 = ((e3 & 3) << 6) | e4;
		 o += String.fromCharCode(c1);
		 if (e3 != 64) { o += String.fromCharCode(c2); }
         if (e4 != 64) { o += String.fromCharCode(c3); }
         c1 = c2 = c3 = e1 = e2 = e3 = e4 = "";
      } while (i < inp.length);
      return o;
   }