////////////
//
// TShopBasket class
//
// (c) Jacek Murawski, Kozienice wrzesien 1999
//
// e-mail: jacek.murawski@elko.com.pl
// http:// www.radom.medianet.pl/~jacek/
//
// Uwaga: Poleca się kopiowanie i zmiany kodu bez zgody autora,
//        lecz autor uprzejmie prosi o e-mail kto i gdzie
//        użył tego kodu lub jak go zmienił.
//
//        A może zrobię jeszcze coś ciekawego i wtedy bedę
//        wiedział kogo o tym poinformować lub czegoś się
//	  nauczę
//
//                         :-)
//
//
//  W TBasketShop uzyto funkcji do obslugi cookies pochodzacych z
//    http://www.netscape.com/newsref/std/cookie_spec.html
//    Bill Dortch, hIdaho Design
//
////////////

//////////////////////////////////////////////////////
//
// definicja klasy TShopBasket
//
//////////////////////////////////////////////////////


////////////////// Klasa TItem ///////////////////////

function TItem()
{
  this.Id = 0;
  this.Code = "";
  this.Name = "";
  this.Quant = 0;
  this.Price = 0.0;
  this.Tax = 0.0

  this.SetId = TItem_SetId;
  this.SetCode = TItem_SetCode;
  this.SetName = TItem_SetName;
  this.SetQuant = TItem_SetQuant;
  this.SetPrice = TItem_SetPrice;
  this.SetTax = TItem_SetTax;

  this.GetId = TItem_GetId;
  this.GetCode = TItem_GetCode;
  this.GetName = TItem_GetName;
  this.GetQuant = TItem_GetQuant;
  this.GetPrice = TItem_GetPrice;
  this.GetTax = TItem_GetTax;

  this.GetNetAmount = TItem_GetNetAmount;
  this.GetBrtAmount = TItem_GetBrtAmount;

  this.ToString = TItem_ToString;
  this.FromString = TItem_FromString;
}

function TItem_SetId(nId)
{
  this.Id = nId;
}
function TItem_SetCode(cCode)
{
  this.Code = cCode;
}
function TItem_SetName(cName)
{
  this.Name = cName;
}
function TItem_SetQuant(nQuant)
{
  this.Quant = nQuant;
}
function TItem_SetPrice(nPrice)
{
  this.Price = nPrice;
}
function TItem_SetTax(nTax)
{
  this.Tax = nTax;
}

function TItem_GetId()
{
  return this.Id;
}
function TItem_GetCode()
{
  return this.Code;
}
function TItem_GetName()
{
  return this.Name;
}
function TItem_GetQuant()
{
  return this.Quant;
}
function TItem_GetPrice()
{
  return this.Price;
}
function TItem_GetTax()
{
  return this.Tax;
}
function TItem_GetNetAmount()
{
  return this.Quant * this.Price;
}
function TItem_GetBrtAmount()
{
  return this.Quant * (this.Price * (1 + this.Tax));
}

// zamiana Item na stringa, znakiem dzielacym elementy jest "\t"
function TItem_ToString()
{
  var cStr = "";
  var nNum = new Number(this.Id);

  // format: Id/Name/Quant/Price/Tax
  cStr += this.Id + "\t";
  cStr += this.Code + "\t";
  cStr += this.Name + "\t";
  cStr += this.Quant + "\t";
  cStr += this.Price + "\t";
  cStr += this.Tax + "\t";

  return cStr;
}
// i odwrotnie
function TItem_FromString(cStr)
{
  var cId = "";
  var cCode = "";
  var cName = "";
  var cQuant = "";
  var cPrice = "";
  var cTax = "";
  var cTemp = "";

  var i = 0;
  var field = 1;
  while (i < cStr.length)
  {
    c = cStr.charAt(i);
    if (c != "\t")
      cTemp += c;
    else
    {
      // jezeli separator to odczyt wartosci
      switch (field)
      {
        case 1: cId = cTemp; break;
        case 2: cCode = cTemp; break;
        case 3: cName = cTemp; break;
        case 4: cQuant = cTemp; break;
        case 5: cPrice = cTemp; break;
        case 6: cTax = cTemp; break;
        default: break;
      }
      cTemp = "";
      field++;
    }
    i++;
  }

  // konwersja na TItem
  this.Id = parseInt(cId);
  this.Code = cCode;
  this.Name = cName;
  this.Quant = parseFloat(cQuant);
  this.Price = parseFloat(cPrice);
  this.Tax = parseFloat(cTax);
}

