// Form validation
// $Id: forms.js,v 1.4 2004/04/10 15:11:40 justin Exp $
var doit = false;

// validate form fields
function checkForm(fm) {
	var fld;
	var sel;
	for (var i=1; i<checkForm.arguments.length; i++) {
		eval("fld = fm."+ checkForm.arguments[i] +";");

		switch (fld.type) {
			case "radio":
			case "select-one":
			case "select-multiple":
				sel = 0;
				for (j=0; j<fld.options.length; j++) {
					if (fld.options[j].selected && fld.options[j].value != "") sel++;
				}

				if (sel == 0) {
					alert("Some of the required fields are missing");
					fld.focus();
					if (doit) { return; } else { return false; }
				}
				break;

			case "checkbox":
				if (fld.checked == false) {
					alert("Some of the required fields are missing");
					fld.focus();
					if (doit) { return; } else { return false; }
				}
				break;

			case "textarea":
			case "text":
			case "password":
				if (fld.value == "") {
					alert("Some of the required fields are missing");
					fld.focus();
					if (doit) { return; } else { return false; }
				}
				if (fld.name == "password" && fld.value != fm.password2.value) {
					alert("Password & Password Verify do not match");
					fld.focus();
					if (doit) { return; } else { return false; }
				}
				break;
		}
	}

	if (doit)
		fm.submit();
	else
		return true;
}


// clear the value of a select box
function clearSelect(ctl) {
	for (var i=0; i<ctl.options.length; i++) ctl.options[i].selected = false;
}


// set the value of a form field
function setFormValue(fld,val) {
		switch (fld.type) {
			case "radio":
			case "undefined":
			default:
				setRadio(fld,val);
				break;

			case "select-one":
			case "select-multiple":
				setOption(fld,val);
				break;

			case "checkbox":
				fld.checked = val;
				break;

			case "hidden":
			case "textarea":
			case "text":
			case "password":
				fld.value = val;
				break;
		}
}

// get the value of a form field
function getFormValue(fld) {
		switch (fld.type) {
			case "radio":
			case "undefined":
			default:
				return getRadio(fld);
				break;

			case "select-one":
			case "select-multiple":
				return getOption(fld);
				break;

			case "checkbox": // do we change the value or check it?
				return fld.checked;
				break;

			case "hidden":
			case "textarea":
			case "text":
			case "password":
				return fld.value;
				break;
		}
}

// set the selection of a select box or radio button group
function setOption(ctl,val) {
	for (var i=0; i<ctl.options.length; i++) ctl.options[i].selected = ctl.options[i].value == val;
}

// get the selected value of a select box
function getOption(ctl) {
	for (var i=0; i<ctl.options.length; i++) if (ctl.options[i].selected)  return ctl.options[i].value;
}

// set the selection of a radio button set
function setRadio(ctl,val) {
	for (var i=0; i<ctl.length; i++) ctl[i].checked = ctl[i].value == val;
}

// return the selected value of a radio button set
function getRadio(ctl) {
	for (var i=0; i<ctl.length; i++) if (ctl[i].checked) return ctl[i].value;
}
