var baskets = new Array();
var strings = new Array();

strings['shop-email-address'] = 'info@wegett.cz';

strings['shop-currency'] = 'Kč';
strings['shop-pcs'] = 'ks';
strings['shop-redirect'] = '/dekujeme.html';
strings['shop-email-subject'] = 'Objednávka č. # z elektronického obchodu Wegett.cz';
strings['shop-email-body'] = "Rekapitulace objednávky\n-----------------------\n\n";
strings['shop-dobirkaCZ'] = 'dobírka PPL Česká republika';
strings['shop-osodber'] = 'hotově na prodejně - osobní odběr';
strings['shop-prevod'] = 'bankovním převodem';
strings['shop-doporucene'] = 'doporučená zásilka - jen pro testery ČR';
strings['shop-dobirkaSK'] = 'dobírka PPL Slovenská republika';
strings['shop-delivery-info'] = '';
strings['shop-info'] = 'Vážený zákazníku, po obdržení Vaší objednávky Vám bude automaticky odeslán e-mail s rekapitulací objednaného zboží. V případě nejasností nás kontaktujte.<br /><sup>*</sup> pole je nutno vyplnit';

strings['Added to basket'] = 'Do košíku bylo přidáno';
strings['add to basket'] = 'vložit do košíku';
strings['Removed from basket.'] = 'Zboží bylo odebráno z košíku.';
strings['Product'] = 'Zboží';
strings['Pcs.'] = 'Počet';
strings['Total pcs.'] = 'Celkem kusů';
strings['Total excl. VAT'] = 'Celkem bez DPH';
strings['Total VAT'] = 'Celkem DPH';
strings['Total incl. VAT'] = 'Celkem vč. DPH';
strings['Delivery'] = 'Dopravné';
strings['Count'] = 'Počet';
strings['Code'] = 'Kód';
strings['Name'] = 'Název produktu';
strings['Excl. VAT'] = 'Bez DPH';
strings['VAT'] = 'Sazba DPH';
strings['Incl. VAT'] = 'Vč. DPH';
strings['Sum incl. VAT'] = 'Celkem vč. DPH';
strings['Remove from basket'] = 'Odebere zboží z košíku';
strings['remove'] = 'odebrat';
strings['Add 1 piece'] = 'Přidat 1 kus';
strings['Remove 1 piece'] = 'Odebrat 1 kus';
strings['empty basket'] = 'vyprázdnit košík';
strings['Total of products with VAT rate'] = 'Celkem zboží se sazbou DPH';
strings['Total goods'] = 'Celkem za zboží';
strings['Total'] = 'Celkem k úhradě';
strings['Fill in informations below, please.'] = 'Prosím vyplňte kontaktní údaje.';
strings['Order no.'] = 'Číslo objednávky';
strings['Name and surname'] = 'Jméno a příjmení';
strings['Telephone'] = 'Telefon';
strings['E-mail address'] = 'E-mail';
strings['Shipping address'] = 'Dodací adresa';
strings['Payment/shipping method'] = 'Forma platby/dodání';
strings['Note'] = 'Poznámka';
strings['Send order'] = 'Odeslat objednávku';
strings['Price incl. VAT'] = 'Cena vč. DPH';
strings['Price excl. VAT'] = 'Cena bez DPH';
strings['Catalogue #'] = 'Katalogové číslo';
strings['Stock'] = 'Dostupnost zboží';
strings['in stock'] = 'skladem';
strings['Group'] = 'Kategorie';
strings['price'] = 'cena';
strings['Fill in your name, please.'] = 'Prosím vyplňte jméno.';
strings['Fill in your telephone number, please.'] = 'Prosím vyplňte telefonní číslo.';
strings['Fill in your email address, please.'] = 'Prosím vyplňte e-mailovou adresu.';
strings['Fill in your shipping address, please.'] = 'Prosím vyplňte dodací adresu.';
strings['Your basket is currently empty.'] = 'Košík je prázdný.';
strings['Show page:'] = 'Zobrazit stránku:';

