// initialize variables


var questionform = $('questionsform');
var testtaken = false;
var yeses = 0;
var nos = 0;
var dn = 0;
var questionArray = [];
var total = 11;
var modalTitle = (lang=='en') ? 'Onmemory - Printout' : 'Sur la m&eacute;moire - Test de m&eacute;moire'
var begin = function(){
	$('intro').hide();
	$('qsection1').removeClassName("hidden");
	pageTracker._trackPageview(location.pathname + 'start/');
}

var nextquestion = function(ind){

	currentRadio = 'question' + ind;
	nextQuestionSection = 'qsection' + (ind+1);
	var selected = '';

	var radioboxes = $(currentRadio);

	var selected = $$('input:checked[type="radio"][name='+ currentRadio +']').pluck('value');

	//return error message if nothing clicked
	if (selected== '') {
		$('qheader').removeClassName("hidden"); // show error message
		return;
	} else {
		// add up yes, no, don't know
		if (selected.toString() == '0' ) yeses++;
		if (selected.toString() == '1' ) nos++;
		if (selected.toString() == '2' ) dn++;	// put response in an array
		questionArray.push(selected);

		var answer;
		switch(selected.toString()){
			case "0":
				answer = "Yes";
				break;
			case "1":
				answer = "No";
				break;
			case "2":
				answer="DK";
				break;
		}

		pageTracker._trackPageview(location.pathname + 'Question' + (ind) + '/' + answer + '/');

		$$(".questionsection").invoke("addClassName","hidden");
		$('qheader').addClassName("hidden");
		if (ind != total) $(nextQuestionSection).removeClassName("hidden");
	}


	if(ind == total){ //  show finish
		testtaken = true;
		$$(".questionsection").invoke("addClassName","hidden");
		$('qheader').addClassName("hidden");

		//google stuff
		$("google-conversion-target").src = siteroot + "elements/ga/complete_tracker.html";
		$('google-pixel').src = 'http://ace-tag.advertising.com/action/type=242763690/bins=1/rich=0/mnum=1516/site=695501/logs=0/betr=pfizpix_cs=1';

		var memtestpath=location.pathname.replace(/memory_test.*/gi,"memory_test/");
		// set display message if user got <> 5 yeses
		if (yeses >= 5) {
			$("finish").removeClassName("hidden");
			$("greaterthan5").removeClassName("hidden");
			$("finish_ga").src=memtestpath + "finished";
			pageTracker._trackPageview(location.pathname + "results_over/")
			pageTracker._setVar('memorytest_over')
		}
		else {
			$("finish").removeClassName("hidden");
			$("lessthan5").removeClassName("hidden");
			$("finish_ga").src=memtestpath + "finished";
			pageTracker._trackPageview(location.pathname + "results_under/")
			pageTracker._setVar('memorytest_under')
		}
		//alert(questionArray)
	}


}

var printout = function(){
	$('google-pixel').src = '';
	// set all checked img sources based on array
	questionArray.each(function(ele, index){
		$("printquestion" + (index+1)).down("img", Number(ele)).src = siteroot + "local/images/checkicon.gif";
	});
	// set totals
	$("printtotals").down("h4",1).innerHTML = yeses;
	$("printtotals").down("h4",2).innerHTML = nos;
	$("printtotals").down("h4",3).innerHTML = dn;

	$$('.layout')[0].addClassName('printmemory');  // for print settings
	Modal.open({contentEl:'printpage', width:'900', modaltitle: modalTitle });
	if (testtaken){
		if (yeses >= 5) {
			pageTracker._trackPageview(location.pathname + "results_over/print/")
		} else {
			pageTracker._trackPageview(location.pathname + "results_under/print/")
		}
	}

	//$('modalmask').style.display="none"; // hide modal mask
	window.print();

	//sem google stuff
	$("google-conversion-target").src = siteroot + "elements/ga/print_tracker.html";

}

