// number formatting function
// copyright Stephen Chapman 24th March 2006, 10th February 2007
// permission to use this function is granted provided
// that this copyright notice is retained intact 
// Jason Miller at Matmon - Converted to JQuery 10-02-2008

(function($){
		$.fn.formatNumber = function(options) {
		var defaults = {
			dec: 2,
			thou: ',',
			pnt: '.',
			curr1: '$',
			curr2: '',
			n1: '',
			n2: ''
		};
		var options = $.extend(defaults, options);
		
		return this.each(function(){
				var $this = $(this);
				//$this.debug();
				
			var num = parseFloat($this.text())
				
			 var x = Math.round(num * Math.pow(10,options.dec));
			
			  if (x >= 0) options.n1=options.n2=''; 
			
			  var y = (''+Math.abs(x)).split('');

			  var z = y.length - options.dec; 
			  	
			  if (z<0) z--; 
			
			  for(var i = z; i < 0; i++) 
				y.unshift('0'); 
			
			  y.splice(z, 0, options.pnt); 
			  if(y[0] == options.pnt) y.unshift('0'); 
			
			  while (z > 3) 
			  {
				z-=3; 
				y.splice(z,0,options.thou);
			  } 
			
   			  var newnum = y.join('');
			  
			  var r = options.curr1+options.n1+newnum+options.n2+options.curr2;
			  
			  $this.text(r);
						  
		});
	};
})(jQuery);


