// function to calculate the total cost field
// functions all copyright 2002 by Webscorpion.com.

function SumItUp() {
  var totalsetup = 0; 
  var totalcost = 0;
  var costname;
  var setupname;
  var i;
  for (i=0; i <= 4; i++) {
    costname   = "cost" + i; 
	setupname  = "setup" + i;
    totalsetup += 1 * document.order[setupname].value;
    totalcost  += 1 * document.order[costname].value;
  }
  yearcost = totalcost * 11;
  document.order.totalsetup.value = totalsetup;
  document.order.totalcost.value = totalcost;
  document.order.yearsetup.value = totalsetup;
  document.order.yearcost.value = yearcost;  
  
  if (totalcost < 10) {
  	document.order.yearly.checked = true;
	document.order.notice.value = "Due to billing overhead minimal monthly charge is $10";
	} else {
	document.order.notice.value = "";
	}

  if (document.order.yearly.checked)
	  document.order.billedccusd.value = yearcost + totalsetup;
  else
	  document.order.billedccusd.value = totalcost + totalsetup;  
  document.order.billedppusd.value = document.order.billedccusd.value;
//  document.order.billedmbeur.value = parseInt(document.order.billedccusd.value * 0.8);

}

// function to update cost when quantity is changed
function UpdateCost(number, unitsetup, unitcost) {
   var costname = "cost" + number;
   var setupname = "setup" + number;
   var qtyname = "qty" + number;
   var input = document.order[qtyname].value;
   if (input == 0) unitsetup = 0;
   document.order[qtyname].value = input;
   document.order[setupname].value = unitsetup;
   document.order[costname].value = document.order[qtyname].value * unitcost; 
   SumItUp();
}

function UpdatePlan() {
  switch (document.order.qty0.value)
  {
    case "R1":
	  document.order.setup0.value = 49;
	  document.order.cost0.value = 37;
	  break;
    case "R2":
	  document.order.setup0.value = 49;
	  document.order.cost0.value = 68;
	  break;
    case "R3":
	  document.order.setup0.value = 49;
	  document.order.cost0.value = 112;
	  break;
    case "R4":
	  document.order.setup0.value = 49;
	  document.order.cost0.value = 149;
	  break;
    default:
	  document.order.setup0.value = 0;
	  document.order.cost0.value = 0;
	  break;
  }
  SumItUp();
}

function recalcAll() {
  UpdatePlan();
  UpdateCost(1, 25, 2);
  UpdateCost(2, 99, 0);
  UpdateCost(3, 14, 0);
  UpdateCost(4, 49, 0);
}

function undo() {
  alert("Please do not update total fields.\nThey are updated automatically based on items chosen in the order column, left.");
  UpdatePlan();
}


