
var dbg = false;

// When the page is loaded, set the calculated fields to readonly.
// This ensures they are not set if script is disabled.
function OnLoadEvent()
{
  var ROFields = ["vv9", "p19", "p29", "st9", "cc9", "tt9"];
  for (i in ROFields)
  {
    document.getElementById(ROFields[i]).className = "readonly";
    document.getElementById(ROFields[i]).setAttribute("readOnly", "readonly");
  }
  for (col = 0; col < 3; col++)
  {
    document.getElementById("p1"+col).className = "readonly";
    document.getElementById("p1"+col).setAttribute("readOnly", "readonly");
    document.getElementById("p2"+col).className = "readonly";
    document.getElementById("p2"+col).setAttribute("readOnly", "readonly");
  }
  document.OrderForm.CalcBtn.className = "ShowButton";
  StyleRadios();
}

function MyFormat(x)
{
  if (x.toFixed)
  {
    return x.toFixed(2);
  }
  else
  {
    return x;
  }
}

function CalcSubtotal()
{
  if (dbg) alert("CalcSubtotal()");
  var goods = Number(document.OrderForm.vv9.value);
  var pp1 = Number(document.OrderForm.p19.value);
  var pp2 = Number(document.OrderForm.p29.value);
  var subtotal = goods + pp1 + pp2;
  document.OrderForm.st9.value = MyFormat(subtotal);
}

function CalcRowTotals()
{
  if (dbg) alert("CalcRowTotals()");
  var RowIDs = ["vv", "p1", "p2"];
  for (ID in RowIDs)
  {
    RowTotal = 0;
    for (Col = 0; Col < 3; Col++)
    {
      var name = RowIDs[ID]+Col;
      RowTotal += Number(document.getElementById(RowIDs[ID]+Col).value);
    }
    document.getElementById(RowIDs[ID]+"9").value = MyFormat(RowTotal);
  }
}

function CalcPostage()
{
  var Nregns = 4;
  var Ntypes = 3;
  if (dbg) alert("CalcPostage");
  var regn = 0;
  if (document.OrderForm.region[1].checked)
  {
    regn = 1;
  }
  else if (document.OrderForm.region[2].checked)
  {
    regn = 2;
  }
// disabled while surface mail removed
//  else if (document.OrderForm.region[3].checked)
//  {
//    regn = 3;
//  }
  var P1 = Post1.slice(Ntypes*regn, Ntypes*(regn+1));
  var P2 = Post2.slice(Ntypes*regn, Ntypes*(regn+1));
  for (Col = 0; Col < Ntypes; Col++)
  {
    var quant = Number(document.getElementById("qq"+Col).value);
    if (quant == 0)
    {
      document.getElementById("p1"+Col).value = "0.00";
      document.getElementById("p2"+Col).value = "0.00";
    }
    else
    {
      document.getElementById("p1"+Col).value = MyFormat(P1[Col]);
      document.getElementById("p2"+Col).value = MyFormat(P2[Col] * (quant-1));
    }
  }
}

function CalcSurcharge()
{
  if (document.OrderForm.payment[0].checked)
  {
    document.OrderForm.cc9.value = MyFormat(0);
  }
  else
  {
    document.OrderForm.cc9.value = MyFormat(document.OrderForm.st9.value * 0.05);
  }
}

function CalcTotal()
{
  document.OrderForm.tt9.value = MyFormat(Number(document.OrderForm.st9.value) + Number(document.OrderForm.cc9.value));
}

function ChangeRadioClass(element, state)
{
  if (element.className == "radioSelected" && !state)
  {
    element.className = "radioNormal";
  }
  else if (element.className == "radioNormal" && state)
  {
    element.className = "radioSelected";
  }
}

function StyleRadios()
{
  if (dbg) alert("StyleRadios");
  for (i = 0; i < 3; ++i)
  {
    ChangeRadioClass(document.getElementById('paymenttext'+i), document.OrderForm.payment[i].checked);
	}
  for (i = 0; i < 3; ++i)
  {
    ChangeRadioClass(document.getElementById('regiontext'+i), document.OrderForm.region[i].checked);
  }
}

function CalcAll()
{
  if (dbg) alert("CalcAll");
  StyleRadios();
  CalcPostage();
  CalcRowTotals();
  CalcSubtotal();
  CalcSurcharge();
  CalcTotal();
}
