var country_prices_arr= new Array();
var country_arr = new Array();

//set prices object
function prices(saver_lan, saver_mob, freedom_lan, freedom_mob, stand_day_lan, stand_day_mob, stand_even_lan, stand_even_mob)
{
	this.saver_lan			= saver_lan;
	this.saver_mob			= saver_mob;
	this.freedom_lan		= freedom_lan;
	this.freedom_mob		= freedom_mob;
	this.stand_day_lan		= stand_day_lan;
	this.stand_day_mob		= stand_day_mob;
	this.stand_even_lan		= stand_even_lan;
	this.stand_even_mob		= stand_even_mob;	
}

// load the calculator xml and then parse it
function loadXML()
{
	var callingPlan = document.getElementById('countrySelection');
	
	if( callingPlan == null){
		return;
	}
	
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = parseXML;
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange=function()
		{
			if (xmlDoc.readyState == 4) parseXML();
		};
 	}
	else
	{
		alert('Your browser can\'t handle the calculator script');
		return;
	}
	xmlDoc.load("../consumer/consumerProducts/js/home_phone_services/int_calculator.xml");
	
}

// parse/sort xml file - create object array and populate dropdown
function parseXML()
{
	var x = xmlDoc.getElementsByTagName('country');
	
	for (i=0;i<x.length;i++)
	{
		var country = x[i].getAttribute('id');
		country_arr.push(country);
		
		var country_str = x[i].getAttribute('text');
		addOption(country_str,i);
		
		var temp_cost = new Array();
		for (j=0; j<x[i].childNodes.length; j++)
		{
			if (x[i].childNodes[j].nodeType != 1) continue;
			if(x[i].childNodes[j].firstChild.nodeValue != "null")
			{
				temp_cost.push("'" + x[i].childNodes[j].firstChild.nodeValue + "p/min'");
			}
			else
			{
				temp_cost.push("'&ndash;'");
			}
		}
		eval("var " + country + "_object = new prices(" + temp_cost.toString() + ")");

		var temp_country = eval(country + "_object");
		country_prices_arr.push(temp_country);	
	}
	// Initial load set to France	
	displayPrices(73);	
}

// populate dropdown list
function addOption(_country,_value)
{	
	var element_option = document.createElement('option');
    element_option.text = _country;
    element_option.value = _value;
	
	var element_selection = document.getElementById('countrySelection');

	try
	{
		element_selection.add(element_option, null);	//standard compliant
	}
	catch(ex)
	{
		element_selection.add(element_option);			//ie
	}
	
	// Initial load set to France
	element_selection.value = 73;
	
	//add function to onchange events
	element_selection.onchange = function() {
		displayPrices(this.value);
	}
}

// display prices based on value of dropdown
function displayPrices(_i)
{		
	// Set the values
	document.getElementById("saverLan").innerHTML = country_prices_arr[_i].saver_lan;
	document.getElementById("saverMob").innerHTML = country_prices_arr[_i].saver_mob;
	document.getElementById("freedomLan").innerHTML = country_prices_arr[_i].freedom_lan;
	document.getElementById("freedomMob").innerHTML = country_prices_arr[_i].freedom_mob;
	document.getElementById("standDayLan").innerHTML = country_prices_arr[_i].stand_day_lan;
	document.getElementById("standDayMob").innerHTML = country_prices_arr[_i].stand_day_mob;
	document.getElementById("standEvenLan").innerHTML = country_prices_arr[_i].stand_even_lan;
	document.getElementById("standEvenMob").innerHTML = country_prices_arr[_i].stand_even_mob;
}

