--- recaptcha.module_original	2008-11-08 01:57:32.000000000 -0800
+++ recaptcha.module	2008-11-08 14:22:31.000000000 -0800
@@ -129,6 +129,31 @@ function recaptcha_admin_settings_valida
 }
 
 /**
+ * Constructs the javascript to modify the reCAPTCHA theme
+ */
+function _recaptcha_construct_javascript($recaptcha_theme, $recaptcha_tabindex) {
+  global $_recaptcha_jsadded;
+  if (!isset($_recaptcha_jsadded)) { // only display the javascript once.
+    $_recaptcha_jsadded = TRUE;
+    $js = "var RecaptchaOptions = {theme : '$recaptcha_theme'";
+    
+    // Add support to display the custom theme
+    if ($recaptcha_theme == 'custom') {
+     $js .= ", custom_theme_widget : 'recaptcha_custom_theme_widget'";
+     $recaptcha_form_value = theme('recaptcha_custom_widget');
+    }
+    
+    // Set the default tab index
+    if (!empty($recaptcha_tabindex)) {
+      $js .= ', tabindex : '. $recaptcha_tabindex;
+    }
+    drupal_add_js($js .'};', 'inline');
+    return $recaptcha_form_value;
+  }
+  return NULL;
+}
+
+/**
  * reCAPTCHA implementation of hook_captcha
  */
 function recaptcha_captcha() {  
@@ -143,7 +168,6 @@ function recaptcha_captcha() {  
       $result = array();
       if ($captcha_type == 'reCAPTCHA') {
         require_once('recaptcha.inc');
-        global $_recaptcha_jsadded;
         @(include_once('recaptcha/recaptchalib.php')) or _recaptcha_library_not_found();
 
         // Check if reCAPTCHA is available and show Math if not
@@ -158,25 +182,9 @@ function recaptcha_captcha() {  
         $recaptcha_theme = variable_get('recaptcha_theme', 'red');
         $recaptcha_tabindex = variable_get('recaptcha_tabindex', NULL);
         $recaptcha_public_key = variable_get('recaptcha_public_key', '');
-        $recaptcha_form_value = NULL;
         
         // Construct the Javascript
-        if (!isset($_recaptcha_jsadded)) { // only display the javascript once.
-          $_recaptcha_jsadded = TRUE;
-          $js = "var RecaptchaOptions = {theme : '$recaptcha_theme'";
-          
-          // Add support to display the custom theme
-          if ($recaptcha_theme == 'custom') {
-           $js .= ", custom_theme_widget : 'recaptcha_custom_theme_widget'";
-           $recaptcha_form_value = theme('recaptcha_custom_widget');
-          }
-          
-          // Set the default tab index
-          if (!empty($recaptcha_tabindex)) {
-            $js .= ', tabindex : '. $recaptcha_tabindex;
-          }
-          drupal_add_js($js .'};', 'inline');
-        }
+        $recaptcha_form_value = _recaptcha_construct_javascript($recaptcha_theme, $recaptcha_tabindex);
         
         // Create the form
         $result['preprocess'] = TRUE; // tell captcha to preprocess the form
@@ -203,6 +211,12 @@ function recaptcha_captcha() {  
       }
       else {
         form_set_error('captcha_response', t('The reCAPTCHA code you entered was incorrect.'));
+        
+	// Construct the Javascript
+        $recaptcha_theme = variable_get('recaptcha_theme', 'red');
+        $recaptcha_tabindex = variable_get('recaptcha_tabindex', NULL);
+        _recaptcha_construct_javascript($recaptcha_theme, $recaptcha_tabindex);
+        
         return FALSE;
       }
     break;
