/*
	returns true if the string is null or of length zero,
	returns false otherwise.
*/
function isStringEmptyOrNull(str) {
	return (str == null || str.length == 0);
}

/*
	performs very basic verification of a valid email syntax
*/
function isValidEmail(str) {
	if (isStringEmptyOrNull(str))
		return false;
	
	if (str.indexOf(" ") > -1 || str.indexOf("&") > -1 || str.indexOf(";") > -1 
			|| str.indexOf("'") > -1 || str.indexOf(",") > -1 || str.indexOf('"') > -1
			|| str.indexOf("<") > -1 || str.indexOf(">") > -1)
		return false;
	
	return (str.indexOf('.') > 0) && (str.indexOf('@') > 0);
}

/*
	returns false if string is empty or null or contains non-numeric chars,
	returns true otherwise.
*/
function isNumeric(str)
{
	if (isStringEmptyOrNull(str)) return false;
	
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;

	//  test str consists of valid characters listed above
	for (i = 0; i < str.length && blnResult == true; i++)
	{
		strChar = str.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;
}

/*
	replace s with t
	for all occurences of s in str
	regardless of upper/lower case
*/
function replaceGlobalIgnoreCase(s, t, str) {
	rExp = new RegExp(s, "gi"); // modifiers g and i are global and ignore-case
	return str.replace(rExp, t, str);
}

/*
	replace apostrophes ' in str with circumflex-accents ^
*/
function replaceApostrophes(str) {
	return replaceGlobalIgnoreCase("'", '^', str);
}

/*
	replace doubleQuotes " in str with circumflex-accents ^
*/
function replaceDoubleQuotes(str) {
	return replaceGlobalIgnoreCase('"', '^', str);
}

/*
	replace lessThans < in str with leftBrackets [
*/
function replaceLessThans(str) {
	return replaceGlobalIgnoreCase('<', '[', str);
}

/*
	replace greaterThans > in str with rightBrackets ]
*/
function replaceGreaterThans(str) {
	return replaceGlobalIgnoreCase('>', ']', str);
}

/*
	replace apostrophes, doubleQuotes, lessThans, and greaterThans in str
	with standard replacement strings defined in methods above.
*/
function replaceOffensiveChars(str) {
	return replaceGreaterThans(replaceLessThans(replaceDoubleQuotes(replaceApostrophes(str))));
}
