function cent(amount) {
  return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}

function getRealMoney(amount) {
  return cent(Math.round(amount*Math.pow(10,2))/Math.pow(10,2));
}

var pricePrefix = 'price_';

function findTotal() {
  salecheck();
  var totalPrice = 0.00;
  for (var num=0; num<document.form.elements.length; num++) {
    if (typeof(document.form.elements[num].name) != undefined && 
        document.form.elements[num].name.substr(0,pricePrefix.length)==pricePrefix) {
//document.form.elements[priceEntries[num]].value = getRealMoney(document.form.elements[priceEntries[num]].value);
      totalPrice += document.form.elements[num].value*1; // has to be *1 so JS realizes it's a number
    }
  }

  // now adjust for the discount factor
  if (document.form.elements['duration'].options[document.form.elements['duration'].selectedIndex].value) {
    discount_factor = document.form.elements['duration'].options[document.form.elements['duration'].selectedIndex].value - 0;
    totalPrice *= discount_factor;
    document.getElementById("totaltotal").innerHTML="(number is a per-month average; actual payments will be <b>$"+getRealMoney(totalPrice)+"</b>)";
    if (discount_factor==2.7) { totalPrice = totalPrice/3; }
    else if (discount_factor==5.25) { totalPrice = totalPrice/6; }
    else if (discount_factor==1.95) { totalPrice = totalPrice/2; }
//    else if (discount_factor==3.7) { totalPrice = totalPrice/4; }
//    else if (discount_factor==4.5) { totalPrice = totalPrice/5; }
    else if (discount_factor==10.2) { totalPrice = totalPrice/12; }
    else { document.getElementById("totaltotal").innerHTML=""; }
  }
  else {
    document.getElementById("totaltotal").innerHTML="";
  }

  document.form.totalPrice.value=getRealMoney(totalPrice);
}

// takes a number x, uses that to choose selectmultiplyx, multiply it with the base price, and put the answer
// in pricex
function selectMultiply(x) {
  document.form.elements[pricePrefix + x].value = getRealMoney(document.form.elements['numslots'].value * document.form.elements[x].value);
  findTotal();
}

function selectIt(x) {
  document.form.elements[pricePrefix + x].value = getRealMoney(document.form.elements[x].value);
  findTotal();
}

function checkIt(x) {
  if ((document.form.elements[x].checked) == true) {
    document.form.elements[pricePrefix + x].value = getRealMoney(document.form.elements[x].value);
  }
  else { document.form.elements[pricePrefix + x].value = "0.00"; }
  findTotal();
}

// similar to selectMultiply, but for checkboxes, and goes off the "startprice" number instead of the "numslots" value.
// that is, it bases it on the adjusted amount for the game and location, and not the base slot price used for all games.
function checkMultiply(x) {
  if (document.form.elements[x].checked == true) {
    document.form.elements[pricePrefix + x].value = getRealMoney(document.form.elements['startprice'].value * document.form.elements[x].value);
  }
  else { document.form.elements[pricePrefix + x].value = "0.00"; }
  findTotal();
}
