<script>
    $(function() {
        var waitIntgrtn = setInterval(function() {
            if (!window.intgrtn) {
                return false;
            }
            clearInterval(waitIntgrtn);
            window.intgrtn.getCurrentLocation(function(response) {
                if (response.data && response.data.countryIsoCode && response.data.country) {
                    var customEvent = new CustomEvent("visitorLocated", {
                        detail: {
                            continent: response.data.continent,
                            country: response.data.country,
                            isoCode: response.data.countryIsoCode,
                        }
                    });
                    document.dispatchEvent(customEvent);
                    $.ajax({
                        url: 'i18n/' + response.data.countryIsoCode.toLowerCase() + ".json",
                        type: "GET",
                        success: function(t) {
                            document.dispatchEvent(new CustomEvent("translationsLoaded", {
                                detail: t
                            }));
                            translations = t;
                            applyTranslations();
                            document.dispatchEvent(new CustomEvent("translationsApplied", {
                                detail: t
                            }))
                        },
                        error: function(t) {
                            var currentIso = response.data.countryIsoCode.toLowerCase(),
                                targetIso = "default";

                            switch (currentIso) {
                                case "mx":
                                case "cl":
                                case "co":
                                case "ar":
                                case "uy":
                                case "cr":
                                case "ec":
                                case "pa":
                                case "pe":
                                case "ve":
                                    targetIso = "es";
                            }

                            $.ajax({
                                url: "i18n/" + targetIso + ".json",
                                type: "get",
                                success: function(t) {
                                    document.dispatchEvent(new CustomEvent("translationsLoaded", {
                                        detail: t
                                    }));
                                    translations = t;
                                    applyTranslations();
                                    document.dispatchEvent(new CustomEvent("translationsApplied", {
                                        detail: t
                                    }))
                                },
                                error: function(t) {
                                    console.error(t)
                                }
                            })
                        },
                        complete: function() {
                            $("body").removeClass("hidden")
                        }
                    });
                }
            });

            window.intgrtn.events.on('form.signup.success', function(form, response) {
                window.intgrtn.store("signupRequestID", response.data.signupRequestID);
                window.intgrtn.store("brokerName", response.data.broker.name);
                window.intgrtn.store("brokerLogo", response.data.broker.logo);
            });
        }, 10);
    });
</script>
</body>
</html>