// JavaScript Document

// INITIATES PRICE REQUEST
function js_PricingRequest() {
	// Testing whether prcing xml has already been loaded
	if(typeof(window["XML_PRICING"])!="undefined") {
		// If yes, displaying price
		js_PricingGet();
	} else {
		// If, not loading pricing xml
		js_PricingLoad();
	}
}

// DISPLAYS PRICING VALUE ON PAGE
function js_PricingDisplay(from_price, our_price) {
	// Displaying price
	document.getElementById("text_from_price").innerText=String(from_price)+"p";
	document.getElementById("text_our_price").innerText=String(our_price)+"p";
}

// DISPLAYS THAT PRICING IS UNKNOWN
function js_PricingUnknown() {
	// Displaying price
	document.getElementById("text_price").innerText="Unknown";
}

// PARSES XML AND DISPLAYS PRICE FOR SELECTED COUNTRIES
function js_PricingGet() {
	// Passing pricing xml to local scope
	var xml_pricing=XML_PRICING;
	// Grabbing to and from countries
	var country_from=document.getElementById("country_from").value;
	var country_to=document.getElementById("country_to").value;
	
	// Looping through source countries
	for(var i=0; i<xml_pricing.getElementsByTagName("source").length; i++) {
		// Grabbing source xml and id
		var xml_source=xml_pricing.getElementsByTagName("source")[i];
		var source_id=xml_source.getAttribute("id");
		
		// Testing for source match
		if(source_id==country_from) {
			// Looping through destination countries
			for(var j=0; j<xml_source.getElementsByTagName("destination").length; j++) {
				// Grabbing destination xml and id
				var xml_destination=xml_source.getElementsByTagName("destination")[j];
				var destination_id=xml_destination.getAttribute("id");
				
				// Testing for destination match
				if(destination_id==country_to) {
					// Grabbing from price and our price
					var from_price=xml_destination.getElementsByTagName("from_price")[0].firstChild.nodeValue;
					var our_price=xml_destination.getElementsByTagName("our_price")[0].firstChild.nodeValue;
					// Displaying price
					js_PricingDisplay(from_price, our_price);
					return;
				}
			}
			
			break;
		}
		
		// Cleaning up
		xml_source=null;
	}
	
	// Cleaning up
	xml_pricing=null;
	
	// If price not found
	js_PricingUnknown();
}

// LOADS PRICING XML DOCUMENT INTO GLOBAL SCOPE
function js_PricingLoad() {
	// Non-IE browsers
	if(document.implementation && document.implementation.createDocument) {
		// Creating xml document object
		XML_PRICING=document.implementation.createDocument("", "", null);
		// Setting function to exec on load
		XML_PRICING.onload=js_PricingGet;
		
	// Internet explorer
	} else if(window.ActiveXObject) {
		// Creating xml document object
		XML_PRICING=new ActiveXObject("Microsoft.XMLDOM");
		
		// Setting function to exec on load
		XML_PRICING.onreadystatechange=function() {
			if(XML_PRICING.readyState==4) {
				js_PricingGet();
			}
		};		
			
	// Not supported
	} else {
		return;
	}
	
	// Loading xml document
	XML_PRICING.load("pricing.xml");
}