// File: forms.js
// Contains Functions used by forms.

// Function: checkFields(string, array, function pointer)
// Parameters: 1. The form to check (a string)
//			   2. The names of the required fields (an array of strings)
//			   3. A function to be called if an empty field is encountered
// 			   The strings are names of fields which must be filled. The function checks them against the form, and
//			   returns false if any one wasn't filled, otherwise it returns true.
//
function checkFields(strFormName, arrayFields, funcCallback) {
	var frmObj;
	frmObj = getFormObj(strFormName);

	// Verify that expected arguments were given:
	if (!strFormName || !arrayFields) {
		return false;
	}
	if (frmObj) {
		// Form exists. Go ahead and check for fields.
		for (var j=0; j<arrayFields.length; j++) {
			// If field name is null, field doesn't exist, or field is empty, return false:
			if (!arrayFields[j] || !frmObj[arrayFields[j]] || frmObj[arrayFields[j]].value == "") {
				if (!funcCallback) {
					return false;
				}
				else {
					return funcCallback(arrayFields[j]);
				}
			}
			// Todo: add hook for validator function here.
		}
		// If everything checks out, go ahead and submit the form:
		frmObj.submit();
	}
	else {
		return false;
	}
}

// Function: getForm(string)
// Parameter: The name of the Form
// Returns: A Form object if a valid form name is passed,
//          Else, returns null
//
function getFormObj(strFormName) {
	var obj;
	
	// ToDo: use proper sniffer code.
	if (document.getElementById) {
		obj = document.getElementById(strFormName);
	}
	else if (document.all) {
		obj = document.all[strFormName];
	}
	else if (document.forms) {
		obj = document.forms[strFormName];
	}
	else {
		obj = null;
	}
	return obj;
}
