// GOOGLE RECAPTCHA
if (typeof grecaptcha_data !== "undefined") {
    var reCaptchaIDs = [];
    var site_key = grecaptcha_data.site_key;
    var secret_key = grecaptcha_data.secret_key;
    var position_badge = grecaptcha_data.position_badge;
    
    grecaptcha.ready(function() {
        grecaptcha.execute(site_key, {action: 'homepage'}).then(function(token) {
            var forms = document.getElementsByTagName( 'form' );

            for ( var i = 0; i < forms.length; i++ ) {
                var fields = forms[ i ].getElementsByTagName( 'input' );

                for ( var j = 0; j < fields.length; j++ ) {
                    var field = fields[ j ];

                    if ( 'g-recaptcha-response' === field.getAttribute( 'name' ) ) {
                        field.setAttribute( 'value', token );
                        break;
                    }
                }
            }
        });
    });

    window.conikalReCaptchaLoad = conikalReCaptchaLoad;
    var conikalReCaptchaLoad = function() {
        jQuery( '.conikal_reCaptcha' ).each( function( index, el ) {
            var tempID = grecaptcha.render( el, {
                'sitekey' : site_key,
                'size': 'invisible',
                'badge' : position_badge,
                'callback' : conikalReCaptchaCallback,
            } );
            reCaptchaIDs.push( tempID );
        } );
    }

    //Reset reCaptcha
    var conikalReCaptchaReset = function() {
        grecaptcha.execute(site_key, {action: 'homepage'}).then(function(token) {
            var forms = document.getElementsByTagName( 'form' );

            for ( var i = 0; i < forms.length; i++ ) {
                var fields = forms[ i ].getElementsByTagName( 'input' );

                for ( var j = 0; j < fields.length; j++ ) {
                    var field = fields[ j ];

                    if ( 'g-recaptcha-response' === field.getAttribute( 'name' ) ) {
                        field.setAttribute( 'value', token );
                        break;
                    }
                }
            }
        });
    };

    // Callback token
    var conikalReCaptchaCallback = function(token) {
        console.log(token)
    }
}