var anuluj;

Array.prototype.in_array = function(data, strict) {
    if (strict) function equals(a,b) { return a === b }
    else function equals(a,b) { return a == b }

    for (var i in this) {
        if (equals(this[i], data)) return true;
    }
    return false;
}

function Koszyk(container, orderButton, orderButton2)
{
	anuluj=false;
	this.koszyk=new Array();
	this.container=container;
	this.orderButton=(orderButton)?orderButton:null;
	this.orderButton2=(orderButton2)?orderButton2:null;
	this.stan='prosty';
	this.cfg = new Config;
	this.checkCookie('koszyk_tai');
	this.platnosc=null;
	this.getItems();
}

Koszyk.prototype.load = function ()
{
	if(this.orderButton) this.orderButton.style.display="none";
	this.showItems();	
}

Koszyk.prototype.checkCookie = function (name)
{
	if(Get_Cookie('koszyk_tai') == null)
	{
		Set_Cookie('koszyk_tai','');
	}
}

Koszyk.prototype.getItems = function()
{
	var kosz;
	kosz = Get_Cookie('koszyk_tai');
	if(kosz == null)
		this.koszyk=new Array();
	else
	{
		this.koszyk = kosz.split("^");
	}
}

Koszyk.prototype.showItems = function()
{
	var k, item, rmv,a;
	var _this=this;

	if(this.koszyk != null)
	{
		this.koszyk=this.koszyk.unique();
		this.koszyk=this.koszyk.sort();
		this.koszyk.removeEmpty();
		this.container.innerHTML='';
		if(this.koszyk.length > 0)
		{
			if(this.orderButton) this.orderButton.style.display="";
			if(this.orderButton2) this.orderButton2.style.display="none";

			for (var key in this.koszyk)
			{	
				if(!isNaN(parseInt(key)))
				{
					var prosty=this.koszyk[key]+'_prosty';
					var prosty=eval("this.cfg."+prosty+";");
					
					if(!prosty) 
					{
						if(this.orderButton) this.orderButton.style.display="none";
						if(this.orderButton2) this.orderButton2.style.display="";						
					}

					if(this.koszyk[key]) {
						var tytul = this.koszyk[key];
						var table = addTag(createTag('TABLE'), this.container);
							table.className="koszykTab";
						var tbody = addTag(createTag('TBODY'), table);
						var tr = addTag(createTag('TR'), tbody);
							tr.className="koszTr";
						var tdL = addTag(createTag('TD'), tr);
							tdL.className="kosztdL";
						var tdR = addTag(createTag('TD'), tr);
							tdR.className="kosztdR";
						tdL.innerHTML=this.koszyk[key];
						if(tytul) tdL.title=eval('this.cfg.'+tytul);
						a = addTag(createTag('A'), tdR);
						a.innerHTML='usuń';
						a._item=this.koszyk[key];
						a.href="#"
						a.onclick=function() {_this.removeItem(this);}
					}
				}
			}
		}
		else
		{
			k = addTag(createTag('DIV'), this.container);
			item = addTag(createTag('SPAN'), k);
			if(this.orderButton) this.orderButton.style.display="none";
			if(this.orderButton2) this.orderButton2.style.display="none";
			item.innerHTML="Twój koszyk jest pusty";
		}
	}
}

Koszyk.prototype.showOrderButton = function ()
{
	this.basketEmpty=false;
}

Koszyk.prototype.putItem = function (name)
{
//	alert('putItem::start'+anuluj);

	if(anuluj)
	{
		anuluj=0;
		return;
	}
	
	if(name) this.koszyk[this.koszyk.length]=name;
	this.koszyk=this.koszyk.unique();
	Delete_Cookie('koszyk_tai');
	Set_Cookie('koszyk_tai',this.koszyk.join("^"));
	this.getItems()
	if(this.orderButton) this.orderButton.style.display="";
	if (name) {		
		name = name.replace('^', ', ');
		alert('Do koszyka dodano produkt(y) o kodzie: '+name + '. Mogą Państwo kontynuować zamawianie wybierając kolejne produkty lub przejść do jego realizacji klikając "Dalej" w koszyku');
	}
	this.showItems();
}