function Basket(p_name)
{
	//----------------------------------------------
	// Nastaveni parametru kosiku
	//----------------------------------------------

	this.name = p_name;
	this.confirmOps = true;
	this.refreshOnAdd = true;
	this.currency = strings['shop-currency'];
	this.pieces = strings['shop-pcs'];
	this.note = '';
	this.clientId = '';

	this.summary = new Array(0, 0, 0, 0, 125);
	this.summaryTable = '';
	this.freeDeliveryLimit = 0;

	this.detailGroup = '';

	baskets[this.name] = this;
	
	//----------------------------------------------
	// Konstanty
	//----------------------------------------------

	var ZID = 0;
	var ZNAME = 1;
	var ZNETTO = 2;
	var ZBRUTTO = 3;
	var ZVAT = 4;
	var ZCOUNT = 5;
	var CATALOG = 6;

	var SUMNET = 0;
	var SUMVAT = 1;
	var SUMBRT = 2;
	var SUMCNT = 3;
	var DELIV = 4;

	//----------------------------------------------
	// Prida zbozi do kosiku s prislusnymi parametry
	//----------------------------------------------

	this.add = function(id, count) {
		
		var addorder = readCookie('addorder');
		var basket = this.fetch();
		
		for(baskid in basket) eraseCookie(this.name + '___' + basket[baskid][ZID]);
		for(baskid in basket) {
			var name = basket[baskid][ZNAME];
			var netto = basket[baskid][ZNETTO];
			var brutto = basket[baskid][ZBRUTTO];
			var vatrate = basket[baskid][ZVAT];
			var catalog = basket[baskid][CATALOG];
			if (basket[baskid][ZID] == id) var ccount = parseInt(count) + parseInt(basket[baskid][ZCOUNT]);
			else ccount = basket[baskid][ZCOUNT];
			createCookie(this.name + '___' + basket[baskid][ZID], basket[baskid][ZID]+'|'+name+'|'+netto+'|'+brutto+'|'+vatrate+'|'+ccount+'|'+catalog, 1);
		}
		
		if (addorder.indexOf('|'+id+'|') == -1 && parseInt(count) > 0) {
			addorder = addorder + id + '|';
			createCookie('addorder', addorder, 1);

			var name = document.getElementById('NAME' + id).firstChild.data.replace(/\//g, ' / ');
			var netto = document.getElementById('NETTO' + id).firstChild.data;
			var brutto = document.getElementById('BRUTTO' + id).firstChild.data;
			var vatrate = document.getElementById('VATRATE' + id).firstChild.data;
			var catalog = '';
			if (document.getElementById('CATALOG' + id).firstChild) catalog = document.getElementById('CATALOG' + id).firstChild.data;
			createCookie(this.name + '___' + id, id+'|'+name+'|'+netto+'|'+brutto+'|'+vatrate+'|'+count+'|'+catalog, 1);
		}
		
		this.refreshSummary(); // obcerstvi summary
		if (this.confirmOps) alert(strings['Added to basket'] + ': ' + count + " " +  this.pieces + " ... " + name + ".");
		if (this.refreshOnAdd) refresh();
	}

	this.summaryAdd = function(id, count) {

		var basket = this.fetch();
		var remid = '';
			
		for(baskid in basket) eraseCookie(this.name + '___' + basket[baskid][ZID]);
		for(baskid in basket) {
			var name = basket[baskid][ZNAME];
			var netto = basket[baskid][ZNETTO];
			var brutto = basket[baskid][ZBRUTTO];
			var vatrate = basket[baskid][ZVAT];
			var catalog = basket[baskid][CATALOG];
			if (basket[baskid][ZID] == id) var ccount = parseInt(count) + parseInt(basket[baskid][ZCOUNT]);
			else ccount = basket[baskid][ZCOUNT];
			if (ccount > 0) createCookie(this.name + '___' + basket[baskid][ZID], basket[baskid][ZID]+'|'+name+'|'+netto+'|'+brutto+'|'+vatrate+'|'+ccount+'|'+catalog, 1);
			else remid = basket[baskid][ZID];
		}
		
		if (remid != '') {this.remove(remid); return 0;}
		
		this.refreshSummary(); // obcerstvi summary
		if (this.confirmOps) alert(strings['Added to basket'] + ': ' + count + " " +  this.pieces + " ... " + name + ".");
		if (this.refreshOnAdd) refresh();
	}
	
	//----------------------------------------------
	// Odebere zbozi z kosiku
	//----------------------------------------------

	this.remove = function(id) {
		
		eraseCookie(this.name + "___" + id);

		var addorder = readCookie('addorder');
		addorder = addorder.replace('|'+id+'|', '|');
		createCookie('addorder', addorder, 1);
		
		try { document.getElementById('HREF' + id).className = 'notinbasket'; } catch(e) {;}
		try { document.getElementById('HREF' + id).innerHTML = "<img src='/cobasket/basket.png' alt='' align='absmiddle'/> " + strings['add to basket'] + " (0 " + this.pieces + ")"; } catch(e) {;}
		try { document.getElementById('H2REF' + id).className = 'notinbasket'; } catch(e) {;}
		try { document.getElementById('H2REF' + id).innerHTML = "<img src='/cobasket/basket.png' alt='' align='absmiddle'/> " + strings['add to basket'] + " (0 " + this.pieces + ")"; } catch(e) {;}
		try { document.getElementById('H3REF' + id).className = 'notinbasket'; } catch(e) {;}
		try { document.getElementById('H3REF' + id).innerHTML = "<img src='/cobasket/basket.png' alt='' align='absmiddle'/> " + strings['add to basket'] + " (0 " + this.pieces + ")"; } catch(e) {;}
		try { document.getElementById('H4REF' + id).className = 'notinbasket'; } catch(e) {;}
		try { document.getElementById('H4REF' + id).innerHTML = "<img src='/cobasket/basket.png' alt='' align='absmiddle'/> " + strings['add to basket'] + " (0 " + this.pieces + ")"; } catch(e) {;}

		this.refreshSummary(); // obcerstvi summary
		if (this.confirmOps) alert(strings['Removed from basket.']);
	}

	this.removeAll = function() {
		var basket = this.fetch();
		for(id in basket) {
			eraseCookie(this.name + "___" + basket[id][ZID]);
			try { document.getElementById('HREF' + basket[id][ZID]).className = 'notinbasket'; } catch(e) {;}
			try { document.getElementById('HREF' + basket[id][ZID]).innerHTML = "<img src='/cobasket/basket.png' alt='' align='absmiddle'/> " + strings['add to basket'] + " (0 " + this.pieces + ")"; } catch(e) {;}
			try { document.getElementById('H2REF' + basket[id][ZID]).className = 'notinbasket'; } catch(e) {;}
			try { document.getElementById('H2REF' + basket[id][ZID]).innerHTML = "<img src='/cobasket/basket.png' alt='' align='absmiddle'/> " + strings['add to basket'] + " (0 " + this.pieces + ")"; } catch(e) {;}
			try { document.getElementById('H3REF' + basket[id][ZID]).className = 'notinbasket'; } catch(e) {;}
			try { document.getElementById('H3REF' + basket[id][ZID]).innerHTML = "<img src='/cobasket/basket.png' alt='' align='absmiddle'/> " + strings['add to basket'] + " (0 " + this.pieces + ")"; } catch(e) {;}
			try { document.getElementById('H4REF' + basket[id][ZID]).className = 'notinbasket'; } catch(e) {;}
			try { document.getElementById('H4REF' + basket[id][ZID]).innerHTML = "<img src='/cobasket/basket.png' alt='' align='absmiddle'/> " + strings['add to basket'] + " (0 " + this.pieces + ")"; } catch(e) {;}
		}
		createCookie('addorder', '|', 1);
		this.refreshSummary(); // obcerstvi summary
	}

	//--------------------------------------------
	// Vrati pole s obsahem kosiku
	//--------------------------------------------

	this.fetch = function() {
		var carr = new Object();
		var product;
		var nameEQ = this.name + "___";
		var ca = document.cookie.split(';');
		var j = 0;
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) {
				product = unescape(c.substring(nameEQ.length,c.length));
				carr[j++] = product.substring(product.indexOf("=") + 1, product.length).split("|");
			}
		}
		
		var addorder = readCookie('addorder');
		var aaddorder = addorder.split('|');
		
		var values = new Object();
		var j = 0;
		for(i = 0; i < aaddorder.length; i++) {
			if (aaddorder[i] != '')
			{
				for (carrid in carr)
				{
					if (aaddorder[i] == carr[carrid][ZID])
					{
						var name = carr[carrid][ZNAME];
						var netto = carr[carrid][ZNETTO];
						var brutto = carr[carrid][ZBRUTTO];
						var vatrate = carr[carrid][ZVAT];
						var count = carr[carrid][ZCOUNT];
						var catalog = carr[carrid][CATALOG];
						values[j++] = new Array(carr[carrid][ZID], name, netto, brutto, vatrate, count, catalog);
					}
				}
			}
		}
		return values;
	}

	//--------------------------------------------
	// Vypise obsah kosiku ve formatu pro FORM
	//--------------------------------------------

	this.dumpForm = function() {

		var prodid;
		var basket = this.fetch();
		var divid = this.name;
		var output = "";
		/*
		for(id in basket) {
			prodid = divid + "_" + id;
			output += "\t<input type='hidden' name='zbozi_" + prodid + "' value='----------------------------' />\n";
			output += "\t<input type='hidden' name='katalogove-cislo-zbozi_" + prodid + "' value='" + basket[id][CATALOG] + "' />\n";
			output += "\t<input type='hidden' name='id-zbozi_" + prodid + "' value='" + basket[id][ZID] + "' />\n";
			output += "\t<input type='hidden' name='nazev-zbozi_" + prodid + "' value='" + basket[id][ZNAME] + "' />\n";
			output += "\t<input type='hidden' name='kusu_" + prodid + "' value='" + basket[id][ZCOUNT] + "' />\n";
			output += "\t<input type='hidden' name='cena-bez-dph_" + prodid + "' value='" + basket[id][ZNETTO] + "' />\n";
			output += "\t<input type='hidden' name='cena-vc-dph_" + prodid + "' value='" + basket[id][ZBRUTTO] + "' />\n";
			output += "\t<input type='hidden' name='sazba-dph_" + prodid + "' value='" + basket[id][ZVAT] + "' />\n";
		}
		*/
		for(id in basket) {
			prodid = divid + "_" + id;
			output += "\n<textarea style='display:none' name='" + strings['Product'] + " id: " +  basket[id][ZID] + "----------------------------'>\n\n" + strings['Catalogue #'] + ": " + basket[id][CATALOG] + "\n" + strings['Name'] + ": " + basket[id][ZNAME] + "\n" + strings['Pcs.'] + ": " + basket[id][ZCOUNT] + " " + this.pieces + "\n" + strings['Price excl. VAT'] + ": " + basket[id][ZNETTO] + " " + this.currency + "\n" + strings['VAT'] + ": " + basket[id][ZVAT] + " %\n" + strings['Price incl. VAT'] + ": " + basket[id][ZBRUTTO] + " " + this.currency + "\n</textarea>";
		}

		val = Math.round((parseFloat(this.summary[DELIV])+parseFloat(this.summary[SUMBRT]))*100)/100;
		var sval = val.toString();
		if (sval.indexOf(',') == -1) valsep = '.'; else valsep = ',';
		var aval = sval.split(valsep);
		if (aval.length == 1) sval = sval + ".00";
		else for (var zerocount = 0; zerocount < 2 - aval[1].length; zerocount++) sval = sval + "0";				
		
		output += "\n<textarea style='display:none' id='dumpformsum' name='" + strings['Total'] + "----------------------------'>\n\n" + strings['Total pcs.'] + ": " + this.summary[SUMCNT] + " " + this.pieces + "\n" + strings['Total excl. VAT'] + ": " + this.summary[SUMNET] + " " + this.currency + "\n" + strings['Total VAT'] + ": " + this.summary[SUMVAT] + " " + this.currency + "\n" + strings['Total incl. VAT'] + ": " + this.summary[SUMBRT] + " " + this.currency + "\n" + strings['Delivery'] + ": " + this.summary[DELIV] + " " + this.currency + "\n" + strings['Total'] + ": " + sval + " " + this.currency + "</textarea>";

		return output;

	}

	//--------------------------------------------
	// Vrati pole sumarnich informaci
	//--------------------------------------------

	this.getSummary = function()
	{
		// summary je vzdy up to date, vytvari se po kazdem add nebo remove
		return this.summary;
	}

	//--------------------------------------------
	// Vypise obsah kosiku v xhtml formatu jako
	// HTML tabulku
	//--------------------------------------------

	this.dumpTable = function()
	{
		// summary table je vzdy up to date, vytvari se po kazdem add nebo remove
		return this.summaryTable;
	}

	//--------------------------------------------
	// Spocita nove summary a vytvori HTML kod
	// pro sumarni tabulku
	//--------------------------------------------

	this.refreshSummary = function() {

		var prodid;
		var basket = this.fetch();
		var divid = "basket_" + this.name;
		var output = "<table class='" + this.name + "' id='" + divid + "'>\n";

		var vatsum = 0;
		var brtsum = 0;
		var netsum = 0;
		var cntsum = 0;
		var deliv = 0;

		var vat = new Object();
		var brt = new Array();
		var net = new Array();

		output += "\t<tr class='" + this.name + "_header'>\n";
		output += "\t\t<td align='right' class='" + this.name + "_header_COUNT'>" + strings['Count'] + "</td>\n";
		output += "\t\t<td align='left'  class='" + this.name + "_header_ID'>" + strings['Code'] + "</td>\n";
		output += "\t\t<td align='left'  class='" + this.name + "_header_NAME'>" + strings['Name'] + "</td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_header_NETTO'>" + strings['Excl. VAT'] + "/" + this.pieces + "</td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_header_VAT'>" + strings['VAT'] + "</td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_header_BRUTTO'>" + strings['Incl. VAT'] + "/" + this.pieces + "</td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_header_SUM'>" + strings['Sum incl. VAT'] + "</td>\n";
		output += "\t</tr>\n";

		for(id in basket) {
			
			// this.showDetail(basket[id][ZID], false);
			try { document.getElementById('HREF' + basket[id][ZID]).className = 'inbasket'; } catch(e) {;}
			try { document.getElementById('HREF' + basket[id][ZID]).innerHTML = "<img src='/cobasket/basket.png' alt='' align='absmiddle'/> " + strings['add to basket'] + " (" + basket[id][ZCOUNT] + " " + this.pieces + ")"; } catch(e) {;}
			try { document.getElementById('H2REF' + basket[id][ZID]).className = 'inbasket'; } catch(e) {;}
			try { document.getElementById('H2REF' + basket[id][ZID]).innerHTML = "<img src='/cobasket/basket.png' alt='' align='absmiddle'/> " + strings['add to basket'] + " (" + basket[id][ZCOUNT] + " " + this.pieces + ")"; } catch(e) {;}
			try { document.getElementById('H3REF' + basket[id][ZID]).className = 'inbasket'; } catch(e) {;}
			try { document.getElementById('H3REF' + basket[id][ZID]).innerHTML = "<img src='/cobasket/basket.png' alt='' align='absmiddle'/> " + strings['add to basket'] + " (" + basket[id][ZCOUNT] + " " + this.pieces + ")"; } catch(e) {;}
			try { document.getElementById('H4REF' + basket[id][ZID]).className = 'inbasket'; } catch(e) {;}
			try { document.getElementById('H4REF' + basket[id][ZID]).innerHTML = "<img src='/cobasket/basket.png' alt='' align='absmiddle'/> " + strings['add to basket'] + " (" + basket[id][ZCOUNT] + " " + this.pieces + ")"; } catch(e) {;}

			prodid = divid + "_" + id;

			var totalbrutto = Math.round(basket[id][ZCOUNT] * basket[id][ZBRUTTO] * 100)/100;
			var stotalbrutto = totalbrutto.toString();
			if (stotalbrutto.indexOf(',') == -1) totalbruttosep = '.'; else totalbruttosep = ',';
			var atotalbrutto = stotalbrutto.split(totalbruttosep);
			if (atotalbrutto.length == 1) stotalbrutto = stotalbrutto + ".00";
			else for (var zerocount = 0; zerocount < 2 - atotalbrutto[1].length; zerocount++) stotalbrutto = stotalbrutto + "0";
			
			output += "\t<tr class='" + this.name + "_product' id='" + prodid + "'>\n";
			output += "\t\t<td align='right' class='" + this.name + "_product_COUNT' id='" + prodid + "_COUNT' nowrap='nowrap'>" + basket[id][ZCOUNT] + "&nbsp;" + this.pieces + " <a title='" + strings['Add 1 piece'] + "' href='#tab=resume' onclick='baskets[\"" + this.name + "\"].summaryAdd(\"" + basket[id][ZID] + "\", 1); refresh();'><img valign='bottom' src='/cobasket/plus.png' /></a>&nbsp;<a title='" + strings['Remove 1 piece'] + "' href='#tab=resume' onclick='baskets[\"" + this.name + "\"].summaryAdd(\"" +  basket[id][ZID] + "\", -1); refresh();'><img valign='bottom' src='/cobasket/minus.png' /></a>&nbsp;<a title='" + strings['Remove from basket'] + "' href='#tab=resume' onclick='baskets[\"" + this.name + "\"].remove(\"" +  basket[id][ZID] + "\"); refresh();'><img valign='bottom' src='/cobasket/remove.png' /></a></td>\n";
			output += "\t\t<td align='left'  class='" + this.name + "_product_ID' id='" + prodid + "_ID'>#" + basket[id][CATALOG] + "</td>\n";
			output += "\t\t<td align='left'  class='" + this.name + "_product_NAME' id='" + prodid + "_NAME'>" + basket[id][ZNAME] + "</td>\n";
			output += "\t\t<td align='right' class='" + this.name + "_product_NETTO' id='" + prodid + "_NETTO'>" + basket[id][ZNETTO] + "&nbsp;" + this.currency + "</td>\n";
			output += "\t\t<td align='right' class='" + this.name + "_product_VAT' id='" + prodid + "_VAT'>" + basket[id][ZVAT] + "%</td>\n";
			output += "\t\t<td align='right' class='" + this.name + "_product_BRUTTO' id='" + prodid + "_BRUTTO'>" + basket[id][ZBRUTTO] + "&nbsp;" + this.currency + "</td>\n";
			output += "\t\t<td align='right' class='" + this.name + "_product_SUM' id='" + prodid + "_SUM'>" + stotalbrutto + "&nbsp;" + this.currency + "</td>\n";
			output += "\t</tr>\n";
			
			vatrate = basket[id][ZVAT];

			if (!vat[vatrate]) vat[vatrate] = 0;
			if (!net[vatrate]) net[vatrate] = 0;
			if (!brt[vatrate]) brt[vatrate] = 0;

			vat[vatrate] += basket[id][ZCOUNT] * (basket[id][ZBRUTTO] - basket[id][ZNETTO]);
			brt[vatrate] += basket[id][ZCOUNT] * basket[id][ZBRUTTO];
			net[vatrate] += basket[id][ZCOUNT] * basket[id][ZNETTO];

			vatsum += basket[id][ZCOUNT] * (basket[id][ZBRUTTO] - basket[id][ZNETTO]);
			brtsum += basket[id][ZCOUNT] * basket[id][ZBRUTTO];
			netsum += basket[id][ZCOUNT] * basket[id][ZNETTO];
			cntsum += parseInt(basket[id][ZCOUNT]);

		} output += "</table>\n";

		output += "<table class='" + this.name + "'>\n";

		output += "\t<tr class='" + this.name + "_summary_header'>\n";
		output += "\t\t<td align='left'  class='" + this.name + "_summary_header_NAME'> <a title='" + strings['Remove from basket'] + "' href='#tab=resume' onclick='baskets[\"" + this.name + "\"].removeAll(); refresh();'>(" + strings['empty basket'] + ")</a></td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_summary_header_NETTO'>" + strings['Excl. VAT'] + "</td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_summary_header_VAT'>" + strings['VAT'] + "</td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_summary_header_BRUTTO'>" + strings['Incl. VAT'] + "</td>\n";
		output += "\t</tr>\n";
		/*
		for(vatrate in vat) {

			output += "\t<tr class='" + this.name + "_summary_row'>\n";
			output += "\t\t<td align='left'  class='" + this.name + "_summary_row_NAME'>Celkem zboží se sazbou DPH " + vatrate + "%</td>\n";
			output += "\t\t<td align='right' class='" + this.name + "_summary_row_NETTO'>" + net[vatrate] + "&nbsp;" + this.currency + "</td>\n";
			output += "\t\t<td align='right' class='" + this.name + "_summary_row_VAT'>" + vat[vatrate] + "&nbsp;" + this.currency + "</td>\n";
			output += "\t\t<td align='right' class='" + this.name + "_summary_row_BRUTTO'>" + brt[vatrate] + "&nbsp;" + this.currency + "</td>\n";
			output += "\t</tr>\n";

		}
		*/
		
		var val = 0;
		for(vatrate in vat) {
			output += "\t<tr class='" + this.name + "_summary_row'>\n";
			output += "\t\t<td align='left'  class='" + this.name + "_summary_row_NAME'>" + strings['Total of products with VAT rate'] + " " + vatrate + "%</td>\n";

			val = Math.round(parseFloat(net[vatrate])*100)/100;
			var sval = val.toString();
			if (sval.indexOf(',') == -1) valsep = '.'; else valsep = ',';
			var aval = sval.split(valsep);
			if (aval.length == 1) sval = sval + ".00";
			else for (var zerocount = 0; zerocount < 2 - aval[1].length; zerocount++) sval = sval + "0";
			output += "\t\t<td align='right' class='" + this.name + "_summary_row_NETTO'>" + sval + "&nbsp;" + this.currency + "</td>\n";

			val = Math.round(parseFloat(vat[vatrate])*100)/100;
			var sval = val.toString();
			if (sval.indexOf(',') == -1) valsep = '.'; else valsep = ',';
			var aval = sval.split(valsep);
			if (aval.length == 1) sval = sval + ".00";
			else for (var zerocount = 0; zerocount < 2 - aval[1].length; zerocount++) sval = sval + "0";
			output += "\t\t<td align='right' class='" + this.name + "_summary_row_VAT'>" + sval + "&nbsp;" + this.currency + "</td>\n";

			val = Math.round(parseFloat(brt[vatrate])*100)/100;
			var sval = val.toString();
			if (sval.indexOf(',') == -1) valsep = '.'; else valsep = ',';
			var aval = sval.split(valsep);
			if (aval.length == 1) sval = sval + ".00";
			else for (var zerocount = 0; zerocount < 2 - aval[1].length; zerocount++) sval = sval + "0";
			output += "\t\t<td align='right' class='" + this.name + "_summary_row_BRUTTO'>" + sval + "&nbsp;" + this.currency + "</td>\n";
			output += "\t</tr>\n";

		}
		
		output += "\t<tr class='" + this.name + "_summary_footer'>\n";
		output += "\t\t<td align='left'  class='" + this.name + "_summary_footer_NAME'>" + strings['Total goods'] + "</td>\n";
		
		val = Math.round(parseFloat(netsum)*100)/100;
		var sval = val.toString();
		if (sval.indexOf(',') == -1) valsep = '.'; else valsep = ',';
		var aval = sval.split(valsep);
		if (aval.length == 1) sval = sval + ".00";
		else for (var zerocount = 0; zerocount < 2 - aval[1].length; zerocount++) sval = sval + "0";
		netsum = sval;
		output += "\t\t<td align='right' class='" + this.name + "_summary_footer_NETTO'>" + sval + "&nbsp;" + this.currency + "</td>\n";

		val = Math.round(parseFloat(vatsum)*100)/100;
		var sval = val.toString();
		if (sval.indexOf(',') == -1) valsep = '.'; else valsep = ',';
		var aval = sval.split(valsep);
		if (aval.length == 1) sval = sval + ".00";
		else for (var zerocount = 0; zerocount < 2 - aval[1].length; zerocount++) sval = sval + "0";		
		vatsum = sval;
		output += "\t\t<td align='right' class='" + this.name + "_summary_footer_VAT'>" + sval + "&nbsp;" + this.currency + "</td>\n";

		val = Math.round(parseFloat(brtsum)*100)/100;
		var sval = val.toString();
		if (sval.indexOf(',') == -1) valsep = '.'; else valsep = ',';
		var aval = sval.split(valsep);
		if (aval.length == 1) sval = sval + ".00";
		else for (var zerocount = 0; zerocount < 2 - aval[1].length; zerocount++) sval = sval + "0";				
		brtsum = sval;
		output += "\t\t<td align='right' class='" + this.name + "_summary_footer_BRUTTO'>" + sval + "&nbsp;" + this.currency + "</td>\n";
		output += "\t</tr>\n";

		output += "</table>\n";
		
		if (parseFloat(brtsum) > 3000) this.summary[DELIV] = 0;
		deliv = this.summary[DELIV];
		val = Math.round((parseFloat(deliv)+parseFloat(brtsum))*100)/100;
		var sval = val.toString();
		if (sval.indexOf(',') == -1) valsep = '.'; else valsep = ',';
		var aval = sval.split(valsep);
		if (aval.length == 1) sval = sval + ".00";
		else for (var zerocount = 0; zerocount < 2 - aval[1].length; zerocount++) sval = sval + "0";				
		output += "<table class='" + this.name + "'>\n";
		output += "\t<tr class='" + this.name + "_header'>\n";
		output += "\t<td>" + strings['Total goods'] + "</td><td>" + strings['Delivery'] + "</td><td>" + strings['Total'] + "</td></tr>\n";
		output += "\t<td align='right'>" + brtsum + "&nbsp;" + this.currency + "</td><td align='right'><span id='delivery'>" + deliv +".00&nbsp;" + this.currency + "</span></td><td class='cobasket_header' align='right'><span id='delivsum'>" + sval + "&nbsp;" + this.currency + "</span></td></tr>\n";
		output += "</table>\n";

		this.summary = new Array(netsum, vatsum, brtsum, cntsum, deliv);
		this.summaryTable = output;		
		return true;
	}

	//--------------------------------------------
	// Vypise obsah kosiku v xhtml formatu jako
	// HTML tabulku
	//--------------------------------------------

	this.showOrder = function(to, subject) {

		var output = "<form onsubmit=\"baskets['" + this.name + "'].removeAll(); eraseCookie('orderno'); document.orderform.poznamka.value = document.orderform.poznamka.value + '\\n\\n'; document.orderform.shop_email_from.value = document.orderform.email.value; return validate();\" name='orderform' action='/cobasket/sendmail.php?to=" + to + "&subject=" + encodeURIComponent(subject) + "' method='post'><input id='shop_email_from' name='shop_email_from' value='' type='hidden' />";

		output += "\t<table class='" + this.name + "'>";
		output += "\t\t<tr><td colspan='2' class='" + this.name + "_contact_header'>" + strings['Fill in informations below, please.'] + "</td></tr>";
		output += "\t\t<tr><td align='right' nowrap='nowrap'>" + strings['Order no.'] + "</td><td><strong>" + readCookie('orderno') + "</strong><input type='hidden' name='" + strings['Order no.'] + "' id='orderno' value='" + readCookie('orderno') + "'/><input type='hidden' name='shop_orderno' id='shop_orderno' value='" + readCookie('orderno') + "'/></td></tr>";
		output += "\t\t<tr><td align='right' nowrap='nowrap'>" + strings['Name and surname'] + "<sup>*</sup></td><td><input class='pvalues' name='" + strings['Name and surname'] + "' id='jmeno' onchange='createCookie(this.id, this.value, 1000)' value='" + readCookie('jmeno') + "'/></td></tr>";
		output += "\t\t<tr><td align='right' nowrap='nowrap'>" + strings['Telephone'] + "<sup>*</sup></td><td><input class='pvalues' name='" + strings['Telephone'] + "' id='telefon' onchange='createCookie(this.id, this.value, 1000)' value='" + readCookie('telefon') + "'/></td></tr>";
		output += "\t\t<tr><td align='right' nowrap='nowrap'>" + strings['E-mail address'] + "<sup>*</sup></td><td><input class='pvalues' name='" + strings['E-mail address'] + "' id='email' onchange='createCookie(this.id, this.value, 1000)' value='" + readCookie('email') + "'/></td></tr>";
		output += "\t\t<tr><td align='right' nowrap='nowrap'>" + strings['Shipping address'] + "<sup>*</sup></td><td><input class='pvalues' name='" + strings['Shipping address'] + "' id='adresa' onchange='createCookie(this.id, this.value, 1000)' value='" + readCookie('adresa') + "'/></td></tr>";
		//output += "\t\t<tr><td align='right'>Firma (název, IČ, adresa)</td><td><input class='pvalues' name='firma' id='firma' onchange='createCookie(this.id, this.value, 1000)' value='" + readCookie('firma') + "'/></td></tr>";
		output += "\t\t<tr><td align='right' nowrap='nowrap'>" + strings['Payment/shipping method'] + "<sup>*</sup></td><td>";
		output += "<select class='pvalues' name='" + strings['Payment/shipping method'] + "' id='platba_dodani' onchange='myBasket.ch_delivery()'><option value='" + strings['shop-dobirkaCZ'] + "' cena='200' zdarma='0'>" + strings['shop-dobirkaCZ'] + "</option><option value='" + strings['shop-osodber'] + "' cena='0' zdarma='0'>" + strings['shop-osodber'] + "</option></select>" + strings['shop-delivery-info'] + "</td></tr>";
		// output += "\t\t<tr><td align='right' nowrap='nowrap'>Účet na www.abclinuxu.cz<sup>1)</sup></td><td><input class='pvalues' name='abcucet' id='abcucet' onchange='createCookie(this.id, this.value, 1000)' value='" + readCookie('abcucet') + "'/></td></tr>";
		// output += "\t\t<tr><td colspan='2' style='text-align:justify'><sup>1)</sup>&nbsp;Jako náš příspěvek aktivním členům komunity uživatelů GNU/Linux poskytuje WebStep, s.r.o. 5%-ní&nbsp;slevu uživatelům registrovaným na www.abclinuxu.cz. Podmínkou slevy je min. 3-měsíce stará registrace. Objednané zboží se nesmí nacházet v akci. Prosím zadejte název Vašeho účtu. Prostřednictvím WWW formuláře na stránkách Vašeho účtu na www.abclinuxu.cz Vám bude zaslán kontrolní email a po jeho potvrzení uplatněna 5%-ní sleva na Vámi objednané zboží.</p></td></tr>";
		output += "\t\t<tr><td align='right' nowrap='nowrap'>" + strings['Note'] + "</td><td><textarea rows='3' class='pvalues' name='" + strings['Note'] + "' id='poznamka'></textarea></td></tr>";
		output += "\t\t<tr><td colspan='2' style='text-align:justify'>" + strings['shop-info'] + "</p></td></tr>";
		output += "\t\t<tr><td colspan='2' width='100%' class='input' style='text-align:right;'><input type='hidden' name='redirect' value='" + strings['shop-redirect'] + "'><textarea style='display: none;' name='body'>" + strings['shop-email-body'] + "</textarea><input type='submit' name='submit' id='submit' value='" + strings['Send order'] + "' /></td></tr>";
		output += "\t</table>";
		output += this.dumpForm();
		output += "</form>";

		return output;
	}

	//--------------------------------------------
	// Zobrazi detail produktu
	//--------------------------------------------

	this.showDetail = function(id)
	{
		if (document.getElementById('t' + id)) return;
		var name = document.getElementById('NAME' + id).innerHTML.replace('<BR>',' ');
		var desc = document.getElementById('DESC' + id).innerHTML;
		var brutto = document.getElementById('BRUTTO' + id).innerHTML;
		var netto = document.getElementById('NETTO' + id).innerHTML;
		var group = document.getElementById('GROUP' + id).innerHTML;
		var catalog = document.getElementById('CATALOG' + id).innerHTML;
		var image = document.getElementById('IMAGE' + id).innerHTML.split(':');

		this.detailGroup = group;

		var html = "<div id='t" + id + "'>";
			html += "<h1>" + name + "</h1><br />";
			html += strings['Price incl. VAT'] + ": <b>" + parseInt(brutto) + " " + this.currency + "</b><br />";
			//html += strings['Price excl. VAT'] + ": " + netto + "  + this.currency + "<br />";
			html += strings['Catalogue #'] + ": " + catalog + "<br />";
			//html += strings['Stock'] + ": " + strings['in stock'] + "<br />";
			html += strings['Group'] + ": <a href='#tab=goods&amp;grep=" + encode64(_to_utf8(group.replace(/&amp;/g,'&'))) + "&preview=1'>" + group + "</a><br /><br />";
			html += "<a id='H2REF" + id + "' class='notinbasket' href=\"javascript:myBasket.add('" + id + "', 1);\"><img src='/cobasket/basket.png' alt='' align='absmiddle'/> " + strings['add to basket'] + " (0 " + this.pieces + ")</a>";
			html += "<div class='line'><img alt='' src='/cobasket/t.png' /></div>";
			html += "<p>" + desc + "</p><br />";
			if (image.length == 5 && image[1]>0) {
				html += "<p><table class='noborder'><tr><td class='coimage'><img alt='" + name + "' width='" + image[1] + "' height='" + image[2] + "' src='/cobasket/data/images/" + image[0] + "'></td></tr></table></p>";
			}
			html += "<div class='line'><img alt='' src='/cobasket/t.png' /></div>";

			html += "</div>";

		if (navigator.appName == 'Microsoft Internet Explorer')
			html = html.replace(/href=\'#/g, "target='conavigate' href='/cobasket/conavigate.htm?");

		var ucho = false;
		/* DON'T DELETE !!!
			var ucho = "<tr><td onclick=\"tab('t" + id +"');\" colspan='5'>";
			ucho += "<table id='t" + id + "ucho' width='100%' class='ucho' cellpadding='0' cellspacing='0'>";
			ucho += "<tr><td class='uchol'></td><td class='uchoc' style='padding-left:11px;'>" + name + "</td><td class='uchoc' style='text-align:right;'>" + brutto + ",-&nbsp;Kč&nbsp;&nbsp;<a id='H2REF" + id + "' class='notinbasket' href=\"javascript:myBasket.add('" + id + "', 1);\">košík&nbsp;(0)</a></td><td class='uchol'></td></tr>";
			ucho +=	"</table></td></tr>";
		*/

		if (ucho) {
			document.getElementById('dtabs').innerHTML += ucho;
			document.getElementById('detail2').innerHTML = html + document.getElementById('detail2').innerHTML;
			if (show) tab('t' + id);
		} else document.getElementById('detail2').innerHTML = html;

		this.refreshSummary();
	}

	this.getPreview = function(id)
	{
		var name = document.getElementById('NAME' + id).innerHTML;
		var shortdesc = document.getElementById('SHORTDESC' + id).innerHTML;
		var brutto = document.getElementById('BRUTTO' + id).innerHTML;
		var netto = document.getElementById('NETTO' + id).innerHTML;
		var image = document.getElementById('IMAGE' + id).innerHTML.split(':');
		var tag = document.getElementById('TAG' + id).innerHTML;

		var html = "";
		/*html += "<div class='cobasket_preview'>";*/
		html += "<table class='cobasket_preview2'>";
		//if (tag.indexOf('$NOV$') >= 0) html += "<tr><td class='novinka'><font face='wingdings'>Ó</font> novinka <font face='wingdings'>Ì</font></td></tr>";
		/*if (tag.indexOf('$NOV$') >= 0) html += "<tr><td class='tag'><img src='/cobasket/novinka.png'></td></tr>";
		if (tag.indexOf('$AKC$') >= 0) html += "<tr><td class='tag'><img src='/cobasket/akce.png'></td></tr>";*/
		html += "<tr><td colspan='2'><h1><a href='#tab=detail&amp;id=" + id + "'>" + name + "</a></h1></td></tr>";
		html += "<tr><td rowspan='5' width='110'>";
		if (image.length == 5) html += "<a href='#tab=detail&amp;id=" + id + "'><img alt='" + name + "' width='" + image[3] + "' height='" + image[4] + "' src='/cobasket/data/images/" + image[0] + "t'></a>";
		else html += "&nbsp;";
		html += "</td><td><span class='shortdesc'>" + shortdesc + "</span></td></tr>";
		html += "<tr><td><h2>" + parseInt(brutto) + " " + this.currency + "</h2></td></tr>";
		html += "<tr><td><a id='H3REF" + id + "' class='notinbasket' href=\"javascript:myBasket.add('" + id + "', 1);\"><img src='/cobasket/basket.png' alt='' align='absmiddle'/> " + strings['add to basket'] + " (0 " + this.pieces + ")</a></td></tr>";
		html += "<tr><td><img src='/cobasket/t.png' alt='' height='10' /></td></tr>";
		html += "<tr><td align='right'><a class='notinbasket' href='#tab=detail&amp;id=" + id + "'>[detail]</a></td></tr>";
		html += "</table>";
		/*html += "</div>";*/

		if (navigator.appName == 'Microsoft Internet Explorer')
			html = html.replace(/href=\'#/g, "target='conavigate' href='/cobasket/conavigate.htm?");

		return html;
	}

	this.ch_delivery = function()
	{
		this.freeDeliveryLimit = document.getElementById('platba_dodani')[document.getElementById('platba_dodani').selectedIndex].getAttribute('zdarma');
		cena = document.getElementById('platba_dodani')[document.getElementById('platba_dodani').selectedIndex].getAttribute('cena');
		if (this.freeDeliveryLimit > 0 && parseFloat(this.summary[SUMBRT]) > this.freeDeliveryLimit) cena = 0;
		this.summary[DELIV] = cena;

		document.getElementById('delivery').innerHTML = cena + '.00&nbsp;' + this.currency;
		
		val = Math.round((parseFloat(cena)+parseFloat(this.summary[SUMBRT]))*100)/100;
		var sval = val.toString();
		if (sval.indexOf(',') == -1) valsep = '.'; else valsep = ',';
		var aval = sval.split(valsep);
		if (aval.length == 1) sval = sval + ".00";
		else for (var zerocount = 0; zerocount < 2 - aval[1].length; zerocount++) sval = sval + "0";
		document.getElementById('delivsum').innerHTML = sval + '&nbsp;' + this.currency;

		document.getElementById('dumpformsum').value = "\n" + strings['Total pcs.'] + ": " + this.summary[SUMCNT] + " " + this.pieces + "\n" + strings['Total excl. VAT'] + ": " + this.summary[SUMNET] + " " + this.currency + "\n" + strings['Total VAT'] + ": " + this.summary[SUMVAT] + " " + this.currency + "\n" + strings['Total incl. VAT'] + ": " + this.summary[SUMBRT] + " " + this.currency + "\n" + strings['Delivery'] + ": " + this.summary[DELIV] + ".00 " + this.currency + "\n" + strings['Total'] + ": " + sval + " " + this.currency;
	  
	}
}


//----------------------------------------------
// Pomocne fce
//----------------------------------------------

function validate()
{
	if (document.getElementById("jmeno").value == "") {
		alert (strings['Fill in your name, please.']);
		return false;
	}

	if (document.getElementById("telefon").value == "") {
		alert (strings['Fill in your telephone number, please.']);
		return false;
	}

	if (document.getElementById("email").value == "") {
		alert (strings['Fill in your email address, please.']);
		return false;
	}

	if (document.getElementById("adresa").value == "") {
		alert (strings['Fill in your shipping address, please.']);
		return false;
	}

	setTimeout('refresh();', 3000);
	return true;
}

function createCookie(name, value, days)
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires=" + date.toGMTString();
	} else var expires = "";
	document.cookie = name + "=" + escape(value) + expires + "; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length));
	} return '';
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

