/*	Load Events
----------------------------------------------- */

addLoadEvent(checkForAnchor)
addLoadEvent(doHelpLinks)
addLoadEvent(checkForSIFR)
addLoadEvent(doPopups)
addLoadEvent(sendFriendMessage)
addLoadEvent(writePrint)
addLoadEvent(writeTracking)

/*	IE4.x and NS4.x Detection for sIFR 2.0.1
	Written by Rob Green at Wheel
	
	Using Browser Detect from Quirksmode
	http://www.quirksmode.org/js/detect.html
----------------------------------------------- */

var detect = navigator.userAgent.toLowerCase();
var browser,version,total,thestring;

if (checkIt('konqueror')) browser = "Konqueror"
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible'))
{
	browser = "Netscape Navigator"
	version = detect.charAt(8);
}
else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

if (!(browser == "Internet Explorer" && version < 5) && !(browser == "Netscape Navigator" && version < 5)) {
	document.write("\<script src=\"\/js\/sifr.js\" type=\"text\/javascript\"><\/script\>");
	document.write("\<script src=\"\/js\/sifr-addons.js\" type=\"text\/javascript\"><\/script\>");
}


/*	Functions
----------------------------------------------- */

function writePrint() { // Insert print page button into nav

	if (!document.getElementById) return false;
	if (!document.createElement) return false;
	
	var target;
	
	if (document.getElementById("toolkit-buttons"))
		var target = document.getElementById("toolkit-buttons");
		
	if (document.getElementById("print-button"))
		var target = document.getElementById("print-button");
	
	if (target) {
		var print_link = document.createElement('a');
		print_link.href = '#';
		print_link.className = 'clean';
		print_link.onclick = function() {
			window.print()
			return false;
		}
		print_link.title = "This link will print the current page";

		var print_button = document.createElement('img');
		print_button.src = "/media/buttons/btn_printpage.gif";
		print_button.alt = "Print page";
		print_button.width = 70;
		print_button.height = 23;

		print_link.appendChild(print_button);

		var print_item = document.createElement('li');
		print_item.appendChild(print_link);
		print_item.className = "toolkit-buttons-print";

		target.appendChild(print_item);
	}
	
}

function writeTracking() { // Insert tracking tages into page
	if (!document.getElementById || !document.getElementById("tracking")) return false;
	if (!document.createElement) return false;
	var target = document.getElementById("tracking");
	var axel = Math.random()+"";
	var a = axel * 10000000000000;
	
	var tracking_img = document.createElement('img');
	tracking_img.width = 1; tracking_img.height = 1;
	tracking_img.alt = '';
	tracking_img.src = 'http://ad.uk.doubleclick.net/activity;src=789034;type=ctfbq664;cat=' + target.className + ';ord=1;num=' + a + '?';
	
	target.appendChild(tracking_img);
}

function sendFriendMessage() {
	if ( (!document.getElementById) || (!document.getElementById("message")) ) return false;	
	var target = document.getElementById("message");	
	target.onchange = function() {
		if (target.value == "") {			
			target.value = "Hello, I thought you might be interested in the Child Trust Fund. It\'s a new savings and investment account designed to give your child a financial head start in life.\n\nYou can find out more at:\nhttp://www.childtrustfund.gov.uk/"
		}
	}	
}

function openLink(oururl,features) { // SP2 friendly popups hopefully
    var winname=""; // Leave blank for multiple windows
	if(!features) { features="" }
	if (oururl) {
		try	{		    
			window.open(oururl,winname,features);		
			return false;
		} catch (e)	{
			return true; // Just use normal link if popup cannot open
		}
	}
}

function doPopups() { // Open links .'class' in new windows
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("popup")) {
      links[i].onclick = function() {
        openLink(this.href);
        return false;
      }
	  links[i].title = "This link will open in a new window";
    }
  }
}

function doHelpLinks() { // setup highlighting from links
  if (!document.getElementById) return false;
  if (!document.getElementsByTagName("body")[0].className == 'faqs') return false;
    if (document.getElementById("questions-listing")) {
	var links = document.getElementById("questions-listing").getElementsByTagName("a")
	for (var i=0; i < links.length; i++) {
            links[i].onclick = function() {
            jump(this.href);
        }
    }
  }
}

var theID = ""; /* I'm the heading ID to style on jump */
function highlighter(styleloop) {
	if (!document.getElementById) return false;
	var limit = 6;
	if (styleloop == 1) {
		document.getElementById(theID).className = "faq-step" + styleloop.toString();
		setTimeout("highlighter(" + (styleloop + 1) + ")", 1000); /* Pause on initial colour */
	} else if (styleloop <= limit) {
		document.getElementById(theID).className = "faq-step" + styleloop.toString();
		setTimeout("highlighter(" + (styleloop + 1) + ")", 33);
	} else {
		document.getElementById(theID).className = "faq-step0";
	}
}

function jump(target) {
	if (!document.getElementById) return false;
	if (target)	{
		if ( target.indexOf("#") >= 0 ) { //Check for anchorness
			
			if ( (theID != "") && document.getElementById(theID) ) { //Clear and check for previous instance
				document.getElementById(theID).className = "faq_off";
			}
			
			theID = target.substring(target.indexOf("#")+1);
			
			if (document.getElementById(theID)) {
				highlighter(0);
			} else {
				//alert("not found: "+theID);
			}
			
		}
	}
}

function checkForAnchor() {
	if (!document.getElementsByTagName("body")[0].className == 'faqs') return false;
	if (location.hash && location.hash!="#content") {
		jump(location.hash);
	}
}

function checkForSIFR() {
	//var productNavHeight = document.getElementById("product-navigation").offsetHeight;
	//if ((productNavHeight < 45) && (document.getElementById) && (typeof sIFR == "function")) {
	if (typeof sIFR == "function") {
		sIFR.replaceElement(named({nWidth:50,nHeight:40,sSelector:"h1", sFlashSrc:"/media/fonts/irmodena.swf", sColor:"#094587", sLinkColor:"#000000", sBgColor:"#FFFFFF", sHoverColor:"#CCCCCC", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0"}));
		sIFR.replaceElement(named({nWidth:50,nHeight:40,sSelector:"h2.replace", sFlashSrc:"/media/fonts/irmodenabold.swf", sColor:"#527FC1", sLinkColor:"#000000", sBgColor:"#FFFFFF", sHoverColor:"#CCCCCC", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0"}));
		if (document.getElementsByTagName("body")[0].id == 'home') {
			sIFR.replaceElement(named({nWidth:50,nHeight:40,sSelector:".more h2", sFlashSrc:"/media/fonts/irmodenabold.swf", sColor:"#527FC1", sLinkColor:"#000000", sBgColor:"#FFFFFF", sHoverColor:"#CCCCCC", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0"}));
			sIFR.replaceElement(named({nWidth:50,nHeight:40,sSelector:"h2", sFlashSrc:"/media/fonts/irmodenabold.swf", sColor:"#248E24", sLinkColor:"#000000", sBgColor:"#FFFFFF", sHoverColor:"#CCCCCC", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0"}));
		}
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
	  		oldonload();
	  		func();
		}
	}
}