Koszyk.prototype.excludeItem = function (name)
{
	var conf;
	var items= new Array;
	var sItems=new String;
	var itemsCount;
	var iKoszykLength;
	var bNameInBasket=false;
	
	items=name.split('^');
	itemsCount=items.length;
	sItems=name.replace('^',", ");
	iKoszykLength=this.koszyk.length;
	
	for (var i=0; i<=iKoszykLength; i++)
	{
		if(this.koszyk.in_array(items[i]))
		{
			bNameInBasket=true;
		}
	}

	if(iKoszykLength && bNameInBasket)
	{
		conf=confirm("Dodanie tego elementu spowoduje usunięcie produktu(ów): " + sItems + " \nCzy jestes pewien, że chcesz go dodać?");
	}
	else
	{
		conf=1;
	}
	
	if (conf)
	{
		for(var i=0; i<itemsCount; i++)
		{
			this.koszyk.remove(items[i]);
		}
		this.putItem();
	}
	else
	{
		anuluj=1;
	}
//	alert('excludeItem::end'+anuluj);
}


Koszyk.prototype.removeItem = function (obj)
{
	if(obj._item)
	{
		this.koszyk.remove(obj._item);
		this.putItem();
	}
}

Koszyk.prototype.removeItem2 = function (obj)
{
	if(obj._item)
	{
		this.koszyk.remove(obj._item);
		Delete_Cookie('koszyk_tai');
		Set_Cookie('koszyk_tai',this.koszyk.join("^"));
		this.statusZamowienia();
	}
}

Koszyk.prototype.onlyRemoveItem = function (obj)
{
	if(obj._item)
	{
		this.koszyk.remove(obj._item);
		Delete_Cookie('koszyk_tai');
		Set_Cookie('koszyk_tai',this.koszyk.join("^"));
	}
}

Koszyk.prototype.removeAllItems = function ()
{
	this.koszyk=new Array();
	Delete_Cookie('koszyk_tai');
}



Koszyk.prototype.statusZamowienia2 = function ()
{
	_this=this;
	this.container.innerHTML='';
	var suma=new Number();
	var obj=this.platnosc;

	if(this.koszyk.length > 0)
	{
		//var p=addTag(createTag('P'), this.container);
		//	p.innerHTML="<B>Lista zamówionych usług:</B>";
		var table = addTag(createTag('TABLE'), this.container);
			table.className="statusZamowienia";
		var tbody = addTag(createTag('TBODY'), table);
	}

	for (var key in this.koszyk)
	{
		if(!isNaN(parseInt(key)))
		{
			if(this.koszyk[key]) {
				var tytul=this.koszyk[key];
				var cena=this.koszyk[key]+'_cena';
				var tr = addTag(createTag('TR'), tbody);
				
				var tdL = addTag(createTag('TD'), tr);
					tdL.className="tdL";
				var tdM = addTag(createTag('TD'), tr);
					tdM.className="tdM";
				var tdR = addTag(createTag('TD'), tr);
					tdR.className="tdR";

				var tdA = addTag(createTag('TD'), tr);
					tdA.className="tdR";

				tdL.innerHTML=this.koszyk[key];
				if(tytul) tdM.innerHTML=eval('this.cfg.'+tytul);
				if(cena) 
				{
					var cena=(eval('this.cfg.'+cena))? eval('this.cfg.'+cena) : 0 ;
//					if(obj) alert(obj.options.length);
					var wsp = (obj!=null) ? obj.options[obj.selectedIndex].value : 1; 
					tdR.innerHTML=cena*wsp;
					//by RP
					a = addTag(createTag('<INPUT type=button>'), tdA);
					a.value="Rezygnuję";
					a.className="button"
					a._item=this.koszyk[key];
					a.onclick=function() {_this.removeItem2(this); location.href=location.href;}

					suma+=parseFloat(tdR.innerHTML);
				}
				else
				{
					tdR.innerHTML="&nbsp;";

				}
			}
		}
	}

	if(this.koszyk.length > 0)
	{
		var tr = addTag(createTag('TR'), tbody);
				
		var tdL = addTag(createTag('TD'), tr);
			tdL.className="tdLs";
		var tdM = addTag(createTag('TD'), tr);
			tdM.className="tdMs";
		var tdR = addTag(createTag('TD'), tr);
			tdR.className="tdRs";

		tdL.innerHTML="&nbsp;";
		tdM.innerHTML="suma:";
		tdR.innerHTML=suma;
		
	}

	// Przeliczenie za okres roziczeniowy
	if(this.koszyk.length > 0)
	{
		this.addPriceRadio();
	}
}

