From 758b5e9f5e6124e09e5584f3f199cd8e2f341815 Mon Sep 17 00:00:00 2001
From: Stefaan Lippens <soxofaan@41478-no-reply.drupal.org>
Date: Thu, 3 Mar 2011 00:58:42 +0100
Subject: [PATCH] first simple implementation: click image for reload

---
 image_captcha/image_captcha.js     |   10 ++++++++++
 image_captcha/image_captcha.module |    5 ++++-
 2 files changed, 14 insertions(+), 1 deletions(-)

diff --git a/image_captcha/image_captcha.js b/image_captcha/image_captcha.js
index 4567465..9466928 100644
--- a/image_captcha/image_captcha.js
+++ b/image_captcha/image_captcha.js
@@ -1,4 +1,14 @@
 
+// JavaScript behaviors for the Image CAPTCHA
+Drupal.behaviors.imageCaptcha = function (context) {
+
+  // Add a click event to CAPTCHA images to reload the CAPTCHA image
+  $(".captcha_image", context).click(function() {
+    $(this).attr('src', $(this).attr('src').replace(/\?.*$/, '') + '?r=' + Math.random());
+  })
+
+};
+
 // JavaScript behaviors for the Image CAPTCHA admin page
 Drupal.behaviors.imageCaptchaAdmin = function (context) {
 
diff --git a/image_captcha/image_captcha.module b/image_captcha/image_captcha.module
index 460d2d1..5f1a391 100644
--- a/image_captcha/image_captcha.module
+++ b/image_captcha/image_captcha.module
@@ -214,6 +214,9 @@ function image_captcha_captcha($op, $captcha_type='', $captcha_sid=NULL) {
         // build the result to return
         $result = array();
 
+        // Use javascript for some added usability (e.g. image reload).
+        drupal_add_js(drupal_get_path('module', 'image_captcha') .'/image_captcha.js');
+
         $result['solution'] = $code;
         // Generate image source URL (add timestamp to avoid problems with
         // client side caching: subsequent images of the same CAPTCHA session
@@ -221,7 +224,7 @@ function image_captcha_captcha($op, $captcha_type='', $captcha_sid=NULL) {
         $img_src = check_url(url("image_captcha/$captcha_sid/". time()));
         $result['form']['captcha_image'] = array(
           '#type' => 'markup',
-          '#value' => '<img src="'. $img_src .'" alt="'. t('Image CAPTCHA') .'" title="'. t('Image CAPTCHA') .'" />',
+          '#value' => '<img src="'. $img_src .'" class="captcha_image" id="captcha_image_'. $captcha_sid .'" alt="'. t('Image CAPTCHA') .'" title="'. t('Image CAPTCHA') .'" />',
           '#weight' => -2,
         );
         $result['form']['captcha_response'] = array(
-- 
1.7.3.4

