﻿if (!window.Cmaeon) { window.Cmaeon = {}; }
if (!window.Cmaeon.Real) { window.Cmaeon.Real = {}; }
if (!window.Cmaeon.Real.Auth) { window.Cmaeon.Real.Auth = {}; }

Cmaeon.Real.Auth.InitializeCaptcha = function(form, options)
{
  Cmaeon.Real.Auth.captchaForm = jQuery(form);
  Cmaeon.Real.Auth.captchaOptions = options || { theme: 'red' };

  Recaptcha.create
  (
    "6LdoW7wSAAAAAE0YJouAywyKzISwHtl24ovGuZHN",
    "recaptcha_div",
    {
      theme: options.theme
    }
  );

  Cmaeon.Real.Auth.captchaForm.bind('submit', Cmaeon.Real.Auth.VerifyCaptcha);
};

Cmaeon.Real.Auth.VerifyCaptcha = function(e)
{
  e.preventDefault();
  jQuery('#recaptcha_error').hide();

  jQuery.getJSON
  (
    'http://secure.1to1real.com/webservices/auth.asmx/captcha?callback=?',
    {
      challenge: Recaptcha.get_challenge(),
      response: Recaptcha.get_response()
    },
    function(data)
    {
      if (data.isVerified)
      {
        Cmaeon.Real.Auth.captchaForm.unbind('submit', Cmaeon.Real.Auth.VerifyCaptcha);
        if (typeof (Cmaeon.Real.Auth.captchaOptions.callback) == 'function')
        { Cmaeon.Real.Auth.captchaOptions.callback(Cmaeon.Real.Auth.captchaForm); }
        else
        { Cmaeon.Real.Auth.captchaForm.submit(); }
      }
      else
      {
        Recaptcha.reload();
        jQuery('#recaptcha_error').html(data.message).show();
      }
    }
  );
};

