/**
 * Substitutes keywords in a string using an object/array.
 * Removes undefined keywords and ignores escaped keywords.
 * 
 * @param object - (mixed) The key/value pairs used to substitute a string.
 * @param regexp - (regexp, optional) The regexp pattern to be used in the string keywords, with global flag. Defaults to /\?{([^}]+)}/g .
 *
 * @return string The substituted string.
 */
String.prototype.substitute = function(object, regexp){
	return this.replace(regexp || (/\\?\{([^}]+)\}/g), function(match, name){
		if (match.charAt(0) == '\\'){ return match.slice(1); }
		return (typeof object[name] !== "undefined") ? object[name] : '';
	});
};

Ajax.MultiUpdater = Class.create(Ajax.Request, {
	initialize: function($super, containers, url, options) {
		this.containers = containers || [];

		options = Object.clone(options);
		var onComplete = options.onComplete;
		options.onComplete = (function(response, json) {
			this.updateContent(response.responseText);
			if (Object.isFunction(onComplete)) onComplete(response, json);
		}).bind(this);

		$super(url, options);
	},

	updateContent: function(responseText) {

		var options = this.options;
		
		if (!options.evalScripts) responseText = responseText.stripScripts();
		
		var tmpElement = new Element('span').insert(responseText);
		
		this.containers.each(function(container) {
			$$(container.target).invoke('update', tmpElement.select(container.source).first());
		});
	}
});