var chartWidth = 850;
var chartHeight = 450;

var label = new Array(
		['A','Algemene bepalingen verkeerswetgeving'],
		['B','Bepalingen rijbevoegdheid en rijbewijzen'],
		['C','Inrichting, belading en slepen van voertuigen'],
		['D','Techniek, onderhoud en controle van voertuigen'],
		['E','Gebruik gordels en de zitplaats voor passagiers'],
		['F','Milieubewust en energiezuinig rijden'],
		['G','De toestand van de bestuurder'],
		['H','De eigenschappen en toestand van het voertuig'],
		['J','De aanwezigheid en gedrag van ander verkeer'],
		['J(G)','De aanwezigheid en gedrag van ander verkeer (Gevaarherkenning)'],
		['K','De weg-, zicht- en weersomstandigheden'],
		['K(G)','De weg-, zicht- en weersomstandigheden (Gevaarherkenning)'],
		['L','Handelen bij ongevallen en pech onderweg'],
		['M','Voor laten gaan op kruispunten'],
		['N','Voor laten gaan bij het afslaan'],
		['O','Voor laten gaan van invaliden en voetgangers'],
		['P','Politie, ambulance, brandweer, militaire colonnes en trams'],
		['Q','Bijzondere manoeuvres'],
		['R','Plaats op de weg en voorsorteren'],
		['S','Inhalen'],
		['T','Snelheid'],
		['U','Stilstaan en parkeren'],
		['V','Tekens, signalen en gevarendriehoek'],
		['W','Gebruik van lichten'],
		['X','Verkeersborden'],
		['Y','Verkeerslichten en aanwijzingen'],
		['Z','Verkeerstekens op het wegdek']
	);
var chart;

function showChart() {
	$("#results").html("<DIV id=\"chart\" style=\"width: " + chartWidth + "px; height: " + chartHeight + "px; text-align: center;\">\</DIV>");
	$("#results").show();
	
	chart = new JSChart("chart", "bar");
	chart.setBarValuesDecimals(0);
	chart.setIntervalStartY(0);
	chart.setIntervalEndY(100);
	chart.setAxisValuesNumberY(11);
	chart.setSize(chartWidth,chartHeight);
	
	
	drawChart(new Array(
		['A',resultsForCategory("A", null)],
		['B',resultsForCategory("B", null)],
		['C',resultsForCategory("C", null)],
		['D',resultsForCategory("D", null)],
		['E',resultsForCategory("E", null)],
		['F',resultsForCategory("F", null)],
		['G',resultsForCategory("G", null)],
		['H',resultsForCategory("H", null)],
		['J',resultsForCategory("J", null)],
		['J(G)',resultsForCategory("J", "G")],
		['K',resultsForCategory("K", null)],
		['K(G)',resultsForCategory("K", "G")],
		['L',resultsForCategory("L", null)],
		['M',resultsForCategory("M", null)],
		['N',resultsForCategory("N", null)],
		['O',resultsForCategory("O", null)],
		['P',resultsForCategory("P", null)],
		['Q',resultsForCategory("Q", null)],
		['R',resultsForCategory("R", null)],
		['S',resultsForCategory("S", null)],
		['T',resultsForCategory("T", null)],
		['U',resultsForCategory("U", null)],
		['V',resultsForCategory("V", null)],
		['W',resultsForCategory("W", null)],
		['X',resultsForCategory("X", null)],
		['Y',resultsForCategory("Y", null)],
		['Z',resultsForCategory("Z", null)]
	));
}

function drawChart(array) {
	if(errorsForPartG()>12 || (errorsForPartI + errorsForPartR)>5) {
		chart.setTitle("Gezakt voor het theorie examen met " + errorsForAllParts() + " fout(en)");
	} else {
		chart.setTitle("Geslaagd voor het theorie examen met " + errorsForAllParts() + " fout(en)");
	}
	chart.setAxisNameX("gevaarherkenning: " + errorsForPartG() + " fout(en), regels en wetten: " + errorsForPartR() + " fout(en), inzicht: " + errorsForPartI() + " fout(en)");
	chart.setAxisNameY('%');
	chart.setDataArray(array);
	for(i=0;i<label.length;i++) {
		chart.setTooltip(label[i]);
	}
	chart.draw();
}

function resultsForCategory(category, type) {
	counter = 0;
	errors = 0;
	for(i=0; i<data.questions.length; i++) {
		if(type!="G" || data.questions[i].type=="G") {
			if(data.questions[i].category==category) {
				counter++;
				if(data.questions[i].kind=="O") {
					if(parseFloat(data.questions[i].answer)!=parseFloat(data.questions[i].given)) errors++;
				} else {
					if(data.questions[i].answer!=data.questions[i].given) errors++;
				}
			}
		}
	}
	
	if(counter==0) {
		return 100;
	} else if(counter==errors) {
		return 0;
	} else {
		return (counter-errors) / counter * 100.00;
	}
}

function errorsForAllParts() {
	errors = 0;
	for(i=0; i<data.questions.length; i++) {
		if(data.questions[i].kind=="O") {
			if(parseFloat(data.questions[i].answer)!=parseFloat(data.questions[i].given)) errors++;
		} else {
			if(data.questions[i].answer!=data.questions[i].given) errors++;
		}
	}
	return errors;
}

function errorsForPartG() {
	errors = 0;
	for(i=0; i<data.questions.length; i++) {
		if(data.questions[i].type=="G") {
			if(data.questions[i].kind=="O") {
				if(parseFloat(data.questions[i].answer)!=parseFloat(data.questions[i].given)) errors++;
			} else {
				if(data.questions[i].answer!=data.questions[i].given) errors++;
			}
		}
	}
	return errors;
}

function errorsForPartR() {
	errors = 0;
	for(i=0; i<data.questions.length; i++) {
		if(data.questions[i].type=="R") {
			if(data.questions[i].kind=="O") {
				if(parseFloat(data.questions[i].answer)!=parseFloat(data.questions[i].given)) errors++;
			} else {
				if(data.questions[i].answer!=data.questions[i].given) errors++;
			}
		}
	}
	return errors;
}

function errorsForPartI() {
	errors = 0;
	for(i=0; i<data.questions.length; i++) {
		if(data.questions[i].type=="I") {
			if(data.questions[i].kind=="O") {
				if(parseFloat(data.questions[i].answer)!=parseFloat(data.questions[i].given)) errors++;
			} else {
				if(data.questions[i].answer!=data.questions[i].given) errors++;
			}
		}
	}
	return errors;
}
