﻿function SubmitForm() {
    //check required fields

    var intQuestionCount = 1;
    var boolReqFieldsOK = true;
    while (document.getElementById("hidQuestionID_" + intQuestionCount) != null) {
        if (document.getElementById("hidRequired_" + intQuestionCount) != null) {

            switch (document.getElementById("hidQuestionType_" + document.getElementById("hidQuestionID_" + intQuestionCount).value).value)
			{
			    case "o":
			        var intAnswerCount = 1;
			        var boolThisReqFieldOK = false;
			        while (document.getElementById("radAnswer_" + intQuestionCount + "_" + intAnswerCount) != null) {
			            if (document.getElementById("radAnswer_" + intQuestionCount + "_" + intAnswerCount).checked == true) {
			                boolThisReqFieldOK = true;
			                break;
			            }
			            intAnswerCount++;
			        }

			        if (!boolThisReqFieldOK) {
			            boolReqFieldsOK = false;
			        }

			        break;
			        
			    case "m":
			        var intAnswerCount = 1;
			        var boolThisReqFieldOK = false;
			        while (document.getElementById("chbAnswer_" + intQuestionCount + "_" + intAnswerCount) != null) {
			            if (document.getElementById("chbAnswer_" + intQuestionCount + "_" + intAnswerCount).checked == true) {
			                boolThisReqFieldOK = true;
			                break;
			            }
			            intAnswerCount++;
			        }

			        if (!boolThisReqFieldOK) {
			            boolReqFieldsOK = false;
			        }

			        break;
			        
			    case "r":
			        var intRowCount = 1;
			        //loop through rows (because this field is requied, all rows must have an answer
			        while (document.getElementById("radAnswer_" + intQuestionCount + "_" + intRowCount + "_1") != null) {
			            var intColCount = 1;
			            var boolThisReqFieldOK = false;
			            while (document.getElementById("radAnswer_" + intQuestionCount + "_" + intRowCount + "_" + intColCount) != null) {
			                if (document.getElementById("radAnswer_" + intQuestionCount + "_" + intRowCount + "_" + intColCount).checked == true) {
			                    boolThisReqFieldOK = true;
			                    break;
			                }
			                intColCount++;
			            }

			            if (!boolThisReqFieldOK) {
			                boolReqFieldsOK = false;
			            }

			            intRowCount++;
			        }

			        break;

			    case "d":
			        if (document.getElementById("selAnswer_" + intQuestionCount).value == "") {
			            boolReqFieldsOK = false;
			        }

			        break;

			    case "t":
			        if (document.getElementById("taAnswer_" + intQuestionCount).value == "") {
			            boolReqFieldsOK = false;
			        }
					
					break;

			}
            
            
        }

        intQuestionCount++;
    }

    if (!boolReqFieldsOK) {
        window.alert("Please answer all of the required fields in the survey.");
    }
    else {
        document.form1.submit();
    }
}
