// Name   : emptyFieldChecker (v1.0)
// Args   : none
// Desc   : This JavaScript object checks for empty fields in a form.
// Author : Gerrie Kimpen (c) Katholieke Hogeschool Kempen - 1999
////////////////////////////////////////////////////////////////////////////////

function doAddRadioButton(name, errorMsg)
{
	if (this.emptyFieldDetected) return;
	var radioObject = this.theForm.elements[name], i;
	if (radioObject.length){
		for (i = 0; i < radioObject.length; i++)
			if (radioObject[i].checked) break;
		this.emptyFieldDetected = !(i < radioObject.length);
	}
	else this.emptyFieldDetected = !(radioObject.checked);
	if (this.emptyFieldDetected) alert(errorMsg);
}

function doAddCheckBox(name, errorMsg) {
	if (this.emptyFieldDetected) return;
	var checkBoxObject = this.theForm.elements[name], i;
	if (checkBoxObject.length){
		for (i = 0; i < checkBoxObject.length; i++)
			if (checkBoxObject[i].checked) break;
		this.emptyFieldDetected = !(i < checkBoxObject.length);
	}
	else this.emptyFieldDetected = !(checkBoxObject.checked);
	if (this.emptyFieldDetected) alert(errorMsg);
}

function doAddListBox(name, errorMsg) {
	if (this.emptyFieldDetected) return;
	var listBoxObject = this.theForm.elements[name], i;
	for (i = 0; i < listBoxObject.length; i++)
		if (listBoxObject.options[i].selected) break;
	this.emptyFieldDetected = !(i < listBoxObject.length);
	if (this.emptyFieldDetected) alert(errorMsg);
}

function doAddTextField(name, errorMsg) {
	if (this.emptyFieldDetected) return;
	var textObject = this.theForm.elements[name];
	this.emptyFieldDetected = (textObject.value == "");
	if (this.emptyFieldDetected) 
		{
		textObject.focus();
		alert(errorMsg);
		}
}

function doCheckForm() {
	if (this.emptyFieldDetected) {
		this.emptyFieldDetected = false; // set back to false
		return false; }
	else {
		this.emptyFieldDetected = false; // set back to false
		return true; }
}

function emptyFieldChecker(formToCheck) {
	// properties
	this.theForm = formToCheck;
	this.emptyFieldDetected = false;
	//methods
	this.addTextField	= doAddTextField;
	this.addListBox		= doAddListBox;
	this.addCheckBox	= doAddCheckBox;
	this.addRadioButton	= doAddRadioButton;
	this.checkForm		= doCheckForm;
}
