var baskets = new Array();
function Basket(p_name)
{
	//----------------------------------------------
	// Nastaveni parametru kosiku
	//----------------------------------------------

	this.name = p_name;
	this.confirmOps = true;
	this.refreshOnAdd = true;
	this.currency = 'Kč';
	this.pieces = 'ks';
	this.note = '';
	this.clientId = '';

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

	baskets[this.name] = this;

	//----------------------------------------------
	// Konstanty
	//----------------------------------------------

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

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

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

	this.add = function(id, count) {

		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;

		try { if (count != 0)
			var ccount = parseInt(readCookie(this.name + '___' + id).split("|")[ZCOUNT]);
			if (!isNaN(ccount))  count += ccount;
		} catch(e) {;}

  		var value = new Array(id , name, netto, brutto, vatrate, count);
		createCookie(this.name + '___' + id, value.join("|"), 1);
		this.refreshSummary(); // obcerstvi summary
		if (this.confirmOps) alert('1ks&nbsp;do&nbsp;košíku bylo přidáno: ' + count + "" +  this.pieces + " ... " + name + ".");
		if (this.refreshOnAdd) refresh();
	}

	//----------------------------------------------
	// Odebere zbozi z kosiku
	//----------------------------------------------

	this.remove = function(id) {

		eraseCookie(this.name + "___" + id);

		try { document.getElementById('HREF' + id).className = 'notinbasket'; } catch(e) {;}
		try { document.getElementById('HREF' + id).innerHTML = '1ks&nbsp;do&nbsp;košíku&nbsp;(0)'; } catch(e) {;}
		try { document.getElementById('H2REF' + id).className = 'notinbasket'; } catch(e) {;}
		try { document.getElementById('H2REF' + id).innerHTML = '1ks&nbsp;do&nbsp;košíku&nbsp;(0)'; } catch(e) {;}
		try { document.getElementById('H3REF' + id).className = 'notinbasket'; } catch(e) {;}
		try { document.getElementById('H3REF' + id).innerHTML = '1ks&nbsp;do&nbsp;košíku&nbsp;(0)'; } catch(e) {;}
		try { document.getElementById('H4REF' + id).className = 'notinbasket'; } catch(e) {;}
		try { document.getElementById('H4REF' + id).innerHTML = '1ks&nbsp;do&nbsp;košíku&nbsp;(0)'; } catch(e) {;}

		this.refreshSummary(); // obcerstvi summary
		if (this.confirmOps) alert("Zboží bylo odebráno z košíku.");
	}

	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 = '1ks&nbsp;do&nbsp;košíku&nbsp;(0)'; } catch(e) {;}
			try { document.getElementById('H2REF' + basket[id][ZID]).className = 'notinbasket'; } catch(e) {;}
			try { document.getElementById('H2REF' + basket[id][ZID]).innerHTML = '1ks&nbsp;do&nbsp;košíku&nbsp;(0)'; } catch(e) {;}
			try { document.getElementById('H3REF' + basket[id][ZID]).className = 'notinbasket'; } catch(e) {;}
			try { document.getElementById('H3REF' + basket[id][ZID]).innerHTML = '1ks&nbsp;do&nbsp;košíku&nbsp;(0)'; } catch(e) {;}
			try { document.getElementById('H4REF' + basket[id][ZID]).className = 'notinbasket'; } catch(e) {;}
			try { document.getElementById('H4REF' + basket[id][ZID]).innerHTML = '1ks&nbsp;do&nbsp;košíku&nbsp;(0)'; } catch(e) {;}
		}
		this.refreshSummary(); // obcerstvi summary
	}

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

	this.fetch = function() {
		var carr = new Array();
		var product;
		var nameEQ = this.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) {
				product = unescape(c.substring(nameEQ.length,c.length));
				carr[i] = product.substring(product.indexOf("=") + 1, product.length).split("|");
			}
		} return carr;
	}

	//--------------------------------------------
	// 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='cislo-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";
		}

		output += "\t<input type='hidden' name='celkem' value='----------------------------' />\n";
		output += "\t<input type='hidden' name='celkem-kusu' value='" + this.summary[SUMCNT] + "' />\n";
		output += "\t<input type='hidden' name='celkem-bez-dph' value='" + this.summary[SUMNET] + "' />\n";
		output += "\t<input type='hidden' name='celkem-dph' value='" + this.summary[SUMVAT] + "' />\n";
		output += "\t<input type='hidden' name='celkem-vc-dph' value='" + this.summary[SUMBRT] + "' />\n";
		output += "\t<input type='hidden' name='celkem-poznamka' value='----------------------------' />\n";
		output += "\t<input type='hidden' name='id-zakaznika' value='" + this.clientId + "' />\n";

		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 vat = new Array();
		var brt = new Array();
		var net = new Array();

		output += "\t<tr class='" + this.name + "_header'>\n";
		output += "\t\t<td align='left'  class='" + this.name + "_header_ID'>Kód</td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_header_COUNT'>Počet</td>\n";
		output += "\t\t<td align='left'  class='" + this.name + "_header_NAME'>Název produktu</td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_header_NETTO'>bez DPH/" + this.pieces + "</td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_header_VAT'>Sazba DPH</td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_header_BRUTTO'>vč. DPH/" + this.pieces + "</td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_header_SUM'>Celkem vč. DPH</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 = '1ks&nbsp;do&nbsp;košíku&nbsp;(' + basket[id][ZCOUNT] + ')'; } catch(e) {;}
			try { document.getElementById('H2REF' + basket[id][ZID]).className = 'inbasket'; } catch(e) {;}
			try { document.getElementById('H2REF' + basket[id][ZID]).innerHTML = '1ks&nbsp;do&nbsp;košíku&nbsp;(' + basket[id][ZCOUNT] + ')'; } catch(e) {;}
			try { document.getElementById('H3REF' + basket[id][ZID]).className = 'inbasket'; } catch(e) {;}
			try { document.getElementById('H3REF' + basket[id][ZID]).innerHTML = '1ks&nbsp;do&nbsp;košíku&nbsp;(' + basket[id][ZCOUNT] + ')'; } catch(e) {;}
			try { document.getElementById('H4REF' + basket[id][ZID]).className = 'inbasket'; } catch(e) {;}
			try { document.getElementById('H4REF' + basket[id][ZID]).innerHTML = '1ks&nbsp;do&nbsp;košíku&nbsp;(' + basket[id][ZCOUNT] + ')'; } catch(e) {;}

			prodid = divid + "_" + id;

			output += "\t<tr class='" + this.name + "_product' id='" + prodid + "'>\n";
			output += "\t\t<td align='left'  class='" + this.name + "_product_ID' id='" + prodid + "_ID'>#" + id + "</td>\n";
			output += "\t\t<td align='right' class='" + this.name + "_product_COUNT' id='" + prodid + "_COUNT'><a title='Odebere zboží z košíku' href='#resume' onclick='baskets[\"" + this.name + "\"].remove(\"" +  basket[id][ZID] + "\"); refresh();'>(odebrat)</a>&nbsp;" + basket[id][ZCOUNT] + "&nbsp;" + this.pieces + "</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'>" + (basket[id][ZCOUNT] * basket[id][ZBRUTTO]) + "&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='Odebere zboží z košíku' href='#' onclick='baskets[\"" + this.name + "\"].removeAll(); refresh();'>(vyprázdnit košík)</a></td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_summary_header_NETTO'>Základ DPH</td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_summary_header_VAT'>DPH</td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_summary_header_BRUTTO'>Včetně DPH</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";

		}

		output += "\t<tr class='" + this.name + "_summary_footer'>\n";
		output += "\t\t<td align='left'  class='" + this.name + "_summary_footer_NAME'>Celkem k úhradě</td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_summary_footer_NETTO'>" + netsum + "&nbsp;" + this.currency + "</td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_summary_footer_VAT'>" + vatsum + "&nbsp;" + this.currency + "</td>\n";
		output += "\t\t<td align='right' class='" + this.name + "_summary_footer_BRUTTO'>" + brtsum + "&nbsp;" + this.currency + "</td>\n";
		output += "\t</tr>\n";

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

		this.summary = new Array(netsum, vatsum, brtsum, cntsum);
		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(); return validate();\" name='orderform' action='/cobasket/sendmail.php?to=" + to + "&subject=" + subject + "' method='post'>";

		output += "\t<table class='" + this.name + "'>";
		output += "\t\t<tr><td colspan='2'>Prosím vyplňte kontaktní údaje.</td></tr>";
		output += "\t\t<tr><td align='right'>Jméno a příjmení<sup>*</sup></td><td><input class='pvalues' name='jmeno' id='jmeno' onchange='createCookie(this.name, this.value, 1000)' value='" + readCookie('jmeno') + "'/></td></tr>";
		output += "\t\t<tr><td align='right'>Telefon<sup>*</sup></td><td><input class='pvalues' name='telefon' id='telefon' onchange='createCookie(this.name, this.value, 1000)' value='" + readCookie('telefon') + "'/></td></tr>";
		output += "\t\t<tr><td align='right'>Email</td><td><input class='pvalues' name='email' id='email' onchange='createCookie(this.name, this.value, 1000)' value='" + readCookie('email') + "'/></td></tr>";
		output += "\t\t<tr><td align='right'>Dodací adresa<sup>*</sup></td><td><input class='pvalues' name='adresa' id='adresa' onchange='createCookie(this.name, 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.name, this.value, 1000)' value='" + readCookie('firma') + "'/></td></tr>";
		output += "\t\t<tr><td align='right'>Forma platby/dodání</td><td>";
		output += "<select class='pvalues' name='platba_dodani' id='platba_dodani'><option value='dobirka'>Dobírkou (dle hmotnosti PPL či Česká pošta)</option><option value='hotove_na_prodejne'>Hotově na prodejně</option><option value='bankovni_prevod'>Bankovním převodem</option></select></td></tr>";
		// output += "\t\t<tr><td align='right'>Účet na www.abclinuxu.cz<sup>1)</sup></td><td><input class='pvalues' name='abcucet' id='abcucet' onchange='createCookie(this.name, 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'>Poznámka</td><td><textarea rows='3' class='pvalues' name='poznamka' id='poznamka'></textarea></td></tr>";
		output += "\t\t<tr><td colspan='2' style='text-align:justify'>Vážený zákazníku, po obdržení Vaší objednávky Vás budeme telefonicky kontaktovat. Veškeré další náležitosti nutné pro dokončení transakce si od Vás náš prodejce vyžádá na základě telefonického rozhovoru, během kterého s Vámi dojedná i detaily dodání zboží vč. daňového dokladu. Více informací najdete v obchodních podmínkách našeho obchodu.<p style='text-align:right;'>Děkujeme Vám za objednávku.</p></td></tr>";
		output += "\t\t<tr><td colspan='2' width='100%' class='input' style='text-align:right;'><input type='submit' name='submit' id='submit' value='Odeslat objednávku' /></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;
		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(':');

		var html = "<div id='t" + id + "'>";
			html += "<h1>" + name + " [ID:" + id + "]</h1><br />";
			html += "Cena vč. DPH: <b>" + brutto + ",- Kč</b><br />";
			html += "Cena bez DPH: " + netto + ",- Kč<br />";
			html += "Katalogové číslo: " + catalog + "<br />";
			html += "Dostupnost zboží: skladem<br />";
			html += "Skupina: <a href='#tab=goods&grep=" + encode64(_to_utf8(group)) + "&preview=1'>" + group + "";
			html += "<p style='text-align:right; margin:0px; padding:0px;'><a style='position:relative; top:-76px;' id='H2REF" + id + "' class='notinbasket' href=\"javascript:myBasket.add('" + id + "', 1);\">1ks&nbsp;do&nbsp;košíku&nbsp;(0)</a></p>";
			html += "<div class='line'><img alt='' src='/templates/webstep/spacer.gif' /></div>";

			if (image.length == 5) {
				html += "<p><table><tr><td class='coimage'><img alt='" + name + "' width='" + image[1] + "' height='" + image[2] + "' style='max-width:450px;' src='/cobasket/data/images/" + image[0] + "'></td></tr></table></p>";
			} html += "<p>" + desc + "</p><br /><div class='line'><img alt='' src='/templates/webstep/spacer.gif' /></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('detail').innerHTML = html + document.getElementById('detail').innerHTML;
			if (show) tab('t' + id);
		} else document.getElementById('detail').innerHTML = html;

		this.refreshSummary();
	}

	this.getPreview = function(id)
	{
		var name = document.getElementById('NAME' + 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>";
		if (tag.indexOf('$TOP$') >= 0) html += "<tr><td class='tag'><img src='/cobasket/top.png'></td></tr>";
		html += "<tr><td><a href='#tab=detail&id=" + id + "'>" + name + "</a></td></tr>";
		if (image.length == 5) html += "<tr><td><a href='#tab=detail&id=" + id + "'><img alt='" + name + "' width='" + image[3] + "' height='" + image[4] + "' src='/cobasket/data/images/" + image[0] + "t'></a></td></tr>";
		html += "<tr><td>Cena: <b><acronym title='" + netto + " Kč bez DPH'>" + brutto + " Kč</acronym></b></td></tr>";
		html += "<tr><td><a id='H3REF" + id + "' class='notinbasket' href=\"javascript:myBasket.add('" + id + "', 1);\">1ks&nbsp;do&nbsp;košíku&nbsp;(0)</a></td></tr>";
		html += "</table></div>";

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

		return html;
	}
}


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

