// gets the selected value of a set of radio buttons
function getRadioButtonValue(buttonGroup)
{
	for (var i = 0; i < buttonGroup.length; i++)
	{
		if (buttonGroup[i].checked)
		{
			return buttonGroup[i].value;
		}
	}
	return false;
}
// determines if the given string is empty or contains only whitespace
function isEmpty(str)
{
	if (str == false)
	{
		return true;
	}
	else
	{
		var pat = /^[\s]*$/;
		return pat.test(str);
	}
}
// performs a regular expression match
function preg_match(regexp, str)
{
	// seperate pattern and flags
	regexp = regexp.substring(1);
	slashPos = regexp.lastIndexOf('/');
	var pattern = regexp.substring(0, slashPos);
	var flags = regexp.substring(slashPos + 1);
	var pat = new RegExp(pattern, flags);
	return pat.test(str);
}
// determines if str is a valid person name
// i.e., made up of alphabetic characters, spaces, -'s, ''s, or .'s
function isPersonName(str)
{
	var pat = /^(([^\W\d_]|[\-'.])+[ ]?)+$/;
	return pat.test(str);
}
// determines if str is made up entirely of alphabetic characters
function isAlpha(str)
{
	var pat = /^[^\W\d_]+$/;
	return pat.test(str);
}
// determines if str is made up entirely of alpha-numeric characters
function isAlnum(str)
{
	var pat = /^[^\W_]+$/;
	return pat.test(str);
}
// determines if str is made up entirely of alpha-numeric characters or spaces
function isAlnumSpc(str)
{
	var pat = /^([^\W_]|[ ])+$/;
	return pat.test(str);
}
// determines if str is made up enitrely of alpha-numeric characters, or the underscore
// (underscore is not permitted at the first character)
function isUsername(str)
{
	var pat = /^[^\W_]\w+$/;
	return pat.test(str);
}
// determines if str is a numeric string
function isNumeric(str)
{
	var pat = /^[\-]?(\d+|(\d*\.\d+))$/;
	return pat.test(str);
}
// wrapper function to provide similarity to php
function is_numeric(str)
{
	return isNumeric(str);
}
// determines if str is an integer
function isInt(str)
{
	var pat = /^[\-]?\d+$/;
	return pat.test(str);
}
// determines if str is a decimal number with the specified number of
// decimal digits
function isDecimal(str, digits)
{
	str = "" + str;
	if (digits == 0)
	{
		return isInt(str);
	}
	else
	{
		return isNumeric(str) && str.indexOf('.') >= 0 && str.length - str.indexOf('.') == digits + 1;
	}
}
// determines if str is a number and in the given range
function isInRange(str, lower, upper)
{
	return isNumeric(str) && lower <= str && upper >= str;
}
// determines if str is a valid email address
function isEmail(str)
{
	var pat = /^[\w.\-]+@((([\w\-]+\.)+[a-z]{2,6})|(\[?(\d{1,3}\.){3}\d{1,3}\]?))$/i;
	return pat.test(str);
}	
// determines if str is a valid phone number
// area-code is optional
function isPhone(str)
{
	var pat = /^(\(?\d{3}\)?)?[\- ]?\d{3}[ \-]?\d{4}$/;
	return pat.test(str);
}
// determines if the given date is a valid one
function isDate(year, month, day)
{
	if (!isNumeric(year) || !isNumeric(month) || !isNumeric(day))
	{
		return false;
	}
	if (year < 0 || month < 1 || month > 12 || day < 1)
	{
		return false;
	}
	daysInMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	// if leap year
	if (month == 2 && year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
	{
		daysInMonth[1] = 29;
	}
	if (day > daysInMonth[month-1])
	{
		return false;
	}
	return true;
}
// determines if str is a postal code
function isPostalCode(str)
{
	var pat = /^[a-z]\d[a-z][ ]?\d[a-z]\d$/i;
	return pat.test(str);
}
// determines if the given string is a valid URL
// boolean second argument indicates whether or not the url should begin with http://
function isHttpUrl(str)
{
	var uchar = "\\w$\-.+!*'(),";
	var uuchar = uchar + ";?&=";
	var uuword = "[" + uuchar + "]+";
	var userPass = "(" + uuword + "(:" + uuword + ")?@)?";
	var topLabel = "([^\\W\\d_]([A-Za-z0-9\\-]*[^\\W_])?)";
	var domainLabel = "([^\\W_]([A-Za-z0-9\\-]*[^\\W_])?)";
	var hostName = "(" + domainLabel + "\\.)*" + topLabel;
	var hostNumber = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
	var host = "((" + hostName + ")|(" + hostNumber + "))";
	var hostPort = host + "(:\\d{1,5})?";
	var hPath = uuword + "(\\/" + uuword + ")*";
	var httpUrl = "^" + userPass + hostPort + "(\\/" + hPath + "(\\?" + uuword + ")?(#\\w+)?)?$";
	var pat = new RegExp(httpUrl);
	return pat.test(str);
}
// returns the selection size for a multiple select box
function selectionSize(box)
{
	var selSize = 0;
	for (var i = 0; i < box.options.length; i++)
	{
		if (box.options[i].selected)
		{
			selSize++;
		}
	}
	return selSize;
}
// takes an array
// returns the number of non-empty members in the array
function getNonEmptyCount(arr)
{
	var count = 0;
	for (var i = 0; i < arr.length; i++)
	{
		if (!isEmpty(arr[i]))
		{
			count++;
		}
	}
	return count;
}