var headContent = "<!-- FORM: HEAD SECTION --><head>\n    <meta charset=\"UTF-8\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    <meta name=\"referrer\" content=\"no-referrer-when-downgrade\">\n    <title><\/title>\n                                        <link href=\"https:\/\/aspeninstitute.tfaforms.net\/dist\/form-builder\/5.0.0\/wforms-layout.css?v=1762906439\" rel=\"stylesheet\" type=\"text\/css\">\n                                                <link href=\"https:\/\/aspeninstitute.tfaforms.net\/dist\/form-builder\/5.0.0\/wforms-jsonly.css?v=1762906439\" rel=\"stylesheet\" type=\"text\/css\">\n                                                <link href=\"https:\/\/aspeninstitute.tfaforms.net\/uploads\/themes\/theme-81185.css?v=1762906439\" rel=\"stylesheet\" type=\"text\/css\">\n                                                        <script src=\"https:\/\/aspeninstitute.tfaforms.net\/js\/FA__DOMContentLoadedEventDispatcher.js?v=1762906439\" type=\"text\/javascript\"><\/script>\n                                                <script src=\"https:\/\/aspeninstitute.tfaforms.net\/wForms\/3.11\/js\/wforms.js?v=1762906439\" type=\"text\/javascript\"><\/script>\n                                                <script src=\"https:\/\/aspeninstitute.tfaforms.net\/wForms\/3.11\/js\/localization-en_US.js?v=1762906439\" type=\"text\/javascript\"><\/script>\n                                                <script src=\"https:\/\/aspeninstitute.tfaforms.net\/wForms\/3.11\/js\/wforms_prefill.js?v=1762906439\" type=\"text\/javascript\"><\/script>\n                                                <script src=\"https:\/\/aspeninstitute.tfaforms.net\/dist\/jquery\/jquery.0416573c648d279fd758.js?v=1762906439\" type=\"text\/javascript\"><\/script>\n                                                <script src=\"https:\/\/aspeninstitute.tfaforms.net\/js\/typeahead\/v1.2.0\/typeahead.bundle.js?v=1762906439\" type=\"text\/javascript\"><\/script>\n                                                <script src=\"https:\/\/aspeninstitute.tfaforms.net\/dist\/analytics\/data-collector.e86d94bd97a6d88cf5db.js?v=1762906439\" type=\"text\/javascript\"><\/script>\n                                                <script src=\"https:\/\/aspeninstitute.tfaforms.net\/api_v2\/sst\/wf-quick-publish?v=1762906439\" type=\"text\/javascript\"><\/script>\n                            \n    <\/head>";
var bodyContent = "<!-- FORM: BODY SECTION --><body class=\"default wFormWebPage\">\n\n    \n\n<div id=\"tfaContent\">\n\n            <div class=\"wFormContainer\">\n    <div class=\"wFormHeader\"><\/div>\n\n    \n    <style type=\"text\/css\"><\/style><div class=\"\"><div class=\"wForm\" id=\"5146611-WRPR\" data-language=\"en_US\" dir=\"ltr\">\n<div class=\"codesection\" id=\"code-5146611\"><\/div>\n<form method=\"post\" action=\"https:\/\/aspeninstitute.tfaforms.net\/api_v2\/workflow\/processor\" class=\"hintsBelow labelsAbove\" id=\"5146611\">\n<div class=\"oneField field-container-D    \" id=\"tfa_1-D\">\n<label id=\"tfa_1-L\" class=\"label preField reqMark\" for=\"tfa_1\">First Name<\/label><br><div class=\"inputWrapper\"><input aria-required=\"true\" type=\"text\" id=\"tfa_1\" name=\"tfa_1\" value=\"\" title=\"First Name\" class=\"required\"><\/div>\n<\/div>\n<div class=\"oneField field-container-D    \" id=\"tfa_2-D\">\n<label id=\"tfa_2-L\" class=\"label preField reqMark\" for=\"tfa_2\">Last Name<\/label><br><div class=\"inputWrapper\"><input aria-required=\"true\" type=\"text\" id=\"tfa_2\" name=\"tfa_2\" value=\"\" title=\"Last Name\" class=\"required\"><\/div>\n<\/div>\n<div class=\"oneField field-container-D    \" id=\"tfa_3-D\">\n<label id=\"tfa_3-L\" class=\"label preField reqMark\" for=\"tfa_3\">Email<\/label><br><div class=\"inputWrapper\"><input aria-required=\"true\" type=\"text\" id=\"tfa_3\" name=\"tfa_3\" value=\"\" title=\"Email\" class=\"validate-email required\"><\/div>\n<\/div>\n<div id=\"tfa_17\" class=\"section inline group\">\n<div class=\"oneField field-container-D    \" id=\"tfa_4-D\">\n<label id=\"tfa_4-L\" class=\"label preField reqMark\" for=\"tfa_4\">Zip Code<\/label><br><div class=\"inputWrapper\"><input aria-required=\"true\" type=\"text\" id=\"tfa_4\" name=\"tfa_4\" value=\"\" title=\"Zip Code\" class=\"validate-integer required\"><\/div>\n<\/div>\n<div class=\"oneField field-container-D    \" id=\"tfa_9-D\">\n<label id=\"tfa_9-L\" class=\"label preField \" for=\"tfa_9\">What Center for Native American Youth programs are you most interested in? <i>(You may select multiple)<\/i><\/label><br><div class=\"inputWrapper\"><select id=\"tfa_9\" multiple name=\"tfa_9[]\" title=\"What Center for Native American Youth programs are you most interested in? (You may select multiple)\" class=\"\"><option value=\"\">Please select...<\/option>\n<option value=\"tfa_10\" id=\"tfa_10\" class=\"\">Brave Heart Fellowship (environment\/food sovereignty)<\/option>\n<option value=\"tfa_11\" id=\"tfa_11\" class=\"\">Building Communities of Hope (child welfare\/foster care)<\/option>\n<option value=\"tfa_12\" id=\"tfa_12\" class=\"\">California Native Youth Collective<\/option>\n<option value=\"tfa_13\" id=\"tfa_13\" class=\"\">Champions for Change<\/option>\n<option value=\"tfa_14\" id=\"tfa_14\" class=\"\">Creative Native (art)<\/option>\n<option value=\"tfa_15\" id=\"tfa_15\" class=\"\">Democracy is Indigenous (civic engagement)<\/option>\n<option value=\"tfa_16\" id=\"tfa_16\" class=\"\">Remembering Our Sisters (MMIP)<\/option><\/select><\/div>\n<\/div>\n<\/div>\n<div class=\"htmlSection\" id=\"tfa_7\"><div class=\"htmlContent\" id=\"tfa_7-HTML\"><i>By submitting this form,\u00a0<\/i><i>I agree that the Aspen Institute can collect, process,\nand retain limited personal information for administrative purposes; and that\nthe Institute can share this limited personal information with its trusted\nservice providers for program administration purposes. I consent to receiving\nemail communications from the Center for Native American Youth at the Aspen\nInstitute. I understand I can withdraw my consent at any time by contacting <\/i><a title=\"mailto:DataPrivacyLegal@aspeninstitute.org\" href=\"mailto:DataPrivacyLegal@aspeninstitute.org\" style=\"background-color: rgb(255, 255, 255);\"><i>DataPrivacyLegal@aspeninstitute.org<\/i><\/a><i>.<br><\/i><i><br>I agree to the Aspen Institute's\n<\/i><a title=\"https:\/\/www.aspeninstitute.org\/terms-of-use\/\" href=\"https:\/\/www.aspeninstitute.org\/terms-of-use\/\" style=\"background-color: rgb(255, 255, 255);\"><i>Terms of Use Agreement <\/i><\/a><i>and\nacknowledge that I have read and understand the Aspen Institute <\/i><a title=\"https:\/\/www.aspeninstitute.org\/privacy\/\" href=\"https:\/\/www.aspeninstitute.org\/privacy\/\" style=\"background-color: rgb(255, 255, 255);\"><i>Privacy Policy <\/i><\/a><i>which\nprovides additional information about how the Institute processes personal\ninformation, as well as rights I may have regarding the personal information\nthe Institute maintains about me.<\/i><\/div><\/div>\n<input type=\"hidden\" id=\"tfa_6\" name=\"tfa_6\" value=\"CNAY1000\" class=\"\"><input type=\"hidden\" id=\"tfa_8\" name=\"tfa_8\" value=\"TRUE\" class=\"\"><div class=\"actions\" id=\"5146611-A\" data-contentid=\"submit_button\"><input type=\"submit\" data-label=\"Subscribe\" class=\"primaryAction\" id=\"submit_button\" value=\"Subscribe\"><\/div>\n<div style=\"clear:both\"><\/div>\n<input type=\"hidden\" value=\"952-259c274471b0b9d7a1bd4bd958677532\" name=\"tfa_dbCounters\" id=\"tfa_dbCounters\" autocomplete=\"off\"><input type=\"hidden\" value=\"5146611\" name=\"tfa_dbFormId\" id=\"tfa_dbFormId\"><input type=\"hidden\" value=\"\" name=\"tfa_dbResponseId\" id=\"tfa_dbResponseId\"><input type=\"hidden\" value=\"829f245e512cdcbd208904b45f9650c8\" name=\"tfa_dbControl\" id=\"tfa_dbControl\"><input type=\"hidden\" value=\"\" name=\"tfa_dbWorkflowSessionUuid\" id=\"tfa_dbWorkflowSessionUuid\"><input type=\"hidden\" value=\"15\" name=\"tfa_dbDataflowId\" id=\"tfa_dbDataflowId\"><input type=\"hidden\" value=\"1762906439\" name=\"tfa_dbTimeStarted\" id=\"tfa_dbTimeStarted\" autocomplete=\"off\"><input type=\"hidden\" value=\"15\" name=\"tfa_dbVersionId\" id=\"tfa_dbVersionId\"><input type=\"hidden\" value=\"1\" name=\"startWorkflow\" id=\"startWorkflow\"><input type=\"hidden\" value=\"\" name=\"tfa_switchedoff\" id=\"tfa_switchedoff\">\n<\/form>\n<\/div><\/div><div class=\"wFormFooter\"><p class=\"supportInfo\"><a target=\"new\" class=\"contactInfoLink\" href=\"https:\/\/aspeninstitute.tfaforms.net\/forms\/help\/5146611\" data-testid=\"contact-info-link\">Contact Information<\/a><br><\/p><\/div>\n\n<\/div>\n\n\n            <script id=\"analytics-collector-script\" type=\"text\/javascript\" src=\"https:\/\/aspeninstitute.tfaforms.net\/dist\/analytics\/data-collector.e86d94bd97a6d88cf5db.js\" data-customer-id=\"125945\" data-endpoint=\"https:\/\/analytics.formassembly.com\/v1\/traces\"><\/script>\n\n    \n<script type=\"text\/javascript\" data-for=\"FA__DOMContentLoadedEventDispatch\" src=\"https:\/\/aspeninstitute.tfaforms.net\/js\/FA__DOMContentLoadedEventDispatcher.js\" defer><\/script>\n\n\n<script type=\"text\/javascript\">\n    document.addEventListener(\"FA__DOMContentLoaded\", function(){\n        const FORM_TIME_START = Math.floor((new Date).getTime()\/1000);\n        let formElement = document.getElementById(\"tfa_0\");\n        if (null === formElement) {\n            formElement = document.getElementById(\"0\");\n        }\n        let appendJsTimerElement = function(){\n            let formTimeDiff = Math.floor((new Date).getTime()\/1000) - FORM_TIME_START;\n            let cumulatedTimeElement = document.getElementById(\"tfa_dbCumulatedTime\");\n            if (null !== cumulatedTimeElement) {\n                let cumulatedTime = parseInt(cumulatedTimeElement.value);\n                if (null !== cumulatedTime && cumulatedTime > 0) {\n                    formTimeDiff += cumulatedTime;\n                }\n            }\n            let jsTimeInput = document.createElement(\"input\");\n            jsTimeInput.setAttribute(\"type\", \"hidden\");\n            jsTimeInput.setAttribute(\"value\", formTimeDiff.toString());\n            jsTimeInput.setAttribute(\"name\", \"tfa_dbElapsedJsTime\");\n            jsTimeInput.setAttribute(\"id\", \"tfa_dbElapsedJsTime\");\n            jsTimeInput.setAttribute(\"autocomplete\", \"off\");\n            if (null !== formElement) {\n                formElement.appendChild(jsTimeInput);\n            }\n        };\n        if (null !== formElement) {\n            if(formElement.addEventListener){\n                formElement.addEventListener('submit', appendJsTimerElement, false);\n            } else if(formElement.attachEvent){\n                formElement.attachEvent('onsubmit', appendJsTimerElement);\n            }\n        }\n    });\n<\/script>\n\n<script type=\"text\/javascript\">\n    if(wFORMS.behaviors.prefill) wFORMS.behaviors.prefill.skip = true;\n<\/script>\n\n\n\n    <\/div>\n\n\n    \n\n\n\n<\/body>";
var resumeUrl = null;
var baseUrl = "https:\/\/aspeninstitute.tfaforms.net";

