//
// String methods
//
NetR = typeof NetR === 'undefined' ? {} : NetR;

NetR.String = {};

// Format-function to format a string.
// Arguments: Unlimited, replaces {0} with first argument, {1} with second argument, and so forth
// Ex: "Hello {0}, this is {1}".format("world", "nice");
// Returns: "Hello world, this is nice"
NetR.String.format = function(string) {
	var args = arguments;
	return string.replace(/{(\d+)}/g, function(o, m) {
		return args[parseInt(m, 10) + 1];
	});
};

// Returns new string with first letter in uppercase and all the rest i lowercase
NetR.String.capitalize = function(string) {
	return string.substring(0, 1).toUpperCase() + string.substring(1).toLowerCase();
};

// Returns true if string is one of the strings passed as arguments
NetR.String.isOneOf = function (string) {
	var args = $.makeArray(arguments);
	// Remove first argument
	args.shift();
	return $.inArray(string.toString(), args) > -1;
};

// jQuery 1.3.2 does not parse URI's with fragments (#) as it should,
// so we have to build the URI ourselves.
// @param String params  Params in query string format: 'key=value&key2=value2'
NetR.String.appendParams = function (string, params) {
	var uri = new URI(string.toString());
	uri.query = (uri.query ? uri.query + '&' : '') + params;
	return uri.toString();
};

// Strip hash part of url
NetR.String.stripHash = function (string) {
	return string.replace(/#.+$/, '');
};

// Replace placehoders in string with corresponding keys from Object o
NetR.String.supplant = function (string, o) {
    return string.replace(/{([^{}]*)}/g,
        function (a, b) {
            var r = o[b];
            return typeof r === 'string' || typeof r === 'number' ? r : a;
        }
    );
};

// Trim leading and trailing whitespace
NetR.String.trim = function (string) {
	return string.replace(/^\s+(.*)\s+/, '$1');
};
