//
//   Support for ezi-merchant interface
//
//   V.B.McKee Mar 2006
//
//   Copyright © 2006 KeeTech Ltd. All rights reserved.
//

function getProduct(id)
{
  // Read from database
  var a1 = getBaseProductDB(id);
  var a2 = getProcessingDB(id);
  var a3 = getPhysFormatDB(id);

  // Discounts, if any
  var d1 = getProductDiscount(id) ? 1.0 - (a1[8]/100.0) : 1.0;
  var d2 = getProcessDiscount(id) ? 1.0 - (a2[8]/100.0) : 1.0;
  var d3 = getPhysicalDiscount(id) ? 1.0 - (a3[8]/100.0) : 1.0;

  // In case of errors in the source data
  if(d1 < 0) d1=0;
  if(d2 < 0) d2=0;
  if(d3 < 0) d3=0;
  if(d1 > 1) d1=1;
  if(d2 > 1) d2=1;
  if(d3 > 1) d3=1;

  // Done 
  return nA(d1*a1[0]+d2*a2[0]+d3*a3[0],a1[1]+a2[1]+a3[1],a1[2]+a2[2]+a3[2],a1[3]+a2[3]+a3[3],a1[4]+a2[4]+a3[4],a1[5],a1[6],a1[7]);
}

function getProductDiscount(id)
{
  switch((id >>> 12)& 0x1)
  {
    case 0:return false;
    case 1:return true;
    default:return false;
  }
}

function setProductDiscount(id)
{
  return (id | (0x1 << 12));
}

function getProcessDiscount(id)
{
  switch((id >>> 13)& 0x1)
  {
    case 0:return false;
    case 1:return true;
    default:return false;
  }
}

function setProcessDiscount(id)
{
  return (id | (0x1 << 13));
}

function getPhysicalDiscount(id)
{
  switch((id >>> 30)& 0x1)
  {
    case 0:return false;
    case 1:return true;
    default:return false;
  }
}

function setPhysicalDiscount(id)
{
  return (id | (0x1 << 30));
}


