// Private function
//	cleanString(str)
//		Sanitize the string
//		- Remove CRs and LFs
//		- Turn consecutive spaces into a single space
//		- If the resultant text ends in ":", remove it.
//	str		String to clean
//	Returns:
//		Cleaned string
function cleanString(str)
{
	str = str.replace(/\r/g, " ");
	str = str.replace(/\n/g, " ");
	str = str.replace(/\s\s/g, " ");
	if (str.length > 0 && str.charAt(str.length-1) == ":")
	{
		str = str.substr(0, str.length-1);
	}

	return trim(str);
}

// Private function
//	compressLines(str)
//		Cleans up a text string by removing any empty lines and turning
//		\r\n into just \n.
//	str		String to compress
//	Returns:
//		Compressed string
function compressLines(str)
{
	str = str.replace(/\r/g, "\n");

	var oldlength = 0;
	while (oldlength != str.length)
	{
		oldlength = str.length;
		str = str.replace(/\n\n/g, "\n");
	}
	
	return str;
}

// Private function
//	compressSpaces(str)
//		Compress multiple contiguous spaces into a single space. Also removes
//		any leading or trailing spaces
//	str		String to compress
//	Returns:
//		Compressed string
function compressSpaces(str)
{
	var oldlength = 0;
	while (oldlength != str.length)
	{
		oldlength = str.length;
		str = str.replace(/\s\s/g, " ");
	}
	
	return trim(str);
}

// Private function
//	parseFilename(str)
//		Return the base filename portion, which is between the
//		path and the extension (/temp/foo/bar.jpg -> "bar")
//	str		String containing full image path
//	Returns:
//		Base filename
function parseFilename(str)
{
	// Remove the quotes around the name and extract the stuff
	// between the path and the extension
	str = str.replace(/'/g, "");

	var loc = str.lastIndexOf("/");
	if (loc != -1)
	{
		str = str.substr(loc+1);
	}

	var result = splitAtString(str, ".");
	if (result == null)
	{
		return str;
	}

	return result.before;
}

// Private function
//	parseRow(str)
//		Parse the elements out of a row
//	Returns:
//		String array of the elements
function parseRow(str)
{
	var elements = new Array();

	for (var i=0; i<20; i++)
	{
		var result = splitBetween(str, "<td", ">");
		if (result == null)
		{
			break;
		}

		result = splitAtString(result.after, "</td>");
		if (result == null)
		{
			break;
		}

		elements.push(removeTags(result.before));
		str = result.after;
	}

	return elements;
}

// Private function
//	removeTags(str)
//		Removes any html tags from the string
function removeTags(str)
{
	str = str.replace(/<br>/g, " ");

	while (true)
	{
		var result = splitBetween(str, "<", ">");
		if (result == null)
		{
			str = str.replace(/\r/g, " ");
			str = str.replace(/\n/g, " ");
			str = str.replace(/\s\s/g, " ");
			return trim(str);
		}
		str = result.before + result.after;
	}
}

// Private function
//	splitAtString(str, tag)
//		Splits a string at a given tag string
//	str		String to be split
//	tag		String where the split should occur
//	Returns:
//		null if tag not found
//		object with .before and .after properties
function splitAtString(str, tag)
{
	var tagLoc = str.indexOf(tag);
	if (tagLoc == -1)
		return null;

	var tagEnd = tagLoc + tag.length;

	var result = new Object();

	result.before = (tagLoc > 0) ? str.substr(0, tagLoc) : "";
	result.after = tagEnd < str.length ? str.substring(tagEnd) : "";

	return result;
}

// Private function
//	splitBetween(str, tag1, tag2)
//		Splits a string into 3 pieces. returning what's before
//		the first string, what's between the strings, and what's
//		after the second. For example
//			splitBetween("Hello <b>world</b>!", "<b>", "</b>") 
//		returns an object
//			result.before = "Hello ";
//			result.between= "world";
//			result.after = "!";
//	str		input string
//	tag1	first string to find
//	tag2	second string to find
//	Returns:
//		null if the strings cannot be found
//		object with .before, .between and .after properties
function splitBetween(str, tag1, tag2)
{
	var temp = splitAtString(str, tag1)
	if (temp == null)
	{
		return null;
	}

	var result = new Object();
	result.before = temp.before;
	
	temp = splitAtString(temp.after, tag2);
	if (temp == null)
	{
		return null;
	}

	result.between = temp.before;
	result.after = temp.after;

	return result;
}

// Private function
//	trim(str)
//		Trims leading and trailing whitespace from the string
function trim(str)
{
        return str.replace(/^\s*|\s*$/g, "");
}
