// globally-useful utility functions

function trim(str)
{
	str = String(str);
	return str.replace(/^\s+|\s+$/g, '');
}

function buildList(array)
{
	var list = '';

	for (var itemNum = 0; itemNum < array.length; ++itemNum)
	{
		if (itemNum)
		{
			if (itemNum == array.length - 1) list += ' and ';
			else list += ', ';
		}

		list += array[itemNum];
	}

	return list;
}

function isValidEmailAddress(value)
{
	if (value == null) return false;

	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/.test(value) ||
		/^[ \w\.\"_-]+<\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+>$/.test(value))
	{
		var tld = RegExp.lastParen.toLowerCase(); // includes the leading dot

		// let any TLD with 4 characters or less through
		if (tld.length <= 5) return true;

		// validate longer TLDs specifically
		if (tld == ".museum" || tld == ".travel") return true;
		else return false;
	}

	return false;
}

function getExtension(fileName, withDot)
{
	fileName = String(fileName).replace(/\\/g, '/');

	var lastSlash = fileName.lastIndexOf('/');
	if (lastSlash != -1) fileName = fileName.substring(lastSlash + 1);

	var lastDot = fileName.lastIndexOf('.');
	if (lastDot == -1)
	{
		// no extension present, so return empty string,
		// even if withDot was requested
		return '';
	}
	else
	{
		if (!withDot) lastDot++;
		return fileName.substring(lastDot);
	}
}