/**
 * Main entry point, called when the DOM is loaded.
 */
function loadFormAssemblyFormHeadAndBodyContents() {
    var targetElement = getTargetElement();
    var formBodyWrapper = createFormBodyWrapper();

    injectHeadContent();
    injectBodyContent(formBodyWrapper, targetElement);
    addResumeLink();

    document.addEventListener('FA__DOMContentLoaded', function () {
        executeBodyScripts(formBodyWrapper);
        
        // Set up FA$ for autosuggest behavior (jQuery in noConflict mode)
        if (typeof $ !== 'undefined' && $.noConflict) {
            window.FA$ = $.noConflict(true);
        }
        
        // Apply prefill behavior to enable URL parameter prefilling
        var forms = document.querySelectorAll('form');
        forms.forEach(function(form) {
            if (wFORMS && wFORMS.behaviors && wFORMS.behaviors.prefill) {
                wFORMS.behaviors.prefill.skip = false;
                wFORMS.behaviors.prefill.applyTo(form);
            }
        });
    });
}
window.addEventListener('DOMContentLoaded', loadFormAssemblyFormHeadAndBodyContents);

/**
 * Finds the target DOM element where the form should be injected.
 * @returns {HTMLElement|null} The target element or null if not found.
 */
function getTargetElement() {
    var quickPublishScript = document.querySelector('script[data-qp-target-id]');
    if (quickPublishScript) {
        var elementID = quickPublishScript.getAttribute('data-qp-target-id');
        return document.getElementById(elementID);
    }
    return null;
}