function validate()
{
	if (document.orderform.jmeno.value == "") {
		alert ("Prosím vyplňte jméno.");
		return false;
	}

	if (document.orderform.telefon.value == "") {
		alert ("Prosím vyplňte telefonní číslo.");
		return false;
	}

	if (document.orderform.adresa.value == "") {
		alert ("Prosím vyplňte dodací adresu.");
		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()
{
	if (myBasket.fetch().length > 0) {
		document.getElementById('resume2').innerHTML = myBasket.dumpTable() +
		myBasket.showOrder('shop@webstep.net', 'Objednávka z shopu www.webstep.net');
	} else document.getElementById('resume2').innerHTML = '<br /><center>košík je prázdný</center>';
}

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 in uriB) {
		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 in myparms) {
		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();
	}

	//-----------------------------
	// 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('pid')).innerHTML;
						if ((tag.indexOf('$NOV$') >= 0) || (tag.indexOf('$AKC$') >= 0) || (tag.indexOf('$TOP$') >= 0)) nov[nov.length] = trs[i].getAttribute('pid');
					}
				}
			}

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

			var prhtml = "<table><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) {;}
		document.title = DocTitle + " | " + document.getElementById(currentTab).getAttribute('desc');
		if (navigator.appName == 'Microsoft Internet Explorer') frames['conavigate'].document.title = document.title;
		if (cmd_tab == 'terms') document.getElementById('terms2').innerHTML = readfile('/www/obchod/obchodni-podminky.html?plain');
	}
}