function _to_utf8(s)
{
	var c, d = "";
	for (var i = 0; i < s.length; i++) {
			c = s.charCodeAt(i);
			if (c <= 0x7f) {
					d += s.charAt(i);
			} else if (c >= 0x80 && c <= 0x7ff) {
					d += String.fromCharCode(((c >> 6) & 0x1f) | 0xc0);
					d += String.fromCharCode((c & 0x3f) | 0x80);
			} else {
					d += String.fromCharCode((c >> 12) | 0xe0);
					d += String.fromCharCode(((c >> 6) & 0x3f) | 0x80);
					d += String.fromCharCode((c & 0x3f) | 0x80);
			}
	}

	return d;
}

function _from_utf8(s)
{
	var c, d = "", flag = 0, tmp;
	for (var i = 0; i < s.length; i++) {
			c = s.charCodeAt(i);
			if (flag == 0) {
					if ((c & 0xe0) == 0xe0) {
							flag = 2;
							tmp = (c & 0x0f) << 12;
					} else if ((c & 0xc0) == 0xc0) {
							flag = 1;
							tmp = (c & 0x1f) << 6;
					} else if ((c & 0x80) == 0) {
							d += s.charAt(i);
					} else {
							flag = 0;
					}
			} else if (flag == 1) {
					flag = 0;
					d += String.fromCharCode(tmp | (c & 0x3f));
			} else if (flag == 2) {
					flag = 3;
					tmp |= (c & 0x3f) << 6;
			} else if (flag == 3) {
					flag = 0;
					d += String.fromCharCode(tmp | (c & 0x3f));
			} else {
					flag = 0;
			}
	}
	return d;
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);

   return output;
}

