/*
author: warren banks
date: feb.19, 2007
purpose:  displays questions based on user choice
*/

function get_radio_result(radioBtn)
{
	//get length of array
	var num_array_elements = radioBtn.length;

	//alert("Length of radio button array: "+num_array_elements);

	if(num_array_elements == undefined)
	{
		//check to see if element is checked
		if(radioBtn.checked == true)
		{
			//alert("radioBtn.value: "+radioBtn.value);
			return radioBtn.value;
		}
		else
		{
			//nothing checked so return false (empty)
			return '';
		}
	}
	else
	{
		//if number (NaN (Not A Number) returns false if value is a number)
		if(!isNaN(num_array_elements))
		{
			//there is a number so use it in 'for loop'
			for(i=0; i < num_array_elements; i++)
			{
				//examine radio button array
				if(radioBtn[i].checked == 'true')
				{
					return radioBtn[i].value;
				}
			}
		}
		else
		{
			//not a number so return false (empty)
			return '';
		}
	}
}


function question_one(radioBtn)
{
	//get value from button
	var Q1 = get_radio_result(radioBtn);
	
	//alert("q1 answer: "+Q1);
	
	//if yes
	if(Q1 == 'no')
	{
		//show Q2
		toggleBox('question_two', 1);
		
		//number of questions showing
		document.size_calc.q_showing.value = 2;		
	}
	else
	{
		//hide Q2 & Q3
		toggleBox('question_two', 0);
		toggleBox('question_three', 0);		
		//
		clear_radio_buttons('ceiling');		
		clear_radio_buttons('wall');
		
		//number of questions showing
		document.size_calc.q_showing.value = 1;
	}	
}


function question_two(radioBtn)
{
	//get value from button
	var Q2 = get_radio_result(radioBtn);
	
	//alert("q2 answer: "+Q2);
	
	//if yes
	if(Q2 == 'no')
	{
		//show Q3
		toggleBox('question_three', 1);
		
		//number of questions showing
		document.size_calc.q_showing.value = 3;		
	}
	else
	{
		//hide Q3
		toggleBox('question_three', 0);	
		clear_radio_buttons('wall');
		
		//number of questions showing
		document.size_calc.q_showing.value = 2;		
	}
}



function toggleBox(DivID, iState) // 1 visible, 0 hidden
{
	//alert("div id: "+DivID+" iState: "+iState);
	
	if(document.layers)	   //NN4+
	{
		//alert("NN4");
		document.layers[DivID].display = iState ? "inline" : "none";
	}
	else if(document.getElementById)	  //gecko(NN6) + IE 5+
	{
		//alert("NN6 & IE5");
		var obj = document.getElementById(DivID);
		obj.style.display = iState ? "inline" : "none";
	}
	else if(document.all)	// IE 4
	{
		//alert("IE4");
		document.all[DivID].style.display = iState ? "inline" : "none";
	}
}


function clear_radio_buttons(name_radio_buttons)
{
	//get length
	var length = eval("document.size_calc."+name_radio_buttons+".length");
	
	//alert("radioBtns.length: "+length);
	
	//shorten element name
	var radioBtn = eval("document.size_calc."+name_radio_buttons);
	
	//loop through radio button array and 'uncheck' all buttons	
	for(i=0; i<length; i++)
	{
		if(radioBtn[i].checked == true)
		{
			radioBtn[i].checked = false;
		}
	}
}


function load_page(num_questions, radio1, radio2, radio3)
{
	//alert("number of questions clicked: "+num_questions);
	
	if(IsVarEmpty(num_questions))
	{
		num_questions = 1;
	}
	
	//set radio buttons
	if(num_questions == 1)
	{
		//get value for radio buttons 1
		if(radio1 == "yes")
		{
			set_radio_button('open_window', radio1);
		}
		
		//make sure q2 & q3 are hidden
		toggleBox('question_two', 0);
		toggleBox('question_three', 0);

		//make sure all buttons are cleared
		clear_radio_buttons('open_window');
		clear_radio_buttons('ceiling');
		clear_radio_buttons('wall');
	}
	else if(num_questions == 2)
	{
		//alert("open_window: "+radio1+"; ceiling: "+radio2);
		
		//get value for radio buttons 1
		if(radio1 == "no")
		{
			set_radio_button('open_window', radio1);
		}

		//get value for radio buttons 2
		if(radio2 == "yes")
		{
			set_radio_button('ceiling', radio2);
		}
		
		//make sure q2 is showing
		toggleBox('question_two', 1);
		
		//make sure q3 is hidden
		toggleBox('question_three', 0);

		//make sure q3 is cleared
		clear_radio_buttons('wall');	
	}
	else if(num_questions == 3)
	{
		//alert("open_window: "+radio1+"; ceiling: "+radio2+"; wall: "+radio3);
		
		//get value for radio buttons 1
		if(radio1 == "no")
		{
			set_radio_button('open_window', radio1);
		}

		//get value for radio buttons 2
		if(radio2 == "yes")
		{
			set_radio_button('ceiling', radio2);
		}

		//get value for radio buttons 3
		set_radio_button('wall', radio3);
		//make sure q2 is showing
		toggleBox('question_two', 1);		
		//make sure q3 is showing
		toggleBox('question_three', 1);		
	}
	else
	{
		//make sure q2 & q3 are hidden
		toggleBox('question_two', 0);
		toggleBox('question_three', 0);

		//make sure q1, q2 & q3 are cleared
		clear_radio_buttons('open_window');
		clear_radio_buttons('ceiling');
		clear_radio_buttons('wall');	
	}

	//number of questions showing
	document.size_calc.q_showing.value = num_questions;	
}


//value should match the value assigned to one the radio buttons
function set_radio_button(name_radio_buttons, value)
{
	//alert("name: "+name_radio_buttons+"; value: "+value);
	
	//get length
	var length = eval("document.size_calc."+name_radio_buttons+".length");
	
	//alert("radioBtns.length: "+length);
	
	//shorten element name
	var radioBtn = eval("document.size_calc."+name_radio_buttons);
	
	//loop through radio button array and 'uncheck' all buttons	
	for(i=0; i<length; i++)
	{
		if(radioBtn[i].value == value)
		{
			radioBtn[i].checked = true;
		}
	}
}


//check to see if variable is empty
function IsVarEmpty(text)
{
	if (text == undefined || text == '')
	{
		return true;
	}
	else
	{
		return false;
	}
}