function displayCountdown(timeDiff, divId) {
    var countdownElement = document.getElementById(divId);
    $("#"+divId).css('display','block');
    // Countdown function
    function countdown() {
        var remainingDays = Math.floor(timeDiff / 86400); // 1 day = 24 * 60 * 60 seconds
        var remainingHours = Math.floor((timeDiff % 86400) / 3600);
        var remainingMinutes = Math.floor((timeDiff % 3600) / 60);
        var remainingSeconds = timeDiff % 60;

            // var remainingSeconds = timeDiff % 60;
            // var remainingMinutes = Math.floor((timeDiff / 60) % 60);
            // var remainingHours = Math.floor(timeDiff / 3600);

            // Display remaining hours, minutes, and seconds
            countdownElement.innerHTML =
                remainingDays + "d " +
                remainingHours + "h " +
                remainingMinutes + "m <span class='text-danger'>" +
                remainingSeconds + "s</span>";

            // Decrement time difference by 1 second
            timeDiff--;

            if (timeDiff >= 0) {
                    // Call the countdown function again after 1 second (1000 milliseconds)
                    setTimeout(countdown, 1000);
            } else {
                    countdownElement.innerHTML = '<a class="badge badge-danger" href="javascript:void(0)">Expired</a>';
            }
    }

    // Start the countdown
    countdown();
}
function WeeklyCountdown(timeDiff, divId) {
    var countdownElement = document.getElementById(divId);
    // Countdown function
    function countdown2() {
        var remainingDays = Math.floor(timeDiff / 86400); // 1 day = 24 * 60 * 60 seconds
        var remainingHours = Math.floor((timeDiff % 86400) / 3600);
        var remainingMinutes = Math.floor((timeDiff % 3600) / 60);
        var remainingSeconds = timeDiff % 60;

            // var remainingSeconds = timeDiff % 60;
            // var remainingMinutes = Math.floor((timeDiff / 60) % 60);
            // var remainingHours = Math.floor(timeDiff / 3600);

            // Display remaining hours, minutes, and seconds
            var color = remainingSeconds % 2 == 0 ? 'danger' : 'success';
            countdownElement.innerHTML =
                remainingDays + "d " +
                remainingHours + "h " +
                remainingMinutes + "m <span class='text-"+color+"'>" +
                remainingSeconds + "s</span>";

            // Decrement time difference by 1 second
            timeDiff--;

            if (timeDiff >= 0) {
                    // Call the countdown function again after 1 second (1000 milliseconds)
                    setTimeout(countdown2, 1000);
            } else {
                    countdownElement.innerHTML = '<a class="badge badge-info" href="javascript:void(0)">In Progress</a>';
            }
    }

    // Start the countdown
    countdown2();
}
// Update the count down every 10 second
var i_time;var i_time2 ;var a_time ;
i_time = i_time2 = a_time =18000;//300 minutes - 5hours
function startTimer(a,y) {
    var countdownTimer = setInterval(function() {
        if(a == 0){
            i_time = 0;
        }
        i_time = i_time - 1;
        if (i_time % 30 ==0 ) {
            // check payment status
            check_payment_status(y);
        }
        if (i_time < 1) {
            clearInterval(countdownTimer);
        } 
    }, 1000);
}
function check_payment_status(x){
    var waiting_status = '<span class="bg-success/20 text-warning rounded px-2 ml-1">Pending</span><div class="spinner-border text-warning spinner-border-sm" role="status"><span class="visually-hidden">Loading...</span></div>';
    var part_payment_status = '<span class="bg-success/20 text-warning rounded px-2 ml-1">Partial payment</span><div class="spinner-border text-warning spinner-border-sm" role="status"><span class="visually-hidden">Loading...</span></div>';
    var complete_payment_status = '<span class="bg-success/20 text-success rounded px-2 ml-1">Payment approved...</span>';
    $.ajax({
        type:'POST',
        url:'../controller/post.php?check_payment_status',
        data:{'payment_id':x},
        success: function(data){
                if(data == 0){
                    /////

                    $(".status_display").html(waiting_status);
                }else if(data == 1 || data == 2){
                    $("#preview3").addClass("d-none");
                    $("#preview4").removeClass("d-none");
                    $(".status_display").html(complete_payment_status);
                    startTimer(0,x);
//                    $("#status_display").html('Confirming payment...<i class="fa fa-spinner fa-spin"></i>');
                }else if(data == 4){
                    $(".status_display").html('This invoice is no longer valid');
                    startTimer(0,x);
                    /////
                    var err = 'This invoice is no longer valid and any funds sent from this moment onward will be lost.';
                    $('#expired_info').html(err);
                }else{
                    var newData = data.split("||")[0];
                    if(newData == 3){
                        var bal = data.split("||")[1];
                        var cur = data.split("||")[2];
                        /// we update the amount in the copy fields
                        //////////////////////////
                        var err = 'Partial payment detected. Send additional <b>'+bal+' '+cur+ '</b> to complete this transaction ';
                        Msg3("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong>"+err,"alert-danger",0,".status_display2");
                        $(".status_display").html(part_payment_status);
                        startTimer(0,x);
                    }else{
                        Msg3("<strong><i class='fa fa-times-circle'></i>&nbsp;</strong>"+data,"alert-danger",0,".status_display2");
                    }
                        
                }
//                    else{
//                        alert(data);
//                    } 
            }
    });
}