function Produkty(){

 this.lista = "";
 this.modValue = "";
 this.cart="";
 this.packmess = "Optymalna kombinacja dostępnych pakietów:<br>";
 
 if(document.getElementById('renewal').checked)
  this.renewal = 30;
 else
   this.renewal = 0;
 this.maintenance = "1";
 this.discount = document.getElementById('discount').value;
 
    req = new XMLHttpRequest();
    rand = Math.random();
    req.open("GET","ajax/productlist.php?r="+rand,false);
    req.send(null);

    if(req.status ==200){
      jresp = req.responseText;
      this.lista = eval("("+jresp+")");
    }
  
  this.getPrdById = function(id){
    for(i=0;i<this.lista.length;i++){
      if(this.lista[i].internalId == id) return this.lista[i];
    }
  }


  this.updateLink = function(){
   links = document.getElementsByName("olink");
   if(links){
   form = document.getElementById("cart");
   l = form.childNodes.length;
   textlink = "show.php?";
   i=0;
   for(i=0; i<l; i++){
    ee = form.childNodes[i];
    if(ee.name != 'undefined')
      textlink += ee.name + "=" + ee.value; 
    if ( i < l-1)
      textlink += "&"
   }
   
   
   l = links.length;   
   for(i=0; i<l; i++)
   links[i].href = textlink;

   }
}  


  this.updateControls = function(){
    this.updateParams();
    items = 1;
    c = Array();
    document.forms["cart"].innerHTML = " ";
    addItem("zam", "1");
    addItem("discount_cupon", this.discount);
    addItem("Maintenance", this.maintenance);    

    
    for(i=0;i<this.lista.length;i++){
      netto=brutto=0;
      id = this.lista[i].internalId;
      el_form = document.getElementById('product'+id);
      el_brutto = document.getElementById('productBrutto'+id);
      el_netto = document.getElementById('productNetto'+id);
      el_info = document.getElementById('info'+id);
      el_infoZestaw = document.getElementById('infoZestaw'+id);
      
      if(el_form != null){
	liczbaLicencji = el_form.value;

	if(liczbaLicencji.match(/[\d]+/g) != null && liczbaLicencji <= parseInt(this.lista[i].maks)){
          liczbaLicencji = parseInt(el_form.value);
	  ceny_eur = explodearray(this.getPrdById(id).prices_eur,'#');
	  ceny_pl = explodearray(this.getPrdById(id).prices_pl,'#');
	  przedzialy =  explodearray(this.getPrdById(id).from,',');

	  for(j=przedzialy.length-1;j>=0;j--){
	    if(przedzialy[j] <= liczbaLicencji){
	      progCenowy = j;
	      break;
	    }
	    else
	      progCenowy = "-1";
	  }  

	  if(progCenowy >= 0 || this.lista[i].type==2 && liczbaLicencji > 0 ){

	    if(this.lista[i].type==0){
	      pakiety = "";

	      cj = ceny_pl[parseInt(this.maintenance)+3*parseInt(progCenowy)-1];
	      calcj = new calcPrice(cj,this.discount,this.renewal);
	      calcbz = new calcPrice(cj,0,0);
	      nt = liczbaLicencji*calcj.netto;
	      brutto = liczbaLicencji*calcj.brutto;

	      pakiety = liczbaLicencji+" x "+calcbz.netto.toFixed(2);
	      
	      	
	      sztuk = liczbaLicencji;		      
	      el_brutto.innerHTML = brutto.toFixed(2);
	      el_netto.innerHTML = nt.toFixed(2);
	      addItem("sztuk" + items, sztuk);
	      addItem("indeks" + items,id);
	      addItem("nazwa" + items,document.getElementById('ptitle'+id).innerHTML);
	      addItem("netto" + items,calcbz.netto);
	      addItem("renewal" + items,this.renewal); 
	      if(sztuk)
		items++;
	    }

	    if(this.lista[i].type==1||this.lista[i].type==2){
	      llt = optimize(liczbaLicencji,this.lista[i].type);
	
	      calculator = Array();
	      cbz = Array();
	      for(j=1;j<llt.length;j++){

	      c[j] = ceny_pl[parseInt(this.maintenance)+3*parseInt(j-1)-1];
	      
	      calculator[j] = new calcPrice(c[j],this.discount,this.renewal);
	      cbz[j] = new calcPrice(c[j],0,0);
	      
	      brutto += llt[j]*calculator[j].brutto;
	      netto += llt[j]*parseFloat(calculator[j].netto);

	      }
	      
	    }
	    
	    if(this.lista[i].type==1){
	      pakiety = zestaw = "";

	      if(llt[4]) pakiety += llt[4]+" x "+cbz[4].netto.toFixed(2);
	      if(llt[4]&&llt[3] || llt[4]&&llt[2] || llt[4]&&llt[1]) pakiety += " + ";
	      if(llt[3]) pakiety += llt[3]+" x "+cbz[3].netto;
	      if(llt[3]&&llt[2] || llt[3]&&llt[1]) pakiety += " + ";
	      if(llt[2]) pakiety += llt[2]+" x "+cbz[2].netto.toFixed(2);   
	      if(llt[2]&&llt[1]) pakiety += " + ";
	      if(llt[1]) pakiety += llt[1]+" x "+cbz[1].netto.toFixed(2);
	      
	      if(llt[4]) zestaw += llt[4]+" x 10";
	      if(llt[4]&&llt[3] || llt[4]&&llt[2] || llt[4]&&llt[1]) zestaw += " + ";
	      if(llt[3]) zestaw += llt[3]+" x 5";
	      if(llt[3]&&llt[2] || llt[3]&&llt[1]) zestaw += " + ";
	      if(llt[2]) zestaw += llt[2]+" x 3";	   
	      if(llt[2]&&llt[1]) zestaw += " + ";
	      if(llt[1]) zestaw += llt[1]+" x 1";	      
	      
	      el_infoZestaw.innerHTML = this.packmess+zestaw;
	      nt = netto;
	     // el_info.innerHTML = pakiety+" = "+ netto.toFixed(2)+"";
	      for(k=1;k<=4;k++){
		if(llt[k]>0){
		  if(k==1) mnz = 1;
		  if(k==2) mnz = 3;
		  if(k==3) mnz = 5;
		  if(k==4) mnz = 10;
		  addItem("sztuk" + items, llt[k]);
		  addItem("indeks" + items,id);
		  addItem("nazwa" + items,document.getElementById('ptitle'+id).innerHTML +" x "+mnz);
		  addItem("netto" +items, cbz[k].netto);
		  addItem("renewal" + items,this.renewal);
		  items++;
		}
	      }
	      el_brutto.innerHTML = brutto.toFixed(2);
	      el_netto.innerHTML = nt.toFixed(2);	      
	    }


	    if(this.lista[i].type==2){
	    
	
	      zestaw = pakiety = "";
	      
	      if(llt[3]) pakiety += llt[3]+" x "+cbz[3].netto.toFixed(2);
	      if(llt[3]&&llt[2] || llt[3]&&llt[1]) pakiety += " + ";
	      if(llt[2]) pakiety += llt[2]+" x "+cbz[2].netto.toFixed(2);   
	      if(llt[2]&&llt[1]) pakiety += " + ";
	      if(llt[1]) pakiety += llt[1]+" x "+cbz[1].netto.toFixed(2);

	      if(llt[3]) zestaw += llt[3]+" x 25";
	      if(llt[3]&&llt[2] || llt[3]&&llt[1]) zestaw += " + ";
	      if(llt[2]) zestaw += llt[2]+" x 10";
	      if(llt[2]&&llt[1] ) zestaw += " + ";
	      if(llt[1]) zestaw += llt[1]+" x 5";	   
	            
	      el_infoZestaw.innerHTML = this.packmess+zestaw;
	      nt = netto; 
	      //el_info.innerHTML = pakiety+" = "+ netto+"";
	      for(k=1;k<=3;k++){
		if(llt[k]>0){
	
		  if(k==1) mnz = 5;
		  if(k==2) mnz = 10;
		  if(k==3) mnz = 25;		
		  addItem("sztuk" + items, llt[k]);
		  addItem("indeks" + items,id);
		  addItem("nazwa" + items,document.getElementById('ptitle'+id).innerHTML+" x "+mnz);
		  addItem("netto" +items, cbz[k].netto);
		  addItem("renewal" + items,this.renewal);
		  items++;
		}
	      }
	      el_brutto.innerHTML = brutto.toFixed(2);
	      el_netto.innerHTML = nt.toFixed(2);
	    }

	    //alert(llt[0]+" "+llt[1]+llt[2]+llt[3]+llt[4]);

	      if(this.discount == 0 && this.renewal ==0)
		el_info.innerHTML = pakiety+" = "+ nt.toFixed(2) +"";
	      if(this.discount != 0 && this.renewal ==0)
		el_info.innerHTML = "("+pakiety+" ) -"+this.discount+"% (plan cenowy "+this.discount+" ) = "+ nt.toFixed(2) +"";		
	      if(this.discount == 0 && this.renewal ==30)
		el_info.innerHTML = pakiety+" -30% za przedłużenie = "+ nt.toFixed(2) +"";		
	      if(this.discount != 0 && this.renewal ==30)
		el_info.innerHTML = "("+pakiety+" -30% za przedłużenie ) -"+this.discount+"% (plan cenowy"+this.discount+") = "+ nt.toFixed(2) +"";	
	    addItem("ilosc", items-1);

	  }
	  else{
	    el_brutto.innerHTML = "0.00";
	    el_netto.innerHTML = "0.00";
	    el_info.innerHTML = "0.00";
	    el_infoZestaw.innerHTML = " ";	    
	  }
	}
	else{
	  el_brutto.innerHTML = "0.00";
	  el_netto.innerHTML = "0.00";
	  el_info.innerHTML = "0.00 ";
	  el_infoZestaw.innerHTML = " ";
	}
      }
    }
   
   
   this.updateLink();
//    c = ceny[parseInt(this.maintenance)+(3*(parseInt(progCenowy)))-1];
//    alert(c);
  }

  this.updateParams = function(){
    this.discount = document.getElementById('discount').value;
    
    if(document.getElementById('renewal').checked)
      this.renewal = 30;
    else
      this.renewal = 0;
    return 0;
    this.updateControls();
  }

  this.updateMaintenance = function(val){
    this.maintenance = val;
    this.updateControls();    
  }
  
}


