{% load l10n %}
<script type="text/javascript">
    window.addEventListener("load", function() {
        (function($) {
            var field;

            {% for field in prepopulated_fields %}
                field = {
                    id: '#{{ field.field.auto_id }}',
                    dependency_ids: [],
                    dependency_list: [],
                    maxLength: {{ field.field.field.max_length|default_if_none:"50"|unlocalize }}
                };

                {% for dependency in field.dependencies %}
                    field['dependency_ids'].push('#{{ dependency.auto_id }}');
                    field['dependency_list'].push('{{ dependency.name }}');
                {% endfor %}

            {% comment %}
            Mark prepopulated fields in the main form and stacked inlines (.empty-form .form-row) and in tabular inlines (.empty-form.form-row)
            {% endcomment %}
                $('.empty-form .form-row .field-{{ field.field.name }}, .empty-form.form-row .field-{{ field.field.name }}').addClass('prepopulated_field');
                $(field.id).data('dependency_list', field['dependency_list'])
                    .prepopulate(field['dependency_ids'], field.maxLength);
            {% endfor %}
        })($);
    });
</script>
