// general function to validate a radio button group within a form. If any 
// button is checked, return true, otherwise focus on the first button and 
// return false. The two parameters are:
//   theForm: object of the form
//   theRadio: string value of the radio button group name

function radioValidator(theForm,theRadio) {
    var radioFlag = false;
    for (i = 0; i < theForm.elements[theRadio].length; ++i) {
        if (theForm.elements[theRadio][i].checked) {
            radioFlag = true;
            break;
        }
    }
    if (radioFlag == false) {
        theForm.elements[theRadio][0].focus();
    }
    return (radioFlag);
}

// main validation routine. Ensure something has been selected/entered for
// each question (optional text fields are not validated)

function validator(theForm) {
	if (radioValidator(theForm,"q1") == false) {
		alert("Please select one of the options in Question 1.");
		return (false);
	}

	if (radioValidator(theForm,"q2") == false) {
		alert("Please select one of the options in Question 2.");
		return (false);
	}

	if (radioValidator(theForm,"q3") == false) {
		alert("Please select one of the options in Question 3.");
		return (false);
	}

	if ((!theForm.q4_1.checked) && (!theForm.q4_2.checked) && (!theForm.q4_3.checked) && (!theForm.q4_4.checked) && (!theForm.q4_5.checked) && (!theForm.q4_6.checked) && (!theForm.q4_7.checked) && (!theForm.q4_8.checked) && (theForm.q4_9.value == "")) {
		alert("Please select one or more of the options in Question 4.");
		theForm.q4_1.focus()
		return (false);
	}

	if (radioValidator(theForm,"q5") == false) {
		alert("Please select one of the options in Question 5.");
		return (false);
		
	if (radioValidator(theForm,"q6") == false) {
		alert("Please select one of the options in Question 6");
		return (false);
		
	if (radioValidator(theForm,"q7") == false) {
		alert("Please select one of the options in Question 7.");
		return (false);
		
	if (radioValidator(theForm,"q8") == false) {
		alert("Please select one of the options in Question 8.");
		return (false);
	}

	return (true);
}
