var xmlHttp;
var preselectCountry="";

function showCountries(country)
{
	loadingMessage();

	preselectCountry = country;

	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
 	{
  		alert ("Your browser does not support AJAX!");
  		return;
	} 

	var url="getpostage.asp";
	url=url+"?sid="+Math.random();
	if (preselectCountry!="")
	{
		url=url+"&preselectcountry="+preselectCountry;
	}
	xmlHttp.onreadystatechange=stateChangedCountry;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

	resetResults();
} 


function showPostage(country)
{
	if (country=="")
	{
		resetResults();
	}
	else
	{
		loadingMessage();

		xmlHttp=GetXmlHttpObject();

		if (xmlHttp==null)
	 	{
	  		alert ("Your browser does not support AJAX!");
	  		return;
		} 

		var url="getpostage.asp";
		url=url+"?sid="+Math.random();
		url=url+"&country="+country;

		xmlHttp.onreadystatechange=stateChangedPostage;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
} 

function loadingMessage()
{
	document.getElementById("results").innerHTML="loading...";
}


function resetResults()
{
	document.getElementById("results").innerHTML="";
}

function stateChangedCountry() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		document.getElementById("dropdowncountries").innerHTML=xmlHttp.responseText;

		if (preselectCountry!="")
		{
			showPostage(preselectCountry);
			preselectCountry="";
		}
	}
}

function stateChangedPostage() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		document.getElementById("results").innerHTML=xmlHttp.responseText;
	}
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
  	{
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}
	catch (e)
  	{
  		// Internet Explorer
  		try
    	{
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
  		catch (e)
    	{
    		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
  	}

	return xmlHttp;
}
