/**
* Geschreven door Edwin Steenwinkel, Edit4U.nl | (c) Copyright 2007 Edit4U
*/

function makePOSTRequest( url, parameters ) {
		
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	window.parameters = parameters;

	http_request.open( 'POST', url, true );
	http_request.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
	http_request.setRequestHeader( "Content-length", parameters.length );
	http_request.setRequestHeader( "Connection", "close" );
	http_request.onreadystatechange = handleResponseSearch; 
	http_request.send(parameters);
	
}

function handleResponseSearch() { 
	/* WORDT nog niet gebruikt */
	if(http_request.readyState == 4 && http_request.status == 200){ 
		if(http_request.responseText) { 
		} else { 
		}
	} else { 
	} 
}

function plastest () {

	var cb11 = false;
	var cb12 = false;
	var cb21 = false;
	var cb22 = false;
	var cb31 = false;
	var cb32 = false;
	var cb41 = false;
	var cb42 = false;
	
	var plastestFORM = document.getElementById ('plastestFORM');
	
	for ( var i=0; i < 4; i++ ) {
		if ( plastestFORM.antwoord11[i].checked ) {
			cb11 = plastestFORM.antwoord11[i].value;
		}
		if ( plastestFORM.antwoord12[i].checked ) {
			cb12 = plastestFORM.antwoord12[i].value;
		}
		if ( plastestFORM.antwoord21[i].checked ) {
			cb21 = plastestFORM.antwoord21[i].value;
		}
		if ( plastestFORM.antwoord22[i].checked ) {
			cb22 = plastestFORM.antwoord22[i].value;
		}
		if ( plastestFORM.antwoord31[i].checked ) {
			cb31 = plastestFORM.antwoord31[i].value;
		}
		if ( plastestFORM.antwoord32[i].checked ) {
			cb32 = plastestFORM.antwoord32[i].value;
		}
		if ( plastestFORM.antwoord41[i].checked ) {
			cb41 = plastestFORM.antwoord41[i].value;
		}
		if ( plastestFORM.antwoord42[i].checked ) {
			cb42 = plastestFORM.antwoord42[i].value;
		}
	}

	if ( cb11 && cb12 && cb21 && cb22 && cb31 && cb32 && cb41 && cb42 ) {

		/* Okee, alles ingevuld. Reken nu maar uit wat het resultaat moet worden. */
		var totaal1 = parseInt(cb11) + parseInt(cb21) + parseInt(cb31) + parseInt(cb41);
		var totaal2 = parseInt(cb12) + parseInt(cb22) + parseInt(cb32) + parseInt(cb42);
		
		totaal1 = parseInt(totaal1);
		totaal2 = parseInt(totaal2);

		/* Alles weer verbergen */
		document.getElementById ( 'uitslag_0' ).style.display 		= 'none';
		document.getElementById ( 'uitslag_1-3' ).style.display 	= 'none';
		document.getElementById ( 'uitslag_1-3_1' ).style.display 	= 'none';
		document.getElementById ( 'uitslag_4-6' ).style.display 	= 'none';
		document.getElementById ( 'uitslag_7-9' ).style.display 	= 'none';
		document.getElementById ( 'uitslag_10-12' ).style.display 	= 'none';
		
		/* En even opvragen wat wel getoond moet worden */
		if ( totaal1 == 0 ) {
			document.getElementById ( 'regel' ).style.display 		= 'block';
			document.getElementById ( 'uitslag_0' ).style.display 	= 'block';
		}
		if ( totaal1 >= 1 && totaal1 <= 3 && totaal2 == 0 ) {
			document.getElementById ( 'regel' ).style.display 		= 'block';
			document.getElementById ( 'uitslag_1-3' ).style.display	= 'block';
		}
		if ( totaal1 >= 1 && totaal1 <= 3 && totaal2 > 0 ) {
			document.getElementById ( 'regel' ).style.display 			= 'block';
			document.getElementById ( 'uitslag_1-3_1' ).style.display	= 'block';
		}
		if ( totaal1 >= 4 && totaal1 <= 6 ) {
			document.getElementById ( 'regel' ).style.display 		= 'block';
			document.getElementById ( 'uitslag_4-6' ).style.display	= 'block';
		}
		if ( totaal1 >= 7 && totaal1 <= 9 ) {
			document.getElementById ( 'regel' ).style.display 		= 'block';
			document.getElementById ( 'uitslag_7-9' ).style.display	= 'block';
		}
		if ( totaal1 >= 10 && totaal1 <= 12 ) {
			document.getElementById ( 'regel' ).style.display 		= 'block';
			document.getElementById ( 'uitslag_10-12' ).style.display	= 'block';
		}
		
		/* Schrijf alle gegevens antwoorden weg naar de db, via de AJAX methode */
		schrijfResultNaarDB();

	} else {
		/* Uhm, wat denk je dat het betekend :p */
		alert ( 'Het is verplicht om op elke vraag een antwoord te geven!' );
	}

}

function printTest() {
	
	var form 	= document.plastestFROM;
	var v1a		= document.getElementById( 'hidAnt1a' ).value;
	var v1b		= document.getElementById( 'hidAnt1b' ).value;
	var v2a		= document.getElementById( 'hidAnt2a' ).value;
	var v2b		= document.getElementById( 'hidAnt2b' ).value;
	var v3a		= document.getElementById( 'hidAnt3a' ).value;
	var v3b		= document.getElementById( 'hidAnt3b' ).value;
	var v4a		= document.getElementById( 'hidAnt4a' ).value;
	var v4b		= document.getElementById( 'hidAnt4b' ).value;
	
	var theLink = 'http://dev.edit4u.nl/plastest/index.php?task=plastest-pdf';
		theLink	= theLink + '&1a=' + v1a;
		theLink	= theLink + '&1b=' + v1b;
		theLink	= theLink + '&2a=' + v2a;
		theLink	= theLink + '&2b=' + v2b;
		theLink	= theLink + '&3a=' + v3a;
		theLink	= theLink + '&3b=' + v3b;
		theLink	= theLink + '&4a=' + v4a;
		theLink	= theLink + '&4b=' + v4b;
	window.open ( theLink, 'PLASTESTPDF', 'width=850,height=600,scrollbars=yes,toolbar=no,location=no,resizable=allow' );
	
}

function schrijfResultNaarDB () {
	
	var form 	= document.plastestFROM;
	var v1a		= document.getElementById( 'hidAnt1a' );
	var v1b		= document.getElementById( 'hidAnt1b' );
	var v2a		= document.getElementById( 'hidAnt2a' );
	var v2b		= document.getElementById( 'hidAnt2b' );
	var v3a		= document.getElementById( 'hidAnt3a' );
	var v3b		= document.getElementById( 'hidAnt3b' );
	var v4a		= document.getElementById( 'hidAnt4a' );
	var v4b		= document.getElementById( 'hidAnt4b' );
	
	var	poststr = "&option=plastest-ajax&task=resultaatOpslaan";
		poststr = poststr + "&v1a=" + v1a.value;
		poststr = poststr + "&v1b=" + v1b.value;
		poststr = poststr + "&v2a=" + v2a.value;
		poststr = poststr + "&v2b=" + v2b.value;
		poststr = poststr + "&v3a=" + v3a.value;
		poststr = poststr + "&v3b=" + v3b.value;
		poststr = poststr + "&v4a=" + v4a.value;
		poststr = poststr + "&v4b=" + v4b.value;
		
	makePOSTRequest( 'index.php', poststr );
}

function plaatsAntwoord ( obj, id ) {
	document.getElementById( 'hidAnt'+id ).value = obj.value;
}