function PatternMatch(reStr,s,flags){
	re = new RegExp(reStr,flags)
	return re.test(s);
}

function IsAlphaNumeric(s){
	return !PatternMatch('[^A-Zza-z0-9]',s,'i');
}

function IsAnInteger(s){
	if (s == '' || isNaN(s) || s.indexOf('.')!= -1) {return false}
	s = String(Math.abs(s))
	return PatternMatch('[0-9]{' + s.length + ',}',s,'i');
}

function IsPositiveInteger(s){
	var iLen = (s.length == 0) ? 1 : s.length
	return PatternMatch('[0-9]{' + iLen + ',}',s,'i');
}

function EmptyString(s){
	return !PatternMatch('\\S',s,'i');
}

function ValidEmail(s){
	return PatternMatch('\\w+@\\w+\\.\\w+',s,'i');
}

function ValidTime(s){
	s = s.toUpperCase()
	var aTime = s.split(' ')

	if ((aTime.length != 2) || (!(aTime[1] == 'AM' || aTime[1] == 'PM'))) return false

	aTime = aTime[0].split(':')
	if (aTime.length != 2 || !IsPositiveInteger(aTime[0]) || !IsPositiveInteger(aTime[1])) return false

	var iHours = Number(aTime[0])
	var iMinutes = Number(aTime[1])

	return (iHours <= 12) && (iMinutes <= 59)
}

function FocusException(Valid,o,errmsg, wnd){
	if (!Valid){
		var ActiveWindow = (wnd == null) ? self : wnd
		ActiveWindow.alert(errmsg)
		o.focus()
		if (o.type == 'text')
			o.select()
	}
	return Valid
}

function AllQuestionsAnswered(Form){
// Returns the 1st form element not answered, or null if all questions have been answered.
	var grp, element
	var i = 0
	while (i < Form.length) {
		element = Form[i];
		switch (element.type) {
			case 'checkbox' :
			case 'radio' :
				grp = Form[element.name];
				if (grpSelected(grp) == -1){
					return element;
				}
				i += grp.length - 1;
				break;
			case 'hidden':
			case 'image':
			case 'select-one':
			case 'submit':
			case 'button':
				break;
			case 'select-multiple':
				if (element.selectedIndex == -1){
					return element;
				}
				break;
			default:
				if (EmptyString(element.value)){
					return element;
				}
		} // switch (element.type)
		i++;
	} // while (i < Form.length)
	return null;
} // function AllQuestionsAnswered(Form)

function ValidateForm(f, wnd){
// Use this function to position the window on the 1st unanswered question
var element

	element = AllQuestionsAnswered(f);
	if (element != null){
		element.focus();
		self.scrollBy(0,-20);
		var ActiveWindow = (wnd == null) ? self : wnd
		ActiveWindow.alert('Please answer this question before proceeding.');
		return false;
	}
	return true;
} // function ValidateForm(f)

function AllElementsAnswered(f,aElements){
var grp, e
var sType, i
	for (i=0; i<aElements.length; i++){
		e = f[aElements[i]];
		sType = (String(e.type) == 'undefined') ? e[0].type : e.type;
		switch (sType) {
			case 'checkbox' :
			case 'radio' :
				if (grpSelected(e) == -1){
					return e[0];
				}
				break;
			case 'select-one':
			case 'submit':
			case 'button':
				break;
			case 'select-multiple':
				if (e.selectedIndex == -1){
					return e;
				}
				break;
			default:
				if (EmptyString(e.value)){
					return e;
				}
		}
	}
	return null;
} //function AllElementsAnswered(f,aElements)

function ValidateFormElements(f,aElements){
// Use this function to position the window on the 1st unanswered question
var element
	element = AllElementsAnswered(f,aElements);
	if (element != null){
		element.focus();
		self.scrollBy(0,-20);
		alert('Please answer this question before proceeding.');
		return false;
	}
	return true;
} // function ValidateFormElements(f,aElements)