document.write('<link rel="StyleSheet" href="/qcaAssets/css/qcaJavascript.css" type="text/css" media="screen" />');

/******************************************************************************************
Global variables
******************************************************************************************/
var assets = "qcaAssets/index.html";

var ua = navigator.userAgent.toLowerCase();

/******************************************************************************************
set bolean to detect safari on mac - the flash text does not seem to work with 
the opacity background on any gecko browsers on mac
******************************************************************************************/
mac = (ua.indexOf("windows") == -1)? true:false; 
safari = (ua.indexOf("safari") != -1)? true:false; 

/******************************************************************************************
set bolean to detect IE7 - the text sizing sniffer has problems with IE7 zoom.
******************************************************************************************/
ie7 = (ua.indexOf("msie 7") != -1)? true:false;

/******************************************************************************************
set bolean to detect Operas 
******************************************************************************************/
op7 = (ua.indexOf("opera/7") != -1)? true:false;
op9 = (ua.indexOf("opera/9") != -1)? true:false;

/******************************************************************************************
set bolean to detect IE5 Mac
******************************************************************************************/
ieMac = ((ua.indexOf("msie") != -1)&&(ua.indexOf("windows") == -1))? true:false;

/******************************************************************************************
set bolean to detect various ie browsers for use with fnMoveFocus()
******************************************************************************************/
iepc = (((ua.indexOf("msie 7") != -1)||(ua.indexOf("msie 6") != -1)||(ua.indexOf("msie 5.5") != -1))&&(ua.indexOf("windows") != -1)&&(ua.indexOf("opera") == -1))? true:false;


/******************************************************************************************
set bolean to detect Netscape - small:large class resize 
******************************************************************************************/
ns6 = (ua.indexOf("netscape6") != -1)? true:false;
ns7 = (ua.indexOf("netscape/7") != -1)? true:false;
ns8 = (ua.indexOf("netscape/8") != -1)? true:false;

/******************************************************************************************
On body load run these configuration functions
@param 		bodyRef 		object reference of the entire body of the page
******************************************************************************************/
function fnConfig() {
	fnFlashDetect();
	fnFRT();
	fnProcessDefinitions();
}



/********************************************************************************
 
Name:     Flash plugin detect
 
*********************************************************************************/
var flashdetect = false;
function fnFlashDetect() {

 if (navigator.plugins && navigator.plugins.length) {
  for (x=0; x < navigator.plugins.length; x++) {
   if (navigator.plugins[x].name.indexOf('Shockwave Flash') != -1) {
     flashdetect = true;      
    break;
   }
  }
 }
 else if (window.ActiveXObject) {
  for (x = 2; x <= 20; x++) {
   try {
    oFlash = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + x + "');");
    if(oFlash) {
     flashdetect = true;  
    }
   }
   catch(e) {}
  }
 }
}
//fnFlashDetect(); // run on file load; 



/******************************************************************************************
Find all <h2> tags with class 0f "flashTxt" and replace content with flash movie with "Myriad Roman" font.
Will not run on Firefox Mac.

NOTES: 	Opera 7 doesn't support wmode transparent, so gets standard text.
		Opera 9 had issues with display:block so display:inline-block was used instead.

******************************************************************************************/
function fnFRT() {
	if(flashdetect && !mac && !op7) {	
		var topHeadings = document.getElementsByTagName("H2");
		var text, swf;
		
		for (i=0;i<topHeadings.length;i++) {
			if (topHeadings[i].className=="flashTxt") {		//look for class "flashTxt"	
				if (document.body.className == "graphicText") {
					text 	= escape(topHeadings[i].innerHTML); // Grab text and encode	
					text = text.replace(/\./g,"%2E");			// Encode dots otherwise if you end with .txt it inserts a text box			
					swf		= fnMakeFlash(assets+"flash/flashTxt.swf?t="+text,532,20);
					topHeadings[i].innerHTML = "<span class=\"alt\">"+topHeadings[i].innerHTML+"</span>"+swf;
					if (op9){						
						topHeadings[i].style.display = "inline-block";
					}
				}		
			}
		}
	}
}






/********************************************************************************

Name: 				make flash
Description:		Return flash code. Fixes Eolas update for IE as well.
@param	swf			String path to swf file
@param	width		Number value of movie width
@param	height		Number value of movie height

*********************************************************************************/

function fnMakeFlash(swf,width,height) {	
	return '<embed src="../qcaAssets/js/'+swf+'" menu="false" width="'+width+'" height="'+height+'" type="application/x-shockwave-flash" wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" />';	
}





/********************************************************************************
 
Name:     Flash embed
Description:  Writeout flash code. Fixes Eolas update for IE as well.
@param swf  String path to swf file
@param width     Number value of movie width
@param height    Number value of movie height
@param alternateContent String of HTML to be served if flash is not installed
 
*********************************************************************************/
 
