// validate a form
function validateForm(theForm) {
	var numFields = theForm.length;
	var fieldValue;
	var errors = '';
	var reqs = 0;
	var confs = 0;
	var isValid = true;
	
	for (i = 0; i < numFields; i++) {
	
		// check required fields
		if (theForm[i].name.indexOf('_req') != -1) {
			// set fieldVal based on type of input
			switch (theForm[i].type) {
				case 'text':
					fieldVal = theForm[i].value; 
					break;
				case 'textarea':
					fieldVal = theForm[i].value;
					break;
				case 'file':
					fieldVal = theForm[i].value;
					break;
				case 'checkbox':
					break;
				case 'radio':
					break;
				case 'select-one':
					fieldVal = theForm[i].options[theForm[i].selectedIndex].value;
					break;
				default: break;
			}
			
			if (fieldVal == '') { reqs++; }
			// check if value is a blank string
			else {
				isBlank = true;
				for (j = 0; j < fieldVal.length; j++) {
					if (fieldVal.charAt(j) != ' ') { isBlank = false; }
				}
				if (isBlank) { reqs++; }
			}
		}
		
		// check confirmed fields
		if (theForm[i].name.indexOf('_conf') != -1) {
			matchName = theForm[i].name.slice(0,theForm[i].name.indexOf('_conf'));
			matchNum = 0;
			// find field it should match
			for (k = 0; k < numFields; k++) {
				if (theForm[k].name == matchName) { matchNum = k; }
			}
			if (theForm[i].value != theForm[matchNum].value) { confs++; }
		}
	}
	
	// build error string
	if (reqs > 0) { errors += 'One or more required fields are blank.\n'; }
	if (confs > 0) { errors += 'One or more confirmed fields do not match.\n'; }
	
	// if errors, alert and dont submit
	if (errors.length > 0) {
		alert(errors);
		isValid = false;
	}
	
	return isValid;
}
		
