var db = new Array();	// array van rijen
var dagen = new Array();
dagen[0] = "zondag";
dagen[1] = "maandag";
dagen[2] = "dinsdag";
dagen[3] = "woensdag";
dagen[4] = "donderdag";
dagen[5] = "vrijdag";
dagen[6] = "zaterdag";
var voorbij = true;
var fromDate = new Date();
var startdatumfrom = new Date();
var startdatumto = new Date();
var aantalPerPagina = 10000;
var huidigePagina = 0;
var aantalPaginas = 0;
// nodig voor enkel per startdatum sorteren
var secretSort = false;

function vorigePagina() {
	if(huidigePagina>0) {
		huidigePagina--;
		redraw();
	}
}
function volgendePagina() {
	if (huidigePagina<aantalPaginas-1) {
		huidigePagina++;
		redraw();
	}
}

function setPageSelectorsIndexes(index) {
	document.forms['selectpageformonderaan'].selectpageonderaan.selectedIndex=index;	// also change the other one !
	document.forms['selectpageform'].selectpage.selectedIndex=index;					// also change the other one !
}
function selectpageChanged() {
	huidigePagina=document.forms['selectpageform'].selectpage.selectedIndex;
	redraw();
}
function selectpageChangedOnderaan() {
	huidigePagina=document.forms['selectpageformonderaan'].selectpageonderaan.selectedIndex;;
	redraw();
}
function setAantalPerPagina(aantal) {
	huidigePagina = 0;	// want dit wordt semi-betekenisloos
	fillSelectPage();
	redraw();
}
function redraw() {
	fillPleaseWait();
	setPageSelectorsIndexes(huidigePagina);
	outputArray(db);
}
function fillSelectPage() {
		// get the selected # per pagina
		var checked = getCheckedValue(document.forms['aantalperpaginaform'].aantalPerPagina);
		if (checked==0) checked = 100;
		aantalPerPagina = checked;
		
		aantalPaginas = Math.round(db.length/aantalPerPagina);
		if (aantalPaginas*aantalPerPagina<db.length) aantalPaginas++;

		// selecteer overal het 1e element
		document.forms['selectpageform'].selectpage.selectedIndex = 0;
		document.forms['selectpageformonderaan'].selectpageonderaan.selectedIndex = 0;

		//options("Voor verwijderen: ");
		for (i=1;i<30;i++) {
			document.forms['selectpageform'].selectpage.options[1] = null;
			document.forms['selectpageformonderaan'].selectpageonderaan.options[1] = null;
		}
		//options("Na verwijderen: ");
		//options("Voor vullen: ");
		for (i=0;i<aantalPaginas;i++) {
			document.forms['selectpageform'].selectpage.options[i] = new Option(i+1,i);
			document.forms['selectpageformonderaan'].selectpageonderaan.options[i] = new Option(i+1,i);
		}
		//options("Na vullen: ");
}
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}
function options(msg) {
		var bla = "";
		var box = document.forms['selectpageform'].selectpage;
		for (i=0;i<box.options.length;i++) bla += box.options[i].index + "," + box.options[i].text + "," + box.options[i].value + "\n";
		alert(msg + bla);
}
// hacked extra function to allow sorting by startdate only
// javascript:alert(parent.frames['midden'].sorteerGeheim(a));
function sorteerGeheim(aan) {
	secretSort = aan;
	if (secretSort==true) {
		alert("Opgelet !\nU gaat nu een geheime zone binnen.\nNiets is nu nog zeker !\nOm de zone te verlaten, typt u:\njavascript:parent.frames.midden.sorteerGeheim(false);\nin de adresbalk.");
	} else if (secretSort==false) {
		alert("Geheime zone veilig verlaten.")
		location.reload(true);
	} else {
		alert("Commando geheime zone fout...\nU gaf: " + secretSort + " mee in plaats van true of false.");
	}
	sortAndOutput(0,true);
}
function sortAndOutput(veldnr, lTH) {
	if (db.length == 0) { fillDb(db); }
	fillSelectPage();
	document.getElementById('databaseform').innerHTML = "Please wait while we sort the database...";
	//document.getElementById('pleasewait').innerHTML = "<IMG SRC='progress.gif'>";
	if (secretSort==true) {
		db.sort(secretCompare);
	} else {
		db.sort(compare);
	}
	outputArray(db);
	//setTimeout("outputArray(db)",1);
}
// decrease every month number, because they have to start from zero
function changeDates(dbb) {
	for (var i=0;i<dbb.length;i++) {
		if (dbb[i][3].getMonth()==0) {
			dbb[i][3].setMonth(11);
		} else {
			dbb[i][3].setMonth(dbb[i][3].getMonth()-1);
		}
		if (dbb[i][4].getMonth()==0) {
			dbb[i][4].setMonth(11);
		} else {
			dbb[i][4].setMonth(dbb[i][4].getMonth()-1);
		}		
	}
}
function compareToInt(a,b) {
	if (a<b) {
		return -1;
	} else if (a>b) {
		return 1;
	} else return 0;
}
function secretCompare(a,b) {
	return compareToInt(a[3],b[3]);
}
function compare(a,b) {
	var stad = compareToInt(a[2],b[2]);
	var museum = compareToInt(a[1],b[1]);
	var startdatum = compareToInt(a[3],b[3]);

	if (stad<0) {
		return -1;
	} else if (stad>0) {
		return 1;
	} else {
		if (museum<0) {
			return -1;
		} else if (museum>0) {
			return 1;
		} else {
			if (startdatum<0) {
				return -1;
			} else if (startdatum>0) {
				return 1;
			} else {
				return 0;
			}
		}
	}
}

