// functions for formatting currency values
// written by Max Cooper

// format an integer value into whole US$
function integerToDollarString(intValue)
{
  // ensure that it is a properly formed integer
  var fmt = ""+parseInt(""+intValue);

  // add commas
  var re = /(-?\d+)(\d{3})/
  while (re.test(fmt)) {
    fmt = fmt.replace(re, "$1,$2");
  }

  // add a dollar sign
  fmt = "$" + fmt;

  return fmt;
}

// format an integer value into US$ including pennies
function integerToDollarAndCentString(intValue)
{
        // ensure that it is a properly formed integer
        var cents = (intValue % 1) * 100;
        cents = Math.round(cents);
        if (cents == 0) {
          cents = "00";
        }
       
        var fmt = ""+parseInt(""+intValue);
                       
        // add commas
        var re = /(-?\d+)(\d{3})/
        while (re.test(fmt))
        {
                fmt = fmt.replace(re, "$1,$2");
        }

        // add a dollar sign
        fmt = "$" + fmt + "." + cents;
        return fmt;
}

// return an integer representing the number in the formatted 
// dollar string passed as the argument
function dollarStringToInteger(dstr)
{
  var idx;
  
  //remove commas
  while ((idx=dstr.indexOf(",")) != -1) {
    dstr = dstr.substring(0,idx) + dstr.substring(idx+1);
  }
  
  //remove dollar signs
  while ((idx=dstr.indexOf("$")) != -1) {
    dstr = dstr.substring(0,idx) + dstr.substring(idx+1);
  }
  
  //return integer
  return parseInt(dstr);
}
