// global variables
var mystrings_whitespace = " \f\n\r\t";
var mystrings_digits = "0123456789";

// does "str" contains whitespace?
function contains_ws(str) {
	for (i=0; i<mystrings_whitespace.length; i++) {
		ch = mystrings_whitespace.charAt(i);
		if (str.indexOf(ch) != -1) return 1;
	}
	return 0;
}

// removes any whitespace characters at the beginning and end of "str"
function trim(str) {
	str = trim_left(str);
	str = trim_right(str);
	return str;
}

function trim_left(str) {
	var newStr = str;
	var inx = 0;
	var ch;

	while (1) {
		if (inx > newStr.length - 1) {
			return "";
		}
		ch = newStr.charAt(inx);
		if (mystrings_whitespace.indexOf(ch) != -1) inx++;
		else break;
	}
	if (inx > 0) newStr = newStr.substring(inx);

	return newStr;
}

function trim_right(str) {
	var newStr = str;
	var ch;
	var inx = newStr.length - 1;

	while (1) {
		if (inx < 0) return "";
		ch = newStr.charAt(inx);
		if (mystrings_whitespace.indexOf(ch) != -1) inx--;
		else break;
	}
	if (inx < newStr.length - 1) newStr = newStr.substring(0,inx+1);

	return newStr;
}

function trim_right_containing(str, containing) {
	var newStr = str;
	var ch;
	var inx = newStr.length - 1;

	while (1) {
		if (inx < 0) return "";
		ch = newStr.charAt(inx);
		if (containing.indexOf(ch) != -1) inx--;
		else break;
	}
	if (inx < newStr.length - 1) newStr = newStr.substring(0,inx+1);

	return newStr;
}


// simply transliterates the string "from" to "to" in the original string "str"
function translit(str, from, to) {
	var newStr = str;

	while ((inx = newStr.indexOf(from)) != -1) {
		newStr = newStr.substring(0,inx) + to + newStr.substring(inx+from.length);
	}

	return newStr;
}


// transliterates a (consecutive) stream of whitespace characters to "to"
function translit_ws_to(str, to) {
	var theStr = str;
	var newStr = "";
	var ch, first, last;

	var i = 0;
	var len = theStr.length;
	while (1) {
		if (i > len - 1) break;

		// find beginning of whitespace
		ch = theStr.charAt(i);
		if (mystrings_whitespace.indexOf(ch) == -1) {
			i++;
			continue;
		}

		// i found whitespace.  now, see how many
		// there might be in a row.
		first = i;
		while (1) {
			i++;
			if (i > len - 1) break;
			ch = theStr.charAt(i);
			if (mystrings_whitespace.indexOf(ch) == -1) break;
		}
		last = i - 1;

		newStr += theStr.substring(0,first);
		newStr += to;

		// start all over again
		theStr = theStr.substring(last+1);
		i = 0;
		len = theStr.length;
	}
	newStr += theStr;

	return newStr;
}

// returns an array of "words" from "str"
function collect_words(str) {
	return collect_fields(str, mystrings_whitespace);
}

// returns an array of "fields" from "str" where fields are separated by any character
// contained in the string "delimeters"
function collect_fields(str, delimeters) {
	var words = new Array();
	var numWords = 0;
	var ch;
	var len = str.length;
	var i = 0;

	while (1) {
		if (i > len - 1) {
			if (numWords == 0) return null;
			return words;
		}
		ch = str.charAt(i);

		if (delimeters.indexOf(ch) != -1) {
			i++;
			continue;
		}
		str = str.substring(i);
		len = str.length;
		i = 0;

		// any whitespace at beginning is now gone.
		// grab chars until eof or whitespace.
		while (1) {
			if (i > len - 1) {
				// ran outta string.  save this last one and return.
				words[numWords++] = str;
				return words;
			}

			ch = str.charAt(i);
			if (delimeters.indexOf(ch) == -1) {
				i++;
				continue;
			} else break;
		}

		// found end of word
		words[numWords++] = str.substring(0, i);
		str = str.substring(i);
		len = str.length;
		i = 0;

	}
	return null; // should never get here

}

// str - the input string
// remove_words - array of words to find from str
function find_bad_words(str, remove_words) {
	if (str == "") return null;
	words = collect_words(str);
	if (words == null) return null;

	var words_temp = new Array();
	keepers = 0;

	for (i=0; ; i++) {
		if (words[i] == null) break; // array exhausted
		str = words[i].toLowerCase();
		found = 0;

		for (j=0; ; j++) {
			if (remove_words[j] == null) break; // array exhausted
			if (str == remove_words[j]) {
				words_temp[keepers++] = remove_words[j];
				break;
			}
		}
	}
	if (keepers < 1) {
		return null;
	}
	return words_temp;
}


// str - the input string
// remove_words - array of words to remove from str
function remove_bad_words(str, remove_words) {
	if (str == "") return str;
	words = collect_words(str);
	if (words == null) return "";

	words_temp = new Array();
	keepers = 0;
	for (i=0; ; i++) {
		if (words[i] == null) break; // array exhausted

		str = words[i].toLowerCase();
		found = 0;

		for (j=0; ; j++) {
			if (remove_words[j] == null) break; // array exhausted
			if (str == remove_words[j]) {
				found = 1;
				break;
			}
		}
		if (! found) {
			words_temp[keepers++] = words[i];
		}
	}
	if (keepers < 1) {
		return "";
	}

	result = "";
	for (i=0; i < keepers; i++) {
		result = result + words_temp[i];
		if (i != (keepers - 1)) result = result + " ";
	}
	return result;
}




