
/*
	STORE ALL RATES IN ORIGINAL CURRENCY IN AN ARRAY TO MAKE
	CONVERTING EASIER
*/
var rates = new Array();
function prepareRates(noswap) {
	var spans = document.getElementsByTagName("span");
	for (var i=0;i<spans.length;i++) {
		if (spans[i].className == "currency") {
			var rate = spans[i].firstChild.nodeValue;
			// strip currency symbol
			var pattern = /\d+\.\d+/;
			rate = pattern.exec(rate);
			// store rate in array
			rates[i] = rate[0];
		}
	}
	
	if (getCookie("currencyrate") != "") {
		var convert_to = getCookie("currencyrate");
		convertRates(convert_to);
		if (noswap !== true) {
			swapPosition(document.getElementById(convert_to));
		}
	}
}

addLoadEvent(prepareRates);

/*
	COVERT ALL RATES
*/
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) {}
		}
	}
	
	if (http_request) {
		http_request.open('GET', "/xml/"+apt_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 the original currency 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 symbol = currencies[j].getAttribute("entity");
								var new_rate = symbol+formatCurrency(amount * exchange_rate); // calculate the new rate
								if (currencies[j].getAttribute("entity") != "£") {
									new_rate += " "+currency;
								}
								spans[i].firstChild.nodeValue = new_rate; // update the rate with the new converted rate
							}
						}
					}
				}
			}
		}
		
		http_request.send(null);
		
	}
	
	// SET A COOKIE TO REMEMBER THE LAST SELECTED CURRENCY
	// DELETE IT IF THE SELECTED CURRENCY IS THE CURRENT APTS CURRENCY
	if (currency == apt_currency) {
		deleteCookie("currencyrate");
	}
	else {
		setCookie("currencyrate",currency);
	}

} // convertrates

function swapPosition(clicked) {
	// find the active currency link
	var active = document.getElementById("selectedcurrency");
	if (active) {
		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 = clicked_title
	}
}

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
		}
	}
	else {
		newrate+=".00";
	}
	return newrate;
}