flash = new Object();
flash.insert = function(swf,width,height) {
	swf = swf.replace(/\./g,"%2E") // encode periods as %2e	
	if(flashdetect) { 	
		flashObject = '<embed src="../qcaAssets/js/'+swf+'" menu="false" quality="high" width="'+width+'px" height="'+height+'px" type="application/x-shockwave-flash" wmode="transparent" salign="T" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
		document.write(flashObject);
	} else {
		//flashObject = alternateContent;
		//fnShowNoFlash();
		alert("alt content");
	} 
}



 
/****************************************************************************************** 
Client Width 
******************************************************************************************/ 

  function clientWidth() {
	var clientWidth
	if (self.innerWidth) {		
		clientWidth = self.innerWidth;	
	} else if (document.documentElement && document.documentElement.clientWidth) {		
		clientWidth = document.documentElement.clientWidth+19;	
	} else if (document.body) {		
		clientWidth = document.body.clientWidth+19;	
	}
	return clientWidth;
}

/* If the broswer is safari the width */
function setThreshold() {
	var threshold	
	if (safari == true) {
		threshold = 1005;
	}
	else if (ns6 == true) {
		threshold = 1014;
	}
	else if (ns7 == true || ns8 == true) {
		threshold = 1015;
	}
	else {
		threshold = 1016;		
	} 	
	return(threshold);
}
		
window.onresize = function() {	
	availablewidth = clientWidth();	
	document.getElementById("wrapper").className = availablewidth < setThreshold()? "small":"large";	
}
window.onload = function() {	
	fnConfig();
	availablewidth = clientWidth();	
	document.getElementById("wrapper").className = availablewidth < setThreshold()? "small":"large";	
}  

 

/********************************************************************************
Graphic Submits

Checks whether class of "graphicText" or "defaultText" is set on the body, then
serves up appropriate inputs of type=image, or type=submit.

Not used for IE7, as there are conflicts when using the zoom function.  IE7 uses
graphic buttons at all text sizes, as the zoom feature allows them to scale.

*********************************************************************************/

 function fnSubmitBut () {
	//alert(ie7);
 
	var topSubmit_img = '<input type="image" src="/qcaAssets/images/goBut_blueBG.gif" name="Submit" value="go" class="submit" />';
	var topSubmit_live = '<input type="submit" name="Submit" value="go" class="submit" />';
	
	var homeSubmit_img = '<input type="image" src="/qcaAssets/images/goBut_blueBG.gif" name="Submit" value="go" class="submit" />';
	var homeSubmit_live = '<input type="submit" name="Submit" value="go" class="submit" />';
	
	var submitGo_img = '<input type="image" src="/qcaAssets/images/submitGo.gif" name="Submit" value="go" class="submit" />';
	var submitGo_live = '<input type="submit" name="Submit" value="go" class="submit" />';
	
	function fnGraphicText() { /* use graphic inputs */
		document.getElementById("topSubmit").innerHTML = topSubmit_img;
		if (document.getElementById("homeSubmit")) {
			document.getElementById("homeSubmit").innerHTML = homeSubmit_img;
		}	
		if (document.getElementById("searchSort")) {
			document.getElementById("searchSubmit1").innerHTML = submitGo_img;
			//document.getElementById("searchSubmit2").innerHTML = submitGo_img;
		}
	}
	function fnDefaultText() { /* use standard inputs */
		document.getElementById("topSubmit").innerHTML = topSubmit_live;
		if (document.getElementById("homeSubmit")) {
			document.getElementById("homeSubmit").innerHTML = homeSubmit_live;
		}
		if (document.getElementById("searchSort")) {
			document.getElementById("searchSubmit1").innerHTML = submitGo_live;
			//document.getElementById("searchSubmit2").innerHTML = submitGo_live;
		}
	}
	
	if(!ie7) {
		if (document.body.className == "graphicText") {
			fnGraphicText();
		}	
		else if (document.body.className == "defaultText") {
			fnDefaultText();
		}
	} else if(ie7) {
		fnGraphicText();
	}
		
}





/***********************************************************************************
Force Focus to move past iFrame

onFocus() event has minor browser compatability issues. Not used for Mozilla Browser.

Selects the moveTo ID and jumps to the first link within 

@param: moveTo - must be an ID within the page

************************************************************************************/

function fnMoveFocus(moveToID) {
	if(iepc) {
		document.getElementById(moveToID).getElementsByTagName("A")[0].focus();
	}
}

// Collection of all .definitionBox elements (or null). Initialised in fnProcessDefinitions.
var oAllDefinitionBoxes = null;

