document.observe('dom:loaded', reset_values);

function reset_values(){
	if (location.pathname.endsWith('/search')){
		var params = location.search.toQueryParams();
		var state = params['homecare_listing[state]'];
		
		if (state)
			observe_state_on_error(controller, state);	
	}
}

function observe_state_on_error(controller, state, county, city){
	new Ajax.Updater('county', '/'+ controller +'/update_counties', {
		parameters: { state: state, authenticity_token: AUTH_TOKEN },
		onComplete: function(){
		  updateFlash(state);
		
			if (county){
				$('county').value = county;
			  
				new Ajax.Updater('city', '/' + controller + '/update_cities', {
					parameters: { county: county, authenticity_token: AUTH_TOKEN },
					onComplete: function(){
					  $('county').value = county;
						if (city)
							$('city').value = city;
					}
				});
			}
		}
	});
}
