var buy_bx = ""
var buy_code=""

var encode64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

var binEncode = new Array();
for (i=0;i<encode64Table.length;i++)
binEncode[binBits(i, 6)] = encode64Table.charAt(i);
function base64_encode(chaineAconvertir) {
var base64String = "";
var groupe128bits = "";
for (var i=0;i<chaineAconvertir.length;i = i+3) {
groupe24bits = chaineAconvertir.substr(i,3);
nbCar = groupe24bits.length;
var binContainer = "";
for (var j=0;j<nbCar;j++)
binContainer += binBits(groupe24bits.charCodeAt(j), 8);
binContainer = nbCar==1?binContainer+"0000":nbCar==2?binContainer+"00":binContainer;
for (var k=0;k<binContainer.length;k = k + 6)
base64String += binEncode[binContainer.substr(k,6)];
}
base64String = nbCar==1?base64String+"==":nbCar==2?base64String+"=":base64String;
return base64String;
}
function binBits(nbr, lng) {
var bin = nbr.toString(2);
return "00000000".substr(0, lng - bin.length ) + bin;
}


function jrxc() {
var futdate = new Date()
return futdate.getTime()
}

function getEle(el)
{
   var div=document.getElementById(el)
   if (!div) return false
   return div
}

function isValid(a) {
   var div=getEle(a)
   if (!div) return false
   var a = div.value.replace(/^\s*|\s*$/g, "")
   div.value = a
   if ((a == '') || (a < 0))  return false
   return true
}

function isInt(a) {
	var div=getEle(a)
    if (!div) return false
	var re1=/[0-9]/;
	var result1=re1.test(div.value);
	var re2=/^[0-9]/;
	var result2=re2.test(div.value);
	return result1 && result2
}

function clickEle(el)
{
	var a = getEle(el);
	if (a) a.click();
}

function onclickEle(el)
{
	var a = getEle(el);
	if (a) a.onclick();
}


function produktADD(pro,txt) {
  	loadXMLDoc(root_url+'shop_produkt_add.php','jrc='+jrxc()+'&pro='+pro+'&txt='+txt);
}

function produkt_listADD(param,id)
{
	if(document.cookie.length == 0) {
		WebAlert('Nemůžete nakupovat, protože máte vypnuté cookies!<br />Více naleznete <a href="'+root_url+'vypnute-cookies.html">zde</a>.');
		return;
	}

var e = 'ks'+id
var el = getEle(e)

if (!el)
{
	produktADD(param+'|1','');
	return;
}

if (isValid(e))
if (isInt(e))
{
if (el.value > 0) {
produktADD(param+'|'+el.value,'')
//el.value=''
return
}
el.focus()
}
}

var xmlhttp
function loadXMLDoc(url,params)
{
xmlhttp=null
// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest()
  }
// code for IE
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
if (xmlhttp!=null)
  {
  xmlhttp.onreadystatechange=state_Change
  xmlhttp.open("POST",root_url+'shop_produkt_add.php',true)

  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.setRequestHeader("Content-length", params.length);

//  xmlhttp.setRequestHeader('Content-type', 'text/plain;charset=UTF-8')
  xmlhttp.send(params)
  } else {
  alert("Your browser does not support XMLHTTP.")
  }
}
function state_Change()
{
if (xmlhttp.readyState==4)
  {
  if (xmlhttp.status==200)
    {
	var s = xmlhttp.responseText
    var a = s.split('|')
	var ff = document.getElementById('id_kus')
	if (ff) ff.innerHTML = a[0]
	ff = document.getElementById('id_celkemDPH')
	if (ff) ff.innerHTML = a[1]
	ff = document.getElementById('id_celkem')
	if (ff) ff.innerHTML = a[2]
		window.location.href = window.location.href;
		//WebAlert('Produkt byl přidán do košíku.'+'<div class="buttony"><a class="continue" href="javascript:WebAlertClose();" title="Pokračovat v nákupu"></a><a href="'+root_url+'shop/kosik/" class="cart_entry" title="Vstoupit do košíku"></a><div class="clear"></div></div>',5000);
		
    }
  else
    {
	 alert("Problem retrieving XML data")
    }
  }
}

function getRequestParameter(name,link)
{
	var s = link;
	if ((link == undefined) || (link == '')) 
	{		
		s = shopquerystring;
		s = s.replace(/&amp;/g, "&");
	} 
	//s = document.location.href;

	var i = s.indexOf('&'+name+'=')
	if (i == -1) i = s.indexOf('?'+name+'=')
	if (i != -1)
	{
		i += name.length+2
		var b = s.indexOf('&',i)
		if (b == -1) b = s.length
		return unescape(s.substring(i,b))
	}
	return ""
}


function replaceRequestParameter(name,param,link)
{
	var s = link;
	if ((link == undefined) || (link == '')) 
	{
		s = shopquerystring;
		s = s.replace(/&amp;/g, "&");
//		s = document.location.href;
	}

	var sh = '&'+name+'='
	var i = s.indexOf(sh)

    if (i == -1)
    {
		sh = '?'+name+'='
		var i = s.indexOf(sh)
	}

	if (i != -1)
	{
		var b = s.indexOf('&',i+1)
		if (b == -1) b = s.length
		var s2 = s.substring(i,b)
		s = s.replace(s2,sh+param)
	} else
	{
		if (s.indexOf('?') == -1) s += '?'+name+'='+param
		else s += '&'+name+'='+param
	}
	return s
}