function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

function load()
{
	if (document.getElementById('grep').value != '')
		grep(document.getElementById('grep').value);

	setTimeout('myBasket.refreshSummary(); refresh();', 100);
}

function refresh()
{
	var basket = myBasket.fetch();
	var basketlen = 0
	for(baskid in basket) basketlen++;
	
	if (basketlen > 0) {
		document.getElementById('resume2').innerHTML = myBasket.dumpTable() +
		myBasket.showOrder(strings['shop-email-address'], strings['shop-email-subject']);
		myBasket.ch_delivery();
	} else document.getElementById('resume2').innerHTML = '<br /><center><strong>' + strings['Your basket is currently empty.'] + '</strong></center><br /><br />';
}

String.prototype.seturiparm = function(parm, value)
{
	var qchar = '#';
	if (this.indexOf('?') > -1) qchar = '?';

	var uriA = this.split(qchar);
	var uriB = uriA[uriA.length - 1].split('&');

	for (var i = 0; i < uriB.length; i++) {
		if (uriB[i].indexOf(parm) == 0)
			uriB[i] = '';
	}

	uriB[uriB.length] = parm + '=' + value;
	uriA[uriA.length - 1] = uriB.join('&').replace(/&&/g, '&');
	var uri = uriA.join(qchar).replace(qchar + '&', qchar).replace(qchar + qchar, qchar);

	return uri;
}

