
var APS_FILL_E_CLEAR	= 0;
var APS_FILL_E_WAIT	= 1;
var APS_FILL_E_LINK	= 2;

function aps_fill(form, callback_name)
{
	
	this.form = form;
	this.name_country = "countrys";
	this.name_region = "regions";
	this.name_town = "towns";

	this.any_country = false;
	this.any_region = false;
	this.any_town = false;
	
	this.null_value	= '';
	this.any_value	= 0;
	
	this.txt_any_country = "Any country"
	this.txt_any_region = "Any region"
	this.txt_any_town = "Any town"
	
	this.txt_sel_country = "(Select country)"
	this.txt_sel_region = "(Select region)"
	
	this.txt_wait = "(Please wait)"
	this.txt_select = "(Please select)"

	this.default_country	= 2;
	this.default_region	= "";
	this.default_town	= "";
	
	this.name = callback_name;

	this.connect = function()
	{
		
		if(this.form.elements[this.name_region])
		{
			//connect region event
			
			var code_region = this.name+".select_region("+this.name+".form.elements[\""+this.name_region+"\"].value);";
			this.form.elements[this.name_region].onchange = function(){eval(code_region)};
		
		}
		
		if(this.form.elements[this.name_country])
		{
			//connect country event
			
			var code_country = this.name+".select_country("+this.name+".form.elements[\""+this.name_country+"\"].value);";
	
			this.form.elements[this.name_country].onchange = function(){eval(code_country)};

			/******************/
			
			this.empty_countries(APS_FILL_E_WAIT)
			var countrys = xajax_aps_get_countries(0, this.name+".fill_countries")

		}
		else
		{
			this.select_country(this.default_country)
		}

		//if(this.default_town != this.null_value)
		//	this.select_town(this.default_town);
	
	}

	this.fill_countries = function(countries)
	{
		
		this.empty_countries(APS_FILL_E_CLEAR)
		
		var count = 0;
		
		if(this.any_country)
			combo_add(this.form.elements[this.name_country], this.any_value, this.txt_any_country, false ? true : false );
		
		for(var id in countries)
		{
			if(!isNaN(id))
			{
				combo_add(this.form.elements[this.name_country], id, countries[id], false ? true : false );
				var count = 0;
			}
		}

	}

	this.select_country = function(country)
	{
	
		if(country && country!=this.any_value && country!=this.null_value)
		{
			this.empty_regions(APS_FILL_E_WAIT);
			xajax_aps_get_regions(country, this.name+".fill_regions")
		}
		else
		{
			this.empty_regions(APS_FILL_E_LINK);
		}

	}

	this.fill_regions = function(regions)
	{
		
		this.empty_regions(APS_FILL_E_CLEAR)
		
		var count = 0;

		if(this.any_region)
			combo_add(this.form.elements[this.name_region], this.any_value, this.txt_any_region, false ? true : false );
		
		for(var id in regions)
		{
			if(!isNaN(id))
			{
				combo_add(this.form.elements[this.name_region], id, regions[id], false ? true : false );
				count++;
			}

		}

		if(count==0)
		{
			combo_add(this.form.elements[this.name_region], this.null_value, this.txt_sel_country, true );
		}
		
		if(this.default_region != this.null_value)
		{
			this.select_region(this.default_region);
			this.default_region = this.null_value;
		}
	}

	this.select_region = function(region)
	{
	
		this.form.elements[this.name_region].selectedIndex = combo_get_id(this.form.elements[this.name_region],region);
	

		if(region && region!=this.any_value && region!=this.null_value)
		{
			this.empty_towns(APS_FILL_E_WAIT);
			xajax_aps_get_towns(region, this.name+".fill_towns")
		}
		else
		{
			this.empty_towns(APS_FILL_E_LINK);
		}
	
	}

	this.fill_towns = function(towns)
	{
		
		this.empty_towns(APS_FILL_E_CLEAR)
	
		var count = 0;

		if(this.any_town)
			combo_add(this.form.elements[this.name_town], this.any_value, this.txt_any_town, false ? true : false );
		
		for(var id in towns)
		{
			if(!isNaN(id))
			{
				combo_add(this.form.elements[this.name_town], id, towns[id], false ? true : false );
				count++;
			}
		}
		
		if(count==0)
		{
			combo_add(this.form.elements[this.name_town], this.null_value, this.txt_sel_region, true );
		}

		if(this.default_town != this.null_value)
		{
			this.select_town(this.default_town);
			this.default_town = this.null_value;
		}
	}

	this.select_town = function(town)
	{
	
		this.form.elements[this.name_town].selectedIndex = combo_get_id(this.form.elements[this.name_town],town);

		//TODO...
	}
	
	
	this.empty_countries = function(event)
	{
		combo_empty(this.form.elements[this.name_country])
		
		if(event==APS_FILL_E_WAIT)
		{
			combo_add(this.form.elements[this.name_country], this.null_value, this.txt_wait, true );
		}
		else if(event==APS_FILL_E_CLEAR)
		{
			combo_add(this.form.elements[this.name_country], this.null_value, this.txt_select, false ? true : false );
		}
		
		this.empty_regions(APS_FILL_E_LINK);
	}
	
	this.empty_regions = function(event)
	{
		combo_empty(this.form.elements[this.name_region])
		
		if(event==APS_FILL_E_WAIT)
		{
			combo_add(this.form.elements[this.name_region], this.null_value, this.txt_wait, true );
		}
		else if(event==APS_FILL_E_LINK)
		{
			combo_add(this.form.elements[this.name_region], this.null_value, this.txt_sel_country, true );
		}
		else if(event==APS_FILL_E_CLEAR)
		{
			combo_add(this.form.elements[this.name_region], this.null_value, this.txt_select, false ? true : false );
		}

		this.empty_towns(APS_FILL_E_LINK);
	
	}
	
	this.empty_towns = function(event)
	{
		combo_empty(this.form.elements[this.name_town])
		
		if(event==APS_FILL_E_WAIT)
			combo_add(this.form.elements[this.name_town], this.null_value, this.txt_wait, true );
		else if(event==APS_FILL_E_LINK)
			combo_add(this.form.elements[this.name_town], this.null_value, this.txt_sel_region, true );
		else if(event==APS_FILL_E_CLEAR)
			combo_add(this.form.elements[this.name_town], this.null_value, this.txt_select, false ? true : false );
		

	}


}

