//trim a string
function trim(inputString) {
	if (typeof inputString != "string") {
		return inputString;
	}
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") { // Check for spaces at the beginning of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") { // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	}
	return retValue; // Return the trimmed string back to the user
}

// Go through the inputString and replaces every occurrence of fromString with toString
function replaceSubstring(inputString, fromString, toString) {
	var temp = inputString;
	if (fromString == "") {
		return inputString;
	}
	if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
		while (temp.indexOf(fromString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			temp = toTheLeft + toString + toTheRight;
		}
	} else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
		var midStrings = new Array("~", "`", "_", "^", "#");
		var midStringLen = 1;
		var midString = "";
		// Find a string that doesn't exist in the inputString to be used
		// as an "inbetween" string
		while (midString == "") {
			for (var i=0; i < midStrings.length; i++) {
				var tempMidString = "";
				for (var j=0; j < midStringLen; j++) { 
					tempMidString += midStrings[i];
				}
				if (fromString.indexOf(tempMidString) == -1) {
					midString = tempMidString;
					i = midStrings.length + 1;
				}
			}
		} // Keep on going until we build an "inbetween" string that doesn't exist
		// Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
		while (temp.indexOf(fromString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			temp = toTheLeft + midString + toTheRight;
		}
		// Next, replace the "inbetween" string with the "toString"
		while (temp.indexOf(midString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(midString));
			var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
			temp = toTheLeft + toString + toTheRight;
		}
	} // Ends the check to see if the string being replaced is part of the replacement string or not
	return temp; // Send the updated string back to the user
}

//return the characters right of the first occurnece of the given substring in the given full string
function rightString(fullString, subString) {
	if (fullString.indexOf(subString) == -1) {
		return "";
	} else {
		return (fullString.substring(fullString.indexOf(subString)+subString.length, fullString.length));
	}
}

//return the characters right of the last occurnece of the given substring in the given full string
function rightBackString(fullString, subString) {
	if (fullString.lastIndexOf(subString) == -1) {
		return "";
	} else {
		return fullString.substring(fullString.lastIndexOf(subString)+1, fullString.length);
	}
}

//return the characters between the first occurnece of the given substring start and end in the given full string
function middleString(fullString, startString, endString) {
	if (fullString.indexOf(startString) == -1) {
		return "";
	} else {
		var sub = fullString.substring(fullString.indexOf(startString)+startString.length, fullString.length);
		if (sub.indexOf(endString) == -1) {
			return sub;
		} else {
			return (sub.substring(0, sub.indexOf(endString)));
		}
	}
}

//return the characters between the last occurnece of the given substring start and end in the given full string
function middleBackString(fullString, startString, endString) {
	if (fullString.lastIndexOf(startString) == -1) {
		return "";
	} else {
		var sub = fullString.substring(0, fullString.lastIndexOf(startString));
		if (sub.indexOf(endString) == -1) {
			return sub;
		} else {
			return (sub.substring(sub.indexOf(endString)+endString.length, sub.length));
		}
	}
}

//return the characters left of the first occurnece of the given substring in the given full string
function leftString(fullString, subString) {
	if (fullString.indexOf(subString) == -1) {
		return "";
	} else {
		return (fullString.substring(0, fullString.indexOf(subString)));
	}
}

//return the characters left of the last occurnece of the given substring in the given full string
function leftBackString(fullString, subString) {
	if (fullString.lastIndexOf(subString) == -1) {
		return "";
	} else {
		return fullString.substring(0, fullString.lastIndexOf(subString));
	}
}
