function convertRates(currency) {
	// create the XMLHTTPRequest object
	http_request = false;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	http_request.open('GET', "currency.xml" , true);
	http_request.onreadystatechange = function() {
		if (http_request.readyState == 4) {
			var xmlDoc = http_request.responseXML;
			var currencies = xmlDoc.documentElement.getElementsByTagName("currency");
			var spans = document.getElementsByTagName("span");	// find all span tags on the page
			for (var i=0;i<spans.length;i++) { // loop through them
				if (spans[i].className == "currency") { // if the span contains a rate
					amount = rates[i]; // get that rate in GBP from the rates array
					for (var j=0;j<currencies.length;j++) { // loop through all currencies
						c_name = currencies[j].getAttribute("name"); // get the currency name
						if (c_name == currency) { // if the currency is the same as the one selected
							var exchange_rate = currencies[j].getAttribute("rate"); // get the exchange rate
							var new_value = formatCurrency(amount * exchange_rate); // calculate the new rate
							// add the correct currency symbol
							switch (currency) {
								case "GBP":
									new_value = "£"+rates[i];
									break;
								case "USD":
									new_value = "$"+new_value+" US";
									break;
								case "EUR":
									new_value = "€"+new_value;
									break;
								case "CAD":
									new_value = "$"+new_value+" CAN";
									break;
								case "AUD":
									new_value = "$"+new_value+" AUS";
									break;
								case "NZD":
									new_value = "$"+new_value+" NZ";
									break;
							}
							spans[i].firstChild.nodeValue = new_value; // update the rate with the new converted rate
						}
					}
				}
			}
		}
	}
	http_request.send(null);
	return true;
}

function formatCurrency(rate) {
	var rlength = 2; // The number of decimal places to round to
	if (rate > 8191 && rate < 10485) {
		rate = rate-5000;
		var newrate = Math.round(rate*Math.pow(10,rlength))/Math.pow(10,rlength);
	} else {
		var newrate = Math.round(rate*Math.pow(10,rlength))/Math.pow(10,rlength);
	}
	newrate = newrate + ""; // conver the number to a string
	if (newrate.indexOf(".") != -1) { // if there is a decimal place in the rate
		if (newrate.split(".")[1].length == 1) { // if there is only 1 digit after the decimal place
			newrate = newrate + 0 // add an extra 0 to the end of it
		}
	}
	return newrate;
}

// store all original rates in GBP in an array to make converting between currencies easier
var rates = new Array();
function prepareRates() {
	var spans = document.getElementsByTagName("span");
	for (var i=0;i<spans.length;i++) {
		if (spans[i].className == "currency") {
			var amount = spans[i].firstChild.nodeValue;
			rates[i] = amount.split("£")[1].replace(",","");
		}
	}
	if (getCookie("currencyrate")!=null) {
		convertRates(getCookie("currencyrate"))
	}
}

function switchCurrency(clicked) {
	// find the active currency link
	var active = document.getElementById("activecurrency");
	var active_c = active.getElementsByTagName("a")[0];
	// store the class name and img of the active and clicked currencies
	active_class = active_c.className;
	clicked_class = clicked.className;
	active_image = active_c.innerHTML;
	clicked_image = clicked.innerHTML;
	active_title = active_c.getAttribute("title");
	clicked_title = clicked.getAttribute("title");
	// swap the active and clicked currencies class and image
	active_c.className = clicked_class;
	clicked.className = active_class;
	active_c.innerHTML = clicked_image;
	clicked.innerHTML = active_image;
	active_c.setAttribute("title",clicked_title);
	clicked.setAttribute("title",active_title);
	// change the active currency message
	var cmsg = document.getElementById("currencymsg");
	cmsg.lastChild.nodeValue = "All rates are shown in "+clicked_title
}

function createLink(currency) {
	var new_element = currency.cloneNode(true);
	return new_element
}


// TOOL TIP FUNCTIONS

