(function(){
    var script = document.currentScript;

    // If there's a script
    if(typeof script === 'object' && script)
    {
        // If the script has a URL
        if(typeof script.src === 'string' && script.src.length > 0)
        {
            try{
                // Decode the form data from the last directory in the script URL
                var formData = script.src.match(/([A-Za-z0-9-_]+)\/?$/),
                    formData = Array.isArray(formData) ? formData[1] : null,
                    formData = typeof formData === 'string' ? atob(formData) : null,
                    formData = typeof formData === 'string' ? JSON.parse(formData) : null;

                // If the form data is now an object
                if(typeof formData === 'object' && formData)
                {
                    var formUrl = formData.url;

                    // If we have a form URL
                    if(typeof formUrl === 'string' && formUrl.length > 0)
                    {
                        // Build the iframe
                        var iFrame = document.createElement('iframe');
                            iFrame.src = formUrl;
                            iFrame.height = '100%';
                            iFrame.width = '100%';
                            iFrame.style.border = 'none';
                        
                        // Handle updating the iframe's height on message
                        window.addEventListener('message', function(e){
                            var name = e.data.name,
                                height = e.data.height;
                            
                            if(name === 'stFormHeight')
                            {
                                if(!isNaN(height))
                                {
                                    iFrame.height = height + 'px';
                                }
                            }
                        });

                        // Replace the script with the iframe
                        script.replaceWith(iFrame);
                    }
                    else
                    {
                        console.log('Missing form URL.');
                    } 
                }
                else
                {
                    console.log('Missing form data.');
                }
            }
            catch(error)
            {
                console.log(error);
            }
        }
        else
        {
            console.log('Unknown script src.');
        }
    }
    else
    {
        console.log('Unknown script.');
    }
})();