//**All javascript is used for the Dental Health Risk Assessments**//
//**Author: Mike Singer**//

//Variables Needed for Image toggle of toothbrush
var plus = new Image();
plus.src = "../images/toothbrush_blank.gif";
var minus = new Image();
minus.src = "../images/toothbrush_monotone.gif";

var currentQuestion=1;//Track question
var numQuestions=0;//Set in content jsp, value set in window.onload fuction
var validateCheck = false;//after form submit this changes, behavior of toggling images/divs differs after submit
var pbrush;//variable to turn off previous questions toothbrush div
var cbrush;//variable to toggle current questions toothbrush div
var ptext;//variable to toggle current questions addl-info paragraph
var panswer;//variable to set answer for question

//Called in content jsp
function setNumQuestions(num){
	numQuestions=num;
	document.getElementById('questionnum').value = num;
	//alert(numQuestions);
}
//Sets hidden form value called on content jsp
function getNumQuestions(){
	return numQuestions;
}
//Hides and shows div
function toggle(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}
//allows you to turn on/off instead of just toggling
function setToggle(obj,showHide){
	var el = document.getElementById(obj);
	if(showHide == 0){
		el.style.display = '';
	}
	else if(showHide == 1){
		el.style.display = 'none';
	}
}

//switches between spacer graphic and toothbrush graphic
function toggleImage(imgName){
	document.images[imgName].src = (document.images[imgName].src==plus.src) ? minus.src:plus.src;
}

//allows switching graphic by argument instead of just toggling
function setImage(imgName,showBrush){
	if(showBrush == 0){
		document.images[imgName].src = minus.src;
	}
	else if(showBrush == 1){
		document.images[imgName].src = plus.src;
	}
}
function disableMouseWheel()//Function to disable mousewheel changing of questions
{
	var sel = document.getElementsByTagName('select');
	for(var i=0;i<sel.length;i++){
		sel[i].onmousewheel=function(){return false}
	}
}
//called in the select boxes onchange, sends in the next question number
function nextQuestion(qnum) {
			//alert('qnum ' + qnum + ' currentQuestion ' + currentQuestion);
	
	//Adult Survey Only - checking for cavity question
	/*
		Code no longer needed as question was removed at the request of TMA
	
	if(numQuestions == 12 && qnum==8){
		if(document.getElementById('answer6').selectedIndex == 1 && document.getElementById('answer7').selectedIndex == 4){
					alert('Error: you noted a history of cavities in the previous question. Please enter a valid response.');
					document.getElementById('answer7').selectedIndex = 0;
					document.getElementById('answer7').focus();
									
		}
		//If you pick no on answer 6, you must pick n/a on answer 7
		else if(document.getElementById('answer6').selectedIndex == 2 && validateCheck == false){
			
			document.getElementById('answer7').selectedIndex = 4;			
			document.getElementById('answer8').focus();
			cbrush = "tbrush" + (qnum);
			pbrush = "tbrush" + (qnum-1);
			ptext = "q-addl-text" + (qnum-1);
			//this can only happen once as it increments the question counter
			if(currentQuestion == 7){
				setImage(cbrush,0);
				setImage(pbrush,1);
				setToggle(ptext,0);
				currentQuestion++;
			}								
		}
		//Needed if answer is modified after validation
		else if(document.getElementById('answer6').selectedIndex == 2 && document.getElementById('answer7').selectedIndex != 4){
			document.getElementById('answer7').selectedIndex = 4;			
			document.getElementById('answer8').focus();
		}
		else if(currentQuestion == 7 && validateCheck == false) {
			pbrush = "tbrush" + (qnum-1);
			cbrush = "tbrush" + (qnum);
			ptext = "q-addl-text" + (qnum-1);
			panswer = "answer" + (qnum-1);
			
			setImage(cbrush,0);
			setImage(pbrush,1);
			setToggle(ptext,0);
			currentQuestion++;
		}
		
	}
	*/
	
	/*
			Code no longer needed as question was removed at the request of TMA
	
	//Child Survey Only - checking for cavity question
	if(numQuestions == 14 && qnum==9){
		if(document.getElementById('answer7').selectedIndex == 1 && document.getElementById('answer8').selectedIndex == 4){
					alert('Error: you noted a history of cavities in the previous question. Please enter a valid response.');
					document.getElementById('answer8').selectedIndex = 0;
					document.getElementById('answer8').focus();				
					
		}
		//If you pick no on answer 7, you must pick n/a on answer 8
		else if(document.getElementById('answer7').selectedIndex == 2 && validateCheck == false){
			
			document.getElementById('answer8').selectedIndex = 4;			
			document.getElementById('answer9').focus();
			cbrush = "tbrush" + (qnum);
			pbrush = "tbrush" + (qnum-1);
			ptext = "q-addl-text" + (qnum-1);
			//this can only happen once as it increments the question counter
			if(currentQuestion == 8){
				setImage(cbrush,0);
				setImage(pbrush,1);
				setToggle(ptext,0);
				currentQuestion++;
			}								
		}
		//Needed if answer is modified after validation
		else if(document.getElementById('answer7').selectedIndex == 2 && document.getElementById('answer8').selectedIndex != 4){
			document.getElementById('answer8').selectedIndex = 4;			
			document.getElementById('answer9').focus();
		}
		else if(currentQuestion == 8 && validateCheck == false) {
			pbrush = "tbrush" + (qnum-1);
			cbrush = "tbrush" + (qnum);
			ptext = "q-addl-text" + (qnum-1);
			panswer = "answer" + (qnum-1);
			
			setImage(cbrush,0);
			setImage(pbrush,1);
			setToggle(ptext,0);
			currentQuestion++;
		}
		
	}
	*/	
	
	//Runs if first time through and questions haven't been skipped
	if(validateCheck == false && ((qnum-1) == currentQuestion)){
		if(currentQuestion < qnum) {
			pbrush = "tbrush" + (qnum-1);
			cbrush = "tbrush" + (qnum);
			ptext = "q-addl-text" + (qnum-1);
			panswer = "answer" + (qnum-1);
			toggleImage(pbrush);
			toggleImage(cbrush);
			setToggle(ptext,0);
			currentQuestion++;
		}
	}
	
	//Error if question is skipped and isn't last question
	else if(validateCheck == false && ((qnum-1) != currentQuestion) && currentQuestion != numQuestions && qnum > currentQuestion){
		alert('Please complete current question before proceeding further.');
		var answerID = "answer" + (qnum-1);
		var currentAnswer = "answer" + currentQuestion;
		document.getElementById(answerID).selectedIndex = 0;
		document.getElementById(currentAnswer).focus();
			
	}
	//before submit, for last question
	else if(validateCheck == false && qnum == 0 && currentQuestion == numQuestions) 
	{
		ptext = "q-addl-text" + currentQuestion;
		cbrush = "tbrush" + currentQuestion;
		setToggle(ptext,0);
		setImage(cbrush,1);
	}
	//don't toggle toothbrush, just set it to disappear after q answered, happens after failed validation
	if(validateCheck == true && qnum != 0){
		ptext = "q-addl-text" + (qnum-1);
		cbrush = "tbrush" + (qnum-1);
		setToggle(ptext,0);
		setImage(cbrush,1);
	}
	//validation failed
	else if(validateCheck == true && qnum == 0)//After validation to hide toothbrush and show text 
	{
		ptext = "q-addl-text" + numQuestions;
		cbrush = "tbrush" + numQuestions;
		setToggle(ptext,0);
		setImage(cbrush,1);
	}	
} 

