--- captcha.module	2008-05-27 13:32:52.000000000 -0600
+++ captcha.module	2008-05-27 13:32:30.000000000 -0600
@@ -401,6 +401,25 @@ function _captcha_persistence_skip($form
     || ($persistence == CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM && ($_SESSION['captcha'][$form_id]['success'] === TRUE));
 }
 
+/*
+ * Helper function that finds the $form element containing the first submit button.
+ */
+function &_captcha_find(&$form) {
+  foreach($form as $key => &$value) {
+    if (is_array($value)) {
+        $found =& _captcha_find($value);
+		if (is_array($found))
+	      return $found;
+		else if ($found === true)
+		  return $form;
+	} else if ($key == '#type' && $value == 'submit') {
+        return true;
+    }
+  }
+
+  return false;
+}
+
 /**
  * Implementation of hook_form_alter().
  *
@@ -447,9 +466,13 @@ function captcha_form_alter($form_id, &$
 
     // Add a CAPTCHA part to the form (depends on value of captcha_description)
     $captcha_description = _captcha_get_description();
+    
+    //Sets the place for the captcha to be placed above the first found submit button
+    $place =& _captcha_find($form);
+    
     if ($captcha_description) {
       // $captcha_description is not empty: CAPTCHA part is a fieldset with description
-      $form['captcha'] = array(
+      $place['captcha'] = array(
         '#type' => 'fieldset',
         '#title' => t('CAPTCHA'),
         '#description' => $captcha_description,
@@ -458,7 +481,7 @@ function captcha_form_alter($form_id, &$
     }
     else {
       // $captcha_description is empty: CAPTCHA part is an empty markup form element
-      $form['captcha'] = array(
+      $place['captcha'] = array(
         '#type' => 'markup',
         '#prefix' => '<div class="captcha">',
         '#suffix' => '</div>',
@@ -466,7 +489,7 @@ function captcha_form_alter($form_id, &$
     }
 
     // Add the form elements of the generated CAPTCHA to the form
-    $form['captcha'] = array_merge($form['captcha'], $captcha['form']);
+    $place['captcha'] = array_merge($place['captcha'], $captcha['form']);
 
     // Store the solution of the generated CAPTCHA as an internal form value.
     // This will be stored later in $_SESSION during the pre_render phase.
@@ -475,7 +498,7 @@ function captcha_form_alter($form_id, &$
     // in a new (POST) request. Consequently the right CAPTCHA solution would be
     // overwritten just before validation. The pre_render functions are not run
     // before validation and are the right place to store the solution in $_SESSION.
-    $form['captcha']['captcha_solution'] = array(
+    $place['captcha']['captcha_solution'] = array(
       '#type' => 'value',
       '#value' => $captcha['solution'],
     );
@@ -485,13 +508,13 @@ function captcha_form_alter($form_id, &$
     // couple of times before submitting them. The solution of the CAPTCHA of
     // each of these form instances will be stored at the pre_render phase in
     // $_SESSION['captcha'][$form_id][$captcha_token]
-    $form['captcha']['captcha_token'] = array(
+    $place['captcha']['captcha_token'] = array(
       '#type' => 'hidden',
       '#value' => md5(mt_rand()),
     );
 
     // other internal values needed for the validation phase
-    $form['captcha']['validationdata'] = array(
+    $place['captcha']['validationdata'] = array(
       '#type' => 'value',
       '#value' => array(
         'form_id' => $form_id,
@@ -505,7 +528,7 @@ function captcha_form_alter($form_id, &$
     $form['#pre_render'] = ((array) $form['#pre_render']) + array('captcha_pre_render', 'captcha_pre_render_place_captcha');
 
     // Add a validation function for the CAPTCHA part of the form
-    $form['captcha']['#validate'] = ((array) $form['captcha']['#validate']) + array('captcha_validate' => array());
+    $place['captcha']['#validate'] = ((array) $place['captcha']['#validate']) + array('captcha_validate' => array());
 
   }
   elseif (user_access('administer CAPTCHA settings') && variable_get('captcha_administration_mode', FALSE) && arg(0) != 'admin') {
@@ -515,14 +538,17 @@ function captcha_form_alter($form_id, &$
       return;
     }
     $captcha_point = db_fetch_object($result);
-    $form['captcha'] = array(
+    
+    $place =& _captcha_find($form);
+    
+    $place['captcha'] = array(
       '#type' => 'fieldset',
       '#title' => 'CAPTCHA',
       '#collapsible' => TRUE,
       '#collapsed' => TRUE,
     );
     if ($captcha_point && $captcha_point->type) {
-      $form['captcha']['#description'] = t('The challenge "@type" (by module "@module") is enabled here for untrusted users: !edit, !disable or !general.', array(
+      $place['captcha']['#description'] = t('The challenge "@type" (by module "@module") is enabled here for untrusted users: !edit, !disable or !general.', array(
         '@type' => $captcha_point->type,
         '@module' => $captcha_point->module,
         '!edit' => l(t('edit challenge type'), "admin/user/captcha/$form_id", array(), drupal_get_destination()),
@@ -532,7 +558,7 @@ function captcha_form_alter($form_id, &$
       );
     }
     else {
-      $form['captcha']['#description'] = l(t('Place a challenge here for untrusted users.'), "admin/user/captcha/$form_id/enable", array(), drupal_get_destination());
+      $place['captcha']['#description'] = l(t('Place a challenge here for untrusted users.'), "admin/user/captcha/$form_id/enable", array(), drupal_get_destination());
     }
     // Add pre_render function for placing the CAPTCHA just above the submit button
     $form['#pre_render'] = ((array) $form['#pre_render']) + array('captcha_pre_render_place_captcha');
@@ -545,8 +571,6 @@ function captcha_form_alter($form_id, &$
 function captcha_validate($form_values) {
   // Check if there is CAPTCHA data available in $_SESSION
   // If not, the user has most likely disabled cookies
-  foreach($form_values as $key => $value)
-	drupal_set_message($key . ' => ' . $value);
   if (!isset($_SESSION['captcha'])) {
     form_set_error('captcha', t('Cookies should be enabled in your browser for CAPTCHA validation.'));
     return;
@@ -632,12 +656,15 @@ function captcha_pre_render($form_id, &$
     unset($_SESSION['captcha'][$form_id]);
     drupal_set_message(t('You can\'t request more than @num challenges without solving them. Your previous challenges were flushed.', array('@num' => CAPTCHA_UNSOLVED_CHALLENGES_MAX)));
   }
+  
+  $place =& _captcha_find($form);
+  
   // store the current CAPTCHA solution in $_SESSION
-  $captcha_token = $form['captcha']['captcha_token']['#value'];
-  $_SESSION['captcha'][$form_id][$captcha_token] = $form['captcha']['captcha_solution']['#value'];
+  $captcha_token = $place['captcha']['captcha_token']['#value'];
+  $_SESSION['captcha'][$form_id][$captcha_token] = $place['captcha']['captcha_solution']['#value'];
   $_SESSION['captcha'][$form_id]['success'] = FALSE;
   // empty the value of the captcha_response form item before rendering
-  $form['captcha']['captcha_response']['#value'] = '';
+  $place['captcha']['captcha_response']['#value'] = '';
 }
 
 /**
@@ -646,16 +673,19 @@ function captcha_pre_render($form_id, &$
 function captcha_pre_render_place_captcha($form_id, &$form) {
   // search the weights of the buttons in the form
   $button_weights = array();
-  foreach (element_children($form) as $key) {
-    if ($form[$key]['#type'] == 'submit' || $form[$key]['#type'] == 'button') {
-      $button_weights[] = $form[$key]['#weight'];
+  
+  $place =& _captcha_find($form);
+  
+  foreach (element_children($place) as $key) {
+    if ($place[$key]['#type'] == 'submit' || $place[$key]['#type'] == 'button') {
+      $button_weights[] = $place[$key]['#weight'];
     }
   }
   if ($button_weights) {
     // set the weight of the CAPTCHA element a tiny bit smaller than the lightest button weight
     // (note that the default resolution of #weight values is 1/1000 (see drupal/includes/form.inc))
     $first_button_weight = min($button_weights);
-    $form['captcha']['#weight'] = $first_button_weight - 0.5/1000.0;
+    $place['captcha']['#weight'] = $first_button_weight - 0.5/1000.0;
     // make sure the form gets sorted before rendering
     unset($form['#sorted']);
   }
