function getAjaxObject()
{
	var xmlhttp = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.

	try
	{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e)
	{
		try
		{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
		catch (E)
		{ xmlhttp = false; }
	}
	@end @*/

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		try
		{ xmlhttp = new XMLHttpRequest(); }
		catch (e)
		{ xmlhttp = false; }
	}
	if (!xmlhttp && window.createRequest)
	{
		try
		{ xmlhttp = window.createRequest(); }
		catch (e)
		{ xmlhttp = false; }
	}

	return xmlhttp;
}


function changeStateLabel(c, elementId)
{
	var o = document.getElementById(elementId);
	if (o)
	{
		var l = (c === 'AU') ? 'State/Territory' :
			(c === 'CA') ? 'Province' :
			(c === 'GB') ? 'County' :
			(c === 'US') ? 'State' :
			'State/Province/Region';
		o.innerHTML = l;
	}
}

function changeZipLabel(c, elementId)
{
	var o = document.getElementById(elementId);

	if (o)
	{
		var l = (c === 'US') ? 'ZIP Code' :
			(c === 'AU' || c === 'GB') ? 'Postcode' :
			'Postal Code';
		o.innerHTML = l;
	}
}

function changeStateSelect(c, elementId)
{
	var currentValue = document.getElementById('state').value;
	var v = '<input id="state" name="state" type="text" value="' + currentValue + '" size="20" />';
	var element = document.getElementById(elementId);
	element.innerHTML = v;

	if (c === 'AU' || c === 'CA' || c === 'GB' || c === 'US' )
	{
		var xmlhttp = getAjaxObject();
		xmlhttp.onreadystatechange = function ()
		{
			if (xmlhttp.readyState === 4)
			{
				if (xmlhttp.status === 200)
				{
					var txt = '<select id="state" name="state">\n';
					txt += '<option value=""></option>\n';

					var group = xmlhttp.responseXML.documentElement;
					var groups = xmlhttp.responseXML.documentElement.getElementsByTagName("group");

					if( groups.length == 0 )
					{
						txt += createOptions(group, currentValue);
					}

					for(var j=0; j < groups.length; j++)
					{
						var states = groups[j].getElementsByTagName("state");
						var label = groups[j].attributes.getNamedItem("name").value;
						txt += '<optgroup label="' + label + '">\n';
						txt += createOptions(groups[j], currentValue);
						txt += '</optgroup>\n';
					}
					txt += '</select>';
					element.innerHTML = txt;
				}
			}
		};
		xmlhttp.open("GET", "/js/states_" + c + ".xml", true);
		xmlhttp.send(null);
	}
}

function createOptions(group, currentValue)
{
	var txt = '';
	var states = group.getElementsByTagName("state");
	for(i=0; i < states.length; i++)
	{
		var name = states[i].getElementsByTagName("name")[0];
		var value = states[i].getElementsByTagName("value")[0];
		if( !value )
		{
			value = name;
		}
		var selected = '';
		if( value.firstChild.data == currentValue )
		{
			selected = 'selected="selected" ';
		}
		txt += '<option value="' + value.firstChild.data + '"' + selected + '>' + name.firstChild.data + '</option>\n';
	}
	return txt;
}


function changeCountry(select)
{
	var c = select.value;

	changeStateLabel(c, 'stateLabel');
	changeZipLabel(c, 'zipLabel');
	changeStateSelect(c, 'stateSelect');
}

