/* A set of utilities to format numbers, floats, strings, et al

*/ 


function ellipsify(string, max_chars) {
  /* place-holder for an upcoming, *GOOD* ellipsify
  */
  var span = $j('<span></span>');
  if (string.length <= max_chars) {
    return span.html(string);
  }
  return span.html(string.slice(0, max_chars) + '...').attr('title', string);
}

function commify(s) {
  /* transform a number into a string, adding commas along the way, i.e.
     1234567 -> 1,234,567 
  */
  s += '';
  x = s.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;
}

function percentify(s) {
  /*
   * transforms a number into a percentage string, commifying the number and 
   * adding a percent symbol to the end.
   *
   * This function will correctly handle 0 and -
   *
   */
  return ($j.inArray(s, ['0', '-', 'N/A']) > 0) ? s : commify(parseFloat(s).toFixed(2)) + '%';
}

