prodrec = {};

document.observe("dom:loaded", function() {
	prodrec.aCount = 0;
	prodrec.answerSelectArray = [];

	initProdRecForm();
});

function initProdRecForm() {
	var prodRecForm = $('prodrec');	
	var qNodes = $$('.question_column');
	// iterate through each question
	qNodes.each(function(ele,qindex) {
		// select the question_answer div from each question
		var qBox = ele.select('.question_num');	
		//iterate through to add the click observe
		qBox.each(function(a,index) {
			// set the question number to pass
			a.parentIndex = qindex;
			// set the array to pass
			a.parentArray = qBox;
			// set answer on or off and make sure everything is unchecked
			a.select('input')[0].checked = false;
			a.isOn = false;
			$('q1val').value = '';
			$('q2val').value = '';
			$('q3val').value = '';
			$('q4val').value = '';
			// set the click observe to call form check function
			// which passes question number (a.parentIndex)			
			// question array (a.parentArray)
			// answer selected node (a)
			// answer selected index (index)
			a.observe('click', function() {
	 			prodRecCheck(a.parentIndex,a,index,a.parentArray);
	 		});
		});
		
	});
}

function prodRecCheck(question,answer,answer_num,answerArray) {
	// checks which question number and calls corresponding function
	switch(question) {
		case 0:
			setSingleAnswers(answer,answerArray,question);
			break;
		case 1:
			setDoubleAnswers(answer,answerArray,question);
			break;
		case 2:
			setSingleAnswers(answer,answerArray,question);
			break;
	}
	
		// console.log('question1 = ' + $('q1val').value)
//   		console.log('question2 = ' + $('q2val').value)
//   		console.log('question3 = ' + $('q3val').value)
// 	  	console.log('question4 = ' + $('q4val').value)
}

function setDoubleAnswers(answer,answerArray,question) {
	// goes through each answer and detects if the question is on or not already	
	// and sets the answerSelectArray depending on whether the answer was selected or not
	answerArray.each(function(a,index) {
		// if answer does not equal what was clicked, it is reset
		if (a != answer) {
			if (a.isOn == false) {
				a.select('input')[0].checked = false;
				a.isOn = false;
			} 
		} else {
			// if it does equal what was clicked, add the index to array
			if (index != prodrec.answerSelectArray[0]) {
				prodrec.answerSelectArray.unshift(index);
			}
			// if the question was already selected, go through this loop			
			if (a.isOn) {
				prodrec.aCount--;
				a.select('input')[0].checked = false;
				a.isOn = false;		
				// if the total boxes checked is 2 or greater
				if (prodrec.aCount == 1) {
					// if the question clicked is the same as what is selected, use
					// the 3 index in the array instead of the second
					if (index == prodrec.answerSelectArray[1]) {
						$('q2val').value = prodrec.answerSelectArray[2] + 1;						
					} else {
						$('q2val').value = prodrec.answerSelectArray[1] + 1;					
					}
					$('q4val').value = '';
				} else if (prodrec.aCount == 0) {
					// if nothing is selected, reset answers
					$('q2val').value = '';
					$('q4val').value = '';
					prodrec.answerSelectArray=[];	
				} 	
			} else {			
				if (prodrec.aCount != 2) {				
					prodrec.aCount++;
				}
				a.select('input')[0].checked = true;
				a.isOn = true;
				// if the total boxes checked is equal to 2, set the first and second
				// array item to the the first and second index of array
				if (prodrec.aCount == 2) {
					// makes sure the array is long enough to reset the 3 item
					if (prodrec.answerSelectArray.length > 2) {
						if (prodrec.answerSelectArray[2] != index) {
							answerArray[prodrec.answerSelectArray[2]].select('input')[0].checked = false;
							answerArray[prodrec.answerSelectArray[2]].isOn = false;	
						}
					}
					$('q2val').value = prodrec.answerSelectArray[1] + 1;
					$('q4val').value = prodrec.answerSelectArray[0] + 1;
					prodrec.aCount = 2;
				} else if (prodrec.aCount == 1) {
					$('q2val').value = prodrec.answerSelectArray[0] + 1;
					$('q4val').value = '';
				} else {
					$('q2val').value = '';
				}
				
			}
		}
	});
}

function setSingleAnswers(answer,answerArray,question) {
	// goes through each answer and detects if the question is on or not already
	answerArray.each(function(a,index) {
		// if this question isn't on it will reset it
		if (a != answer) {
			//a.select('span')[0].setStyle({'backgroundPosition':'0 0'});
			a.select('input')[0].checked = false;
			a.isOn = false;
		} else {
			// if this question matches what was selected, it will either delete
			// the value or add the value if it was already selected
			if (a.isOn) {
				$('q'+(question+1)+'val').value = '';
				//a.select('span')[0].setStyle({'backgroundPosition':'0 0'});
				a.select('input')[0].checked = false;
				a.isOn = false;			
			} else {
				$('q'+(question+1)+'val').value = index + 1;
				//a.select('span')[0].setStyle({'backgroundPosition':'0 -34px'});
				a.select('input')[0].checked = true;
				a.isOn = true;			
			}
		} 		
	});
}

function initSubmitValidation() {
	if ($('q1val').value != '' && $('q2val').value != '' && $('q3val').value != '') {
		this.document['prodrec'].submit(); 
		$$('.error_msg')[0].removeClassName('on');
	} else {
		$$('.error_msg')[0].addClassName('on');
	}
}
