/* ######################### /common/js/quizfuncs.js #########################
define all variables and set defaults
these are can be overidden in a local, (quiz-specific) "quiz.js"
*/

var path = "";
var current_points;
var score = 0; 					// holds cumulative/final score
var qscore = 10; 				// points for correct answer
var q_num = 10; 				// total # of questions

var strR = "<b>Congratulations, you're correct!</b>";
var strW = "<b>Sorry, try again.</b>";

// ans[] array holds answers:
var ans = new Array();

/* checkAnswer is called with question # to be checked and nextpage to be sent to */
// #########################
function checkAnswer(question_num, next_page) { 
	var current_choice;
	var isChecked = false;
	for (var i = 0; i < document.quiz.question.length; i++) {
		if ( document.quiz.question[i].checked ) {
			isChecked = true;
			score = getScore();
			current_choice = document.quiz.question[i].value;
			if (current_choice == ans[question_num]) score += qscore;
		}
	}
	if (!isChecked) {
		alert("Please make a selection");
		return false;
	} else {
		//cp = currentPoints, cc = currentChoice
		location.href = path + next_page + "?cp=" + score + "&cc=" + current_choice;
		return;
	}
}

//#########################
function getScore() {
	//var qryStr = window.location.search;
	var current_points = getParam("cp");
	var pts = (current_points) ? parseInt(current_points) : 0;

/***
	if (qryStr.charAt(0) == "?") {
		qryStr = qryStr.substring(1,qryStr.length); // get rid of starting "?"
		pts = parseInt(qryStr.substring((qryStr.indexOf("=")+1),qryStr.length));
	}
***/
	return (pts);
}

/* #########################
function writeAnswer()
This function evaluates the answer submitted on the previous page, and prints the appropriate response. The first two conditions test if the answer is wrong. If the answer passes the first two conditions, then it is right. Be sure to substitute the correct letter into the evaluation, as well as changing the output message.
*/

function getAnswer(question) {
	//var qryStr = window.location.search;
	var current_choice = getParam('cc');
	//ltr = qryStr.substring((qryStr.lastIndexOf("="))+1,qryStr.length);
	var strAns = getResponse(question, current_choice);

	document.write(strAns);

}

function getResponse(question, choice) {
	var strResp;
	if (choice == ans[question]) strResp = "<b>" + strR + "</b>";
	else strResp = "<b>" + strW + "</b>";

	return (strResp);
}

function getTotal() {
	var totalscore = parseInt(qscore * q_num);
	return totalscore;
}

function getParam(paramName,pos) { // pulls values from url
	var startPos = (pos) ? pos : 0;
	var url = window.location.search;
	var paramBegin = url.indexOf(paramName,startPos);

	if (paramBegin == -1) return false
	var paramEnd = paramBegin + paramName.length;
	//var param = url.substring(paramBegin,paramEnd);
	
	var valBegin = url.indexOf("=",paramEnd);
	var nextParam = url.indexOf("&",paramEnd);
	var valLen = (nextParam == -1) ? (url.length-(valBegin)) : (nextParam-valBegin);
	var valEnd = (nextParam == -1) ? valBegin+valLen : nextParam;

    var valueBegin = paramEnd + 1
    var paramValue = url.substring(valueBegin, valEnd)

	if ( (nextParam != -1) && (valBegin > nextParam) ) return getParam(param,paramEnd);
	else {

		 for (i = 0; i < paramValue.length; i++) {
			 paramValue = paramValue.replace("+", " ");
			 paramValue = paramValue.replace("%20", " ");
			 paramValue = paramValue.replace("%26", "&");
			 paramValue = paramValue.replace("%27", "'");
			 paramValue = paramValue.replace("%28", "(");
			 paramValue = paramValue.replace("%29", ")");
		}

    return paramValue
	}
}
