function	formatCurrency(Amount)
{
  var i = parseFloat(Amount);
  i = parseInt(i*100)/100;
  s = new String(i);
  if(s.indexOf('.') < 0)
  {
     s += '.00';
  }
  else if(s.indexOf('.') == (s.length - 2))
  {
     s += '0';
  }
  return s;
}

function	Calculate(form)
{
  var i = 0;
  var SubTotal = 0.00;
  var Shipping = 0;
  var Qty = form.elements["ProductQty[]"];
  var UnitPrice = form.elements["ProductUnitPrice[]"];
  var Price = form.elements["ProductPrice[]"];

  for(i=0;i < Qty.length;i++)
  {
    if(isNaN(parseInt(Qty[i].value)))
    {
      Qty[i].value = 0;
    }
    else
    {
      Qty[i].value = parseInt(Qty[i].value);
    }
    if(parseInt(Qty[i].value) < 0)
    {
      Qty[i].value = 0;
    }

    Price[i].value = formatCurrency(parseInt(Qty[i].value) * parseFloat(UnitPrice[i].value));
    SubTotal += parseFloat(Price[i].value);
    Shipping += parseInt(Qty[i].value);
  }

  form.SubTotal.value = formatCurrency(SubTotal);

  Shipping = parseInt((Shipping+4)/5)*2;
  if(Shipping>4)
  {
    Shipping = 4;
  }

  form.Shipping.value = formatCurrency(Shipping);

  form.Total.value = formatCurrency(parseFloat(form.SubTotal.value) + parseFloat(form.Shipping.value));
}

function VerifyForm(form)
{
  if(form.first_name.value == '')
  {
     form.first_name.focus();
     alert("You must enter a first name.");
     return false;
  }

  if(form.last_name.value == '')
  {
     form.last_name.focus();
     alert("You must enter a last name.");
     return false;
  }

  if(form.email.value == '')
  {
     form.email.focus();
     alert("You must enter a valid e-mail address.");
     return false;
  }

  if(form.address1.value == '')
  {
     form.address1.focus();
     alert("Your address must have a first line.");
     return false;
  }

  if(form.zip.value == '')
  {
     form.zip.focus();
     alert("Your address must have a zip or post-code.");
     return false;
  }

  if(form.Total.value == '0.00')
  {
     alert("You have not ordered anything.");
     return false;
  }

  return true;
}

function AddItem(form, ItemNo, ItemQty, ItemText, ItemPrice)
{
  // Set up items. We need hidden elements for each item something like:
  //
  // <input type="hidden" name="item_name_1" value="First Item" />
  // <input type="hidden" name="quantity_1" value="2" />
  // <input type="hidden" name="amount_1" value="0.01" />
  // <input type="hidden" name="shipping_1" value="0.00" />
  // <input type="hidden" name="shipping2_1" value="0.00" />
  // <input type="hidden" name="handling_1" value="0.00" />
  //
  var ni = document.getElementById('ItemContainer');
  var newdiv = document.createElement('div');
  var divIdName = 'item_' + ItemNo;
  newdiv.setAttribute('id', divIdName);
  newdiv.innerHTML = '<input type="hidden" name="item_name_' + ItemNo + '" value="' + ItemText + '" /><input type="hidden" name="quantity_' + ItemNo + '" value="' + ItemQty + '" /><input type="hidden" name="amount_' + ItemNo + '" value="' + ItemPrice + '" /><input type="hidden" name="shipping_' + ItemNo + '" value="0.00" /><input type="hidden" name="shipping2_' + ItemNo + '" value="0.00" /><input type="hidden" name="handling_' + ItemNo + '" value="0.00" />';
  ni.appendChild(newdiv);
}

function DoPayPal(form)
{
  Calculate(form);
  if(false == VerifyForm(form))
  {
    return false;
  }

  var i = 0;
  var j = 1;
  var Qty = form.elements["ProductQty[]"];
  var UnitDescription = form.elements["ProductDescription[]"];
  var UnitPrice = form.elements["ProductUnitPrice[]"];

  for(i=0;i < Qty.length;i++)
  {
    if(parseInt(Qty[i].value) > 0)
    {
      AddItem(form, j, parseInt(Qty[i].value), UnitDescription[i].value, UnitPrice[i].value );
      j++;
    }
  }
  if(parseFloat(form.Shipping.value) != 0.00)
  {
    AddItem(form, j, 1, "Shipping", form.Shipping.value);
    j++;
  }

  return true;
}

function changeVisibility(prefix)
{
  var select = document.getElementsByName(prefix);

  if(1 != select.length)
  {
    return;
  }

  var td = document.getElementById(prefix + "Td");

  for(var i=0;i<td.childNodes.length; i++)
  {
      if(td.childNodes[i].id == select[0].value)
      {
	td.childNodes[i].style.display = "block";
      }
      else
      {
	td.childNodes[i].style.display = "none";
      }
  }
}

