function setCookie(cname, cvalue, exdays) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays*24*60*60*1000));
        var expires = "expires="+d.toUTCString();
        document.cookie = cname + "=" + cvalue + ";secure" + expires;
    }
    function getCookie(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for(var i=0; i<ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1);
            if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
        }
        return "";
    }

    // define the new script element for gtm_call.js
    var gtm_call = document.createElement('script');
    gtm_call.type = 'text/javascript';
    gtm_call.src = 'https://www.crowdstrike.com/wp-content/custom_js/gtm_call.js';

    // var bizible_call = document.createElement('script');
    // bizible_call.type = 'text/javascript';
    // bizible_call.src = '//cdn.bizible.com/scripts/bizible.js';

    $( document ).ready(function() {

        $('#opt-in-btn').click(function(){
            // toggle cookies via gtm
            $('head').append(gtm_call);
            // $('head').append(bizible_call);
            // hide opt-in message
            // $('#optInMessage').fadeOut(666);
            $('#optInMessage').hide();
            // save the user's choice in a cookie
            setCookie('tracking', 'on', '365');
        });

    });

    var trackingChoice = getCookie('tracking');

    if (trackingChoice == 'on') {
        $('head').append(gtm_call);
        // $('head').append(bizible_call);
    }

    if (trackingChoice != 'on') {

        jQuery.ajax({ 
            url: 'https://api.ipstack.com/check?access_key=c4145bb60c6eaa1379ba0a6589da27de&legacy=1', 
            type: 'POST', 
            dataType: 'jsonp',
            success: function(location) {
                var $userLocation = location.country_code;
                var $europeanUnion = [
                    'AT', 'BE', 'BG',
                    'HR', 'CY', 'CZ',
                    'DE', 'DK', 'EE',
                    'FI', 'FR', 'GR',
                    'HU', 'IE', 'IT',
                    'LV', 'LT', 'LU',
                    'MT', 'NL', 'PL',
                    'PT', 'RO', 'SK',
                    'SI', 'ES', 'SE',
                    'GB',
                ];

                // if userlocation in europeanunion: do stuff
                var found = $.inArray($userLocation, $europeanUnion);
                if (found > -1) {
                    $('#optInMessage').show();
                }
                else {
                    $('#optInMessage').hide();

                    // AND FIRE ALL TAGS SEND THE EVENT TRIGGER
                    $('head').append(gtm_call);
                    // $('head').append(bizible_call);
                    // set tracking cookie
                    setCookie('tracking', 'on', '365');
                }
            }
        });
    }