/**
 * Creates the main wrapper div for the form's body content.
 * @returns {HTMLDivElement}
 */
function createFormBodyWrapper() {
    var bodyWrapperDiv = document.createElement('div');
    bodyWrapperDiv.id = "FA__QP__BODYWRAPPERDIV";
    return bodyWrapperDiv;
}

/**
 * Injects the form's header content (styles, scripts) into the main document.
 */
function injectHeadContent() {
    var head = document.createElement('head');
    head.innerHTML = headContent;

    var headElements = Array.from(head.children);
    var scriptElements = headElements.filter(function(el) { return el.nodeName === 'SCRIPT'; });
    var otherElements = headElements.filter(function(el) { return el.nodeName !== 'SCRIPT'; });

    otherElements.forEach(function(el) { document.head.appendChild(el); });
    loadHeadScriptsSequentially(scriptElements);
}

/**
 * Loads header script tags sequentially to maintain order.
 * @param {HTMLScriptElement[]} scriptElements - An array of script elements to load.
 */
function loadHeadScriptsSequentially(scriptElements) {
    var script = scriptElements.shift();
    if (!script) {
        document.dispatchEvent(new Event('FA__DOMContentLoaded'));
        return;
    }

    var newScript = document.createElement('script');
    Array.from(script.attributes).forEach(function(attr) { newScript.setAttribute(attr.name, attr.value); });
    newScript.innerHTML = script.innerHTML;

    if (script.src) {
        newScript.onload = function() { loadHeadScriptsSequentially(scriptElements); };
        document.head.appendChild(newScript);
    } else {
        document.head.appendChild(newScript);
        loadHeadScriptsSequentially(scriptElements);
    }
}

