

<script>
document.addEventListener('DOMContentLoaded', function () {

    const eventDate = document.querySelector(
        '[name="twib_event_date"]'
    );

    const comingStart = document.querySelector(
        '[name="twib_coming_start"]'
    );

    const comingWeeks = document.getElementById(
        'twib_coming_weeks'
    );


    /*
     * Only Coming Soon promotions have
     * Start Week and Weeks fields.
     */
    if (!eventDate || !comingStart || !comingWeeks) {
        return;
    }


    /*
     * Get the Tuesday that starts the TWIB week.
     */
    function getTuesday(date) {

        const d = new Date(date);

        while (d.getDay() !== 2) {
            d.setDate(d.getDate() - 1);
        }

        d.setHours(12, 0, 0, 0);

        return d;
    }


    /*
     * Rebuild the number-of-weeks dropdown.
     */
    function buildWeeks() {

        if (!eventDate.value || !comingStart.value) {
            return;
        }


        const eventWeek =
            getTuesday(eventDate.value);

        const startWeek =
            getTuesday(comingStart.value);


        const diffWeeks = Math.round(
            (eventWeek - startWeek) /
            (1000 * 60 * 60 * 24 * 7)
        );


        /*
         * Coming Soon cannot run during
         * the week of the event.
         */
        const weeks = diffWeeks - 1;


        const currentValue =
            comingWeeks.value;


        comingWeeks.innerHTML = '';


        if (weeks < 1) {

            comingWeeks.innerHTML =
                '<option value="">No weeks available</option>';

            return;
        }


        /*
         * Put the longest available
         * promotion first.
         */
        for (let i = weeks; i >= 1; i--) {

            const option =
                document.createElement('option');

            option.value = i;

            option.textContent = i;


            /*
             * Keep the existing value if
             * it is still available.
             */
            if (
                parseInt(currentValue, 10) === i
            ) {

                option.selected = true;
            }


            comingWeeks.appendChild(option);
        }

    }


    /*
     * Recalculate when the Start Week changes.
     */
    comingStart.addEventListener(
        'change',
        buildWeeks
    );


    /*
     * Recalculate when the Event Date changes.
     */
    eventDate.addEventListener(
        'change',
        buildWeeks
    );


    /*
     * Build the correct list when
     * the Edit Promotion page loads.
     */
    buildWeeks();

});
</script>


<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://italianpride.com/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>http://italianpride.com/wp-sitemap-posts-post-1.xml</loc></sitemap><sitemap><loc>http://italianpride.com/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>http://italianpride.com/wp-sitemap-taxonomies-category-1.xml</loc></sitemap><sitemap><loc>http://italianpride.com/wp-sitemap-taxonomies-post_tag-1.xml</loc></sitemap><sitemap><loc>http://italianpride.com/wp-sitemap-users-1.xml</loc></sitemap></sitemapindex>
