var current_element = new Object();
var customizedJS = false;

function check_and_submit(){
        f = document.forms[0];

//        if (f["_STAGE_"]){
//           if (f["_STAGE_"].value == "1"){
//               if ((f.qB[15].checked) && ((f["oth'qB"].value == null) || (f["oth'qB"].value == ""))){
//                    alert("Please specify for question B");
//                    return false;
//               }
//           }
//        }

//        if (f["_STAGE_"]){
//           if (f["_STAGE_"].value == "2"){
//               if ((f.q6[15].checked) && ((f["oth'q6"].value == null) || (f["oth'q6"].value == ""))){
//                    alert("Please specify for question 6");
//                    return false;
//               }
//           }
//        }

        for (i=0;i<f.elements.length;i++){

                if (f.elements[i].name.indexOf("^h") > -1){

                        current_element.hidden_name = f.elements[i].name;
                        current_element.question_name = f.elements[i].name.substring(0,f.elements[i].name.indexOf("^"));
                        current_element.value = f.elements[i].value;
                        current_element.type = f.elements[i].value.substring(0,1);
                        current_element.required = f.elements[i].value.substring(1,2);

                        if (current_element.required == "1"){

                                if (current_element.type == "m"){  // this is a menu
                                        // this is a bit tricky because menu"s can either be radio or options
                                        var is_nonempty = false;
                                        var _menu = f[current_element.question_name];

                                        if (_menu.length > 0 && _menu[0].type == "radio"){
                                                for (j=0;j<_menu.length;j++){
                                                        if (_menu[j].checked){
                                                                is_nonempty = true;
                                                        }
                                                }
                                        }
                                        else if (_menu.type == "select-one"){
                                                if (_menu.selectedIndex > 0){
                                                        is_nonempty = true;
                                                }
                                        }
                                        else{
                                                is_nonempty = true; // we already have this - it"s hidden and it should be nonempty
                                        }
                                        if (!is_nonempty){
                                             if (f._elems){
                                                  alert("Please answer all the questions on this page");
                                                  return is_nonempty;
                                             }
                                        	else if (current_element.question_name.substring(0,1) == "q"){
                                             	alert("Please answer question " + current_element.question_name.substring(1));
                                             	return is_nonempty;
                                             }
                                             else if (current_element.question_name.substring(0,1) == "x"){
                                             	alert("Please fill in your " + current_element.question_name.substring(1));
                                             	return is_nonempty;
                                             }
									else if (current_element.question_name.substring(0,4) == "oe'q"){
                                             	alert("Please answer question " + current_element.question_name.substring(5));
                                             	return is_nonempty;
                                             }
									else if (current_element.question_name.substring(0,5) == "oth'q"){
                                             	alert("Please answer question " + current_element.question_name.substring(6));
                                             	return is_nonempty;
                                             }
                                             else{
                                             	alert("Please answer all the questions on the page.");
                                             	return is_nonempty;
                                             }
                                        }
                                }

                                else if (current_element.type == "u"){  // this is a multipunch

                                        var choices = (current_element.value.substring(current_element.value.indexOf(";") + 1)).split(",");
                                        var is_nonempty = false;

                                        for (j=0;j<choices.length;j++){
                                                if ( f[current_element.question_name + "^~" + choices[j]] ){
                                                        if ((f[current_element.question_name + "^~" + choices[j]].type) == "hidden"){
                                                                is_nonempty = true;
                                                                break;
                                                        }
                                                        else if ((f[current_element.question_name + "^~" + choices[j]].type) == "checkbox"){
                                                                if (f[current_element.question_name + "^~" + choices[j]].checked){
                                                                        is_nonempty = true
                                                                        break;
                                                                }
                                                        }
                                                }
                                        }

                                        if (!is_nonempty){

                                             if (f._elems){
                                                  alert("Please answer all the questions on this page");
                                                  return is_nonempty;
                                             }

                                        	else if (current_element.question_name.substring(0,1) == "q"){
                                             	alert("Please answer question " + current_element.question_name.substring(1));
                                             	return is_nonempty;
                                             }
                                             else if (current_element.question_name.substring(0,1) == "x"){
                                             	alert("Please fill in your " + current_element.question_name.substring(1));
                                             	return is_nonempty;
                                             }
									else if (current_element.question_name.substring(0,4) == "oe'q"){
                                             	alert("Please answer question " + current_element.question_name.substring(5));
                                             	return is_nonempty;
                                             }
									else if (current_element.question_name.substring(0,5) == "oth'q"){
                                             	alert("Please answer question " + current_element.question_name.substring(6));
                                             	return is_nonempty;
                                             }
                                             else{
                                             	alert("Please answer all the questions on the page.");
                                             	return is_nonempty;
                                             }
                                        }
                                }

                                else if (current_element.type == "n"){  // this is a numeric
                                        if (f[current_element.question_name].value == "" || (!f[current_element.question_name].value)){

                                             if (f._elems){
                                                  alert("Please answer all the questions on this page");
                                                  return false;
                                             }

                                        	else if (current_element.question_name.substring(0,1) == "q"){
                                             	alert("Please answer question " + current_element.question_name.substring(1));
                                             	return false;
                                             }
                                             else if (current_element.question_name.substring(0,1) == "x"){
                                             	alert("Please fill in your " + current_element.question_name.substring(1));
                                             	return false;
                                             }
									else if (current_element.question_name.substring(0,4) == "oe'q"){
                                             	alert("Please answer question " + current_element.question_name.substring(4));
                                             	return false;
                                             }
									else if (current_element.question_name.substring(0,5) == "oth'q"){
                                             	alert("Please answer question " + current_element.question_name.substring(5));
                                             	return false;
                                             }
                                             else{
                                             	alert("Please answer all the questions on the page.");
                                             	return false;
                                             } 
                                        }
                                        else if (isNaN(parseFloat(f[current_element.question_name].value))){
                                                alert("Question " + current_element.question_name.substring(1) + " must be a number.  You have " + f[current_element.question_name].value);
                                                return false;
                                        }
                                        // do range checking
                                        else if (current_element.value.substring(2,3) == 1){
                                                var ranges = (current_element.value.substring(3)).split(",");
                                                var low_range = parseInt(ranges[0]);
                                                var high_range = parseInt(ranges[1]);
                                                if (!isNaN(low_range) && !isNaN(high_range)){
                                                        if ((low_range > parseFloat(f[current_element.question_name].value)) || (high_range < parseFloat(f[current_element.question_name].value))){
                                                                alert("Question " + current_element.question_name.substring(1) + " must be between " + low_range + " and " + high_range + ".  You have " + f[current_element.question_name].value + ".");
                                                                return false;
                                                        }
                                                }
                                        }
                                }
                                else if (current_element.type == "s" || current_element.type == "o"){  // this is a string
                                        if (f[current_element.question_name].value == "" || (!f[current_element.question_name].value)){
                                             if (f._elems){
                                                  alert("Please answer all the questions on this page");
                                                  return false;
                                             }

                                        	else if (current_element.question_name.substring(0,1) == "q"){
                                             	alert("Please answer question " + current_element.question_name.substring(1));
                                             	return false;
                                             }
                                             else if (current_element.question_name.substring(0,1) == "x"){
                                             	alert("Please fill in your " + current_element.question_name.substring(1));
                                             	return false;
                                             }
									else if (current_element.question_name.substring(0,4) == "oe'q"){
                                             	alert("Please answer question " + current_element.question_name.substring(4));
                                             	return false;
                                             }
									else if (current_element.question_name.substring(0,5) == "oth'q"){
                                             	alert("Please answer question " + current_element.question_name.substring(5));
                                             	return false;
                                             }
                                             else{
                                             	alert("Please answer all the questions on the page.");
                                             	return false;
                                             }          
                                        }
                                }
                        }
                }
        }
        
        if (customizedJS){
           return cJS();
        }
        
        
        return true;
}
