
//
function trim_spaces(from_where) {
    
    // Store the string in a temporary variable    
    var temp_string = this

    // If no argument, then trim from both sides
    if (arguments.length == 0) {
        from_where = "BOTH"
    }
    
    // Trim spaces from the left
    if (from_where.toUpperCase() == "LEFT" || from_where == "BOTH") {
        while (temp_string.Left(1) == " ") {
            temp_string = temp_string.substring(1)
        }
    }
    
    // Trim spaces from the right
    if (from_where.toUpperCase() == "RIGHT" || from_where == "BOTH") {
        while (temp_string.Right(1) == " ") {
            temp_string = temp_string.substring(0, temp_string.length - 2)
        }
    }
    return temp_string   

}
//
function extract_left(total_chars) {
    return this.substring(0, total_chars)
}
//
function extract_right(total_chars) {
    return this.substring(this.length - total_chars)
}
//
function title_case() {
    var temp_string = this.toLowerCase()
    var left_string
    var right_string
    var new_letter
    
    // Make the first character uppercase
    var first_char = temp_string.Left(1).toUpperCase()
    temp_string = first_char + temp_string.substring(1)
    
    // Get the position of the first space
    var space_location = temp_string.indexOf(" ")
    
    // Loop until there are no more spaces
    while (space_location != -1) {
        
        // Get the part up to and including the current space
        left_string = temp_string.Left(space_location + 1)
        
        // Get the first letter of the next word and convert it to uppercase
        new_letter = temp_string.charAt(space_location + 1).toUpperCase()
        
        // Get the rest of the string after that letter
        right_string = temp_string.Right(temp_string.length - space_location - 2)
        
        // Put it all together
        temp_string = left_string + new_letter + right_string
        
        // Get the next space
        space_location = temp_string.indexOf(" ", space_location + 1)
    }
    return temp_string   

}
//
function replace_string(find_string, replace_string) {

    var temp_string = this
    var left_string
    var right_string
    var start_location
    
    // Get the position of the first instance of find_string
    var replace_location = temp_string.indexOf(find_string)
    
    // Loop until there are no more instances
    while (replace_location != -1) {
        
        // Get the part up to find_string
        left_string = temp_string.Left(replace_location)
        
        // Get the part after find_string
        right_string = temp_string.substring(replace_location + find_string.length)
        
        // Put it all together
        temp_string = left_string + replace_string + right_string
        
        // Make sure the next search begins after the replacement
        start_location = replace_location + replace_string.length
        
        // Get the next instance
        replace_location = temp_string.indexOf(find_string, start_location)
    }
    return temp_string   

}
//
//
function format_number(num) {

    if (num >= 1000 || num <= -1000) {
        
        // Work with the absolute value
        var number_string = Math.abs(num).toString()
        var insert_position

        // Calculate the position of the first comma
        switch (number_string.length % 3) {
            case 1 :
                insert_position = 1
                break
            case 2 :
                insert_position = 2
                break
            case 0 :
                insert_position = 3
                break
        }
        while (insert_position < number_string.length) {
            number_string = number_string.Left(insert_position) + "," + 
                            number_string.substring(insert_position)
            insert_position += 4
        }
        
        // If the original number was negative, tack on the minus sign
        if (num < 0) {
            return "-" + number_string
        }
        else {
            return number_string
        }
    }
    else {
    
        // If the number is between -1000 and 1000, just return it
        return num.toString()
    }
}
//
function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}
//
function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {
    
        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}
//
function doUCase() {
	return this.toUpperCase()
}
//
function doLCase() {
	return this.toLowerCase()
}
//
//
String.prototype.Left = extract_left
String.prototype.Right = extract_right
String.prototype.Trim = trim_spaces
String.prototype.toTitleCase = title_case
String.prototype.UCase = doUCase
String.prototype.LCase = doLCase
String.prototype.replace = replace_string





