var btnWhichButton;

function tallyCards()
{
   var total = 0;
   var submitbutton = document.getElementById('submitbutton');

   var lowrate = parseFloat(document.getElementById('lowrate').innerHTML);
   var highrate = parseInt(document.getElementById('highrate').innerHTML);
   var threshold = parseInt(document.getElementById('threshold').innerHTML);
   var priceeach = parseFloat(document.getElementById('priceeach').innerHTML);
   var minorder = parseInt(document.getElementById('min-order').innerHTML);

   var outcome = false;

   var cvalue =0, q=0;

   for (q=1; q<11; q++)
   {
     cvalue = parseInt(document.getElementById('card'+q).value);
     if (isNaN(cvalue)) {cvalue = 0; }
     total = total + cvalue;
   }

// are we recalculating or validating?
   if (btnWhichButton.value == 'Recalculate order')
   {
// recalculating...
      document.getElementById('ordersize').innerHTML=total;

// calculate price - 1.70p for each card

      if (total<=threshold)
      {
         document.getElementById('shipping').innerHTML = lowrate.toFixed(2);
         total = (total * priceeach) + lowrate;
      }
      else
      {
         document.getElementById('shipping').innerHTML = highrate.toFixed(2);
         total = (total * priceeach) + highrate;
      }
      cvalue = parseFloat(document.getElementById('donationbox').value);
      if (isNaN(cvalue)) {cvalue = 0;}
      total = total + cvalue;
      document.getElementById('ordertotal').innerHTML = total.toFixed(2);
      submitbutton.disabled=false;
   }
   else
   {
// validating...
     submitbutton.disabled=true;
     if (total>=minorder)
     {
        outcome=true;
     }
     else
     {
        alert("Please select " + minorder + " or more cards to make up an order.");
        outcome=false;
        submitbutton.disabled=false;
     }
   }
return outcome;
}


