// Thanks to https://codepen.io/rebelchris/pen/abZRpqM
let timer = null;
function startCountdown(dateString) {
    if (timer) clearInterval(timer);
	let cd = document.getElementById("countdown")
	if (cd) {
		cd.classList.remove("hide");
		if (dateString == "None") cd.classList.add("hide");
	}
	const end = new Date(dateString).getTime();
	const daysEl = document.getElementById('days');
	const hoursEl = document.getElementById('hours');
	const minutesEl = document.getElementById('minutes');
	const secondsEl = document.getElementById('seconds');
	const seconds = 1000;
	const minutes = seconds * 60;
	const hours = minutes * 60;
	const days = hours * 24;
	timer = setInterval(updateTime, seconds);
	function updateTime() {
		if (daysEl) {
			let now = new Date().getTime();
			const difference = end - now;
			
			if (difference < 0) {
				if (timer) clearInterval(timer);
				cd.classList.add("hide");
				return;
			}
				
			daysEl.innerText = Math.floor(difference / days);
			hoursEl.innerText = Math.floor( (difference % days) / hours );
			minutesEl.innerText = Math.floor( (difference % hours) / minutes );
			secondsEl.innerText = Math.floor( (difference % minutes) / seconds );  
		}
	}
	updateTime();
}