<style>
    .download-email-modal form {font-size:14px !important; width:auto !important; margin:10px 20px;}
    .download-email-modal label {width:125px !important;}
    .download-email-modal .mktoOffset {display: none;}
    .download-email-modal .mktoFormCol, .download-email-modal .mktoFieldWrap {width:100%;}
    .download-email-modal input {width:250px !important; padding:5px !important; margin-bottom:10px !important;}
    .download-email-modal .mktoButtonWrap {margin-left:135px !important;}
    .download-email-modal button[type="submit"] {background-image:none !important; background-color:#0166AE !important; border-radius:4px !important; border-color:#2e6da4 !important;}
    .download-email-modal .privacy-notice {background-color:#eee; padding:20px; border-radius:10px; margin:20px; font-size:13px;}
</style>

<div class="modal fade download-email-modal" id="download-email-modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Download</h4>
            </div>
            <div class="modal-body">
                <form id="mktoForm_2997"></form>
                <p class="text-center" style="margin-top:10px;"><a class="skip-to-download" href="">Skip to download</a></p>
            </div>
        </div>
    </div>
</div>

<script>
    window.addEventListener('load', function(){
        window.Redgate.Marketo.create({
            formId: 2997,
            callback: function(marketoForm) {
                marketoForm.onValidate(function(successful) {
                    if (!successful) {
                        marketoForm.submittable(false);
                    } else {

                        // Do some custom validation.

                        // Get the fields and their values from the form.
                        var fields = marketoForm.vals();

                        // Custom object for storing info about the fail.
                        var fail = {
                            isFail: false,
                            message: '',
                            element: null,
                        };

                        // Email validation.
                        if (typeof fields.Email !== 'undefined') {

                            // Email regex provided by https://regex101.com/r/L9Z2N0/1.
                            // Check that the format is {something}@{something}.{something}.
                            var emailRegex = /\S+@\S+\.\S+/;

                            if (emailRegex.test(fields.Email) === false) {
                                fail.isFail = true;
                                fail.message = 'Please enter a valid email address.';
                                fail.element = marketoForm.getFormElem().find('input[name="Email"]');
                            }
                        }

                        // If form validation fails.
                        if (fail.isFail) {

                            // Stop the form from being submittable.
                            marketoForm.submittable(false);

                            // Show an error message against the invalid field.
                            marketoForm.showErrorMessage(fail.message, fail.element);

                            // Display the field as invalid using the Marketo class.
                            fail.element.get(0).classList.add('mktoInvalid');
                        } else {

                            // All is good, continue as normal.
                            marketoForm.submittable(true);
                        }
                    }
                });

                // On successful submission of the form, redirect the user to the relevant download page.
                marketoForm.onSuccess(function() {
                    var downloadLink = document.querySelector('.download-email-modal .skip-to-download');

                    if (downloadLink) {
                        
                        // Redirect the user to the download page.
                        window.location.href = downloadLink;

                        // Return false to stop the form from reloading the page.
                        return false;
                    }
                });
            },
        });
    });

    function updateModalVersion (e) {
        var downloadUrl = e.target.getAttribute('data-download-url');
        var downloadLink = document.querySelector('.download-email-modal .skip-to-download');
        downloadLink.href = downloadUrl;
    }
</script>