// Example usage. This is an AHAH callback function.
function mymodule_ahah() {
  // Update the form and render only the wrapper inside the "Refund Settings"
  // fieldset.
  $form_update_callbacks['some_fieldset][important_data_wrapper'] = array(
    'callback'           => '_mymodule_form_important_data',
    'callback arguments' => array($_POST['some_setting'], $_POST['another_setting'], 'and another one'),
  );
  // Possibly more entries for $form_update_callbacks here.
  $form_element_to_render = 'some_fieldset][important_data_wrapper';
  ahah_helper_render($form_update_callbacks, $form_element_to_render);
}




/**
 * This will become ahah_helper.module?!
 */
function ahah_helper_render($form_update_callbacks = array(), $form_element_to_render) {
  $form_state = array('submitted' => FALSE);
  $form_build_id = $_POST['form_build_id'];

  // Add the new element to the stored form. Without adding the element to the
  // form, Drupal is not aware of this new elements existence and will not
  // process it. We retrieve the cached form, add the element, and resave.
  $form = form_get_cache($form_build_id, $form_state);
  foreach ($form_update_callbacks as $form_element_name => $settings) {
    $form_element =& ahah_helper_get_form_element($form, $form_element_name);
    $form_element = call_user_func_array($settings['callback'], $settings['callback arguments']);
  }
  form_set_cache($form_build_id, $form, $form_state);

  // Rebuild the form.
  $form += array(
    '#post'       => $_POST,
    '#programmed' => FALSE,
  );
  $form = form_builder($_POST['form_id'], $form, $form_state);

  drupal_json(array(
    'status' => TRUE,
    'data'   => drupal_render(ahah_helper_get_form_element($form, $form_element_to_render)),
  ));
}


function &ahah_helper_get_form_element(&$form, $parents) {
  // Allow $parents to be either an array of the element's parents or the name
  // of an element.
  if (strpos($parents, ']') !== FALSE) {
    $parents = explode('][', $parents);
  }

  // Recursively seek the form element.
  if (count($parents)) {
    $parent = array_shift($parents);
    return ahah_helper_get_form_element(&$form[$parent], $parents);
  }
  else {
    return $form;
  }
}