function tip(msg,myEvent){ 
	scrollposY=0;
	if (window.pageYOffset){
		scrollposY = window.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop){
		scrollposY = document.documentElement.scrollTop
	}
	else if (document.body.scrollTop){
		scrollposY = document.body.scrollTop;
	}

	document.getElementById("message").innerHTML=msg;
	document.getElementById("tip").style.top = scrollposY + myEvent.clientY + 15+"px";
	document.getElementById("tip").style.left = myEvent.clientX - 25+"px"; 
	document.getElementById("tip").style.zIndex=9;
	document.getElementById("tip").style.visibility="visible";
}

function hide(){
	document.getElementById("tip").style.visibility="hidden";
}

function addConverter() {
	if (testBrowser()) {
		document.writeln("<tr>");
		document.writeln("<td class='filler' height='40'>&nbsp;</td>");
		document.writeln("</tr>");
		document.writeln("<tr>");
		document.writeln("<td class='fulllist2'><b><font color='#093274'>&nbsp;Currency Converter</font></b></td>");
		document.writeln("</tr>");
		document.writeln("<tr>");
		document.writeln("<td class='leftnavother' id='converter' style='font-size:10px'>");
		document.writeln("<div style='font-size:10px;position:relative;margin-bottom:15px' id='activecurrency'>");
		switch (getCookie("currencyrate")) {
			case "GBP":
				document.writeln("<a href='#' onmousemove=\"tip('Rates are shown in '+this.getAttribute('title'),event)\" onmouseout=\"hide();\" onclick='currencyConverter(this.className);return false;' class='GBP' title='British Pounds'><img src='images/flag-gbp.gif' /></a>");
				document.writeln("<span style='position:absolute;left:28px' class='currencymsg' id='currencymsg'>All rates are shown in British Pounds</span>");
				break;
			case "USD":
				document.writeln("<a href='#' onmousemove=\"tip('Rates are shown in '+this.getAttribute('title'),event)\" onmouseout=\"hide();\" onclick='currencyConverter(this.className);return false;' class='USD' title='US Dollars'><img src='images/flag-usd.gif' /></a>");
				document.writeln("<span style='position:absolute;left:28px' class='currencymsg' id='currencymsg'>All rates are shown in US Dollars</span>");
				break;
			case "EUR":
				document.writeln("<a href='#' onmousemove=\"tip('Rates are shown in '+this.getAttribute('title'),event)\" onmouseout=\"hide();\" onclick='currencyConverter(this.className);return false;' class='EUR' title='Euros'><img src='images/flag-euro.gif' /></a>");
				document.writeln("<span style='position:absolute;left:28px' class='currencymsg' id='currencymsg'>All rates are shown in Euros</span>");
				break;
			case "CAD":
				document.writeln("<a href='#' onmousemove=\"tip('Rates are shown in '+this.getAttribute('title'),event)\" onmouseout=\"hide();\" onclick='currencyConverter(this.className);return false;' class='CAD' title='Canadian Dollars'><img src='images/flag-can.gif' /></a>");
				document.writeln("<span style='position:absolute;left:28px' class='currencymsg' id='currencymsg'>All rates are shown in Canadian Dollars</span>");
				break;
			case "AUD":
				document.writeln("<a href='#' onmousemove=\"tip('Rates are shown in '+this.getAttribute('title'),event)\" onmouseout=\"hide();\" onclick='currencyConverter(this.className);return false;' class='AUD' title='Australian Dollars'><img src='images/flag-aus.gif' /></a>");
				document.writeln("<span style='position:absolute;left:28px' class='currencymsg' id='currencymsg'>All rates are shown in Australian Dollars</span>");
				break;
			case "NZD":
				document.writeln("<a href='#' onmousemove=\"tip('Rates are shown in '+this.getAttribute('title'),event)\" onmouseout=\"hide();\" onclick='currencyConverter(this.className);return false;' class='NZD' title='New Zealand Dollars'><img src='images/flag-nzd.gif' /></a>");
				document.writeln("<span style='position:absolute;left:28px' class='currencymsg' id='currencymsg'>All rates are shown in New Zealand Dollars</span>");
				break;
			default:
				document.writeln("<a href='#' onmousemove=\"tip('Rates are shown in '+this.getAttribute('title'),event)\" onmouseout=\"hide();\" onclick='currencyConverter(this.className);return false;' class='GBP' title='British Pounds'><img src='images/flag-gbp.gif' /></a>");
				document.writeln("<span style='position:absolute;left:28px' class='currencymsg' id='currencymsg'>All rates are shown in British Pounds</span>");
				break;
		}
		document.writeln("</div>");
		document.writeln("<div style='margin-left:-2px' id='inactivecurrencies'>");
		if ((getCookie("currencyrate") != "GBP") && (getCookie("currencyrate") != null)) {
			document.writeln("<a href='#' onmousemove=\"tip('Show rates in '+this.getAttribute('title'),event)\" onmouseout=\"hide();\" onclick='currencyConverter(this.className);switchCurrency(this);return false;' class='GBP' title='British Pounds'><img src='images/flag-gbp.gif' /></a>");
		}
		if ((getCookie("currencyrate") != "USD") || (getCookie("currencyrate") == null)) {
			document.writeln("<a href='#' onmousemove=\"tip('Show rates in '+this.getAttribute('title'),event)\" onmouseout=\"hide();\" onclick='currencyConverter(this.className);switchCurrency(this);return false;' class='USD' title='US Dollars'><img src='images/flag-usd.gif' /></a>");
		}
		if ((getCookie("currencyrate") != "EUR") || (getCookie("currencyrate") == null)) {
			document.writeln("<a href='#' onmousemove=\"tip('Show rates in '+this.getAttribute('title'),event)\" onmouseout=\"hide();\" onclick='currencyConverter(this.className);switchCurrency(this);return false;' class='EUR' title='Euros'><img src='images/flag-euro.gif' /></a>");
		}
		if ((getCookie("currencyrate") != "CAD") || (getCookie("currencyrate") == null)) {
			document.writeln("<a href='#' onmousemove=\"tip('Show rates in '+this.getAttribute('title'),event)\" onmouseout=\"hide();\" onclick='currencyConverter(this.className);switchCurrency(this);return false;' class='CAD' title='Canadian Dollars'><img src='images/flag-can.gif' /></a>");
		}
		if ((getCookie("currencyrate") != "AUD") || (getCookie("currencyrate") == null)) {
			document.writeln("<a href='#' onmousemove=\"tip('Show rates in '+this.getAttribute('title'),event)\" onmouseout=\"hide();\" onclick='currencyConverter(this.className);switchCurrency(this);return false;' class='AUD' title='Australian Dollars'><img src='images/flag-aus.gif' /></a>");
		}
		if ((getCookie("currencyrate") != "NZD") || (getCookie("currencyrate") == null)) {
			document.writeln("<a href='#' onmousemove=\"tip('Show rates in '+this.getAttribute('title'),event)\" onmouseout=\"hide();\" onclick='currencyConverter(this.className);switchCurrency(this);return false;' class='NZD' title='New Zealand Dollars'><img src='images/flag-nzd.gif' /></a>");
		}
		document.writeln("</div>");
		document.writeln("Converted rates are for guidance purposes only and are approximate. The final rate you will pay is that displayed in GBP.");
		document.writeln("<div id='tip' style='position:absolute;top:250px;left:250px;visibility:hidden;color:#000000;z-index:60;'><table border='0'><tr><td style='background-color:white;color:black;border:1px solid black;font-size:10px;white-space:nowrap' id='message'><br/></td></tr></table></div>");
		document.writeln("</td>");
		document.writeln("</tr>");
	}
}

function currencyConverter(currency) {
	document.cookie = "currencyrate="+escape(currency);
	convertRates(currency);
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return null
}

// test that the broswer can handle all objects in the script
function testBrowser() {
	if ((window.XMLHttpRequest || window.ActiveXObject) && document.getElementsByTagName && document.getElementById) {
		return true;
	}
	else {
		return false;
	}
}

// function to queue up converter functions in onload so as not to interfere with other onload events
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

if (testBrowser()) {
	addLoadEvent(prepareRates);
}