function formatNum(tbx)
{
	tbx = addCommas(format_number(stripNonNumeric(tbx), 2));
	return tbx;
}
function addCommas(nStr)
{
   nStr += '';
   x = nStr.split('.');
   x1 = x[0];
   x2 = x.length > 1 ? '.' + x[1] : '';
   var rgx = /(\d+)(\d{3})/;
   while (rgx.test(x1))
   {
      x1 = x1.replace(rgx, '$1' + ',' + '$2');
   }
   return x1 + x2;
}

// This function removes non - numeric characters
function stripNonNumeric( str )
{
   str += '';
   var rgx = /^\d|\.|-$/;
   var out = '';
   for( var i = 0; i < str.length; i ++ )
   {
      if( rgx.test( str.charAt(i) ) )
      {
         if( ! ( ( str.charAt(i) == '.' && out.indexOf( '.' ) != - 1 ) ||
         ( str.charAt(i) == '-' && out.length != 0 ) ) )
         {
            out += str.charAt(i);
         }
      }
   }
   
   return out;
}

function format_number(pnumber,decimals){
	if(pnumber.charAt(0) == '.') pnumber = '0' + pnumber;
	
    if (isNaN(pnumber)) { return 0};
    if (pnumber=='') { return 0};
    var snum = new String(pnumber);
    var sec = snum.split('.');
    var whole = parseFloat(sec[0]);
    var result = '';
    if(sec.length > 1){
        var dec = new String(sec[1]);
        dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
        dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
        var dot = dec.indexOf('.');
        if(dot == -1){
            dec += '.'; 
            dot = dec.indexOf('.');
        }
        while(dec.length <= dot + decimals) { dec += '0'; }
        result = dec;
    } else{
        var dot;
        var dec = new String(whole);
        dec += '.';
        dot = dec.indexOf('.');        
        while(dec.length <= dot + decimals) { dec += '0'; }
        result = dec;
    }
    return result;
}