/*

	Name			: /self-assessment/self-assessment.js
	Author			: Frank Marion
	Purpose			: It's a quiz: Collect the values of the form inputs (radio buttons). 
					: Count them. Assign a response based on the number of points per section, 
					: and overall, then display their ratings and the message

	History			: 2007-10-10 Creation. (FM)

*/

/* --------------------------------------------------------------------------- */
// OPTIONS: Init global array and vars
var sections = 1;  	// How many sections of questions?
var questions = 27; 	// How many questions per section?

// colours for results based on score
var color_high_score = 'red';
var color_high1_score = '#CCCCCC';
var color_medium_score = 'orange';
var color_low_score = 'yellow';

// Do we want Yes's or No's to count as the point? (Sometimes we want to score a 'negatively' phrased question)
var score_value = 1; 


/* --------------------------------------------------------------------------- */
// Build the 2d array to keep track of answers...
function initScoreChartArray() {
	answer = new Array(sections);
	for ( var i=0; i < answer.length; i++ ) {
	  answer[i] = new Array(questions);
	}
	// ... then initialize both dimensions with zeros
	for ( var i=0; i < answer.length; i++ ) {
		for ( var j=0; j < questions; j++ ) {
			answer[i][j]	= 0;
		}
	}
}
initScoreChartArray();


/* --------------------------------------------------------------------------- */
// Build the array to hold the scores, and intitialize it with zeros
function initScoresArray() {
	scores = new Array(sections);
	for ( var i=0; i < scores.length; i++ ) {
	  scores[i] = 0;
	}
	return scores;
}
initScoresArray();


/* --------------------------------------------------------------------------- */
// Get inputs, parse field names, add value the value of the checked field
function collectValues() {
	
	// Get all the list of inputs
	inputs = document.getElementsByTagName('input');

	// Loop though our collection of inputs
    for( var k=0; k < inputs.length; k++ ) { 
	
		// Be a lazy typist and make code more readable
		var inp = inputs[k];
		
		// Check that it's a radio button AND that it's checked
        if(inp.type== 'radio' && inp.checked==true) {
			
			// Split the name on the :
			var tmp = new Array();
			var tmp = inp.name.split(':');
			
			// Trim off the leading alphachar (alpha used for w3c standards)
			tmp[0] = tmp[0].substring(1,tmp[0].length);
			
			// Finally update the answer array with the new values
			answer[tmp[0]-1][tmp[1]] = inp.value;
		}
    } 
	scoreValues(answer);
}


/* --------------------------------------------------------------------------- */
// Add up and score values
function scoreValues(array) {
	var section, question;
	
	// Reset all scores to zero
	initScoresArray();

	// Loop over the array...
	for ( section=0; section < array.length; section++ ) {

		// ... and total up all the values
		for ( question=0; question < array[section].length; question++ ) { 
			scores[section] = parseInt(scores[section]) + parseInt(array[section][question]);
		}
	}
	// Get overall score by adding each question/section scores
	var  overall_score=0;
	for (i=0; i < array.length; i++ ) {
		overall_score = overall_score +  scores[i];
	}
	displayScores(scores, overall_score) //return scores;
}



/* --------------------------------------------------------------------------- */
// Display the scores
function displayScores(array, overall_score) { 

	// ID the divs we'll show/hide, style and fill with text
	var results = document.getElementById('results'); 					 			// Result container div
	var r_divs = getElementsByClassName(document, "div", "response_div"); 			// Individual divs
	var ro_div = getElementsByClassName(document, "div", "response_div_overall");	// Overall results div

	// Loop over per-question scores array, get each value, score it and display the per-question results
	var score_value;
	for ( var a=0; a < array.length; a++ ) {

		var elmP = r_divs[a].getElementsByTagName("p");
		var elmPCount = elmP.length;

		if (array[a] <= 1) {
			setStyle(r_divs[a], color_low_score,array[a]);
			resetResults(elmP,elmPCount);
			showResultPs(elmP,1);

		} else if (array[a] >= 2 && array[a] <= 3) {
			setStyle(r_divs[a], color_medium_score,array[a]);
			resetResults(elmP,elmPCount);
			showResultPs(elmP,2);
			
		} else if (array[a] >= 4 && array[a] <= 10) {
			setStyle(r_divs[a], color_high1_score,array[a]);
			resetResults(elmP,elmPCount);
			showResultPs(elmP,3);

		} else if (array[a] >= 11) {
			setStyle(r_divs[a], color_high_score,array[a]);
			resetResults(elmP,elmPCount);
			showResultPs(elmP,4);
		}
	}

	// Display the results of the overall score
	var elmPO = ro_div[0].getElementsByTagName("p");

	if (overall_score <= 1) {
		setStyle(ro_div[0],color_low_score);
		resetResults(elmPO,elmPCount);
		showResultPs(elmPO,1);

	} else if (overall_score >= 2 && overall_score <= 3) {
		setStyle(ro_div[0],color_medium_score);
		resetResults(elmPO,elmPCount);
		showResultPs(elmPO,2);
		
	} else if (overall_score >= 4 && overall_score <= 10) {
		setStyle(ro_div[0],color_high1_score);
		resetResults(elmPO,elmPCount);
		showResultPs(elmPO,3);

	} else if (overall_score >= 11) {
		setStyle(ro_div[0],color_high_score);
		resetResults(elmPO,elmPCount);
		showResultPs(elmPO,4);
	}  
	
}


/* --------------------------------------------------------------------------- */
// Make the result divs visible and set their style
function setStyle(elm,colour) {
	elm.style.backgroundColor = colour;
	//elm.style.border = '1px solid lightgray';
	elm.style.padding = '5px';
	elm.style.margin = '5px';
	elm.style.display = 'block';
}

/* --------------------------------------------------------------------------- */
// Clear all visible paragraphs first (like a re-init)
function resetResults(elm,count) {
	for ( var c=0; c < count; c++ ) {
		elm[c].style.display = 'none';
	}
}

/* --------------------------------------------------------------------------- */
// All the P's are hidden by css. These sets the "correct" scored response to "display:block;"
function showResultPs(elm,val) {
	elm[0].style.display = 'block';
	elm[val].style.display = 'block';
}


/* --------------------------------------------------------------------------- */
// Thanks to Jonathan Snook, http://www.snook.ca/jonathan for saving me the hassle!
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];		
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}	
	}
	return (arrReturnElements)
}




