String.prototype.trim=function(){
	return this.replace(/(^\s*)|(\s*$)/g,'');
}

var TSWIDGET;
{TSWIDGET = {};}

/** 
 * @fileoverview Base TSWidget namespace and namespace globals. Also contains some non-namespaced utility functions
 *
 * @author mowings@turbosquid.com
 */
 
 



/**
 *  Remove all children from DOM node
*/
function removeAllDomChildren(node) {
	if (typeof node == 'undefined' || node == null) return;
	while ( node.hasChildNodes() ) { node.removeChild(node.firstChild); }
}

/**
* Create a dom element with optional class, id, innerhtml, etc
* @param {string} tagname -- required
* @param {string} className optional (can be null)
* @param {string} id optional (can be null)
* @param {string} innerHTML optional (can be null)
*/
function Element(tag, elemClass, id, html) {
	(typeof elemClass == 'undefined') ? elemClass = null : elemClass=elemClass;
	(typeof id == 'undefined') ? id = null : id=id;
	(typeof html == 'undefined') ? html = null : html=html;	
	var ret = document.createElement(tag);
	if( elemClass != null ) ret.className = elemClass;
	if( id != null ) ret.id = id;	
	if( html != null) ret.innerHTML = html;
	return ret;
}

/**
*	String format
* 	@param {string} str Format string with argument replacement tokens '{0}' ... '{n}'
*   Additional parameters are replacement valuses
*   @returns formatted string
*/
function format(str)
{
  for(i = 1; i < arguments.length; i++)
  {
	var token='{' + (i - 1) + '}';
	var intPos = str.indexOf( token );
	while (intPos != -1) {
		str = str.replace(token, arguments[i]);
		intPos = str.indexOf( token );
	}
  }
  return str;
}
/**
 * Dollar format
 * @param {string} value
 *  @returns formatted dollar
 */
function FormatDollarValue(value){
	value = value + "";
	value = value.trim();
	if(value!=""){
		var s = value.split(".");
		var re=/(\d{1,3})(?=(\d{3})+(?:$|\D))/g 
		var s1 = s[0].replace(re,"$1,");
		var s2;
		if(s[1]==undefined || s[1]==""){
			s2 = ".00";
		}
		else if(s[1].length==1){
			s2 = "." + s[1] + "0";
		}
		else if(s[1].length>2){
			s2 = "." + s[1].substr(0,2);
		}
		else{
			s2 = "." + s[1];
		}
		return "$" + s1 + s2;
	}
	return "$0.00";
}

/**
* log to firebug console, if present. Uses same format as format() above
*/
TSWIDGET.log = function(str) {
	if(typeof(console) == "undefined") return; // bail if no console
	for(i = 1; i < arguments.length; i++)
  	{
		var token='{' + (i - 1) + '}';
		var intPos = str.indexOf( token );
		while (intPos != -1) {
			str = str.replace(token, arguments[i]);
			intPos = str.indexOf( token );
		}
	}
	console.log(str);
}

/**
* 
* Subscribe to a CustomEvent by event type <br/>
* Monkey patch for YAHOO EventProvider. Adds a subscribe method that creates the event if needed
*
* @method subscribe
* @param p_type     {string}   the type, or name of the event
* @param p_fn       {function} the function to exectute when the event fires
* @param p_obj      {Object}   An object to be passed along when the event 
*                              fires
* @param p_override {boolean}  If true, the obj passed in becomes the 
*                              execution scope of the listener
*/
YAHOO.util.EventProvider.prototype.subscribeEx = function( p_type , p_fn , p_obj , p_override ) {
	this.createEvent(p_type); // create event if it doesn't exist
	this.subscribe( p_type , p_fn , p_obj , p_override );
}

if (typeof TSWIDGET.EVENTS === 'undefined') {
	TSWIDGET.EVENTS= new YAHOO.util.EventProvider(); 
	}

