/**
* Some common, reusable form validation functions
*/

/**
*-------------------------------------------------------------------------------
* VERSION:  $Id: form_validation.js,v 1.2 2002/02/06 22:30:09 markf Exp $
* PURPOSE:  Check a form for empty fields before submittal
* REQUIRES: oForm - refernce to form object to check
*           sNotNullFields - string; pipe separated list of field names in 
*             the form to check
*           sNotNullFieldsDesc - string; pipe separarted descriptions of 
*             corresponding elements from sNotNullFields
*           sMsg - string; custom error message.  Use 0-len string for default.  The marker
*             %field% will be replaced with the current field name, and %field_desc% will 
*             be replaced with the field description.
*             i.e. 'Make sure you fill out the %field_desc% field! (%field%)'
* ENSURES:  Returns true if all required fields are filled out, false otherwise.
*           If false, it also pops up an alert and positions the curson at the
*           field in question.
* NOTES:    Usage on the form:
*   <form name="appform" onsubmit="return bCheckNulls(this, 'name|email', 'Full name|Email Address', '');">
*-------------------------------------------------------------------------------
*/

function bCheckNulls(oForm, sNotNullFields, sNotNullFieldsDesc, sMsg) {
	var isGood = true;
	// notNullFields holds the names of any fields that shouldn't be null.
	var notNullFields = new String(sNotNullFields);
	// notNullFieldsDesc holds English descriptions corresponding to entires in notNullFields.
	var notNullFieldsDesc = new String(sNotNullFieldsDesc);
	var notNullArray = notNullFields.split('|');
	var notNullArrayDesc = notNullFieldsDesc.split('|');

	// Figure out what the alert message should be
	var sAlert;
	sAlert = new String(sMsg);
	sAlert.trim;

	if (sAlert.length != 0) {
		// use custom alert message, which is already set
	} else {
		// use default alert
		sAlert = 'Please fill out the %field_desc% field.';
	}

	// loop through each field that is not allowed to be null, and check it
	for (var i = 0; notNullArray.length > i; i++) {
		var curElement = oForm.elements[notNullArray[i]];  // check if anything has been entered into current field
		curElement.trim;
		if (curElement.value == '') {
			isGood = false;
			sAlert = sAlert.replace(/%field%/g, notNullArray[i]);           // replace tokens with correct value
			sAlert = sAlert.replace(/%field_desc%/g, notNullArrayDesc[i]);
			alert(sAlert);           // tell the user
			curElement.focus();      // move cursor to element with missing data
			break;
		}
	}
	return isGood;
}

// Quick way to stop people from editing the contents of a text box
function noEdit(obj, msg) {
	var m = new String(msg);
	if (m.length > 0) {
		alert(m);
	}
	obj.blur();
}

// email address validation
// Regex from http://regexlib.com/REDetails.aspx?regexp_id=72
// If you don't want an alert message, set (sBadMsg = '')
function bIsEmail(sEmail, sBadMsg) {
	var re = /^(([^<>;()[\]\\.,;:@\"]+(\.[^<>()[\]\\.,;:@\"]+)*)|(\".+\"))@((([a-z]([-a-z0-9]*[a-z0-9])?)|(#[0-9]+)|(\[((([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))\.){3}(([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))\]))\.)*(([a-z]([-a-z0-9]*[a-z0-9])?)|(#[0-9]+)|(\[((([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))\.){3}(([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))\]))$/;
	if (re.test(sEmail)) {
		return true;
	} else {
		if (sBadMsg.length > 0) { alert(sBadMsg) }
		return false;
	}
}
