document.addEventListener('DOMContentLoaded', function() {
    const popup = document.getElementById('paymentPopup');
    const closeBtn = document.getElementById('closePopup');
    
    // Check if popup should be shown (only during notification period)
    const currentDay = new Date().getDate();
    const paymentDates = "1-4".split('-');
    const startDay = parseInt(paymentDates[0]);
    const endDay = paymentDates[1] ? parseInt(paymentDates[1]) : startDay;
    
    if (currentDay >= startDay && currentDay <= endDay) {
        // Show popup only if it hasn't been shown 5 times today
        const popupCount = localStorage.getItem('popupCount_Sheen_oil_traders') || 0;
        const lastPopupDate = localStorage.getItem('lastPopupDate_Sheen_oil_traders');
        const today = new Date().toDateString();
        
        if (lastPopupDate !== today) {
            // New day, reset counter
            localStorage.setItem('popupCount_Sheen_oil_traders', 0);
            localStorage.setItem('lastPopupDate_Sheen_oil_traders', today);
        }
        
        if (popupCount < 5) {
            popup.style.display = 'flex';
            localStorage.setItem('popupCount_Sheen_oil_traders', parseInt(popupCount) + 1);
            
            // Close popup when X is clicked
            closeBtn.addEventListener('click', function() {
                popup.style.display = 'none';
                // Redirect to original content after closing popup
                window.location.href = 'original_content.php';
            });
        } else {
            popup.style.display = 'none';
        }
    } else {
        popup.style.display = 'none';
    }
});