/** Create a string replacement object (using regexp rules so remember to double escape your strings):
	ReplaceUnit keeps an array of regexp strings to find and replacements to make when those values are found,
	it searches in the original array order on the previous replacements so that a search where:
		{'a'->'b', 'b'->'c'} (written as new ReplaceObject( ['a','b'], ['b','c'] )
		would turn a string like 'a b c' into 'c c c' (whereas {b->c, a->b} gives us 'b c c')
	
	It's important to remember that these are strings being passed that are then turned to RegExp so we have to
	double escape them '\\$' = /\$/g (find all $ characters) and '\\\\$' = /\\$/g (find all \ characters at the end)
	 
	@constructor
	@param {Mixed} find can be a string or an array of strings
	@param {Mixed} replace is a string (or an array when find is an array)
*/
function ReplaceObject( find, replace )
{	this.farr_	= (typeof find=='object');
	this.rarr_	= (this.farr_ && typeof replace=='object' && replace.length==find.length);
	if (this.farr_)
	{	this.find = new Array( find.length );
		for (var i in find)
			this.find[i] = new RegExp(find[i],'g');
	}	else
		this.find = new RegExp(find,'g');
	if (this.rarr_)
		this.repl = replace.slice();
	else
		this.repl = (typeof replace=='object') ? replace[0] : replace;
}
ReplaceObject.prototype.replace = function( string )
{	if (this.farr_)
	{	var replaced = string;
		for (var i in this.find)
			replaced = replaced.replace( this.find[i], (this.rarr_ ? this.repl[i] : this.repl) );
		return replaced;
	}	else
		return string.replace( this.find, this.repl );
}

/** Creates and adds DOM Elements to an existant Node from template of a simple object (or objects).
	(Uses recursion, so may have deep templates may invoke stack memory issues in some browsers)
	@param {Node} addto		Dom Element to add the newly created nodes to (using addChild())
	@param {Mixed} templ	template to use to create elements from, arrays add each entry via recursion,
							strings become textNodes, and objects create DOM elements of type obj.node & set all
							subsequent values in the created element (exception child fields are processed as a 
							new template via recursion)
	@param {ReplaceUnit} replacer	Optional parameter, if set runs all strings (textNodes and fields) thru the 
									ReplaceUnit replace() function (set to false if we don't want to use it)
	@param {Node} before	Optional node to place stuff before
*/
function addFromDOMTemplate( addto, templ, replacer, before )
{	if (typeof templ=='string')
	{	var txtnode = document.createTextNode( (replacer ? replacer.replace(templ) : templ) );
		if (before)
			addto.insertBefore( txtnode, before );
		else
			addto.appendChild( txtnode );
	}	else
		if (typeof templ=='object')
		{	if (typeof templ.node=='string')
			{	var toadd = document.createElement( templ.node );
				for (var field in templ)
					switch (field)
					{	case 'node':
							break;
						case 'child':
							addFromDOMTemplate( toadd, templ[field], replacer);
							break;
						default:
							toadd[field] = (typeof templ[field]=='string' && replacer) ? replacer.replace(templ[field]) : templ[field];
					}
				if (before)
					addto.insertBefore(toadd,before);
				else
					addto.appendChild(toadd);
			}	else
				for (var i in templ)
					addFromDOMTemplate( addto, templ[i], replacer, before );
		}
}


function DOMElemRemoveChildren( domelem, del )
{	while (domelem.hasChildNodes())
	{	DOMElemRemoveChildren( domelem.lastChild );
		domelem.removeChild( domelem.lastChild );
	}
}
