function locationCleanup(prefix)
{
	//console.log("locationCleanup()", prefix);
	
	countrySelectId = prefix+'-country';
	stateSelectId = prefix+'-state';
	citySelectId = prefix+'-city';
	
	countryId = document.getElementById(countrySelectId).value;
	stateId = document.getElementById(stateSelectId).value;
	cityId = document.getElementById(citySelectId).value;
	
	//console.log(countryId, stateId, cityId);
	
	if (countryId == '0')
	{
		//console.log("Hiding state");
		hideState(prefix);
	}
	else if (document.getElementById(stateSelectId).options.length > 1)
	{
		showState(prefix);
	}
	
	if (stateId == '0')
	{
		//console.log("Hiding city");
		hideCity(prefix);
	}
	else if (document.getElementById(citySelectId).options.length > 1)
	{
		showCity(prefix);
	}
}

function hideState(prefix)
{
	//console.log("hideState()", prefix);
	
	emptySelector(prefix+"-state");
	document.getElementById(prefix+"-divstate").style.display = "none";
	
	hideCity(prefix);
}

function showState(prefix)
{
	//console.log("showState()", prefix);
	
	document.getElementById(prefix+"-divstate").style.display = "block";
}

function hideCity(prefix)
{
	//console.log("hideCity()", prefix);
	
	emptySelector(prefix+"-city");
	document.getElementById(prefix+"-divcity").style.display = "none";
}

function showCity(prefix)
{
	//console.log("showCity()", prefix);
	
	document.getElementById(prefix+"-divcity").style.display = "block";
}

// ================================================================================================
// CHANGE COUNTRIES

var AjaxCountryChange = 
{
	prefix: false,
	handleSuccess: function(o)
	{
		// o.responseText
		// o.responseXML
		//console.log(o.responseText);
		this.processResult(o);
	},
	handleFailure: function(o)
	{
		// o.responseText
		// o.responseXML
	},
	processResult: function(o)
	{
		//console.log("prefix=", this.prefix);
		
		stateSelectId = this.prefix+"-state";
		
		try
		{
			var states = YAHOO.lang.JSON.parse(o.responseText);
		}
		catch (e)
		{
			// Error in JSON data!
		}
		
		//console.log("states:", states);
		//console.log("states length:", states.length);
		
		if (typeof(states) == "undefined")
		{
			hideState(this.prefix);
		}
		else
		{
			//console.log("states is not undefined",1);
			
			if (states.length == 0)
			{
				emptySelector(stateSelectId);
				hideState(this.prefix);
			}
			else
			{
				emptySelector(stateSelectId);
				
				for (var i = 0; i < states.length; i++)
				{
					state = states[i];
					//console.log(state);
					document.getElementById(stateSelectId).options[(i+1)] = new Option(state.n, state.id);
				}
				
				locationCleanup(this.prefix);
			}
		}
	},
	startRequest: function(prefix, countryId)
	{
		this.prefix = prefix;
		YAHOO.util.Connect.asyncRequest('GET', '/ajax/states.php?countryid='+countryId, countryChangeCallback);
	}
};

var countryChangeCallback = {
	success: AjaxCountryChange.handleSuccess,
	failure: AjaxCountryChange.handleFailure,
	scope: AjaxCountryChange
};

function changeCountry(prefix)
{
	countryId = document.getElementById(prefix+'-country').value;
	
	AjaxCountryChange.startRequest(prefix, countryId);
}

// ================================================================================================
// CHANGE STATES

var AjaxStateChange = 
{
	prefix: false,
	handleSuccess: function(o)
	{
		// o.responseText
		// o.responseXML
		//console.log(o.responseText);
		this.processResult(o);
	},
	handleFailure: function(o)
	{
		// o.responseText
		// o.responseXML
	},
	processResult: function(o)
	{
		//console.log("prefix=", this.prefix);
		
		citySelectId = this.prefix+"-city";
		
		try
		{
			var cities = YAHOO.lang.JSON.parse(o.responseText);
		}
		catch (e)
		{
			// Error in JSON data!
		}
		
		//console.log("cities:", cities);
		
		if (typeof(cities) == "undefined")
		{
			hideCity(this.prefix);
		}
		else 
		{
			//console.log(cities, cities.length);
			
			if (cities.length == 0)
			{
				emptySelector(citySelectId);
				hideCity(this.prefix);
			}
			else
			{
				emptySelector(citySelectId);
				
				for (var i = 0; i < cities.length; i++)
				{
					city = cities[i];
					//console.log(city);
					document.getElementById(citySelectId).options[(i+1)] = new Option(city.n, city.id);
				}
				
				locationCleanup(this.prefix);
			}
		}
		
	},
	startRequest: function(prefix, stateId)
	{
		this.prefix = prefix;
		
		YAHOO.util.Connect.asyncRequest('GET', '/ajax/cities.php?stateid='+stateId, stateChangeCallback);
	}
};

var stateChangeCallback = {
	success: AjaxStateChange.handleSuccess,
	failure: AjaxStateChange.handleFailure,
	scope: AjaxStateChange
};

function changeState(prefix)
{
	stateId = document.getElementById(prefix+'-state').value;
	
	if (stateId > 0)
	{
		AjaxStateChange.startRequest(prefix, stateId);
	}
	else
	{
		locationCleanup(prefix);
	}
}

// ================================================================================================

function emptySelector(selectid)
{
	//console.log("emptySelector()", selectid);
	document.getElementById(selectid).options.length = 1;
}