var _____WB$wombat$assign$function_____=function(name){return (self._wb_wombat && self._wb_wombat.local_init && self._wb_wombat.local_init(name))||self[name];};if(!self.__WB_pmw){self.__WB_pmw=function(obj){this.__WB_source=obj;return this;}}{
let window = _____WB$wombat$assign$function_____("window");
let self = _____WB$wombat$assign$function_____("self");
let document = _____WB$wombat$assign$function_____("document");
let location = _____WB$wombat$assign$function_____("location");
let top = _____WB$wombat$assign$function_____("top");
let parent = _____WB$wombat$assign$function_____("parent");
let frames = _____WB$wombat$assign$function_____("frames");
let opens = _____WB$wombat$assign$function_____("opens");
var onloadCallback = function() {
    const containers = document.querySelectorAll('.recaptcha-container');
        for (let i = 0; i < containers.length; i++) {
            containers[i].closest('form').setAttribute('data-captcha', `${i}`)
            grecaptcha.render(containers[i], {
            'sitekey' : '6Le1yZInAAAAAAWv6apYolVMnRq3jx04fiTs80D0',
            'theme' : 'light'
        })
    }
};

const forms = document.querySelectorAll('form.protected-form');
forms.forEach(form => {
    form.addEventListener('submit', (e) => {

        console.log(e.target)
        // first, validate the fields
        const thisForm = e.target;
        if (!thisForm.checkValidity()) {
            e.preventDefault();
            e.stopPropagation()
        } else {
            e.preventDefault();
            e.stopPropagation();

            // find the nearest captcha widget div
            const id = e.target.dataset.captcha;
            const captchaResponse = grecaptcha.getResponse(id);
    
            if (!captchaResponse.length > 0) {
                alert('You have to complete captcha to proceed!')
                throw new Error('Captcha incomplete')
            }
    
            const params = new URLSearchParams()
            params.append('g-captcha', captchaResponse)
    
            fetch(`https://preschoolfinder.net/captcha-verification`, {
                method: 'POST',
                body: params
            })
            .then(res => res.json())
            .then(data => {
                if (data.captchaSuccess) {

                    return e.target.submit()
                }
            })
            .catch(err => console.log(err))
            
        }
        form.classList.add('was-validated')
        
        // e.stopPropagation();
        // e.preventDefault();
        
    })
})
}