// Form validation

// $Id: forms.js,v 1.1 2004/06/09 15:01:13 justin Exp $

var doit = false;

var regex_email = /(([A-Za-z0-9_\-\.]+)@([A-Za-z0-9\+_\-])+\.([A-Za-z0-9\+_\-\.])+)/;



// validate form fields

function checkForm(fm) {

	var intro = "The following required fields were not filled in correctly.\n";

	var msg = "";

	var fld;

	var fldFocus = false;

	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) {

					msg += checkForm.arguments[i] +"\n";

					if (!fldFocus) fldFocus = fld;

				}

				break;



			case "checkbox":

				if (fld.checked == false) {

					msg += checkForm.arguments[i] +"\n";

					if (!fldFocus) fldFocus = fld;

				}

				break;



			case "textarea":

			case "text":

			case "password":

				if (fld.value == "") {

					msg += checkForm.arguments[i] +"\n";

					if (!fldFocus) fldFocus = fld;

				}

				if (fld.name == "password" && fld.value != fm.password2.value) {

					msg += checkForm.arguments[i] +"\n";

					if (!fldFocus) fldFocus = fld;

				}

				break;

		}

	}



	if (msg.length > 0) {

		fldFocus.focus();

		alert(intro+msg);

		return false;

	} else {

		if (doit)

			fm.submit();

		else

			return true;

	}

}





// return true if field values match

function isMatch(fld1, fld2) {

	return getFormValue(fld1) == getFormValue(fld2);

}





// return true if field value is a valid email address

function isEmail(fld) {

	return fld.value.match(regex_email) != null;

}



// 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;

}



