function Validator(formObj, parentObj)
{
	this.form = formObj || null;
	this.parent = parentObj || null;
	this.date_format = 'MM/dd/yy';
	this.time_format = 'h:mm a';
	this.protocols = ['http:', 'file:/', 'mailto:', 'ftp:', 'javascript:', 'news:', 'gopher:'];
	
	this.msgCorrect = 'Please correct these problems and submit the form again.';
	this.msgMaxLen = ' must be less than ';
	this.msgNonEmpty = ' cannot be empty.';
	this.msgFormattedDate = ' does not contain a date in the format ';
	this.msgFormattedTime = ' does not contain a time in the format hh:mm am/pm, for example, 6:30 pm.';
	this.msgURL = ' must be a URL with the syntax http://www.host.com/path.';
	this.msgEmail = ' must be a syntactically valid email address.';
	this.msgNonNum = ' must be numeric.';
	this.msgSelect = ' must have a value selected.';
	this.msgSelectMulti = ' must have at least one value selected.';
	this.msgCheck = ' must have at least one value checked.';
	this.msgMatch = ' do not match.  Please correct.';
	
	
	if(this.form)
	{
		this.validate = function()
		{
			msgArray = [];
			for(i = 0; i < this.form.elements.length; i++)
			{
				msg = '';
				fld = this.form.elements[i];
				idArray = fld.id.split(',');
	
				for(j = 0; j< idArray.length; j++)
				{
					idPlusAttribs = idArray[j].split('|');
					id = idPlusAttribs[0];
					attr = idPlusAttribs.length > 1 ? idPlusAttribs[1] : null;
					if(id.indexOf('reqSelect') == 0)
						msg = this.valReqSelect(fld, attr);
					else if(id.indexOf('reqCheck') == 0)
						msg = this.valReqCheck(fld, attr);
					else if(id.indexOf('reqText') == 0)
						msg = this.valReqText(fld, attr);
					else if(id.indexOf('reqDate') == 0)
						msg = this.valReqDate(fld, attr);
					else if(id.indexOf('reqTime') == 0)
						msg = this.valReqTime(fld, attr);
					else if(id.indexOf('valTime') == 0)
						msg = this.valTime(fld, attr);
					else if(id.indexOf('reqNum') == 0)
						msg = this.valReqNum(fld);
					else if(id.indexOf('reqURL') == 0)
						msg = this.valReqURL(fld, attr);
					else if(id.indexOf('reqEmail') == 0)
						msg = this.valReqEmail(fld);
					else if(id.indexOf('reqMatch') == 0)
						msg = this.valReqMatch(fld, attr);
					else if(id.indexOf('reqDepFld') == 0)
						msg = this.valReqDepFld(fld, attr);
					if(msg != '')
						msgArray[msgArray.length] = msg;
				}
			}
			return this.returnHandler(msgArray);
		}
		this.valReqSelect = function(fld, minValue)
		{
			msg = '';
			minVal = minValue || 0;
			fldName = fld.title || 'Field';
			sel = false;

			if(fld.size > 1)
			{
				for(k = 0; k< fld.options.length; k++)
					if(fld.options[k].selected)
						sel = true;
			}
			else if(fld.selectedIndex > minVal)
			{
				sel = true;
			}

			if(!sel && fld.size > 1)
				msg = fldName + this.msgSelectMulti;
			else if(!sel)
				msg = fldName + this.msgSelect;
			return msg;
		}

		this.valReqCheck = function(targetFld, checkFlds)
		{
			msg = '';
			target = targetFld || null;
			fldName = targetFld.title || 'Field';
			checks = this.form[checkFlds] || null;
			vals = [];

			if(target && checks && checks.length)
			{
				if(target.value.length > 0) 
					vals[vals.length] = target.value;
				for(k=0; k < checks.length; k++)
					if(checks[k].checked && target.value.indexOf(checks[k].value) == -1)
						vals[vals.length] = checks[k].value;
			
				target.value = vals.join(', ');
			}
			else if(target && checks && checks.checked)
				target.value = checks.value;
				
			if(target && checks && target.value == '')
				msg = fldName + this.msgCheck;
			return msg;
		}

		this.valReqText = function(fld, maxlen)
		{
			msg = '';
			maxlength = maxlen || -1;
			fldName = fld.title || 'Field';
			if(fld.value == '')
			{
				msg = fldName + this.msgNonEmpty;
			}
			else if(maxlength > 0 && fld.value.length > maxlength)
			{
				val = fld.value.substr(0, maxlength);
				fld.value = val;
				msg = fldName + this.msgMaxLen + maxlength + ' characters.';
			}
			return msg;
		}
		
		this.valReqURL = function(fld, req)
		{
			msg = '';
			fldName = fld.title || 'Field';
			required = req == null? true : (req == 'true');
			if(required && fld.value == '')
			{
				msg = fldName + this.msgURL;
			}
			else if(fld.value != '')
			{
				match = false;
				for(k = 0; k < this.protocols.length; k++)
					if(fld.value.indexOf(this.protocols[k]) == 0)
						match = true;
				if(!match)
					msg = fldName + this.msgURL;
			}
			return msg;
		}
		
		this.valReqEmail = function(fld)
		{
			msg = '';
			fldName = fld.title || 'Field';
			
			if(fld.value == '' || fld.value.indexOf('@') == -1 || fld.value.indexOf('.') == -1)
				msg = fldName + this.msgEmail;

			return msg;
		}
		
		this.valReqDate = function(fld,format)
		{
			msg = '';
			mask = format || this.date_format;
			fldName = fld.title || 'Field';
			
			if(getDateFromFormat(fld.value, mask) == 0)
			{
				dateVal = parseDate(fld.value);
				fld.value = dateVal ? formatDate(dateVal, mask) : '';
			}
			
			if(fld.value == '')
				msg = fldName + this.msgFormattedDate + mask;
			
			return msg;
		}
		
		this.valReqTime = function(fld,format)
		{
			msg = '';
			mask = format || this.time_format;
			fldName = fld.title || 'Field';
			
			if(getDateFromFormat(fld.value, mask) == 0)
			{
				dateVal = parseDate(fld.value);
				fld.value = dateVal ? formatDate(dateVal, mask) : '';
			}
			else
			{
				dateVal = new Date(getDateFromFormat(fld.value, mask));
				fld.value = formatDate(dateVal, mask);
			}
			if(fld.value == '')
				msg = fldName + this.msgFormattedTime;
			
			return msg;
		}
		
		this.valTime = function(fld,format)
		{
			msg = '';
			if(fld.value != '')
				return this.valReqTime(fld, format);
			else return msg;
		}
		
		this.valReqMatch = function(fld, fldMatch)
		{
			msg = '';
			match = this.form[fldMatch] || null;
			fldName = fld.title || 'Field';
			fldNameMatch = match.title || 'Field';
			if(fld.value != match.value)
				msg = fldName + ' and ' + fldNameMatch +  this.msgMatch;
			return msg;
		}
		
		this.valReqNum = function(fld)
		{
			msg = '';
			fldName = fld.title || 'Next field';
			if(parseInt(fld.value) == NaN && parseFloat(fld.value) == NaN)
				msg = fldName + this.msgNonNum;
			return msg;
		}
		
		this.valReqDepFld = function(fld, dep)
		{
			msg = '';
			dependent = this.form[dep] || null;
			fldName = dependent.title || 'Field';
			if(fld && fld.value != '' && dependent && dependent.value == '')
			{
				msg = fldName + this.msgNonEmpty;
			}
			return msg;
		}
		
		this.returnHandler = function(msgArray)
		{
			if(msgArray.length > 0)
			{
				txtAlert = msgArray.join('\n');
				txtAlert += '\n\n' + this.msgCorrect;
				alert(txtAlert);
				return false;
			}
			else return true;
		}
	}
}

