var countdown_stop = false;

function countdown(year, month, day, hour, minute, format){
	if (!countdown_stop) {
		Today = new Date();
		Todays_Year = Today.getFullYear() - 2000;
		Todays_Month = Today.getMonth();
		
		//Convert both today's date and the target date into miliseconds.
		Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(), Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();
		var Target_Date = (new Date(year, month - 1, day, hour, minute, 00)).getTime();
		
		//Find their difference, and convert that into seconds.
		var Time_Left = Math.round((Target_Date - Todays_Date) / 1000);
		
		if (Time_Left < 0) 
			Time_Left = 0;
		
		switch (format) {
			case 0:
				//The simplest way to display the time left.
				$("#countdown").html(Time_Left + ' seconds');
				break;
			case 1:
				//More datailed.
				days = Math.floor(Time_Left / (60 * 60 * 24));
				Time_Left %= (60 * 60 * 24);
				hours = Math.floor(Time_Left / (60 * 60));
				Time_Left %= (60 * 60);
				minutes = Math.floor(Time_Left / 60);
				Time_Left %= 60;
				seconds = Time_Left;
				
				var dayLen = days.toString().length;
				if (dayLen < 3) {
					if (dayLen == 2) { days = '0' + days; }
					if (dayLen == 1) { days = '00' + days; }
				}
				
				var hrsLen = hours.toString().length;
				if (hrsLen < 2) { hours = '0' + hours; }
				
				var minLen = minutes.toString().length;
				if (minLen < 2) { minutes = '0' + minutes; }
				
				var secLen = seconds.toString().length;
				if (secLen < 2) { seconds = '0' + seconds;}
				
				var str = days + ' : ' + hours + ' : ' + minutes + ' : ' + seconds;
				$("#countdown").html(str);
				break;
			default:
				$("#countdown").html(Time_Left + ' seconds');
		}
		
		//Recursive call, keeps the clock ticking.
		setTimeout('countdown(' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + format + ');', 1000);
	} else {
		$("#countdown").html("");
	}
}

function countup(el, hours, minutes, seconds){
    seconds += 1;
    if (seconds == 60) {
        seconds = 0;
        minutes += 1;
    }
    if (minutes == 60) {
        minutes = 0;
        hours += 1;
    }
    
    var secLen = seconds.toString().length;
    if (secLen == 1) { seconds = "0" + seconds; }

    var minLen = minutes.toString().length;
    if (minLen == 1) { minutes = "0" + minutes; }

    var hrsLen = hours.toString().length;
    if (hrsLen == 1) { hours = "0" + hours; }
    
    var str = hours + ":" + minutes + ":" + seconds;
    $("#" + el).html(str);
    
    //Recursive call, keep counting.
    setTimeout('countup("' + el + '",' + hours + ',' + minutes + ',' + seconds + ')', 1000);
}

