// Determines if a character is whitespace
var _whitespace = " \t\n\r";

function _isWhite(c)
{
	if (_whitespace.indexOf(c) >= 0)
		return true;
	else
		return false;
}

// Trims a  string
function _trim(text)
{
	var value = text;
	
	while ((value.length > 0) && (_isWhite(value.charAt(0))))
		value = value.substring(1, value.length);

	while ((value.length > 0) && (_isWhite(value.charAt(value.length - 1))))
		value = value.substring(0, value.length - 1);

	return value;
}

// Replaces all incidences of a substring with another string
function _replace(str, a, b)
{
	var s = str;
	var idx;

	while ((idx = s.indexOf(a)) >= 0)
	{
		var ns = "";
		
		if (idx > 0) 
			ns = s.substring(0, idx);
			
		ns += b;
		
		if ((idx + a.length) < s.length)
			ns += s.substring(idx + a.length, s.length);
	
		s = ns;
	}
	
	return s;
}

// Parses a numeric field, returning the value
function _parseNumeric(value)
{
	return parseFloat(_replace(value, ",", ""));
}

// Checks a valid to see if it's numeric
function _isNumeric(value, allowFloat, allowNegative)
{
	if (value.length == 0)
		return false;

	// Check for float
	if ((!allowFloat) && (value.indexOf(".") >= 0))
		return false;

	// Check for negative
	if ((!allowNegative) && (value.charAt(0) == "-"))
		return false;

	// Check for more than one "."
	if (value.indexOf(".") != value.lastIndexOf("."))
		return false;
		
	// Get the position of the "."
	var decindex = (value.indexOf(".") >= 0) ? value.indexOf(".") : value.length;

	// The valid characters
	var validChars = "0123456789.,";

	for (var i = ((value.charAt(0) == '-') ? 1 : 0); i < value.length; i++)
	{
		// Grab the i'th character and check it
		var c = value.charAt(i);

		if (validChars.indexOf(c) < 0)
			return false;

		if ((c == ',') && (((decindex - i) % 4) > 0))
			return false;
	}
	
	return true;
}

// Parses a list of items and returns an array
function _splitText(rstr, delimiters, eliminateEmptyItems)
{
	var r = rstr;
	var delim = delimiters;
	
	if (typeof delimiters == "object")
	{
		delim = delimiters[0];
		
		for (var i = 1; i < delimiters.length; i++)
			r = _replace(r, delimiters[i], delimiters[0]);
	}
	
	if (eliminateEmptyItems)
	{
		while (r.indexOf(delim + '' + delim) >= 0)
			r = _replace(r, delim + '' + delim, delim);

		while ((r.length > 0) && (r.indexOf(delim) == 0))
			r = r.substr(1, r.length);
			
		while ((r.length > 0) && (r.lastIndexOf(delim) == (r.length - 1)))
			r = r.substr(0, r.length - 1);
	}
	
	if (r.length == 0)
		return new Array();
	else
		return r.split(delim);
}

// Validates a list of email address
function _validateDelimitedEmailList(emailText)
{
	// Perform the split
	var emailList = _splitText(emailText, new Array('\r', '\n', ';', ',', ' '), true);
	var invalidEmails = new Array();
	
	for (var i = 0; i < emailList.length; i++)
		if (!_isValidEmailAddress(_trim(emailList[i])))
			invalidEmails[invalidEmails.length] = _trim(emailList[i]);
			
	return invalidEmails;
}

// Validates an e-mail address
function _isValidEmailAddress(addr)
{
	// Test the address with a regular expression
	var filter = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	if ( filter.test(addr) )
		return true;
	else
		return false;
}

// Checks if one of a set of options is selected
function _isOptionSelected(optarray)
{
	for (var i = 0; i < optarray.length; i++)
		if (optarray[i].checked)
			return true;
			
	return false;
}

// Checks if one of a set of options is selected
function _getSelectedValue(optarray)
{
	for (var i = 0; i < optarray.length; i++)
		if (optarray[i].checked)
			return optarray[i].value;
			
	return null;
}
