/***********************************************************************
	Functions for checking if required fields have been filled in
	started: 5/2/2003
	last update: 6/2/2003
	
	(c) 2003, A. Craig. All rights reserved.
*************************************************************************/

/**************************************************
	Checks whether the text fields lists in fields
	are empty or not
***************************************************/
function check_txt_fields(form, fields) {
	var errs = new String();
	var num_fields = fields.length;
	
	//Loop through the fields list
	for ( var i = 0; i < num_fields; i++ ) {
		if (form[fields[i][0]].value == "") {
			errs += fields[i][1] + "\n";
		}
	} //end for 
	
	return errs;
} //end check_txt_fields

/**********************************************************
	Checks whether one of the check boxes listed in fields
	has been checked
***********************************************************/
function check_chk_fields(form, fields) {
	var errs = new String();
	var checked = false;
	var num_fields = fields.length;
	
	//Loop through the list
	for ( var i = 0; i < num_fields; i++ ) {
		if ( form[fields[i][0]].checked ) {
			checked = true;
			break;
		} //end if
	} //end for
	
	if ( !checked ) {
		for ( var i = 0; i < num_fields; i++ ) {
			errs += fields[i][1] + "\n";
		} //end for
	} //end if
	
	return errs;
} //end check_chk_fields()

/*************************************************
	Wrapper function calls the above functions
	and displays any errors found to the user
	
	The chk_fields arg is optional.
**************************************************/
function check_required(form, txt_fields, chk_fields) {
	var arg_len = arguments.length;
	var err_str = new String();
	var submit = true;
	
	err_str = check_txt_fields(form, txt_fields);
	if ( arg_len == 3 ) { //chk_fields variable given: verify the required checkboxes
		err_str += check_chk_fields(form, chk_fields);
	} //end if
	
	if (err_str.length > 0) {
		alert("INPUT ERROR:\nPlease fill in the following required fields:\n" + err_str);
		submit = false;
	} //end if
	
	return submit;
} //end check_required