/***********************************************************************************
Initialise definition boxes on the page. Called on page load.

It finds all jump link definitions (<a name="info"> tags) with a parent with class
definitionBox, and sets the ID of the definitionBox to the same as the name tag of the href.
************************************************************************************/
function fnProcessDefinitions () {
	var aTags = document.getElementsByTagName("a");
	
	for (var i = 0; i < aTags.length; i++) {

		if (aTags[i].className == "definitionLink") {
			// For definition links, add a title to explain what they do. Unless CMS editor has specified their own title.
			if (aTags[i].title.length == 0) {
				aTags[i].title = "Show or hide explanatory note";
			}
		}
		
		if (aTags[i].href.length == 0 && aTags[i].name.length > 0) {
			// It has no href but does have a name, so its a jump link. These must be placed in the definition box to a) define the ID of the box and b) to jump down to the box for no-script.
			
			var oDefBox = aTags[i];
			
			// Find the .definitionBox element (somewhere above the named href in the DOM)
			while (oDefBox.parentNode != null) {
				oDefBox = oDefBox.parentNode;
				
				if (oDefBox.className == "definitionBox") {
					// Set the ID of the .definitionBox to the same as the named href so can find it using getElementById
					oDefBox.id = aTags[i].name;
					
					// Add this definition box to the array of all def boxes (used to show/hide all)
					if (oAllDefinitionBoxes == null) {
						oAllDefinitionBoxes = new Array();
					}
					oAllDefinitionBoxes[oAllDefinitionBoxes.length] = oDefBox; // IE 5.01 (and other old browsers) don't support .push()
					
					// Insert HTML for the close button.
					var sCloseHTML = "<a class=\"definitionBoxCross\" title=\"Hide this explanatory note\" href=\"#\" onclick=\"fnHideDefinition('" + oDefBox.id + "'); return false;\">Close</a>";
					oDefBox.innerHTML = sCloseHTML + oDefBox.innerHTML;
					
					i++;		// Because we created an <a> tag just above, need to skip it in the automatically updating aTags collection.
					
					break;	// jump out of while loop - do not process more parent nodes
				}
			}
		}
	}
}

// Which definition box to close the next time one is opened? (object reference)
var oDefinitionToClose = null;

/***********************************************************************************
Show or hide a definition box given the link (<a href="#info"> object)

It will find the element with the ID same as the # and show/hide it based on whether
it was already showing. Keeps track of which element is showing using oDefinitionToClose.
************************************************************************************/
function fnShowHideDefinition (oHref) {
	var iHashPos = oHref.href.indexOf("#");
	
	if (iHashPos > 0) {
		var sJumpName = oHref.href.substring(iHashPos + 1);
		
		if (sJumpName.length == 0) {								// This jump link is empty (<a href="#">) so finish and run normally.
			return true;
		}
		var oDefBox = document.getElementById(sJumpName);
		if (oDefBox == null) {										// Could not find the definition box - follow the href normally (could be wrongly applied Definition Link attribute to a normal jump link)
			return true;
		}
		
		if (oDefBox.className == "definitionBoxOpen") {				// Close the definition box as its already open.
			oDefBox.className = "definitionBox";
			oDefinitionToClose = null;
		}
		else {														// Show the (closed) definition box.
			oDefBox.className = "definitionBoxOpen";

			if (oDefinitionToClose != null) {						// Hide a currently open definition box. Comment out next line to stop auto-closing of boxes.
				oDefinitionToClose.className = "definitionBox";
			}
			oDefinitionToClose = oDefBox;
		}
		
		if (safari && mac) {
			oDefBox.innerHTML = oDefBox.innerHTML + " ";		// Safari doesnt always redraw the screen unless you force it to.
		}
	}
	else {
		// There was no # in the href - the Definition Link attribute may have been wrongly applied.
		// Follow the hyperlink.
		return true;
	}
		
	return false; // Nothing went wrong if you get this far - do not follow href.
}


/***********************************************************************************
Hide the currently showing definition box (used on Close link)
************************************************************************************/
function fnHideDefinition (sId) {
	if (document.getElementById(sId)) {
		if (document.getElementById(sId).className == "definitionBoxOpen") {
			document.getElementById(sId).className = "definitionBox";
			oDefinitionToClose = null;
		}
	}
}

// This is set to true when the "Show all explanatory notes" is clicked.
var bAllDefinitionsShowing = false;

/***********************************************************************************
Show or Hide all definition boxes. Swaps the text on the link too.
************************************************************************************/
function fnToggleAllDefinitions (oLink) {
	if (oAllDefinitionBoxes != null) {
		for (i = 0; i < oAllDefinitionBoxes.length; i++) {
			oAllDefinitionBoxes[i].className = bAllDefinitionsShowing ? "definitionBox" : "definitionBoxOpen";
		}
		oDefinitionToClose = null;
		oLink.innerHTML = bAllDefinitionsShowing ? "Show all explanatory notes" : "Hide all explanatory notes";
		bAllDefinitionsShowing = !bAllDefinitionsShowing;
	}
}