Koszyk.prototype.statusZamowienia = function ()
{
	_this=this;
	this.container.innerHTML='';
	var suma=new Number();
	var obj=this.platnosc;

	if(this.koszyk.length > 0)
	{
		//var p=addTag(createTag('P'), this.container);
		//	p.innerHTML="<B>Lista zamówionych usług:</B>";
		var table = addTag(createTag('TABLE'), this.container);
			table.className="statusZamowienia";
		var tbody = addTag(createTag('TBODY'), table);
	}

	for (var key in this.koszyk)
	{
		if(!isNaN(parseInt(key)))
		{
			if(this.koszyk[key]) {
				var tytul=this.koszyk[key];
				var cena=this.koszyk[key]+'_cena';
				var tr = addTag(createTag('TR'), tbody);
				
				var tdL = addTag(createTag('TD'), tr);
					tdL.className="tdL";
				var tdM = addTag(createTag('TD'), tr);
					tdM.className="tdM";
//				var tdR = addTag(createTag('TD'), tr);
//					tdR.className="tdR";

				var tdA = addTag(createTag('TD'), tr);
					tdA.className="tdR";

				tdL.innerHTML=this.koszyk[key];
				if(tytul) tdM.innerHTML=eval('this.cfg.'+tytul);
				if(cena) 
				{
//					var cena=(eval('this.cfg.'+cena))? eval('this.cfg.'+cena) : 0 ;
//					if(obj) alert(obj.options.length);
//					var wsp = (obj!=null) ? obj.options[obj.selectedIndex].value : 1; 
//					tdR.innerHTML=cena*wsp;
					//by RP
					a = addTag(createTag('<INPUT type=button>'), tdA);
					a.value="Rezygnuję...";
					a.className="button"
					a._item=this.koszyk[key];
					a.onclick=function() {_this.removeItem2(this); location.href=location.href;}

//					suma+=parseFloat(tdR.innerHTML);
				}
				else
				{
//					tdR.innerHTML="&nbsp;";

				}
			}
		}
	}

}

Koszyk.prototype.addPriceRadio = function ()
{
		var _this=this;
		
		var select=getObject("przelicz_select");
			if(select) return;

		var table = createTag('TABLE');
			table.id="przelicz_select";
			this.container.insertAdjacentElement('afterEnd', table);
		var tbody = addTag(createTag('TBODY'), table);

		var tr = addTag(createTag('TR'), tbody);
				
		var tdL = addTag(createTag('TD'), tr);
		var tdM1 = addTag(createTag('TD'), tr);
		var tdM2 = addTag(createTag('TD'), tr);
		var tdR = addTag(createTag('TD'), tr);

		var tekst=addTag(createTag('SPAN'), tdL);
			tekst.innerHTML="Wybierz rodzaj płatności: ";

		var sel=addTag(createTag('SELECT'), tdM1);
			sel.name="przelicz";
		var o1=createTag('<OPTION selected>');
			o1.value=1;
			o1.text="miesięcznie";
		var o2=createTag('OPTION');
			o2.value=3;
			o2.text="kwartalnie";
		var o3=createTag('OPTION');
			o3.value=6;
			o3.text="półrocznie";
		var o4=createTag('OPTION');
			o4.value=12;
			o4.text="rocznie";
		
			sel.add(o1);
			sel.add(o2);
			sel.add(o3);
			sel.add(o4);


		sel.onchange=function () { _this.statusZamowienia2(); }
		this.platnosc=sel;
}

Koszyk.prototype.countItems = function ()
{
	return this.koszyk.length;
}

