(function ($) { var recaptha_disabled = <%= Recaptcha.configuration.skip_verify_env.include?(Rails.env.to_s ) %> var print_results = function (element, result) { if (result instanceof Array) { var resp = ""; $.each(result, function (index, value) { resp += "" + value + ""; }); } else { resp = result } resp = '
' + resp + '
'; $(element).append(resp); }; var add_errors = function (form, errors) { $.each(errors, function (name, value) { var input = $(form).find("[name*='" + name + "']"); $(input).addClass("invalid"); if ($(input).parent().children('.error-message').length == 0) { $(input).parent().append("" + value + ""); } else { $('.error-message').show(); } }); }; var clear_deafult_message = function (form) { var message_box = $(form).parent().find(".messages")[0]; var default_message = $(message_box).find(".default_message"); var custom_message = $(message_box).find(".custom_message"); if ($(default_message).length > 0) { $(default_message).remove(); } if ($(custom_message).length == 0) { $(message_box).html(""); } } var add_loader = function (form) { if ($(form).children('.ajax_forms_loader').length == 0) { $(form).append('
'); } else { $(form).children('.ajax_forms_loader').fadeIn(); } } var hide_loader = function (form) { $(form).children('.ajax_forms_loader').fadeOut(); } var submit_function = function (form, method, action) { $(form).trigger("submitted"); add_loader(form); if ($(form).find("[name='_dontcare']").length < 1) { $(form).append("") } var formdata = new FormData($(form)[0]) $.ajax({ method: method, url: action, dataType: "json", data: formdata, contentType: false, processData: false, success: function (response, status, xhr) { clear_deafult_message(form); var message_box = $(form).parent().find(".messages")[0]; if (!$(message_box).find(".custom_message").length > 0) { print_results(message_box, response.messages); } $(form).trigger("successed"); $(message_box).addClass('ok'); $(form).trigger("reset"); }, error: function (xhr, status, errorThrown) { var response = JSON.parse(xhr.responseText); var message_box = $(form).parent().find(".messages")[0]; clear_deafult_message(form); print_results(message_box, response.messages); $(form).trigger("failed", [xhr, status, errorThrown]); $(message_box).addClass('ko'); add_errors(form, response.errors); }, complete: function () { if (!recaptha_disabled) { grecaptcha.reset(); } $(form).find("input[type='submit']").attr("disabled", false); hide_loader(form); } }); } load_invisible_recaptcha = function () { $(document).ready(function () { $(".ajax_forms").each(function (element, index) { var form = this; var action = $(this).attr("action"); var method = $(this).attr("method"); var submit_button = $(form).find(".submit").last(); if (!recaptha_disabled) { var container_div = $('
'); $(submit_button).before(container_div); var widget_id = grecaptcha.render(container_div[0], { 'sitekey': '<%= Recaptcha.configuration.site_key! %>', 'badge': 'inline', 'size': 'invisible', 'callback': function () { submit_function(form, method, action); } }); $(form).data("widget-id", widget_id) } $(form).children('.ajax_forms_loader').fadeOut(); }); $(document).on("submit", ".ajax_forms", function (event) { event.preventDefault ? event.preventDefault() : (event.returnValue = false); var form = $(event.target);//.closest("form"); var action = $(form).attr("action"); var method = $(form).attr("method"); var widget_id = $(form).data("widget-id"); // disable all submit buttons $(form).find("input[type='submit']").attr("disabled", true) if (!recaptha_disabled) { grecaptcha.execute(widget_id) } else { submit_function(form, method, action); } }); $('.ajax_forms').on('change', '.invalid', function () { $(this).removeClass("invalid"); $(this).parent().children('.error-message').hide(); }); }); }; $.getScript("//www.google.com/recaptcha/api.js?onload=load_invisible_recaptcha&render=explicit") })(jQuery);