// isIntegerInRange (STRING s, INTEGER a, INTEGER b)
function isIntegerInRange(s, a, b) {
	if (isEmpty(s))
		if (isIntegerInRange.arguments.length == 1)
			return false;
		else
			return (isIntegerInRange.arguments[1] == true);

	// Catch non-integer strings to avoid creating a NaN below,
	// which isn't available on JavaScript 1.0 for Windows.
	if (!isInteger(s, false))
		return false;

	// Now, explicitly change the type to integer via parseInt
	// so that the comparison code below will work both on
	// JavaScript 1.2 (which typechecks in equality comparisons)
	// and JavaScript 1.1 and before (which doesn't).
	var num = parseInt(s);
	return ((num >= a) && (num <= b));
}

function isInteger(s) {
	var i;

	if (isEmpty(s))
		if (isInteger.arguments.length == 1)
			return 0;
		else
			return (isInteger.arguments[1] == true);

	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);

		if (!isDigit(c))
			return false;
	}

	return true;
}

function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

function isDigit(c) {
	return ((c >= "0") && (c <= "9"))
}

function calctotal(id, items_left, price_piece) {
	var item_amount = document.getElementById('amount' + id).value;
	if (isIntegerInRange(item_amount, 0, items_left) || item_amount == '') {
		document.getElementById('total' + id).innerHTML = item_amount
				* price_piece;
	} else {
		document.getElementById('amount' + id).value = items_left;
		document.getElementById('total' + id).innerHTML = (items_left * price_piece);
		alert('Er zijn enkel ' + items_left + ' delen over! Je kan niet meer bestellen.');
	}
	document.getElementById('submit' + id).style.color='red';
	document.getElementById('update_message').innerHTML = '<font color="red">Vergeet niet op de rode update knop te drukken om je wijzigingen op te slaan</font>';
}





function countdown_clock(year, month, day, hour, minute, format) {
	// I chose a div as the container for the timer, but
	// it can be an input tag inside a form, or anything
	// who's displayed content can be changed through
	// client-side scripting.
	html_code = '<div id="countdown"></div>';

	document.write(html_code);

	countdown(year, month, day, hour, minute, format);
}

function countdown(year, month, day, hour, minute, format) {
	Today = new Date();
	Todays_Year = Today.getFullYear();
	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();
	Target_Date = (new Date(year, month - 1, day, hour, minute, 00)).getTime();

	// Find their difference, and convert that into seconds.                  
	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.
		document.getElementById('countdown').innerHTML = Time_Left + ' seconden';
		break;
	case 1:
		// More detailed.
		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;

		// ps is short for plural suffix.
		if (days == 1) dps = 'dag';
		else dps = 'dagen';
		if (hours == 1)	hps = 'uur';
		else hps = 'uren';
		if (minutes == 1) mps = 'minuut';
		else mps = 'minuten'
		if (seconds == 1) sps = 'second';
		else sps = 'seconden';

		document.getElementById('countdown').innerHTML = days + ' '+ dps + ' ';
		document.getElementById('countdown').innerHTML += hours + ' ' + hps + ' ';
		document.getElementById('countdown').innerHTML += minutes + ' ' + mps + ' ';
		document.getElementById('countdown').innerHTML += seconds + ' ' + sps;
		break;
	default:
		document.getElementById('countdown').innerHTML = Time_Left + ' seconden';
	}

	// Recursive call, keeps the clock ticking.
	setTimeout('countdown(' + year + ',' + month + ',' + day + ',' + hour + ','
			+ minute + ',' + format + ');', 1000);
}
