function FormValidator(frm)
{
	var _fieldCount = 0;
	var _aRequiredFields = Array;
	var _aMessages = new Array;
	
	this.Form = frm;
	this.Validate = Validate;
	this.Require = Require;	
	
	function Require(sFieldName, sMessage)
	{
		var fld = this.Form.elements[sFieldName];
		
		if (fld == null)
		{
			alert("FormValidator: Couldn't find field '" + sFieldName + "'.");
			return false;
		}
		
		_aRequiredFields[_fieldCount] = fld;
		_aMessages[_fieldCount] = sMessage;
		_fieldCount++;	
	}

	function Validate()
	{
		for (var i = 0; i < _fieldCount; i++)
		{
			var fld = _aRequiredFields[i];			
			if (fld.type == "text")
			{
				if (fld.value == "")
				{
					alert(_aMessages[i]);
					fld.focus();
					return false;
				}
			}
			
			if (fld.type == "select-one")
			{
				if (fld.selectedIndex == 0)
				{
					alert(_aMessages[i]);
					fld.focus();
					return false;
				}
			}
		}
		
		return true;
	}
}

function HasRadioChoice(radioGroup)
{
	if (radioGroup.length)
	{
		for (var i = 0; i < radioGroup.length; i++)
		{
			if (radioGroup[i].checked)
				return true;
		}
	}
	else
	{
		return radioGroup.checked;
	}
		
	return false;
}

function IsValidEmail(sValue)
{
	//thanks to http://www.quirksmode.org/js/mailcheck.html
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return (filter.test(sValue));
}

function IsValidDate(value)
{
	if (value == "")
		return false;
		
	try
	{
		var d = new Date(value);
		return true;
	}
	catch(ex)
	{
		return false;
	}
}

function OptionSelected(radioGroup, sValue)
{
	if (radioGroup.length)
	{
		for (var i = 0; i < radioGroup.length; i++)
		{
			if (radioGroup[i].checked && radioGroup[i].value == sValue)
				return true;
		}
	}
	else
	{
		if (radioGroup.checked && radioGroup.value == sValue)
			return true;
	}
	return false;
}
