// JavaScript used by the help.html, helpfx.html and help_both.html pages. Note
// that is should support both IE and Firefox, despite their implementations of
// XML handling being different

var xmlDoc;

// Loads and caches the XML document
function intialiseXml() {

	if (isIe()) {
		xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
	} else {
		xmlDoc = document.implementation.createDocument("", "", null);
	}
	
	xmlDoc.async = false;
	xmlDoc.load("help_contactdetails.xml");

}

// Are we running in IE?
function isIe() {

	return window.ActiveXObject;

}

// Populates the country drop down, depending on the displayNonFx and displayFx
// variables
function populateCountryDropDown() {

	var path;
	if (displayNonFx && displayFx) {
		path = "/countries/country/@name";
	} else if (displayNonFx) {
		path = "/countries/country[non-fx]/@name";
	} else if (displayFx) {
		path = "/countries/country[fx]/@name";
	}
	
	var countries = document.getElementById("countries");
	
	if (isIe()) {
	
		var nodes = xmlDoc.selectNodes(path)

		for (var i = 0; i < nodes.length; i++) {
			var option = document.createElement("option");
			option.value = nodes[i].childNodes[0].text;
			option.text = option.value;
			countries.add(option);
		}

	} else {
	
		var xpe = new XPathEvaluator();
		var nodes = xpe.evaluate(path, xmlDoc, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null );

		var node = nodes.iterateNext();
		while (node) {
			var option = document.createElement("option");
			option.value = node.value;
			option.text = option.value;
			countries.options.add(option);
  			node = nodes.iterateNext();
		}
	
	}

}

// Provides a single implementation to return a single node for a given path. Works for both IE
// and Firefox
function SelectSingleNode(xmlDoc, elementPath)
{
    if(isIe()) {
		return xmlDoc.selectSingleNode(elementPath);
    } else {
        var xpe = new XPathEvaluator();
        var nsResolver = xpe.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
        var results = xpe.evaluate(elementPath,xmlDoc,nsResolver,XPathResult.FIRST_ORDERED_NODE_TYPE, null);
        return results.singleNodeValue; 
    }
    
}

// Displays the contract details for a specific country
function displayDetails(country) {

	// Special case if user selects the "Select country..." or divider options
	if (country == "" || country == "-------------------------") {

		document.getElementById("details").style.display = "none";

	} else {

		var node = SelectSingleNode(xmlDoc, "/countries/country[@name='" + country + "']")

		var nonFx = document.getElementById("non-fx");
		var fx = document.getElementById("fx");

		nonFx.style.display = "none";
		fx.style.display = "none";

		if (displayNonFx) {
			var nonFxNode = SelectSingleNode(node, "non-fx");
			if (nonFxNode != null) {
				updateDetails("non-fx", nonFxNode);
				nonFx.style.display = "block";
			}
		}

		if (displayFx) {
			var fxNode = SelectSingleNode(node, "fx");
			if (fxNode != null) {
				updateDetails("fx", fxNode);
				fx.style.display = "block";
			}
		}

		document.getElementById("details").style.display = "block";

	}

}

// Updates the HTML elements for a particular contact type (non-fx or fx)
function updateDetails(prefix, node) {

	var tollFree = document.getElementById(prefix + "-toll-free");
	var phone = document.getElementById(prefix + "-phone");
	var emailLink = document.getElementById(prefix + "-email-link");
	var email = document.getElementById(prefix + "-email");

	var tollFreeNode = SelectSingleNode(node, "toll-free");
	var phoneNode = SelectSingleNode(node, "phone");
	var emailNode = SelectSingleNode(node, "email");
	
	if (isIe()) {
		tollFree.innerHTML = tollFreeNode.text;
		phone.innerHTML = phoneNode.text;
		emailLink.href = "mailto:" + emailNode.text;
		email.innerHTML = emailNode.text;
	} else {
		tollFree.innerHTML = tollFreeNode.textContent;
		phone.innerHTML = phoneNode.textContent;
		emailLink.href = "mailto:" + emailNode.textContent;
		email.innerHTML = emailNode.textContent;
	}

}