
function filterAll(selObj)
{
	switch(selObj.id)
	{
		case 'region':
			filterStates();
			break;
		case 'state':
			filter('county', 'state');
		case 'county':
			filter('city', 'county');
		case 'city':
			filter('property', 'city');
	}
}

function filterStates()
{
	reset('state');
	$('state').getElements('option')
			  .each(function (opt)
					{
						if (opt.value && state_filter[opt.value] != $('region').value)
						{
							opt.dispose();
						}
					});
	filter('county', 'state');
	filter('city', 'county');
	filter('property', 'city');
}

function filter(thing, parentThing)
{
	reset(thing);
    var x = eval(thing + '_filter');
	if ($(parentThing).value == '')
	{
		var parent_values = Array();
		state_opts = $A($(parentThing).getElements('option'));
		state_opts.each(function(opt){ parent_values[parent_values.length] = opt.value });
		//console.log(parent_values);
		$(thing).getElements('option')
				   .each(function(opt)
				   {
						if (opt.value && !hasValue(parent_values, x[opt.value]))
						{
							opt.dispose();
						}
				   });
	}
	else
	{
		$(thing).getElements('option')
			  .each(function (opt)
					{
						if (opt.value && x[opt.value] != $(parentThing).value)
						{
							opt.dispose();
						}
					});
	}
}

function reset(thing)
{
	$(thing).getElements('option')
	  .each(function(opt) { opt.dispose() } );
(new Element('option', 
	 {'value':'', 'html':'Any ' + thing.capitalize()}))
.inject($(thing), 'top');
(new Hash(eval(thing + '_list')))
.each(function (value, key) {
	new Element('option', 
				{'value': key, 'html': value })
	.inject($(thing),'bottom');
 });	
}

function hasValue(theArray, theVal)
{
	var found = false;
	theArray.each(function (t) 
					{
						if (t == theVal)
						{
							found = true;
						}
					} );
	return found;
}