/**
 * Injects the form's body content into the target element.
 * @param {HTMLDivElement} formBodyWrapper - The div that will contain the form.
 * @param {HTMLElement|null} targetElement - The element to inject the form into.
 */
function injectBodyContent(formBodyWrapper, targetElement) {
    var body = document.createElement('body');
    body.innerHTML = bodyContent;

    Array.from(body.children).forEach(function(el) { formBodyWrapper.appendChild(el); });

    if (targetElement) {
        targetElement.appendChild(formBodyWrapper);
    } else {
        document.body.appendChild(formBodyWrapper);
    }
}

/**
 * If a resume URL is provided, it finds the "Save and Resume" containers
 * and injects the resume link.
 */
function addResumeLink() {
    if (!resumeUrl) return;

    var oneFieldContainers = document.querySelectorAll('.wForm .saveAndResume > .oneField');
    oneFieldContainers.forEach(function (container) {
        var separator = document.createTextNode(' | ');

        var resumeLink = document.createElement('a');
        resumeLink.href = baseUrl + resumeUrl;
        resumeLink.innerHTML = 'Resume a previously saved form';
        
        resumeLink.addEventListener('click', function(event) {
            if (!confirm('Are you sure you want to resume a previous response? Any information you just entered on this page will be discarded.')) {
                event.preventDefault();
            }
        });

        container.appendChild(separator);
        container.appendChild(resumeLink);
    });
}

/**
 * Finds and re-executes script tags from the form's body.
 * @param {HTMLDivElement} formBodyWrapper - The wrapper containing the form.
 */
function executeBodyScripts(formBodyWrapper) {
    var scriptTags = formBodyWrapper.querySelectorAll('script');
    scriptTags.forEach(function (scriptTag) {
        // Skip jQuery, Typeahead, Kalendae, and Calendar scripts that are already loaded in the head
        if (scriptTag.src && (
            scriptTag.src.includes('jquery.js') || 
            scriptTag.src.includes('typeahead.bundle.js') ||
            scriptTag.src.includes('/js/kalendae/') ||
            scriptTag.src.includes('wforms_calendar.js')
        )) {
            return;
        }
        
        // Skip noConflict scripts that are meant for jQuery/Typeahead
        if (scriptTag.innerHTML && scriptTag.innerHTML.includes('noConflict')) {
            return;
        }
        
        var script = document.createElement('script');
        Array.from(scriptTag.attributes).forEach(function(attr) { script.setAttribute(attr.name, attr.value); });
        script.innerHTML = scriptTag.innerHTML;
        scriptTag.parentNode.replaceChild(script, scriptTag);
    });
}