////////////////// Klasa TListItem ////////////////////

function TListItem()
{
  this.List = new Array(1);
  this.Counter = 0;

  this.Add = TListItem_Add;
  this.Remove = TListItem_Remove;
  this.Update = TListItem_Update;
  this.NetAmount = TListItem_NetAmount;
  this.BrtAmount = TListItem_BrtAmount;
  this.Clear = TListItem_Clear;
  this.Get = TListItem_Get;

  this.ToString = TListItem_ToString;
  this.FromString = TListItem_FromString;
  this.Count = TListItem_Count;

  this.Write = TListItem_Write;
  this.Read = TListItem_Read;
}

// dodanie pozycji do listy
function TListItem_Add(nId, cCode, cName, nQuant, nPrice, nTax)
{
  var item = new TItem;

  item.SetId(nId);
  item.SetCode(cCode);
  item.SetName(cName);
  item.SetQuant(nQuant);
  item.SetPrice(nPrice);
  item.SetTax(nTax);

  this.List[this.Counter] = item;
  this.Counter++;
}

function TListItem_Remove(nId)
{
  var item = new TItem;
  var i;

  i = 0;
  while (i < this.Counter)
  {
    item = this.List[i];
    if (nId == item.GetId())
      break;
    i++;
  }
  if (i == this.Counter)
    return false;

  // przpisanie danych
  i++;
  while (i < this.Counter)
  {
    item = this.List[i];
    this.List[i-1] = item;
    i++;
  }
  this.Counter--;

  return true;
}

// aktualizacja elementu
function TListItem_Update(nId, cCode, cName, nQuant, nPrice, nTax)
{
  var item = new TItem;
  var i;

  i = 0;
  while (i < this.Counter)
  {
    item = this.List[i];
    if (nId == item.GetId())
      break;
    i++;
  }
  if (i == this.Counter)
    return false;

  item.SetId(nId);
  item.SetCode(cCode);
  item.SetName(cName);
  item.SetQuant(nQuant);
  item.SetPrice(nPrice);
  item.SetTax(nTax);

  this.List[i] = item;
  return true;
}

// czyszczenie listy
function TListItem_Clear()
{
  this.Counter = 0;
}

function TListItem_Get(nIndex)
{
  return this.List[nIndex];
}

function TListItem_NetAmount()
{
  var item = new TItem;
  var i = 0;
  var nVal = 0.0;

  while (i < this.Counter)
  {
    item = this.List[i];
    nVal += item.GetNetAmount();
    i++;
  }

  return nVal;
}

function TListItem_BrtAmount()
{
  var item = new TItem;
  var i = 0;
  var nVal = 0.0;

  while (i < this.Counter)
  {
    item = this.List[i];
    nVal += item.GetBrtAmount();
    i++;
  }

  return nVal;
}

// zapis listy w wstring'u
function TListItem_ToString()
{
  var item = new TItem;
  var cStr = "";
  var i = 0;

  while (i < this.Counter)
  {
    item = this.List[i];
    cStr += item.ToString();
    i++;
  }

  return cStr;
}
// i odwrotnie
function TListItem_FromString(cStr)
{
  var item = new TItem;
  var cItem = "";
  var cnt_tab = 0;
  var i = 0;
  var c;

  this.Clear();

  while (i < cStr.length)
  {
    c = cStr.charAt(i);

    if (c != "\t")
      cItem += c;
    else
    {
      cnt_tab++;

      if (cnt_tab == 6)  // 6 to ilosc pol pojedynczego elementu koszyka
      {
        cnt_tab = 0;
        cItem += "\t";
        item.FromString(cItem);
        this.Add(item.GetId(), item.GetCode(), item.GetName(), item.GetQuant(), item.GetPrice(), item.GetTax());
        cItem = "";
      }
      else
        cItem += c;
    }

    i++;
  }
}

