var Geo;
var CountryId;
var StateId;
var City;
var CountryObj;
var StateObj;
var CityObj;
var CityContObj;
var FirstOptEmpty;
var CityElements;

function LoadBlank(o, x)
{
	o.length = 1;
	o.options[0] = new Option("");
	o.options[0].value = "0";
	o.options[1] = new Option(x);
	o.options[1].value = "0";
}

function LoadBlankCity(x)
{
	CityContObj.innerHTML = "<select name=\"t\"><option value=\"\"></option><option value=\"\">" + x + "</option></select>";
}

function LoadCities()
{
	var c = CountryObj.options[CountryObj.selectedIndex].value;
	var s = StateObj.options[StateObj.selectedIndex].value;
	
	if (c == "0" || s == "0")
		LoadBlankCity("Select a State");
	else
	{
		Loading(CityObj);
		var x = AjaxGet("http://dork.com/ajax/geo/state-" + c + "-" + s + ".txt",false,true);
		
		if (x == "err=1" || x == "err=2")
		{
			alert("There was a connection timeout. Please try your selection again. If you continue to experience problems, please try a different browser such as Internet Explorer 7.0 (or above) or FireFox 2.0 (or above).");
			StateObj.options[0].selected = true;
			LoadBlankCity("Select a State")
		}
		else
		{
			CityContObj.innerHTML = "";
			
			var y = x.split(";");
			var z = "<select id=\"" + CityObj.name + "\" name=\"" + CityObj.name + "\"" + CityElements + "><option></option>";
			for (var i = 0; i <= (y.length - 1); i++)
			{
				z += (y[i] == City) ? "<option value=\"" + y[i] + "\" selected=\"selected\">" + y[i] + "</option>" : "<option value=\"" + y[i] + "\">" + y[i] + "</option>";
			}
			z += "</select>";
			CityContObj.innerHTML = z;
		}
	}
}

function LoadCountries()
{
	Geo = LoadGeoData();
	var g = Geo.split("*");
	
	CountryObj.length = 0;
	
	if (FirstOptEmpty == true)
	{
		CountryObj.options[0] = new Option("");
		CountryObj.options[0].value = "0";
	}
	var j = CountryObj.length;
	
	for (var i = 0; i <= (g.length - 1); i++)
	{
		var x = g[i].split(":");
		CountryObj.options[j] = new Option(x[1]);
		CountryObj.options[j].value = x[0];
		if (x[0] == CountryId) CountryObj.options[j].selected = true;
		j++;
	}
	
	LoadStates();
}

function LoadGeoData()
{
	Loading(CityObj);
	var x = AjaxGet("http://dork.com/ajax/geo/countries-states.txt",false,true);
	
	if (x == "err=1" || x == "err=2")
		alert("There was a connection timeout. Please reload this page if you cannot see the countries or states. If you continue to experience problems, please try a different browser such as Internet Explorer 7.0 (or above) or FireFox 2.0 (or above).");
	else
		return x;
}

function Loading(o)
{
	o.length = 0;
	o.options[0] = new Option("Loading Data . . .");
	o.options[0].value = "0";
}

function LoadStates()
{
	var c = CountryObj.options[CountryObj.selectedIndex].value;
	var g = Geo.split("*");
	
	if (c == "0")
	{
		LoadBlank(StateObj, "Select a Country");
		LoadBlankCity("Select a State")
	}
	else
	{
		StateObj.length = 0;
		StateObj.options[0] = new Option("");
		StateObj.options[0].value = "0";
		
		var j = StateObj.length;
		
		for (var i = 0; i <= (g.length - 1); i++)
		{
			var x = g[i].split(":");
			
			if (x[0] == c)
			{
				var y = x[2].split(";");
				
				for (var i = 0; i <= (y.length - 1); i++)
				{
					var f = y[i].split("#");
					
					StateObj.options[j] = new Option(f[1]);
					StateObj.options[j].value = f[0];
					if (f[0] == StateId) StateObj.options[j].selected = true;
					j++;
				}
				break;
			}
		}
	}
	
	LoadCities();
}

function SetGeoVar(a, b, c, d, e, f, g, h, i)
{
	CountryObj = a;
	StateObj = b;
	CityObj = c;
	CityContObj = d;
	CountryId = e;
	StateId = f;
	City = g;
	FirstOptEmpty = h;
	CityElements = i;
}
