function Trim(s) {
	while (s.substring(0, 1) == ' ')
		s = s.substring(1, s.length);
	while (s.substring(s.length - 1, s.length) == ' ')
		s = s.substring(0, s.length - 1);
	return s;
}

function JAVAS_bValidateForm(form, title) {
	var result = true;
	var nosubmit = false;
	var missinginfo = title;
	var confirmation = 'Are your sure you wish to continue?';
	//loop through all the elements on the form and check if the have the validate attribute
	for ( var i = 0; i < form.elements.length; i++) {
		var validateType = form.elements[i].getAttribute('validate');
		var validateObj = form.elements[i];
		if (validateType) {
			var params = validateType.split("|");
			// each element requires one or more type of validation
			for ( var j = 0; j < params.length; j++) {
				switch (params[j]) {
				case 'NOSUBMIT': {
					nosubmit = true;
					confirmation = validateObj.getAttribute('emsg');
				}
					break;
				case 'ZERO':
					if (Trim(validateObj.value) == 0) {
						missinginfo += "\n     -  "
								+ validateObj.getAttribute('emsg');
						if (result)
							validateObj.focus();
						result = false;
					}
					break;
				case 'NUMERIC':
					if (isNaN(validateObj.value)
							|| (Trim(validateObj.value) == '')) {
						missinginfo += "\n     -  "
								+ validateObj.getAttribute('emsg');
						if (result)
							validateObj.focus();
						result = false;
					}
					break;

				case 'BLANK':
					if (Trim(validateObj.value) == '') {
						missinginfo += "\n     -  "
								+ validateObj.getAttribute('emsg');
						if (result)
							validateObj.focus();
						result = false;
					}
					break;
				case 'EMAIL':
					var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
					if ((validateObj.value == '') || (validateObj.value == ' ')
							|| !re_mail.test(validateObj.value)) {
						missinginfo += "\n     -  "
								+ validateObj.getAttribute('emsg');
						if (result)
							validateObj.focus();
						result = false;
					}
					break;

				case 'RANGE':
					var lb = parseInt(validateObj.getAttribute('mini'));
					var ub = parseInt(validateObj.getAttribute('maxi'));
					if (isNaN(validateObj.value) || (validateObj.value < lb)
							|| (validateObj.value > ub)) {
						missinginfo += "\n     -  "
								+ validateObj.getAttribute('emsg');
						if (result)
							validateObj.focus();
						result = false;
					}
					break;
				case 'COMB':
					var combineObjs = validateObj.getAttribute('combine');
					var combineObj = combineObjs.split("|");
					result = false;
					for ( var k = 0; k < combineObj.length; k++) {
						var x = combineObj[k];
						for ( var l = 0; l < form.elements.length; l++)
							if (x == form.elements[l].name)
								if (form.elements[l].value != 0)
									result = true;
					}
					if (!result)
						missinginfo += "\n     -  "
								+ validateObj.getAttribute('emsg');
					break;
				}
				if (!result)
					break;
			}
		}
	}
	if (result) {
		if (!nosubmit)
			form.submit();
		else {
			// requires confirmation
			if (confirm(confirmation)) {
				form.submitted = true;
				form.submit();
			}
			return true;
		}
	} else
		alert(missinginfo);
	return false;
}