function outputArray(dbb)  {
	fromDate = new Date(document.forms['fromdate'].year.options[document.forms['fromdate'].year.selectedIndex].value,document.forms['fromdate'].month.options[document.forms['fromdate'].month.selectedIndex].value-1,document.forms['fromdate'].day.options[document.forms['fromdate'].day.selectedIndex].value);
	startdatumfrom = new Date(document.forms['fromdate'].startdatumfromyear.options[document.forms['fromdate'].startdatumfromyear.selectedIndex].value,document.forms['fromdate'].startdatumfrommonth.options[document.forms['fromdate'].startdatumfrommonth.selectedIndex].value-1,document.forms['fromdate'].startdatumfromday.options[document.forms['fromdate'].startdatumfromday.selectedIndex].value);
	startdatumto = new Date(document.forms['fromdate'].startdatumtoyear.options[document.forms['fromdate'].startdatumtoyear.selectedIndex].value,document.forms['fromdate'].startdatumtomonth.options[document.forms['fromdate'].startdatumtomonth.selectedIndex].value-1,document.forms['fromdate'].startdatumtoday.options[document.forms['fromdate'].startdatumtoday.selectedIndex].value);


	// set correct weekday name
	var weekdag = document.getElementById('weekdag');
	var dag = getDag(fromDate.getDay());
	weekdag.innerHTML = dag;

	// set correct size
	var weekdag = document.getElementById('size');
	weekdag.innerHTML = dbb.length;

	// schrijf de (dynamische) table headers
	var newHTML = '<TABLE cellpadding="5" border=5><TR>';
	for (var i=0;i<getNrOfColumns();i++) {
		newHTML += '<TD><CENTER><B><FONT SIZE=4>' + getColumnName(i) + '</FONT></B></CENTER></TD>';
	}
	newHTML += '</TR>';

	// get the selected # per pagina
	var checked = getCheckedValue(document.forms['fromdate'].filterradio);

	// voeg de rijen in in de tabel
	for (var i=huidigePagina*aantalPerPagina;i<dbb.length && i<((huidigePagina+1)*aantalPerPagina);i++) {	
		if ( (checked == "") || ((checked == "ondate") && ((dbb[i][4] >= fromDate) && (dbb[i][3] <= fromDate))) || ( (checked == "betweendates")   && ( (dbb[i][3] >= startdatumfrom) && (dbb[i][3] <= startdatumto))) ) {
			newHTML += '<TR>';
			newHTML += "<TD> " + dbb[i][2] + " </TD>";
			newHTML += "<TD> " + dbb[i][1] + " </TD>";
			newHTML += '<TD><A HREF="' + dbb[i][5] + '" onClick="popup = window.open(\'' + dbb[i][5] + '\', \'PopupPage\', \'height=500,width=624,scrollbars=yes,resizable=yes,menubar=yes,location=yes,top=0,left=400,status=no,toolbar=yes\'); return false" target="_blank">' + dbb[i][0] + '</A></TD>';
			newHTML += "<TD> " + getDag(dbb[i][3].getDay()) + " " + dbb[i][3].getDate() + "/" + (dbb[i][3].getMonth()+1) + "/" + dbb[i][3].getFullYear() + " </TD>";
			newHTML += "<TD> " + getDag(dbb[i][4].getDay()) + " " + dbb[i][4].getDate() + "/" + (dbb[i][4].getMonth()+1) + "/" + dbb[i][4].getFullYear() + " </TD>";
			newHTML += '</TR>';
		}
	}

	// sluit pagina, dan document af
	newHTML += '</TABLE>';
	var databaseform = document.getElementById('databaseform').innerHTML = newHTML;
	document.getElementById('pleasewait').innerHTML = "";
}

