// Generic positive number decimal formatting function
function formatNumber(expr, decplaces) {
// raise incoming value by power 10 times the number of incoming decimal place, then
// round to an integer and convert to a string.
   var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces));
// pad small value strings with zeroes to the left of the rounded number
   while (str.length <= decplaces) {
      str = "0" + str;
   }
// establish position of decimal point
   var decpoint = str.length - decplaces;
// assemble final result from: (a) the string up to the position of the decimal point;
// (b) the decimal point; and (c) the balance of the string.  return finished product.
   return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}

// Function will force numeric input in a field which is the value of a form object.
// Works well if called as follows: onchange="return isNumber(this)"
function isMoney(obj)
 {
 strInput = new String(obj.value);
 var digit;
 for (i = 0;i < strInput.length;i++)
  {
   if (strInput.substr(i,1) != ".")
    {
     digit = parseInt(strInput.substr(i,1));
     if (isNaN(digit))
       {
        alert("Input must be numeric containing only numbers and a period!!\r\n" + strInput + " Please re-enter");
        obj.value="";
        obj.focus();
        obj.select();
        return false;
       }
    }
  }
 return true;
 }

// Turn incoming expression into a dollar value
function NumberToDollars(obj) {
   if (!isMoney(obj)) return false;
   var result = formatNumber(obj.value,2);
   if (String(result) == "N.aN") {
     result = "";
    }
   else {
     obj.value = result;
    }
   return true;
 }
//   var output = "";
//   var stop = result.length - 3;
//   var mantissa = result.substr(0,stop);
//   var decima = result.substr(stop,3);
//   if (mantissa.length > 3) {
//      var numcommas = parseInt(mantissa.length / 3);
//      if ((numcommas*3) == mantissa.length) {
//          numcommas--;
//      }
//   }
//   else {
//      var numcommas = 0;
//   }
//   var commacnt = 0;
//   var digitcnt = 0;
//   for (i=mantissa.length-1;i>=0;i--) {
//      output = mantissa.substr(i,1) + output;
//      digitcnt++;
//      if (digitcnt == 3) {
//         if (commacnt < numcommas) {
//            output = "," + output;
//            commacnt++;
//            digitcnt = 0;
//         }
//
//      }
//   }
//   output = output + decima;
//   obj.value = output;
//   return true;
//}
