
/******************************************************************************************/
// Objects

function StockObject()
{  
  // Variables
  this.xhttp = null;
  this.imgObj = null;
  this.cellObj = null;

  // Methods
  this.SendReq = function(imgObj, rec) 
  {  
    this.xhttp = xHttpInit(this.xhttp);
    if (this.xhttp) 
    {
      this.imgObj = imgObj;
      this.imgObj.src = "images/icons/loading_xsmall.gif";
      this.cellObj = imgObj.parentNode;
  
      var params = ajax_params + "&page=store&opt=1&rec=" + rec;
      this.xhttp.open("POST", ajax_url, true);
      this.xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      this.xhttp.setRequestHeader("Content-length", params.length);
      this.xhttp.setRequestHeader("Connection", "close");
  
      var self = this;               // fix loss-of-scope in inner function  
      this.xhttp.onreadystatechange = function() 
      {
        if (self.xhttp.readyState == 4) 
        {
          self.imgObj.src = "images/icons/nav_store.png";
          if (self.xhttp.status < 200 && self.xhttp.status > 299)  
          {
            Exception('Błąd', 'Wystąpił błąd podczas realizacji połączenia z serwerem.');
            return;
          }
          self.cellObj.innerHTML = self.xhttp.responseXML.getElementsByTagName("cdata")[0].childNodes[0].nodeValue;
        }  
      };  
      
      this.xhttp.send(params);  
    }
  };
  
}

var Stock = new StockObject();  

/******************************************************************************************/
// Objects

function BasketObject () 
{  
  // Variables
  this.xhttp = null;
  this.xhttp2 = null;
  this.imgObj = null;
  this.inputObj = null;
  this.basketListId = "BasketContent";


  // Methods
  this.SendReq = function(imgObj, inputId, rec, qty_min, qty_max) 
  {  
    this.xhttp = xHttpInit(this.xhttp);
    if (this.xhttp) 
    {
      this.imgObj = imgObj;
      this.inputObj = document.getElementById(inputId);
  
      var amount = parseInt(this.inputObj.value, 10);
      if (isNaN(amount) || amount < qty_min)
      {
        this.inputObj.value = '';
        Exception('Informacja', 'Zamawiana ilość musi być większa/równa ' + qty_min + '.');
        return;
      }
      /*
      var store = parseInt(qty_max, 10);
      if (amount > store)
      {
        this.inputObj.value = '';
        Exception('Informacja', 'Zamawiana ilość nie może przekraczać stanów magazynowych (' + qty_max + ').');
        return;
      }
      */
      this.imgObj.src = "images/icons/loading_small.gif";

      var params = ajax_params + "&page=store&opt=3&rec=" + rec + "&amount=" + amount;
      this.xhttp.open("POST", ajax_url, true);
      this.xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      this.xhttp.setRequestHeader("Content-length", params.length);
      this.xhttp.setRequestHeader("Connection", "close");
  
      var self = this;               // fix loss-of-scope in inner function  
      this.xhttp.onreadystatechange = function() 
      {
        if (self.xhttp.readyState == 4) 
        {
          self.imgObj.src = "images/icons/icon_basket.png";
          if (self.xhttp.status < 200 && self.xhttp.status > 299) 
          {
            Exception('Błąd', 'Wystąpił błąd podczas realizacji połączenia z serwerem.');
            return;
          }
          self.inputObj.value = '';
          self.UpdateBasket();
        }  
      };  
      
      this.xhttp.send(params);
    }  
  };


  this.UpdateBasket = function()
  {
    this.xhttp2 = xHttpInit(this.xhttp2);
    if (this.xhttp2) 
    {
      document.getElementById(this.basketListId).innerHTML = "<img src=\"images/icons/loading_small.gif\" alt=\"ładowanie danych\" />";

      var params = ajax_params + "&page=store&opt=2";
      this.xhttp2.open("POST", ajax_url, true);
      this.xhttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      this.xhttp2.setRequestHeader("Content-length", params.length);
      this.xhttp2.setRequestHeader("Connection", "close");
  
      var self = this;               // fix loss-of-scope in inner function  
      this.xhttp2.onreadystatechange = function() 
      {
        if (self.xhttp2.readyState == 4) 
        {
          if (self.xhttp2.status < 200 && self.xhttp.status > 299)  
          {
            self.imgObj.src = "images/icons/nav_store.png";
            Exception('Błąd', 'Wystąpił błąd podczas realizacji połączenia z serwerem.');
            return;
          }
          document.getElementById(self.basketListId).innerHTML = self.xhttp2.responseXML.getElementsByTagName("cdata")[0].childNodes[0].nodeValue;
        }  
      };  
      
      this.xhttp2.send(params);  
    }
  };

  this.RemoveItem = function(imgObj, rec) 
  {  
    this.xhttp = xHttpInit(this.xhttp);
    if (this.xhttp) 
    {
      this.imgObj = imgObj;
      this.imgObj.src = "images/icons/loading_xxsmall.gif";

      var params = ajax_params + "&page=store&opt=4&rec=" + rec;
      this.xhttp.open("POST", ajax_url, true);
      this.xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      this.xhttp.setRequestHeader("Content-length", params.length);
      this.xhttp.setRequestHeader("Connection", "close");
  
      var self = this;               // fix loss-of-scope in inner function  
      this.xhttp.onreadystatechange = function() 
      {
        if (self.xhttp.readyState == 4) 
        {
          if (self.xhttp.status < 200 && self.xhttp.status > 299) 
          {
            Exception('Błąd', 'Wystąpił błąd podczas realizacji połączenia z serwerem.');
            return;
          }
          self.imgObj.src = "images/icons/item_remove.png";
          location.reload()
        }  
      };  
      
      this.xhttp.send(params);
    }  
  };


  this.UpdateItem = function(imgObj, rec, amount, qty_min, qty_max) 
  {  
    this.xhttp = xHttpInit(this.xhttp);
    if (this.xhttp) 
    {
      this.imgObj = imgObj;

      var text = prompt("Proszę wpisać nową ilość zamawianego produktu", amount);
      var new_amount = parseInt(text, 10);
      if (isNaN(new_amount) || new_amount < qty_min)
      {
        Exception('Informacja', 'Zamawiana ilość musi być większa/równa ' + qty_min + '.');
        return;
      }
      if (new_amount == amount)
      {
        Exception('Informacja', 'Wpisano tę samą ilość.');
        return;
      }
      /*
      var amountStore = parseInt(qty_max, 10);
      if (new_amount > amountStore)
      {
        Exception('Informacja', 'Zamawiana ilość nie może przekraczać stanów magazynowych (' + qty_max + ').');
        return;
      }
      */
      this.imgObj.src = "images/icons/loading_xxsmall.gif";

      var params = ajax_params + "&page=store&opt=5&rec=" + rec + "&amount=" + new_amount;
      this.xhttp.open("POST", ajax_url, true);
      this.xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      this.xhttp.setRequestHeader("Content-length", params.length);
      this.xhttp.setRequestHeader("Connection", "close");
  
      var self = this;               // fix loss-of-scope in inner function  
      this.xhttp.onreadystatechange = function() 
      {
        if (self.xhttp.readyState == 4) 
        {
          if (self.xhttp.status < 200 && self.xhttp.status > 299) 
          {
            Exception('Błąd', 'Wystąpił błąd podczas realizacji połączenia z serwerem.');
            return;
          }
          self.imgObj.src = "images/icons/item_params.png";
          location.reload()
        }  
      };  
      
      this.xhttp.send(params);
    }  
  };


}

var Basket = new BasketObject();  

/******************************************************************************************/

















