/*
Field Validation
by Rich Goidel
v2.2
4.21.03

theFormName = name of form on page
theData = value pairs passed as one long string
	each pair delimited by ,
	items in pair delimited by &
	items = fieldname & error display phrase for the item
	if fieldname is prefaced with e. then it must evaluate to an email
	if fieldname is prefaced with c. then it must evaluate to check or radio selection
	if fieldname is prefaced with s. then it must evaluate to select option; use "0" as unselected value (ex: <option value="0">select...</option>)
	if fieldname is prefaced with n. then it must evaluate to a number
	if fieldname carries an o. after the e. (OR n.) declaration, this item is optional, but must conform to the format if entered
theMessage = initial alert message to display, followed by the related error messages
*/

function form_check(theFormName, theData, theMessage) {
	var theForm = eval('document.' + theFormName);
	var originalMessage = (theMessage) ? theMessage + '\n' : 'The following fields are incomplete or contain errors:\n';
	var newMessage = originalMessage;

	dataArray = theData.split(",")
	for (x = 0; x < dataArray.length; x++) {
		var itemArray = dataArray[x].split('&')
		
		// is this an email or number, and is it optional?
		var isEmail = false;
		var isNumber = false;
		var isCheckbox = false;
		var isSelect = false;
		var isOptional = false;
		// hasLimit = false;
		
		if (itemArray[0].substr(0,2) == 'e.' || itemArray[0].substr(0,2) == 'n.' || itemArray[0].substr(0,2) == 'c.' || itemArray[0].substr(0,2) == 's.') {
			if (itemArray[0].substr(0,2) == 'e.') {
				isEmail = true;
			} else {
				if (itemArray[0].substr(0,2) == 'n.') {
					isNumber = true;
				} else {
					if (itemArray[0].substr(0,2) == 'c.') {
						isCheckbox = true;
					} else {
						isSelect = true;
					}
				}
			}
			itemArray[0] = itemArray[0].substr(2);
			if (itemArray[0].substr(0,2) == 'o.') {
				itemArray[0] = itemArray[0].substr(2);
				isOptional = true;
			}
		}
		
		thisObj = eval('theForm.' + itemArray[0])
		
		if ((! isCheckbox) && (! isSelect)) {
			var thisItem = eval('theForm.' + itemArray[0] + '.value')
			// trim tail...
			while (thisItem.substr(thisItem.length-1,1) == ' ' && thisItem.length > 0) {
				thisItem = (thisItem.length > 2) ? thisItem.substr(0,thisItem.length-1) : '';
			}
			// trim front...
			while (thisItem.substr(0,1) == ' ' && thisItem.length > 0) {
				thisItem = (thisItem.length > 2) ? thisItem = thisItem.substr(1) : '';
			}
			
			if (thisItem == '' && isOptional == false) {
				newMessage += itemArray[1] + '\n'
			} else {
				if (thisItem != '') {
					// check for email...
					if (isEmail) { // check valid email
						if (thisItem.indexOf("@") > 0) {
							// check for "."
							var strEmail = thisItem.split("@");
							if ((strEmail[1].indexOf(".") < 1) || (strEmail[1].indexOf(".") == strEmail[1].length - 1)) newMessage += itemArray[1] + '\n';
						}else{
							newMessage += itemArray[1] + '\n';
						}
					} else {
						if (isNumber) {
							// check for multiple "."
							if (thisItem.indexOf('.') != thisItem.lastIndexOf('.')) {
								newMessage += itemArray[1] + '\n';
							} else {
								var nString = '0123456789.'
								for (y = 0; y < thisItem.length; y++) {
									if (nString.indexOf(thisItem.substr(y,1)) < 0) {
										newMessage += itemArray[1] + '\n';
										break;
									}
								}
							}
						}
					}
				}
			}
				
		} else {
			var thisObj = eval('theForm.' + itemArray[0]);
			if (isCheckbox) {
				boxChecked = false;
				for (y = 0; y < thisObj.length; y++) {
					if (thisObj[y].checked) boxChecked = true;
				}
				if (! boxChecked) newMessage += itemArray[1] + '\n';
			} else {
				if (thisObj.options[thisObj.selectedIndex].value == '0') newMessage += itemArray[1] + '\n';
			}
		}
	}
	
	
	
	
	if (newMessage != originalMessage) {
		alert(newMessage);
		return false;
	} else {
		return true;
	}
}
