function VARKResponse( myQuestion, responseText, responseType ){
	this.myQuestion = myQuestion;
	this.responseText = responseText;
	this.responseType = responseType;
}
VARKResponse.prototype.responseText;
VARKResponse.prototype.responseType;
VARKResponse.prototype.myQuestion;
VARKResponse.prototype.display = function(){
	if( currentLanguage == "Arabic" || currentLanguage == "Hebrew" || currentLanguage == "Farsi" ){
		document.write( '<div class="VARKResponseArabic">' );
		document.write( '<label>' );
		var id = 'C' + this.myQuestion.number + this.responseType ;
		document.write( '<input type="checkbox" id="' + id + '" name="' + id + '" value="' + this.responseType + '"/>');
		document.write( '&#160;&#160;' + this.responseText );
		document.write( '</label>' );
		document.write( '</div>' );
	} else if( currentLanguage == "Hindi" ){
    document.write( '<div class="VARKResponseHindi">' );
		document.write( '<label>' );
		var id = 'C' + this.myQuestion.number + this.responseType;
		document.write( '<input type="checkbox" id="' + id + '" name="' + id + '" value="' + this.responseType + '"/>' );
		document.write( this.responseText + '</label></div>' );
	} else {
		document.write( '<div class="VARKResponse">' );
		document.write( '<label>' );
		var id = 'C' + this.myQuestion.number + this.responseType;
		document.write( '<input type="checkbox" id="' + id + '" name="' + id + '" value="' + this.responseType + '"/>' );
		document.write( this.responseText + '</label></div>' );
	}
}

function VARKQuestion( questionNumber, questionText ){
	this.allMyResponses = new Array(4);
	this.number = questionNumber;
	this.question = questionText;
}
VARKQuestion.prototype.number;
VARKQuestion.prototype.question;
VARKQuestion.prototype.allMyResponses;
VARKQuestion.prototype.addResponse = function( responseText, responseType ){
	for( var r = 0; r < this.allMyResponses.length; r++ ){
		if( this.allMyResponses[r] == null ){
			this.allMyResponses[r] = new VARKResponse( this, responseText, responseType );
			break;
		}
	}
}
VARKQuestion.prototype.display = function(){
	document.write( '<div class="VARKQuestion' );
	if( currentLanguage == "Arabic" || currentLanguage == "Hebrew" || currentLanguage == "Farsi" ){
		document.write (' arabic');
	} else if( currentLanguage == "Hindi" ){
    document.write (' hindi' );
	}
	document.write( '"><div class="VARKQuestionText">' + this.question + '</div>' );
	var displayedResponses = new String();
	var responsesDisplayed = 0;
	var r = 0;
	var response; 
	
	var responsesDisplayed = 0;
	while( responsesDisplayed < this.allMyResponses.length ){
		r = Math.round( this.allMyResponses.length*Math.random());
		if( r < 0 ){
			r = 0;
		} else if( r >= this.allMyResponses.length ){
			r = this.allMyResponses.length - 1;
		}
		response = this.allMyResponses[r];
		var responseType = 'Z';
		if( response != null ){
			responseType = response.responseType;
		}
		var alreadyDisplayed = false;
		alreadyDisplayed = displayedResponses.indexOf( responseType ) > -1;
		if( ! alreadyDisplayed ){
			displayedResponses = new String( displayedResponses.valueOf() + responseType );
			if( response != null ){
				response.display();
			}
			responsesDisplayed++;
		}
	}
	document.write( '</div>' );
}

function VARKQuestionnaire(){
	this.allMyQuestions =  new Array(16);
}
VARKQuestionnaire.prototype.allMyQuestions;
VARKQuestionnaire.prototype.addQuestion = function(questionText){
	for( var q = 0; q < this.allMyQuestions.length; q++ ){
		if( this.allMyQuestions[q] == null ){
			this.allMyQuestions[q] = new VARKQuestion( q+1, questionText );
			return this.allMyQuestions[q];
		}
	}
}
VARKQuestionnaire.prototype.display = function(){
	var displayedQuestions = new String("|");
	var questionOrder = "";
	var numberDisplayed = 0;
	var q = Math.round( this.allMyQuestions.length*Math.random()) - 1;
	while( numberDisplayed < this.allMyQuestions.length - 2 ){
		var q = -1;
		while( q == -1 | displayedQuestions.indexOf("|" + q + "|") > -1 ){
			q = Math.round( this.allMyQuestions.length*Math.random()) - 1;
			if( q < 0 ){
				q = 0;
			} else if( q >= this.allMyQuestions.length ){
				q = this.allMyQuestions.length  - 1;
			}
		}
		if( q > -1 && q < this.allMyQuestions.length ){
			var question = this.allMyQuestions[q];
			if( question != null ){
				question.display();
				questionOrder = questionOrder + question.number + ",";
			}
			displayedQuestions = new String( displayedQuestions.valueOf() + q + "|" );
			numberDisplayed++;
		}
	}
	for( var q = 0; q < this.allMyQuestions.length; q++ ){
		if (displayedQuestions.indexOf("|" + q + "|") < 0 ){
			this.allMyQuestions[q].display();
			questionOrder = questionOrder + this.allMyQuestions[q].number + ",";
		}

	}
	questionOrder += '7.7';
	document.write( '<input type="hidden" name="resultsQuestions"/>');
    document.write( '<input type="hidden" name="resultsTotal"/>');
    document.write( '<input type="hidden" name="resultsV"/>');
    document.write( '<input type="hidden" name="resultsA"/>');
    document.write( '<input type="hidden" name="resultsR"/>');
    document.write( '<input type="hidden" name="resultsK"/>');
    document.write( '<input type="hidden" name="resultsHighest"/>');
    document.write( '<input type="hidden" name="resultsPref"/>');
    document.write( '<input type="hidden" name="resultsStrength"/>');
    document.write( '<input type="hidden" name="questionOrder" value="' + questionOrder + '"/>' );
    document.frmQ.reset();
}
VARKQuestionnaire.prototype.checkResults = function(){
	// checks that enough (12) questions have been answered.
	var x = 0;
	var val;
	var min = 12;
	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 {
		this.calcResults();
	}
}
function node( newWord, newSymbol, newValue ) {
	this.word = newWord;
	this.symbol = newSymbol;
	this.val = newValue;
	this.incl = false;
}
VARKQuestionnaire.prototype.calcResults = function(){
	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 > 32 ) { limit = 4; }
	if( total <= 32 ) { limit = 3; }
	if( total <= 27 ) { limit = 2; }
	if( total <= 21 ) { 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;
		if( diff <= 2 ){
			strength = "mild";
		} else if( diff <= 4 ){
			strength = "strong";
		} else {
			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( this.allMyQuestions.length + 1 );
	for( var q = 1; q < questions.length; 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++;
	}
	// put the info into hidden fields in the form
	var frm = document.frmQ;
	frm.resultsQuestions.value = "";
	for( q=1; q<=this.allMyQuestions.length; q++ ){
		frm.resultsQuestions.value += questions[q] + "|";
	}
	frm.resultsTotal.value = total;
	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();
}
