var controller = 'homecare';
jq(reset_values);

function reset_values() {
  var state = jq('#state');
	
	// county requirement on page load, callback for chosing search_type
  require_county();
  jq('[name=search_type]').click(function() { require_county() });
  
	if ( location.pathname.endsWith('/quick-search') ) {
		if ( state.val() )
			observe_state_on_error(controller, state.val());	
	}
	state.change(function() { observe_state(this.value, controller) });
	
  // set auto complete for housing
  jq('#auto_search').autocomplete('/housing/auto_search', { minChars: 2, max: 150 });
}

// populate county select options if error and state selected
function observe_state_on_error(controller, state) {
  jq.get('/' + controller + '/update_counties', { state: state, authenticity_token: AUTH_TOKEN }, function(data) {
    jq('#county').html(data);
    updateFlash(state);
  });
}

// require county selection if search_type is not housing
function require_county() {
  var search_type = jq('[name=search_type]:checked').val();
  
  if ( !jq('#county_error span').length )
    jq('#county_error').prepend('<span>*</span>');
}