var timeout, doUpdateFlash;

function asCurrency(num) {
  if(isNaN(num))
    num = "0";
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
    cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+' '+
  num.substring(num.length-(4*i+3));
  return (num + '.' + cents);
}

function retallyTotal() {
  domELM=document.getElementById("tallyTotal");
  oldTotal=parseInt(domELM.innerHTML);
  
  total=0;
  domELM=document.getElementById("cSize");
  numElm=parseInt(domELM.value);
  
  for (i=0; i<numElm; i++) {
    domELM=document.getElementById("price"+i);
    unitPrice=parseFloat(domELM.value);
    domELM=document.getElementById("amount"+i);
    unitAmount=parseInt(domELM.value);
    unitAmount=(isNaN(unitAmount) || unitAmount < 0?0:unitAmount);
    domELM=document.getElementById("del"+i);
    unitKeep=!domELM.checked;
    
    subTotal=unitAmount*unitPrice;
    
    domELM=document.getElementById("tally"+i);
    domELM.innerHTML=asCurrency(subTotal);
    
    if (unitKeep) {
      domELM.style.textDecoration='none';
      total+=subTotal;
    } else {
      domELM.style.textDecoration='line-through';
    }
  }
  
  domELM=document.getElementById("tallyTotal");
  domELM.innerHTML=asCurrency(total);
  
  if (total!=oldTotal) {
    doUpdateFlash = true;
    flashUpdate();
  }
}

function checkUpdate() { if (doUpdateFlash) {
  return confirm("Are you sure you want continue?\n(Unless you cancel and hit Update, you will lose your changes.)");
} return true; }

function flashUpdate() { if (doUpdateFlash) {
  clearTimeout(timeout);
  domELM=document.getElementById("Update");
  if (domELM.style.color=="red")
    domELM.style.color="white";
  else
    domELM.style.color="red";
  timeout = setTimeout('flashUpdate()',750);
} }