function formatQuantity(numFld)
{
  var result = formatNumber(numFld, "###,###,###");
  if ( parseIntVal(numFld) > 2000000000 )
  {
    formatAlert(numFld, "Numbers in excess of 2 billion not allowed.")
    setDefaultAmount(numFld);
  }
  if (result == "") setDefaultAmount(numFld);
  return result;
}

function formatDecimal(numFld)
{
  var result = formatNumber(numFld, "###,###,###.##");
  return result;
}

function formatDollars(numFld)
{
  var result = formatNumber(numFld, "$###,###,###");
  if (result == "") setDefaultDollars(numFld);
  return result;
}


function formatNumber(numFld, inFmt)
/*
  purpose:  Formats a number using given format.  If invalid,
            alerts user and sets focus back to form field (if field passed)
  params:    numFld:  Form field or string containing number with
                    possible format chars ($%-,).
                    if a form field, places formatted value into it.
                    if a string variable, returns formatted value.
            inFmt:  Output format of number (see html for examples)
                    Defined format chars: "$", "%", "-", ",", ".", 0-9
                    inFmt can define the ceiling for number input
                    ie: "100%".  the "-" can be placed at front or end.
            parm 3:  Optional, set to "Y" if mandatory entry.  Thought of
                    adding an "M" to format, but didn't
  author:    Adam Burton: burton29@bigfoot.com
  date:      4/21/01
*/
{
  var intVal = parseIntVal(numFld);

  //check for empty value and if it's mandatory. (optional param 3 = "Y")
  if (intVal == "")
  {
    if (arguments.length == 3)
      if (arguments[2].charAt(0).toUpperCase() == "Y")
      {
        formatAlert(numFld, "Must enter value - Format is " + inFmt)
      }
    return("")
  }

  if(isNaN(intVal))
  {
    formatAlert(numFld, "Invalid number - Format is " + inFmt + " : " + intVal)
    if (isField) numFld.focus()
    return("")
  }

  //strip format characters($%-,) from the format
  fmt = inFmt.replace(/\$|%|-|\,/g,'')

  //get number of format's digits left and right of decimal
  digits = fmt.indexOf(".")
  if (digits < 0)
  {
    digits = fmt.length
    decs = 0
  }
  else
  {
    decs = fmt.length - digits - 1
  }

  //check for negative
  var negPos = -1
  if (inVal.indexOf("-") >= 0)
  {
    negPos = (inFmt.indexOf("-"))
    if (negPos >= 0)
      makeNeg = true
    else
    {
      formatAlert(numFld, "Negative number not allowed - Format is " + inFmt)
      if (isField) numFld.focus()
      return("")
    }
  }

  //get input's digits left and right of decimal
  ary = intVal.split(".")
  intVal = ary[0]
  var decVal = ""
  if (ary.length > 1)
  {
    decVal= ary[1].replace(/0+$/,"")  //rid of trailing 0's
    if (decVal.length > decs)
    {
      decVal = decVal.substr(0, decs);
    }
  }

  //check for too big (format can control ceiling - ie format="100.00%")
  if (Number(intVal) > Number(inFmt))
  {
    formatAlert(numFld, "Out of range - Max value is " + inFmt + " : " + Number(intVal))
    if (isField) numFld.focus()
    return("")
  }

  //build formatted number
  for (i=0; decVal.length < decs; i++) decVal = decVal + "0"  //pad decimal

  if (inFmt.indexOf(",") > 0)  //add commas
  {
    for (var i = 0; i < Math.floor((intVal.length-(1+i))/3); i++)
    intVal = intVal.substring(0,intVal.length-(4*i+3))+','+intVal.substring(intVal.length-(4*i+3));
  }
  if (decVal.length > 0) intVal = intVal + '.' + decVal
  if (inFmt.indexOf("$") >= 0) intVal = "$" + intVal
  if (inFmt.indexOf("%") >= 0) intVal = intVal + "%"
  if (negPos == 0) intVal = "-" + intVal
  if (negPos > 0) intVal = intVal + "-"  //assume neg at end
  if (isField)
  {
    numFld.value = intVal
  }
  else
  {
    return(intVal)
  }
}

function parseIntVal( numFld )
{
  var intVal;
  //can pass form field or string value
  isField = (typeof(numFld) == "object")  //
  if (isField)
  {
    inVal = numFld.value
  }
  else
  {
    inVal = numFld
  }
  // this has to be a string or else indexOf will fail later
  inVal = inVal + "";

  //strip format characters($%-,) from input
  intVal = inVal.toString().replace(/\$|%|-|\,/g,"");

  return intVal;
}

function formatAlert(input, msg)
{
  alert(msg);
  if (input != null) input.value = "";
}