function resetSurvey()//called in window.onload using WOM
{
	var i;
	var ct;
	var ctSelect;
	for (i=1;i<=numQuestions;i++){
		ct = "q-addl-text" + i;
		toggle(ct);
	}
	var selects = document.forms["riskSurvey"].getElementsByTagName("SELECT");
		for (var i=0; i<selects.length; i++){
			selects.item(i).selectedIndex = 0;
	}
	toggleImage("tbrush1");
	document.riskSurvey.answer1.focus();
}

function checkForm()//Validation function if passed calls SurveyController
{
	validateCheck = true;
	var validateSelect = document.forms["riskSurvey"].getElementsByTagName("SELECT");
	var invalid = false;
	var cbrush;
	
	/*
		Code no longer needed as question was removed at the request of TMA
		
		if(numQuestions == 12 && document.getElementById('answer6').selectedIndex == 1 && document.getElementById('answer7').selectedIndex == 4)
			document.getElementById('answer7').selectedIndex = 0;
		if(numQuestions == 14 && document.getElementById('answer7').selectedIndex == 1 && document.getElementById('answer8').selectedIndex == 4)
			document.getElementById('answer8').selectedIndex = 0;	
	*/
	for (var i=0;i<validateSelect.length;i++){
		if(validateSelect.item(i).selectedIndex == 0){
			cbrush = "tbrush" + (i+1);
			setImage(cbrush,0);
			invalid = true;
		}
	}
	
	/*
		Code no longer needed as question was removed at the request of TMA
	
			//Needed in case validation is missed these answers must be changed
			if(numQuestions == 12 && document.getElementById('answer6').selectedIndex == 2 && document.getElementById('answer7').selectedIndex != 4)	
				document.getElementById('answer7').selectedIndex = 4;
			if(numQuestions == 14 && document.getElementById('answer7').selectedIndex == 2)	
				document.getElementById('answer8').selectedIndex = 4;
	
	*/
	if(invalid == true){
		alert("All questions must be answered to continue");	
		return false;
	}
	else{
		return true;
	}
}	




	

