var arrQuiz = new Array();
var intWindowSize = 18; //show 5 questions at a time
var intCurrentWindowPosition = 0; //initialize the starting window position
var intNumberOfWindows = null; //initialize the number of possible 'windows'

function objQuestion(strText) {
	this.textValue = strText;
	this.selection = null;
}

function initializeQuiz() {
	arrQuiz.push(new objQuestion("Do you make sure there are fresh veggies and fruit at home so the kids can grab them if  they choose?"));
	arrQuiz.push(new objQuestion("Do you eat at least one dark green and one orange vegetable a day?"));
	arrQuiz.push(new objQuestion("Do you and your family eat breakfast most days?"));
	arrQuiz.push(new objQuestion("Do you usually choose water, milk, or 100% pure juice over sugared beverages?"));
	arrQuiz.push(new objQuestion("Do you eat 4 meals a week (or more) together as a family?"));
	arrQuiz.push(new objQuestion("Do you believe that poor mental health can increase the risk of developing physical problems such as diabetes, heart disease, gastrointestinal problems and more?"));
	arrQuiz.push(new objQuestion("Do you laugh everyday?"));
	arrQuiz.push(new objQuestion("Do you sometimes take 10 minutes for yourself and tune everything else out?"));
	arrQuiz.push(new objQuestion("Do you try to make meal time fun for the whole family?"));
	arrQuiz.push(new objQuestion("Do you know your kids&rsquo; friends?"));
	arrQuiz.push(new objQuestion("Do your kids have adults in their lives (besides you) that they can talk to?"));
	arrQuiz.push(new objQuestion("Are your school-aged children and teens accumulating 90 minutes of physical activity on most days?"));
	arrQuiz.push(new objQuestion("Do you get at least 30 minutes of physical activity on 5 or more days per week?"));
	arrQuiz.push(new objQuestion("Do you encourage your kids to walk or bike instead of driving them or loaning them the car?"));
	arrQuiz.push(new objQuestion("Do you have rules to limit screen time in your home?"));
	arrQuiz.push(new objQuestion("Do you usually take the stairs instead of waiting for an elevator?"));
	arrQuiz.push(new objQuestion("Do you believe exercise can have a positive impact on our mental health?"));
	arrQuiz.push(new objQuestion("Do you keep smoke outside your home or car?"));
	
	intNumberOfWindows = parseInt(arrQuiz.length / intWindowSize);
	if ((arrQuiz.length % intWindowSize) > 1) {
		intNumberOfWindows++;
	}
	drawQuizQuestions();
}

function drawQuizQuestions() {
	var strOutput = "";
	var strNavigation = "";
	for (x=intCurrentWindowPosition; x<(intCurrentWindowPosition + intWindowSize); x++) {
		if (arrQuiz[x] != undefined) {
			strOutput = strOutput + "<span class='question'>" + arrQuiz[x].textValue + "</span>\n";
			var strNoCheckedValue = "";
			var strYesCheckedValue = "";
			if (arrQuiz[x].selection == 0) {
				strNoCheckedValue = " checked='checked'";
			} else if (arrQuiz[x].selection == 1) {
				strYesCheckedValue = " checked='checked'";
			}
			strRadioButtonContent = "<label for='qy" + x + "'>Yes</label> <input type='radio' value='1' name='q" + x + "' id='qy" + x + "'" + strYesCheckedValue + " />&nbsp;&nbsp;&nbsp;" + "<label for='qn" + x + "'>No</label> <input type='radio' value='0' name='q" + x + "' id='qn" + x + "'" + strNoCheckedValue + " />";
			strOutput = strOutput + "<span class='radio'>" + strRadioButtonContent + "</span><hr />";
		}
	}
	//add navigation buttons
	if ((intCurrentWindowPosition == 0) && (intNumberOfWindows > 1)) {
		strNavigation = "<input type='button' value='Next' onclick='JavaSc" + "ript:setCurrentWindowPosition(1);' />";
	} else if ((arrQuiz.length - intCurrentWindowPosition) > intWindowSize) {
		strNavigation = "<input type='button' value='Previous' onclick='JavaSc" + "ript:setCurrentWindowPosition(0);' />" + "<input type='button' value='Next' onclick='JavaSc" + "ript:setCurrentWindowPosition(1);' />";
	} else if ((intCurrentWindowPosition == 0) && (intNumberOfWindows == 1)) {
		strNavigation = "<input type='button' value='Submit' onclick='JavaSc" + "ript:setQuestionResponseValues();getQuizResults();' /> <input type='reset' value='Reset' />";
	} else {
		strNavigation = "<input type='button' value='Previous' onclick='JavaSc" + "ript:setCurrentWindowPosition(0);' />" + "<input type='button' value='Submit' onclick='JavaSc" + "ript:setQuestionResponseValues();getQuizResults();' /> <input type='reset' value='Reset' />";
	}
	strNavigation = "<div class='formButtons'>" + strNavigation + "</div>";
	strIntro = "<p>How do you rate as a role model when it comes to wellness? Take two minutes to complete this quiz! When you know the score, you can start to take simple steps to do more to improve you and your family&rsquo;s health and well-being.</p>";
	document.getElementById("quizContents").innerHTML = strIntro + strOutput + strNavigation;
}