function explodearray(item,delimiter) {
  temparray=new Array(1);
  var Count=0;
  var tempstring=new String(item);

  while (tempstring.indexOf(delimiter)>0) {
  temparray[Count]=tempstring.substr(0,tempstring.indexOf(delimiter));
  tempstring=tempstring.substr(tempstring.indexOf(delimiter)+1,tempstring.length-tempstring.indexOf(delimiter)+1);
  Count=Count+1
  }

  temparray[Count]=tempstring;
  return temparray;
} 



function calcPrice(cena,znizka,odnowienie){

  this.brutto = "";
  this.netto = "";

  if(odnowienie)
    cena = cena*0.7;

  cena = cena*(100-znizka)/100;

  this.brutto = cena*1.23; 
  this.netto = cena;
}


function optimize(liczba,type) {

  l1 = l3 = l5 = l10 = 0;
  l25 = 0;
  
  l = Array();
  if(type == 0)
    l[0]= l[1] = liczba;
  if(type == 1){
    if(Math.floor(liczba/10)){
      r = liczba%10;
      l10 =  Math.floor(liczba/10);
      }
    else
      r = liczba;

    if(r==1)
      l1 = 1;
    else if(r==2||r==3)
      l3 = 1;
    else if(r==4||r==5)
      l5 = 1;
    else if(r==6){ 
      l5 = 1;
      l1 = 1;      
    }
    else if(r==7||r==8){ 
      l5 = 1;
      l3 = 1;     
    }
    else if(r==9)
      l10++;
    l[0] = l1+3*l3+5*l5+10*l10;
    l[1] = l1;
    l[2] = l3;
    l[3] = l5; 
    l[4] = l10;    

  }


  if(type == 2){

    if(l25 = Math.floor(liczba/25))
      r = liczba%25;
    else
      r = liczba;

    if(l10 = Math.floor(r/10))
      r = r%10;

    if(l5 = Math.floor(r/5))
      r = r%5;

    if(r > 0 )
      l5++;

   
   if(l10 >1 && r >0){
    l10-=2;
    l25++;
    l5--;
    }    
    
   if(l5 >1){
    l5-=2;
    l10++;
    }

    if(liczba <= 5){
      l5 = 1;
    }
    

    
    l[0] = 5*l5+10*l10+25*l25;
    l[1] = l5;
    l[2] = l10; 
    l[3] = l25;    
  }
  
  return l;
}

function addItem(name, value)
{
    currentElement = document.createElement("input");
    currentElement.setAttribute("type", "hidden");
    currentElement.setAttribute("name", name);
    currentElement.setAttribute("value", value);
    document.forms["cart"].appendChild(currentElement);

}



