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) { 
	/*
	var type = getCookie("pt");
	var count = getCookie("pc");
	var arr = pro.split('|');
	if(type == "") {
		type = "";
		count = arr[3];
	}
	else {
		
	}
	if(parseFloat(arr[1]) < 0) {
		if(type == "") {
			setCookie("pt","1");
		}
		else {
			if(type == "0") {
				return alert("V košíku máte výkupní materiál! Nyní lze přidat jen tento druh materiálu. Pokud chcete nakupovat klasické zboží, musíte nejprve vymazat obsah košíku.");
			}
		}
	}
	else {
		if(type == "") {
			setCookie("pt","0");
		}
		else {
			if(type == "1") {
				return alert("Do košíku lze přidat jen zboží s kladnou cenou!");
			}
		}
	}
	*/
    loadXMLDoc(root_url+'shop_produkt_add.php','jrc='+jrxc()+'&pro='+pro+'&txt='+txt);
}

function produkt_listADD(param,id)
{
var e = 'ks'+id
var el = getEle(e)
if (isValid(e))
if (isInt(e))
{
if (el.value > 0) {
produktADD(param+'|'+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)
    {
		try {
			eval("var responce = "+xmlhttp.responseText+";");
			
			var ff = document.getElementById('id_kus')
			if (ff) ff.innerHTML = responce.count;
			ff = document.getElementById('id_celkemDPH')
			if (ff) ff.innerHTML = responce.pricevat;
			ff = document.getElementById('id_celkem')
			if (ff) ff.innerHTML = responce.price;

			alert(responce.message)
		}
		catch(e) { }
    }
  else
    {
	 alert("Problem retrieving XML data")
    }
  }
}

function getRequestParameter(name,link)
{
	var s = link;
	if ((link == undefined) || (link == '')) 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 = 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 != "") {		
		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('buyidbtn').click();
    //getEle('buyidtxt').focus()
	}
	else {
		//getEle('buyidbtn').click()
	}
	return false
}
return true
}

function product_buy(divname)
{
	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 */
	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\" value=\"1\" 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=\"shop_images/common/btn_ok.gif\" OnClick = \"return product_frm_buy()\"/></div></div>"
	div.className = 'buy_window_show'
	getEle('buyidcount').focus();
	getEle('buyidcount').select()
	setCenterMinY(div,306,0,0)
}

function setMenu(type)
{

   sendCommand("CmdSetTypeMenu",type);
}

// http://www.w3schools.com/JS/js_cookies.asp
function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}
// http://www.w3schools.com/JS/js_cookies.asp
function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

