function checkbox_checker(e){
	var obj = document.getElementsByName(e);
	for (x=0; x<obj.length; x++){
		if (obj[x].checked){
			return true;
		}
	}
	return false;
}

function checkbox_counter(e){
	var obj = document.getElementsByName(e);
	totalChecked = 0;
	for (i=0; i<obj.length; i++){
		if (obj[i].checked){
			totalChecked = totalChecked + 1;
		}
	}
	//alert(totalChecked);
	return totalChecked;
}

function checkbox_group_checker(cbset) {
	var arrCB = cbset.split(",");
	//alert(arrCB.length);
	cbset_flag = 0;
	for (i=0; i<arrCB.length; i++) {
		//alert('step ' + i + '\n\n' + arrCB[i]);
		if (checkbox_checker(arrCB[i])) {
			cbset_flag = 1;
			break
		}
	}
	//alert(cbset_flag);
	if (cbset_flag == 1) {
		return true
	}
	return false;
}

function radio_checker(e){
	var obj = document.getElementsByName(e);
	for (i=0; i<obj.length; i++) { 
		if (obj[i].checked) { 
			return true
		} 
	}
	return false;
}


function validate() {
	var ErrorExists = 0;
	var ErrorHTML = '';
	var ErrorJS = '';
	arrError = new Array(4);
	arrError[0] = new Array();
	arrError[1] = new Array();
	arrError[2] = new Array();
	arrError[3] = new Array();
	
	var RowData, RD_Group, RD_Type, RD_Field, RD_Friendly;
	for (row=0; row<arrData.length; row++) {
		RowData = arrData[row].split("|");
		RD_Group    = RowData[0] - 1;
		RD_Type     = RowData[1];
		RD_Field    = RowData[2];
		RD_Friendly = RowData[3];
		
		if (RD_Type == 1) { //text
			if (document.getElementById(RD_Field).value=='') {
				arrError[RD_Group][arrError[RD_Group].length] = RD_Friendly + ' is a required field.';
				ErrorExists = 1;
			}
		}
		else if (RD_Type == 2) { //dropdown
			if (document.getElementById(RD_Field)[document.getElementById(RD_Field).selectedIndex].value=='') {
				arrError[RD_Group][arrError[RD_Group].length] = RD_Friendly + ' requires a selection.';
				ErrorExists = 1;
			}
		}
		else if (RD_Type == 3) { //checkbox
			if (checkbox_counter(RD_Field) < 1) {
				arrError[RD_Group][arrError[RD_Group].length] = RD_Friendly + ' requires at least one selection.';
				ErrorExists = 1;
			}
		}
		else if (RD_Type == 4) { //checkbox group
			if (!checkbox_group_checker(RD_Field)) {
				arrError[RD_Group][arrError[RD_Group].length] = RD_Friendly + ' requires at least one selection.';
				ErrorExists = 1;
			}
		}
		else if (RD_Type == 5) { //radio
			if (!radio_checker(RD_Field)) {
				arrError[RD_Group][arrError[RD_Group].length] = RD_Friendly + ' requires a selection.';
				ErrorExists = 1;
			}
		}
		else if (RD_Type == 6) { //text group
			var arrFields = RD_Field.split('+');
			var oneValue = 0;
			/* loop over the list of text fields and increment the oneValue counter for each field that has an entry */
			for (var tfpt=0; tfpt<arrFields.length; tfpt++) {
				if (document.getElementById(arrFields[tfpt]).value != '') {
					oneValue++;
				}
			}
			/* there should be only 1 field with a value, otherwise include the error message */
			if (oneValue != 1) {
				arrError[RD_Group][arrError[RD_Group].length] = RD_Friendly;
				ErrorExists = 1;
			}
		}
	}
	
	/* custom errors go here */
	
	
	if (ErrorExists) {
		for (row=0; row<arrError.length; row++) {
			if (arrError[row].length > 0) {
				rowdisplay = row + 1;
				ErrorHTML = ErrorHTML + '<span style="color:000099; font-weight:bold">Section ' + rowdisplay + ':</span><br>';
				ErrorJS = ErrorJS + '\n';
				//Old Line: ErrorJS = ErrorJS + 'Section ' + rowdisplay + ':\n';
				for (col=0; col<arrError[row].length; col++) {
					ErrorHTML = ErrorHTML + '&nbsp;&nbsp;<span style="color:cc0000">&#8226;</span>&nbsp;' + arrError[row][col] + '<br>';
					ErrorJS = ErrorJS + '  - ' + arrError[row][col] + '\n';
				}
			}
		}
		/* Error Display: choice of using an alert box or setting the innerHTML of a div tag */
		alert(ErrorJS);
		//errorMessage.innerHTML = ErrorHTML;
	}
	else {
		//alert('send it');
		return true
	}
	
	return false
}