function setCurrentWindowPosition(intWhichDirection) {
	//save current radio button values
	setQuestionResponseValues();
	if (intWhichDirection == 1) {
		intCurrentWindowPosition = intCurrentWindowPosition + intWindowSize;
	} else {
		intCurrentWindowPosition = intCurrentWindowPosition - intWindowSize;
	}
	drawQuizQuestions();
}

function setQuestionResponseValues() {
	for (x=intCurrentWindowPosition; x<(intCurrentWindowPosition + intWindowSize); x++) {
		if (arrQuiz[x] != undefined) {
			if (eval("document.quizForm.q" + x + "[0].checked")) {
				arrQuiz[x].selection = 1;
			} else if (eval("document.quizForm.q" + x + "[1].checked")) {
				arrQuiz[x].selection = 0;
			}
		}
	}
}

function getQuizResults() {
	intQuizResult = 0;
	var strQuizResultComment = "";
	for (x=0; x < arrQuiz.length; x++) {
		intQuizResult = intQuizResult + arrQuiz[x].selection;
	}
	switch(true) {
		case (intQuizResult < 10):
			strQuizResultComment = "<strong class='quizResult'>You Have Lots Of Room For Improvement</strong><img src='images/quiz_result_poor.gif' alt='Poor'><p>It&rsquo;s time to jump in and take action. The steps you take today towards influencing your family&rsquo;s eating choices, physical activity levels and overall mental well-being, will have a major impact their overall health and wellness in the years ahead! Get wellness soon!</p>";
			break;
		case ((intQuizResult > 10) && (intQuizResult < 15)):
			strQuizResultComment = "<strong class='quizResult'>You&rsquo;re On The Right Track</strong><img src='images/quiz_result_average.gif' alt='Average'><p>It&rsquo;s easy to start taking small steps to improve your score and become a better role model. Remember, you hold the ropes when it comes to influencing your family&rsquo;s healthy eating choices, physical activity levels and overall mental well-being. Spread the word on wellness in New Brunswick!</p>";
			break;
		case (intQuizResult > 14):
			strQuizResultComment = "<strong class='quizResult'>You&rsquo;re A Good Role Model</strong><img src='images/quiz_result_good.gif' alt='Good'><p>When it comes to improving the health of children, role models play a key important role, influencing healthy eating choices, physical activity levels and overall mental well-being. Keep up the good work! Spread the word on wellness in New Brunswick!</p>";
			break;
	}
	strIntro = "<p>Thank you for taking the time to fill out the Wellness quiz. Here are the results:</p>";
	document.getElementById("quizContents").innerHTML = strIntro + strQuizResultComment + "<ul><li><a href='takethequiz.cfm'>Take the quiz again</a></li></ul>";
}