var DocTitle = document.title;
var currentCmd = '';
var nov = false;

function chooser(cmd)
{
	// Priprav parametr

	if ((cmd == 'undefined') || (cmd == undefined)) cmd = location.href;
	cmd = cmd.replace(/.*\#/, '').replace(/.*\?/, '');
	if (cmd == currentCmd)	return;
	currentCmd = cmd;

	// Precti parametry

	var cmd_tab = 'groups';
	var cmd_detail = null;
	var cmd_grep = null;
	var cmd_preview = null;
	var cmd_id = null;

	myparms = cmd.replace(/&amp;/, '&').split('&');

	for (var i = 0; i < myparms.length; i++) {
		var pos = myparms[i].indexOf('=');
  		if (pos > -1) eval("var cmd_" + myparms[i].substr(0, pos) + " = '" + myparms[i].substr(pos + 1) + "';");
	}

	//-----------------------------
	// Zobraz detail zboží
	//-----------------------------

	if (cmd_id != null) {
		myBasket.showDetail(cmd_id);
		/*document.getElementById('detail').setAttribute('desc', 'Detail zboží #' + cmd_id);*/
	}

	//-----------------------------
	// Handluj style
	//-----------------------------

	if (cmd_preview != null) {
		document.getElementById('preview').checked = (cmd_preview != '0');
		pstart = parseInt(cmd_preview);
		preview();
	}

	if (cmd_tab != 'groups')
	{
		if (cmd_grep != null) group_name = cmd_grep; else group_name = encode64(_to_utf8(myBasket.detailGroup.replace(/&amp;/g,'&')));
		
		goodsrows = document.getElementById('cobasketgoods').rows.length;
		var grepi = 0;
		for (var goodsi = 0; goodsi < goodsrows; goodsi++)
		{
			if (goodsi > 0 && goodsi < (goodsrows - 2))
			{
				document.getElementById('cobasketgoods').rows[goodsi].style.display = 'none';
				document.getElementById('cobasketdetail').rows[goodsi - 1].style.display = 'none';
			}
			if (document.getElementById('cobasketgoods').rows[goodsi].getAttribute('id') == 'goods_' + group_name) grepi = goodsi;
		}
		if (grepi)
		{
			try {document.getElementById('cobasketgoods').rows[grepi].style.display = 'table-row';} catch(e) {document.getElementById('cobasketgoods').rows[grepi].style.display = 'block';}
			try {document.getElementById('cobasketdetail').rows[grepi - 1].style.display = 'table-row';} catch(e) {document.getElementById('cobasketdetail').rows[grepi - 1].style.display = 'block';}
		}
	}

	//-----------------------------
	// Handluj grep
	//-----------------------------

	if (cmd_grep != null) {
		if (cmd_preview == null) pstart = 0;
		if (cmd_grep == 'AA==') cmd_grep = '';
		if (cmd_grep != '') cmd_grep = _from_utf8(decode64(cmd_grep));
		realgrep(cmd_grep);
	}

	//-----------------------------
	// Handluj taby
	//-----------------------------

	if (cmd_tab != null) {
		
		if (currentTab) {
			document.getElementById(currentTab).style.display = 'none';
			try { document.getElementById(currentTab + 'ucho').className = 'ucho'; } catch(e) {;}
		} currentTab = cmd_tab;

		if (currentTab == 'groups') {

			if (!nov) {
				nov = new Array();
				var trs = document.getElementsByTagName('tr');
				var trslength = trs.length;
				for(var i = 0; i < trslength; i++) {
					if (trs[i].className == 'cobasket_row') {
						var tag = document.getElementById('TAG' + trs[i].getAttribute('title')).innerHTML;
						if ((tag.indexOf('$NOV$') >= 0) || (tag.indexOf('$AKC$') >= 0)) nov[nov.length] = trs[i].getAttribute('title');
					}
				}
			}

			var ida = new Object();
			while (nov.length && true) {
				var idacount = 0;
				for (var myid in ida) idacount++;
				if (idacount >= 3 || idacount == nov.length) break;
				var myid = nov[parseInt(Math.random() * nov.length)];
				ida[myid] = myid;
			}

			var prhtml = "<table class='noborder'><tr>";
			for (var myid in ida) prhtml += "<td class='cobasket_preview_td'>" + myBasket.getPreview(myid) + "</td>";
			prhtml += '</tr></table>';

			//document.getElementById('vyber').innerHTML = prhtml.replace(/H3REF/g, "H4REF");
			myBasket.refreshSummary();
		}

		document.getElementById(currentTab).style.display = 'block';
		try { document.getElementById(currentTab + 'ucho').className = 'uchoa'; } catch(e) {;}
		if (cmd_id == null) document.title = DocTitle; else document.title = document.getElementById('NAME' + cmd_id).innerHTML.replace('<BR>', '') + " | " + DocTitle;
		if (navigator.appName == 'Microsoft Internet Explorer') frames['conavigate'].document.title = document.title;
		/*if (cmd_tab == 'terms') document.getElementById('terms2').innerHTML = readfile('/cs/obchodni-podminky.html?plain');*/
	}
}

window.onload = function() {

	load();

	if ((location.hash == '') && (location.query == ''))
		location.href = '#tab=groups';

	if (navigator.appName == 'Microsoft Internet Explorer') {
		var atags = document.getElementsByTagName('a');
		for (var id = 0; id < atags.length; id++) {
			var ahref = atags[id].getAttribute('href');
			if (ahref && ahref.indexOf(location.pathname + '#') >= 0) {
				atags[id].setAttribute('href', '/cobasket/conavigate.htm?' + ahref.replace(/.*\#/, ''));
				atags[id].setAttribute('target', 'conavigate');
			}
		}
	}

	preview();
	setInterval("chooser();", 200);
}

function readfile(url)
{
	var conn = false;

	if (!conn) try { conn = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {;}
	if (!conn) try { conn = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {;}
	if (!conn) try { conn = new XMLHttpRequest(); } catch(e) {;}

	if (!conn)
		return "Could not create connection object.";

	conn.open('GET', url, false);
	conn.send('');

	return conn.responseText;
}

function grep(grepval)
{
	if ((grepval == undefined) || (grepval == 'undefined')) grepval = document.getElementById('grep').value;
	if ((grepval == undefined) || (grepval == 'undefined')) grepval = '';
	grepval = encode64(_to_utf8(grepval));

	if (navigator.appName == 'Microsoft Internet Explorer') frames['conavigate'].location.href = frames['conavigate'].location.href.seturiparm('tab', 'goods').seturiparm('grep', grepval).seturiparm('preview', (document.getElementById('preview').checked ? '1' : '0'));
	else location.href = location.href.seturiparm('tab', 'goods').seturiparm('grep', grepval).seturiparm('preview', (document.getElementById('preview').checked ? '1' : '0'));
}

var hGrep;
function asyncgrep(grepval)
{
	clearTimeout(hGrep);
	hGrep = setTimeout("grep('" + grepval + "');", 500);
}

var pstart = 0;
function realgrep(grepval)
{
	var divcounter = 0;
	var pager = '';
	var prhtml = '';
	var last = '';
	var endtr = 0;

	document.getElementById('grep').value = grepval;
	document.getElementById('grsearch').value = grepval;

	var trs = document.getElementsByTagName('tr');
	var trslength = trs.length;
	var mkpreview = document.getElementById('preview').checked;
	mkpreview = true;
	var myhref = location.href;
	var greptr = '';
	for(var i = 0; i < trslength; i++) {
		if (trs[i].className == 'cobasket_row') {
			greptr = trs[i].firstChild.innerHTML.toLowerCase().replace(/&amp;/g, '&');
			if ((greptr.indexOf(grepval.toLowerCase()) >= 0)
				|| (grepval == '')) {
				if (mkpreview) {
					divcounter++;
					if ((divcounter >= pstart) && (divcounter < (pstart + 8))) {
						endtr++;
						if ((divcounter % 2) == 1) prhtml += "<tr>";
						if ((divcounter % 2) == 0) last = "_last"; else last = "";
						prhtml += "<td class='cobasket_preview_td" + last + "'><img src='/cobasket/shoptop.png' alt='' />" + myBasket.getPreview(trs[i].getAttribute('title')) + "</td>";
						if ((divcounter % 2) == 0) {prhtml += "</tr><tr><td><img src='/cobasket/shopbottom.png' alt='' /></td><td><img src='/cobasket/shopbottom.png' alt='' /></td></tr><tr><td colspan='4'><img src='/cobasket/t.png' alt='' height='7' /></td></tr>"; endtr = 0;}
						if ((divcounter % 8) == 1) pager += (divcounter + 7)/8 + "&nbsp; ";
					} else if ((divcounter % 8) == 1) pager += "<a href='" + myhref.seturiparm('preview', divcounter) + "'>" + ((divcounter + 7)/8) + "</a>&nbsp; ";
				} else { try { trs[i].style.display = 'table-row'; } catch(e) { trs[i].style.display = 'block'; }}
			} else trs[i].style.display = 'none';
		}
	}
	if (endtr)
	{
		prhtml += "</tr><tr>";
		for (var i = 0; i < endtr; i++) prhtml += "<td><img src='/cobasket/shopbottom.png' alt='' /></td>";
		prhtml += "</tr>";		
	}

	if (divcounter <= 8) pager = '';
	document.getElementById('pager1').innerHTML = '<strong>' + strings['Show page:'] + '</strong> ' + pager;
	document.getElementById('pager2').innerHTML = '<strong>' + strings['Show page:'] + '</strong> ' + pager;
	document.getElementById('pager1').style.display = (pager == '' ? 'none' : 'block');
	document.getElementById('pager2').style.display = (pager == '' ? 'none' : 'block');
	if (prhtml != '') {
		/*
		document.getElementById('previewshop').innerHTML = "<table class='noborder'>" + prhtml + "</table>";
		myBasket.refreshSummary();
		*/
		//hack kvuli tomu, ze ve 'vyber' a 'previewshop' byly soucasne elementy se stejnyma id
		//document.getElementById('vyber').innerHTML = '';
		document.getElementById('previewshop').innerHTML = "<table class='noborder'>" + prhtml + "</table>";
	}
	else document.getElementById('previewshop').innerHTML = '';
	myBasket.refreshSummary();

	/*setTimeout("document.getElementById('grep').focus();", 100);*/
}

function preview()
{
	if (document.getElementById('preview').checked) {
		document.getElementById('shop').style.display = 'none';
		document.getElementById('previewshop').style.display = 'block';
	} else {
		document.getElementById('previewshop').style.display = 'none';
		document.getElementById('shop').style.display = 'block';
	}
}