function itemLoc(link)
{
	var sx = getRequestParameter('param4',link)
	if (sx == '') {
		sx = getRequestParameter('param4')
		if (sx != '') {
		    var a = sx.split(',')
		    a[4] = ''
		    sx = '';
		    for (var x = 0;x < a.length;x++) {
		    	if (sx != '') sx = sx+','+a[x]
		    	else sx = a[x]
		    }

            var i = link.indexOf('&');
            if (i == -1)
			{
				i = link.indexOf('?');
				if (i == -1)
				{
					document.location.href = link+'?param4='+sx;
				}
			}
			document.location.href = link+'&param4='+sx;
			return
		}
	}
	document.location.href = link
}


function itemHrefLoc(item)
{
	var link = item.href
	var sx = getRequestParameter('param4',link)
	if (sx == '') {
		sx = getRequestParameter('param4')
		if (sx != '') {
            var i = link.indexOf('&');
            if (i == -1)
			{
				i = link.indexOf('?');
				if (i == -1)
					{
					   item.href = link+'?param4='+sx;
					   return
					}
			}
			item.href = link+'&param4='+sx;
			return
		}
	}
	item.href = link
}

function product_buy_hide()
{
	if (buy_bx != "") {
		var div = getEle('frm_buy_shadow')
		div.className = 'buy_window_hide'

		div = getEle(buy_bx)
		div.className = 'buy_window_hide'
		div.innerHTML=""

	}
}


function product_frm_buy()
{
	var ret = isValid('buyidcount') && isInt('buyidcount')
	var pocet = getEle('buyidcount').value
	var popis = getEle('buyidtxt').value
	if (ret && pocet > 0) {
		product_buy_hide()
		produktADD(buy_code+"|"+pocet,popis)
	}
	else {
		var div = getEle('buyidcount')
		div.className = 'product_buy_insert_err'
	}
   return ret
}


function product_buyKey(field,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;
if (keycode == 13)
{
	if (field.id == "buyidcount")
	{
		getEle('buyidtxt').focus()
	}
	else {
		getEle('buyidbtn').click()
	}
	return false
}
return true
}

function product_buy(divname)
{
	if(document.cookie.length == 0) {
		WebAlert('Nemůžete nakupovat, protože máte vypnuté cookies!<br />Více naleznete <a href="'+root_url+'vypnute-cookies.html">zde</a>.');
		return;
	}
	
	product_buy_hide()
	var div = getEle(divname)
	if (!div) return
	buy_bx = divname
	/* Změna START: Zajistí přidání 1ks produktu do košíku */
	buy_code=div.getAttribute('name')
	produktADD(buy_code+"|1","");
	return;
	/* Změna END */
	buy_code=div.getAttribute('name')
	div.innerHTML = "<div class=\"product_buy_holder\"><div class=\"product_buy_n\" OnClick=\"product_buy_hide()\">Přidat do košíku</div><div class=\"product_buy_d\">Požadovaný počet:<input id=\"buyidcount\" type=\"text\" maxlength=\"5\"class=\"product_buy_insert\" onKeyPress=\"return product_buyKey(this,event)\"/>&nbsp;ks<br><div class=\"product_buy_txt\">Poznámka ke zboží (barva, velikost ...)</div><input type=\"text\" id=\"buyidtxt\" maxlength=\"50\"class=\"product_buy_info\" onKeyPress=\"return product_buyKey(this,event)\"/></div><div class=\"product_buy_ok\"><input type=\"image\" id=\"buyidbtn\" src=\"images/shop/btn_ok.gif\" OnClick = \"return product_frm_buy()\"/></div></div>"
	div.className = 'buy_window_show'
	getEle('buyidcount').focus()
	setCenterMinY(div,306,0,0)

	var div2 = getEle('frm_buy_shadow')
	div2.className = 'product_buy_shadow'
	setCenterMinY(div2,306,6,6)
}






function number_format( number, decimals, dec_point, thousands_sep ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // +    revised by: Luke Smith (http://lucassmith.name)
    // +     bugfix by: Diogo Resende
    // +     bugfix by: Rival
    // +     input by: Kheang Hok Chin (http://www.distantia.ca/)
    // +     improved by: davook
    // +     improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: number_format(1234.56);
    // *     returns 1: '1,235'
    // *     example 2: number_format(1234.56, 2, ',', ' ');
    // *     returns 2: '1 234,56'
    // *     example 3: number_format(1234.5678, 2, '.', '');
    // *     returns 3: '1234.57'
    // *     example 4: number_format(67, 2, ',', '.');
    // *     returns 4: '67,00'
    // *     example 5: number_format(1000);
    // *     returns 5: '1,000'
    // *     example 6: number_format(67.311, 2);
    // *     returns 6: '67.31'
    // *     example 7: number_format(1000.55, 1);
    // *     returns 7: '1,000.6'
    // *     example 8: number_format(67000, 5, ',', '.');
    // *     returns 8: '67.000,00000'
    // *     example 9: number_format(0.9, 0);
    // *     returns 9: '1'
    var n = number, prec = decimals;
    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };
 
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
 
    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
 
    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;
 
    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;
 
        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }
    if (s.indexOf(dec) === -1 && prec > 1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}

