//Sets the stylesheet

function setActiveStyleSheet(sheet) {

	//Added 08/06/2009
	//JT
	//Apply the style appropriate to the browser
  
	if (isIE(6))
  
  	{
  		//window.alert("IE 6 " + sheet);
  		
  		//Code if title no colour
  		if (sheet == "noColour") {
	  		
  			//window.alert("noColour");
			//removejscssfile("CSS_IE6.css", "css");
  			document.getElementById("defaultStyle").href = "/_Stylesheets/css/CSS_IE6NoColour.css";
  			
		} else {
	  		
	  		//window.alert("defaultStyle");
	  		document.getElementById("defaultStyle").href = "/_Stylesheets/css/CSS_IE6.css";
	  		
  		}
  		
	}
  
	else if (isIE(7))
  
  	{
  		//window.alert("IE 7 " + sheet);

  		//Code if title no colour		
  		 if (sheet == "noColour") {
	  		
  			//window.alert("noColour");
			//removejscssfile("CSS_IE7.css", "css");
  			document.getElementById("defaultStyle").href = "/_Stylesheets/css/CSS_IE7NoColour.css";
  			
  		} else {
	  		
	  		//window.alert("defaultStyle");
	  		document.getElementById("defaultStyle").href = "/_Stylesheets/css/CSS_IE7.css";
	  		
  		}

  		
	}
  
	else //For all other browsers
  
  	{
  		
  		//window.alert("Not IE " + sheet);

		//Code if title no colour/default
  		if (sheet == "noColour") {
	  		
  			//window.alert("Not IE noColour");
  			document.getElementById("defaultStyle").href = "/_stylesheets/css/CSS_NoColour.css";
  
  		} else {
	  		
	  		//window.alert("Not IE default");
	  		document.getElementById("defaultStyle").href = "/_Stylesheets/css/CSS_FF.css";
	  		
  		}
	
	}
	
	//END OF ADDED SECTION
  	
}

//Gets the href for the active default stylesheet

function getActiveStyleSheet() {

	//window.alert("getActiveStyleSheet");

	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {

		//window.alert(a.getAttribute("id"));

		if(a.getAttribute("rel").indexOf("style") != -1 
		&& a.getAttribute("id") == "defaultStyle" 
		&& !a.disabled) return a.getAttribute("href");

	}
	return null;
}

function getPreferredStyleSheet() {

	//window.alert("getPreferredStyleSheet")

	var i, a;
	for(i=0; (a = document.getElementsByTagName("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;
}

//Creates the cookie to remember which stylesheet to use

function createCookie(name,value,days) {

	//window.alert("createCookie name,value,days " + name + " " + value + " " + days)

	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
    		var expires = "; expires="+date.toGMTString();
  	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

//Reads the cookie to set the correct stylesheet

function readCookie(name) {

	//window.alert("readCookie name " + name)

	var nameEQ = 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(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

//To load the correct stylesheet on page load

window.onload = function(e) {

	//window.alert("window onload function e")

	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
}

//To set the cookie on page unload

window.onunload = function(e) {

	//window.alert("window onunload function e")

	var href = getActiveStyleSheet();

	//window.alert(href);

	if (href.indexOf("NoColour") != -1) {
		var sheet = "noColour"
  	}
	else sheet = "colour";

	//window.alert(sheet)

	//createCookie("style", title, 365);
	createCookie("style", sheet, 365);

}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);


//Added 08/06/2009
//JT
function isIE(versionNumber) {

	var detect = navigator.userAgent.toLowerCase();
	
	if(!(navigator && navigator.userAgent && navigator.userAgent.toLowerCase)) {
		
		//window.alert("False");
  		return false;
  	    
  	} else {
  	        
		var valid = false;
  		
		if(detect.indexOf('msie') + 1) {
  	    	// browser is internet explorer

	  		// Returns the version of Internet Explorer or a -1
	  		// (indicating the use of another browser).
	  		var rv = -1; // Return value assumes failure

	  		if (navigator.appName == 'Microsoft Internet Explorer') {
		  	        
	  			var ua = navigator.userAgent;
	  			var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
	  	            
	  		            	if (re.exec(ua) != null) {
					rv = parseFloat( RegExp.$1 );
					
					if (rv == versionNumber) {	  	        
						valid = true;
					}
	  	 	           	}
	  		}

		}

		return valid;
	}
}

//END OF function isIE(versionNumber)
