
// We use a document ready jquery function.
jQuery(document).ready(function(){

	$('#select_country').change(function(){
		country = $(this).val();
		if(country == '') {
			document.getElementById('location').innerHTML = '';
		} else {
			pos = country.indexOf('|');
			countryCode = country.substr(0, pos);
			countryName = country.substr(pos + 1);
			document.getElementById('location').innerHTML = '<img src="/images/flags/' + countryCode + '.png" width="16" border="0"/> ' /*+ countryName*/;
			
			$.ajax({
			    type: "GET",
			    url: "pricingdata.php?country=" + countryCode,
			    dataType: "xml",
			    success: parseXml
			  });
		
		}
	});
	
	country = document.getElementById('select_country').value;
	
	pos = country.indexOf('|');
	countryCode = country.substr(0, pos);

	$.ajax({
	    type: "GET",
	    url: "pricingdata.php?country=" + countryCode,
	    dataType: "xml",
	    success: parseXml
	  });
	
});

function parseXml(xml)
{
  tableContent = '<tr><th>Destination</th><th>Rate</th></tr>';
  i = 0;	
  $(xml).find("destination-text").each(function()
  {
	  tableContent += '<tr ' + (i++%2!=0 ? 'class="even"' : '') + '><td class="destination">' + $(this).attr("name") + '</td><td class="rate">' + $(this).attr("rate") + '</td></tr>';
  });
  
  if(i == 0) {
	  tableContent += '<tr height="25"><td class="destination">&nbsp;</td><td class="rate">&nbsp;</td></tr>';
  }
  
  document.getElementById('pricing_table_text').innerHTML = '<table id="pricing_table_text" class="pricing_table">' + tableContent + '</table>' + '<p id="pricingComment">Pricing is subject to change at any time, and excludes VAT.</p>';
  
  tableContent = '<tr><th>Destination</th><th>Rate</th></tr>';
  i = 0;	
  $(xml).find("destination-voice").each(function()
  {
	  tableContent += '<tr ' + (i++%2!=0 ? 'class="even"' : '') + '><td class="destination">' + $(this).attr("name") + '</td><td class="rate">' + $(this).attr("rate") + '</td></tr>';
  });
  
  if(i == 0) {
	  tableContent += '<tr height="25"><td class="destination"></td><td class="rate"></td></tr>';
  }
  
  document.getElementById('pricing_table_voice').innerHTML = '<table id="pricing_table_voice" class="pricing_table">' + tableContent + '</table>' + '<p id="pricingComment">Pricing is subject to change at any time, and excludes VAT.</p>';
  
  $(xml).find("media-voice-local").each(function()
  {
	  document.getElementById('pricing-local').innerHTML = pricingBlock($(this).attr("active"), $(this).attr("month"), $(this).attr("unit"), $(this).attr("setup"), 'Local', 'minute');
  });
  
  $(xml).find("media-voice-tollfree").each(function()
  {
	  document.getElementById('pricing-tollfree').innerHTML = pricingBlock($(this).attr("active"), $(this).attr("month"), $(this).attr("unit"), $(this).attr("setup"), 'Toll Free', 'minute');
	  
  });
  
  $(xml).find("media-text").each(function()
  {
	  document.getElementById('pricing-text').innerHTML = pricingBlock($(this).attr("active"), $(this).attr("month"), $(this).attr("unit"), $(this).attr("setup"), 'SMS Texting', 'message');
  });

}

function pricingBlock(active, pricePerMonth, pricePerUnit, setupPrice, header, unitTitle) {
	
	rez  = '<h3>' + header + '</h3>'
	if(active != "yes") {
		rez += '<div style="height: 42px" class="no_coverage"></div><a href="/contact-us?type=p" id="request-link">On Request</a>';
	} else {
		rez += '<div id="price_content">';
		rez += '  <div class="price_sign_big">&#8364;</div>';
		rez += '  <div class="price_big">' + pricePerMonth + '</div>'; 
		rez += '  <div class="price_details">per month</div>';
		rez += '</div>';
		rez += '<div id="price_content_additional">';
		rez += '  <div class="price_small">+' + pricePerUnit + '</div>';
		rez += '  <div class="price_sign_small">¢</div>';  
		rez += '  <div class="price_details"> per ' + unitTitle + '</div>';
		rez += '</div>';
		if(setupPrice != '0') {
			rez += '<div id="price_content_additional">';
			rez += '  <div class="price_small">+' + setupPrice + '</div>';
			rez += '  <div class="price_sign_small">€</div>';  
			rez += '  <div class="price_details"> setup fee</div>';
			rez += '</div>';
		}
	}
	return rez;
}

