<!--

///////////////////////////////////////////////////////////////////////////////////////////
function node( newWord, newSymbol, newValue ) {
	this.word = newWord;
	this.symbol = newSymbol;
	this.val = newValue;
	this.incl = false;
}

////////////////////////////////////////////////////////////////////////////////////////////

function checkResults() {
	// checks that enough (10) questions have been answered.
	var x = 0;
	var val;
	var min = 10;
	var numAnswered = 0;
	var lastAnswered = -1;

	while( x < document.frmQ.elements.length && numAnswered < min ) {
		if( document.frmQ.elements[x].type == "checkbox" ) {
			if( document.frmQ.elements[x].checked == true ) {
				var chkName = new String( document.frmQ.elements[x].name );
				var qNum = parseInt(chkName.substring( 1, chkName.length ));
				if( qNum != lastAnswered ) {
					lastAnswered = qNum;
					numAnswered++;
				}

			}
		}
		x++;
	}

	if( numAnswered < min ) {
		alert( "Please answer at least " + min + " questions, so that we can give you a more accurate idea of your learning preferences." );
		//return false;
	} else {

		calcResults();
	}
}

function calcResults() {
	var nodes = new Array(4);
	nodes[0] = new node( "Visual", "V", 0 );
	nodes[1] = new node( "Aural", "A", 0 );
	nodes[2] = new node( "Read/Write", "R", 0 );
	nodes[3] = new node( "Kinesthetic", "K", 0 );

	// count up the ticks
	var x = 0;
	while( x < document.frmQ.elements.length ) {
		if( document.frmQ.elements[x].type == "checkbox" ) {
			if( document.frmQ.elements[x].checked == true ) {
				switch( document.frmQ.elements[x].value ) {	
					case "V":
						nodes[0].val++;
						break;
					case "A":
						nodes[1].val++;
						break;
					case "R":
						nodes[2].val++;
						break;
					case "K":
						nodes[3].val++;
						break;
				}				
			}
		}
		x++;
	}

	// calculate the total ticks and corresponding limit
	var total = nodes[0].val + nodes[1].val + nodes[2].val + nodes[3].val;

	var limit = 0;
	if( total > 26 ) { limit = 4; }
	if( total <= 26 ) { limit = 3; }
	if( total <= 22 ) { limit = 2; }
	if( total <= 16 ) { limit = 1; }	
	
	// sort the score nodes from highest to lowest score
	var temp;
	var n = 0;
	var r;
	var h;
	while( n < 3 ) {
		r = n + 1;
		h = n;
		high = nodes[n];
		while( r < 4 ) {
			if( nodes[r].val > nodes[h].val ) {
				h = r;
			}
			r++;
		}
		if( h != n ) {
			temp = nodes[h];
			nodes[h] = nodes[n];
			nodes[n] = temp;
		}
		n++;
	}

	// store the highest preference word
	highest = nodes[0].word;

	// now see which of the scores are to be included as preferences
	nodes[0].incl = true;
	var bigStep = false;
	n = 1;
	while( (n < 4) && (bigStep == false) ) {
		if( ( nodes[n-1].val - nodes[n].val ) <= limit ) {
			nodes[n].incl = true;
		} else {
			bigStep = true;
		}
		n++;
	}

	// find out if there is a single preference (i.e. only the highest (first) score is 
	// included) and if there is find out how strong the preference is.
	var strength = "";
	if( nodes[1].incl == false ) {
		var diff = nodes[0].val - nodes[1].val - limit;
		switch( diff ){
			case 1:
				strength = "mild";
				break;
			case 2:
				strength = "strong";
				break;
			default:
				strength = "very strong";
		}
	}

	// put the nodes back in order
	var found;
	var vark = new String( "VARK" );
	var m = 0;
	while( m < 4 ) {	// for each character in vark
		n = 0;
		found = false;
		while( n < 4 && found == false ) {	// look for match in nodes
			if( nodes[n].symbol == vark.charAt( m ) ) {
				temp = nodes[n];
				nodes[n] = nodes[m];
				nodes[m] = temp;
				found = true;
			}
			n++;
		}
		m++;
	}

	// get the string of preference letters
	var pref = "";
	n = 0
	while( n < 4 ) {
		if( nodes[n].incl == true ) {
			pref += nodes[n].symbol;
		}
		n++;
	}
	var questions = new Array( 14 );
	for( var q = 1; q < 14; q++ ){
		questions[q] = "";
	}
	var x = 0;
	while( x < document.frmQ.elements.length ) {
		if( document.frmQ.elements[x].type == "checkbox" ) {
			if( document.frmQ.elements[x].checked == true ) {
				var num = new String( document.frmQ.elements[x].name );
				var len = num.length;
				num = num.substring( 1, len-1);
				num = parseInt( num );
				questions[num] += document.frmQ.elements[x].value;			
			}
		}
		x++;
	}

	
	var frm = document.frmQ;
	frm.resultsQuestions.value = "";
	for( q=1; q<14; q++ ){
		frm.resultsQuestions.value += questions[q] + "|";
	}

	frm.resultsV.value = nodes[0].val;
	frm.resultsA.value = nodes[1].val;
	frm.resultsR.value = nodes[2].val;
	frm.resultsK.value = nodes[3].val;
	frm.resultsHighest.value = highest;
	frm.resultsPref.value = pref;
	frm.resultsStrength.value = strength;

	frm.submit();

}

//-->