--- captcha_old.module	2008-05-28 17:33:58.000000000 -0600
+++ captcha.module	2008-05-28 17:26:31.000000000 -0600
@@ -402,6 +402,36 @@ function _captcha_persistence_skip($form
 }
 
 /**
+ * Helper function that finds the $form element containing the first submit button.
+ * 
+ * This function will return the reference to the array which contains the button
+ * if a submit button is found. If a submit button is not found it returns false.
+ */
+function &_captcha_find_first_submit(&$form) {
+  foreach ($form as $key => &$value) {
+   //If the value is an array recur this function 
+  if (is_array($value)) {
+    $found =& _captcha_find_first_submit($value);
+    // If $found is an array that means that it is the reference to the array containing
+    // the submit button. If it is true then this is the array that contains the submit
+    // button.
+    if (is_array($found)) {
+      return $found;
+    }
+    elseif ($found === true) {
+      return $form;
+    }
+  } elseif ($key == '#type' && $value == 'submit') {
+      // Return true to let the function that called it know that it is the array
+      // containing the submit button.
+      return true;
+    }
+  }
+
+  return false;
+}
+
+/**
  * Implementation of hook_form_alter().
  *
  * This function adds a CAPTCHA to forms for untrusted users if needed and adds
@@ -444,7 +474,19 @@ function captcha_form_alter($form_id, &$
         WATCHDOG_ERROR);
       return;
     }
-
+    
+    // handle the pre_render functions
+    // Assigned before $form is set to array above submit button
+    $form['#pre_render'][] = 'captcha_pre_render';
+    $form['#pre_render'][] = 'captcha_pre_render_place_captcha';
+    
+    // If there is a submit button on the form it is found and $form is assigned to the
+    // array above it.
+    if ($first_submit =& _captcha_find_first_submit($form)) {
+      unset($form);
+      $form =& $first_submit;
+    }
+    
     // Add a CAPTCHA part to the form (depends on value of captcha_description)
     $captcha_description = _captcha_get_description();
     if ($captcha_description) {
@@ -501,10 +543,6 @@ function captcha_form_alter($form_id, &$
       ),
     );
 
-    // handle the pre_render functions
-    $form['#pre_render'][] = 'captcha_pre_render';
-    $form['#pre_render'][] = 'captcha_pre_render_place_captcha';
-
     // Add a validation function for the CAPTCHA part of the form
     $form['captcha']['#validate']['captcha_validate'] = array();
 
@@ -515,6 +553,17 @@ function captcha_form_alter($form_id, &$
     if (!$result) {
       return;
     }
+    
+    // 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');
+    
+    // If there is a submit button on the form it is found and $form is assigned to the
+    // array above it.
+    if ($first_submit =& _captcha_find_first_submit($form)) {
+      unset($form);
+      $form =& $first_submit;
+    }
+    
     $captcha_point = db_fetch_object($result);
     $form['captcha'] = array(
       '#type' => 'fieldset',
@@ -542,8 +591,6 @@ function captcha_form_alter($form_id, &$
         '#value' => l(t('Place a CAPTCHA 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');
   }
 }
 
@@ -642,6 +689,14 @@ function captcha_pre_render($form_id, &$
       }
     }
   }
+  
+  // If there is a submit button on the form it is found and $form is assigned to the
+  // array above it.
+  if ($first_submit =& _captcha_find_first_submit($form)) {
+    unset($form);
+    $form =& $first_submit;
+  }
+  
   // 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'];
@@ -654,6 +709,13 @@ function captcha_pre_render($form_id, &$
  * Pre_render function to place the CAPTCHA form element just above the last submit button
  */
 function captcha_pre_render_place_captcha($form_id, &$form) {
+  // If there is a submit button on the form it is found and $form is assigned to the
+  // array above it.
+  if ($first_submit =& _captcha_find_first_submit($form)) {
+    unset($form);
+    $form =& $first_submit;
+  }
+ 
   // search the weights of the buttons in the form
   $button_weights = array();
   foreach (element_children($form) as $key) {
