﻿if (!window.Cmaeon)
  window.Cmaeon = {};
if (!window.Cmaeon.LeadCapture)
  window.Cmaeon.LeadCapture = {};


Cmaeon.LeadCapture.InitializeCaptcha = function(theme) {
  if (!theme)
    theme = "red"
    
  Recaptcha.create(
        "6LdgVgoAAAAAAAziy2gFoVX8mY7e-EL6SI4FS3sd",
        "recaptcha_div",
        {
          theme: theme,
          callback: Recaptcha.focus_response_field
        });

  var form = jQuery(jQuery('#recaptcha_error').parents('form')[0]);
  form.bind('submit', Cmaeon.LeadCapture.VerifyCaptcha);
}

Cmaeon.LeadCapture.VerifyCaptcha = function(e) {
  e.preventDefault();
  jQuery('#recaptcha_error').hide();

  jQuery.getJSON(
    'http://1to1red.com/LeadCapture/webservices/Captcha.asmx/VerifyResponse?callback=?',
    {
      challenge: Recaptcha.get_challenge(),
      response: Recaptcha.get_response()
    },
    function(data) {
      if (data.isVerified) {
        var form = jQuery(jQuery('#recaptcha_error').parents('form')[0]);
        form.unbind('submit', Cmaeon.LeadCapture.VerifyCaptcha);
        form.submit();
      }
      else {
        Recaptcha.reload();
        var error = jQuery('#recaptcha_error');
        error.html(data.message);
        error.show(data.message);
      }
    });
};