window.onload = function() {

	// custom onload pro www.webstep.net
	try {
		if (window.attachEvent && navigator.appName == 'Microsoft Internet Explorer')
			sfHover();
	} catch(e) {;}

	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 + "');", 2000);
}

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

	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;
	var myhref = location.href;

	for(var i = 0; i < trslength; i++) {
		if (trs[i].className == 'cobasket_row') {
			if ((trs[i].firstChild.innerHTML.toLowerCase().indexOf(grepval.toLowerCase()) >= 0)
				|| (grepval == '')) {
				if (mkpreview) {
					divcounter++;
					if ((divcounter >= pstart) && (divcounter < (pstart + 9))) {
						if ((divcounter % 3) == 1) prhtml += "<tr>";
						prhtml += "<td class='cobasket_preview_td'>" + myBasket.getPreview(trs[i].getAttribute('pid')) + "</td>";
						if ((divcounter % 3) == 3) prhtml += "</tr>";
						if ((divcounter % 9) == 1) pager += "<font color='black'>" + (divcounter + 8)/9 + "</font>&nbsp; ";
					} else if ((divcounter % 9) == 1) pager += "<a href='" + myhref.seturiparm('preview', divcounter) + "'>" + ((divcounter + 8)/9) + "</a>&nbsp; ";
				} else { try { trs[i].style.display = 'table-row'; } catch(e) { trs[i].style.display = 'block'; }}
			} else trs[i].style.display = 'none';
		}
	}

	if (divcounter <= 9) pager = '';
	document.getElementById('pager1').innerHTML = pager;
	document.getElementById('pager2').innerHTML = pager;
	document.getElementById('pager1').style.display = (pager == '' ? 'none' : 'block');
	document.getElementById('pager2').style.display = (pager == '' ? 'none' : 'block');
	if (prhtml != '') {
		document.getElementById('previewshop').innerHTML = "<table>" + prhtml + "</table>";
		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';
	}
}