function TListItem_Count()
{
  return this.Counter;
}

// zapis/odczyt listy jako cookies
function TListItem_Write()
{
  var cList;

  cList = this.ToString();
  SetCookie ("SHOPBASKET", cList, null, "/");
}
function TListItem_Read()
{
  var cList;

  cList = GetCookie("SHOPBASKET");
  //DEBUG
  // alert("cList = " + cList);
  if (cList == null)
  {
    this.Clear();
  }
  else
  {
    if (cList.length > 10)
    {
      this.FromString(cList);
    }
    else
    {
      this.Clear();
    }
  }
}

////////////////// Klasa TShopBasket ////////////////////

function TShopBasket()
{
  this.Items = new TListItem();

  this.Add = TShopBasket_Add;
  this.Remove = TShopBasket_Remove;
  this.Update = TShopBasket_Update;
  this.NetAmount = TShopBasket_NetAmount;
  this.BrtAmount = TShopBasket_BrtAmount;
  this.Clear = TShopBasket_Clear;

  this.GetId = TShopBasket_GetId;
  this.GetCode = TShopBasket_GetCode;
  this.GetName = TShopBasket_GetName;
  this.GetQuant = TShopBasket_GetQuant;
  this.GetNetPrice = TShopBasket_GetNetPrice;
  this.GetBrtPrice = TShopBasket_GetBrtPrice;
  this.GetTax = TShopBasket_GetTax;
  this.GetNetAmount = TShopBasket_GetNetAmount;
  this.GetBrtAmount = TShopBasket_GetBrtAmount;
  this.Count = TShopBasket_Count;

  this.Write = TShopBasket_Write;
  this.Read = TShopBasket_Read;
}

function TShopBasket_Add(nId, cCode, cName, nQuant, nPrice, nTax)
{
  this.Items.Add(nId, cCode, cName, nQuant, nPrice, nTax);
}

function TShopBasket_Remove(nId)
{
  return this.Items.Remove(nId);
}
function TShopBasket_Update(nId, cCode, cName, nQuant, nPrice, nTax)
{
  this.Items.Update(nId, cCode, cName, nQuant, nPrice, nTax);
}
function TShopBasket_Get(nIndex)
{
  this.Items.Get(nIndex);
}
function TShopBasket_NetAmount()
{
  return this.Items.NetAmount();
}
function TShopBasket_BrtAmount()
{
  return this.Items.BrtAmount();
}
function TShopBasket_Clear()
{
  this.Items.Clear();
}
function TShopBasket_Read()
{
  this.Items.Read();
}
function TShopBasket_Write()
{
  this.Items.Write();
}

function TShopBasket_GetId(nIndex)
{
  var item = new TItem();

  item = this.Items.Get(nIndex);
  return item.GetId();
}
function TShopBasket_GetCode(nIndex)
{
  var item = new TItem;

  item = this.Items.Get(nIndex);
  return item.GetCode();
}
function TShopBasket_GetName(nIndex)
{
  var item = new TItem;

  item = this.Items.Get(nIndex);
  return item.GetName();
}

function TShopBasket_GetQuant(nIndex)
{
  var item = new TItem;

  item = this.Items.Get(nIndex);
  return item.GetQuant();
}
function TShopBasket_GetNetPrice(nIndex)
{
  var item = new TItem;

  item = this.Items.Get(nIndex);
  return item.GetPrice();
}
function TShopBasket_GetBrtPrice(nIndex)
{
  var item = new TItem;

  item = this.Items.Get(nIndex);
  return item.GetPrice();
}
function TShopBasket_GetTax(nIndex)
{
  var item = new TItem;

  item = this.Items.Get(nIndex);
  return item.GetTax();
}
function TShopBasket_GetNetAmount(nIndex)
{
  var item = new TItem;

  item = this.Items.Get(nIndex);
  return item.GetNetAmount();
}
function TShopBasket_GetBrtAmount(nIndex)
{
  var item = new TItem;

  item = this.Items.Get(nIndex);
  return item.GetBrtAmount();
}
function TShopBasket_Count()
{
  return this.Items.Count();
}

