    var color1 = "FF0000";
    var color2 = "666666";
    
    function addProduct(id,quantity,label,formx,minOrder)
    {
        if(!quantity.match(/^[0-9]+$/))
        {
            document.getElementById("addProductData").style.display = "none";
       //     document.getElementById("addProductData2").style.display = "none";
            return false;
        }
        if(quantity<=0)
        {
            document.getElementById("addProductData").style.display = "none";
      //      document.getElementById("addProductData2").style.display = "none";
            return false;
        }
        if (quantity<minOrder)
        {
            document.getElementById("addProductData").style.display = "none";
            window.alert('Promiňte, ale nelze objednat toto zboží v menším množství nežli '+minOrder+' ks, děkujeme.');
            formx.quantity.value = minOrder;
            return false;
	}
        serverUpdateProduct(0,quantity,id,true);
        document.getElementById("addProductData").style.display = "block";
      //  document.getElementById("addProductData2").style.display = "block";
        document.getElementById("addProductData").innerHTML = "Do košíku bylo vloženo <strong>"+quantity+"ks</strong><br />\n"+label;
      //  document.getElementById("addProductData2").innerHTML = "Do košíku bylo vloženo <strong>"+quantity+"ks</strong><br />\n"+label; 
        return false;
    }

    function deleteProduct(id, price, tax, phe)
    {
        if(quantityX[id]>0)
        {
            fader("price1",document.getElementById("priceTotal1"),color1,color2);
            fader("price2",document.getElementById("priceTotal2"),color1,color2);
            if(phe!=0)
            {
                if(document.getElementById("pricePHErow"))
                {
                    fader("pricePHE",document.getElementById("pricePHE"),color1,color2);
                }
            }
        }
        document.getElementById("productRow"+id).style.display = "none";
        document.getElementById("productHidden1"+id).name = "Hidden1"+id;
        document.getElementById("productHidden2"+id).name = "Hidden2"+id;
        priceTotal  -= quantityX[id]*(price+phe);
        priceTotal2 -= Math.round((quantityX[id]*(price)*(1+tax/100)+quantityX[id]*phe*(1+tax/100))*100)/100;
        pricePHE    -= Math.round(quantityX[id]*phe*(1+tax/100)*100)/100;
        quantityX[id] = 0;
        serverUpdateDeleteProduct(id);
        if(--productCount==0)
        {
            document.getElementById("globalForm").style.display = "none";
            document.getElementById("tableEmpty").style.display = "block";
        }
        checkDeliveryAll();
        return false;
    }

    function checkProductBlur(id,obj,minOrder)
    {
        quantity = obj.value;
        if(!obj.value.match(/^[0-9]+$/))
        {
            window.alert('Vámi zadaný počet ks není rozpoznatelné číslo, počet ks položky se nastaví na předchozí hodnotu.');
            obj.value = quantityX[id];
            return;
        }
        if(quantity < minOrder)
        {
            window.alert('Promiňte, ale nelze objednat toto zboží v menším množství nežli '+minOrder+' ks, děkujeme.');
            obj.value = quantityX[id];
            return;
        }

    }

    function updateProduct(id,obj,price,tax,product_id,phe,minOrder)
    {
        if(obj.value=="")
        {
            quantity = 0;
            serverUpdateProduct(id, quantity, product_id, "false");
            priceTotal  -= quantityX[id]*(price+phe);
            priceTotal2 -= Math.round((quantityX[id]*(price)*(1+tax/100)+quantityX[id]*phe*(1+tax/100))*100)/100;
            pricePHE    -= Math.round(quantityX[id]*phe*(1+tax/100)*100)/100;
            quantityX[id] = 0;
            document.getElementById("productPrice"+id).innerHTML = "-----";
            updatePrice();
            checkDeliveryAll();
            return;
        }
        if(!obj.value.match(/^[0-9]+$/))
        {
            return;
        }
        quantity = obj.value;
        if(quantity == quantityX[id])
        {
            return;
        }
        if(quantity < minOrder)
        {
            return;
        }
        if(phe>0)
        {
            if(document.getElementById("pricePHErow"))
            {
                fader("pricePHE",document.getElementById("pricePHE"),color1,color2);
            }
        }
        fader("price1",document.getElementById("priceTotal1"),color1,color2);
        fader("price2",document.getElementById("priceTotal2"),color1,color2);
        serverUpdateProduct(id, quantity, product_id, "false");
        priceTotal   -= quantityX[id]*(price+phe);
        priceTotal2  -= Math.round((quantityX[id]*(price)*(1+tax/100)+quantityX[id]*phe*(1+tax/100))*100)/100;
        pricePHE     -= Math.round(quantityX[id]*phe*(1+tax/100)*100)/100;
        priceTotal   += quantity*(price+phe);
        priceTotal2  += Math.round((quantity*(price)*(1+tax/100)+quantity*phe*(1+tax/100))*100)/100;
        pricePHE     += Math.round(quantity*phe*(1+tax/100)*100)/100;
        pricen        = quantity*price;
        quantityX[id] = quantity;
        if(pricen == 0)
        {
            document.getElementById("productPrice"+id).innerHTML = "-----";
        }
        else
        {
            fader("productPrice"+id,document.getElementById("productPrice"+id),color1,color2);
            document.getElementById("productPrice"+id).innerHTML = niceFormat(Math.round(pricen*(1+tax/100)*100)/100);
        }
        checkDeliveryAll();
    }

    function checkDelivery(type, id)
    {
        serverUpdateDelivery(type, id);
        deliveryChange(type, id);
    }

    function checkDeliveryAll()
    {
        for(x=0; x<deliveryDataType.length; x++)
        {
            deliveryChange(deliveryDataType[x], -1);
        }
    }

    function deliveryChange(type, id)
    {
        if(id == -1)
        {
            id = deliveryData[type][0][0];
        }
        if(id == 0)
        {
            updateDelivery(type,id,0,"","-----",0);
            return;
        }
        d = deliveryData[type][1][id];
        if(d[1].length == 1)
        {
            updateDelivery(type,id,0,"","-----",0);
            return;
        }
        for(i=0; i<d[1].length; i++)
        {
            if(d[1][i][0]>(priceTotal2-deliveryTotal-pricePHE))
            {
                specialPrice = d[1][i][1];
                break;
            }
        }
        if(i==d[1].length)
        {
            updateDelivery(type,id,0,"","zdarma",0,0);
        }
        else
        {
            updateDelivery(type,id,specialPrice,(d[0]==0?"":d[0]+"%"),niceFormat(specialPrice*(1+d[0]/100)),d[0]);
        }
    }

    function updatePrice()
    {
        if(document.getElementById("pricePHErow"))
        {
            if(pricePHE == 0)
            {
                document.getElementById("pricePHErow").style.visibility = "hidden";
            }
            else
            {
                document.getElementById("pricePHErow").style.visibility = "visible";
            }
        }
        if(Math.round(priceTotal) == 0 || Math.round(priceTotal2) == 0)
        {
            document.getElementById("priceTotal1").innerHTML = document.getElementById("priceTotal2").innerHTML = "-----";
            return;
        }
        if(document.getElementById("pricePHErow"))
        {
            document.getElementById("pricePHE").innerHTML    = niceFormat(Math.round(pricePHE*100)/100);
        }
        document.getElementById("priceTotal1").innerHTML = niceFormat(Math.round(priceTotal));
        document.getElementById("priceTotal2").innerHTML = niceFormat(Math.round(priceTotal2));
    }

    function updateDelivery(type, id, price, percent, label, tax)
    {
            deliveryPrice = deliveryData[type][0][1];
            deliveryPrice2 = deliveryPrice*(1+deliveryData[type][0][2]/100);
            if(deliveryPrice2 != price*(1+tax/100))
            {
                fader("deliveryLabelTax"+type,document.getElementById("deliveryLabelTax"+type),color1,color2);
                fader("deliveryLabelPrice"+type,document.getElementById("deliveryLabelPrice"+type),color1,color2);
                fader("price1",document.getElementById("priceTotal1"),color1,color2);
                fader("price2",document.getElementById("priceTotal2"),color1,color2);
            }
            priceTotal  -= deliveryPrice;
            priceTotal2 -= deliveryPrice2;
            deliveryTotal -= deliveryPrice2;
            priceTotal  += price;
            priceTotal2 += price*(1+tax/100);
            deliveryTotal += price*(1+tax/100);
            deliveryPrice  = price;
            deliveryPrice2 = price*(1+tax/100);
            document.getElementById("deliveryLabelTax"+type).innerHTML = percent;
            document.getElementById("deliveryLabelPrice"+type).innerHTML = label;
            deliveryData[type][0][0] = id;
            deliveryData[type][0][1] = price;
            deliveryData[type][0][2] = tax;
            updatePrice();
            //alert("deliveryTotal="+deliveryTotal+", priceTotal="+priceTotal+", priceTotal2="+priceTotal2)
    }

    function CurrencyFormatted(amount)
    {
       	var i = parseFloat(amount);
       	if(isNaN(i))
        {
            i = 0.00;
        }
       	var minus = '';
       	if(i < 0)
        {
            minus = '-';
        }
       	i = Math.abs(i);
       	i = parseInt((i + .005) * 100);
       	i = i / 100;
       	s = new String(i);
       	if(s.indexOf('.') < 0)
        {
            s += '.00';
        }
       	if(s.indexOf('.') == (s.length - 2))
        {
            s += '0';
        }
       	s = minus + s;
       	return s;
    }
    
    function niceFormat(amount)
    {
        amount = CurrencyFormatted(amount);
        var delimiter = " "; // replace comma if desired
        var a = amount.split('.',2);
        var d = a[1];
        var i = parseInt(a[0]);
        if(isNaN(i))
        {
            return '';
        }
        var minus = '';
        if(i < 0)
        {
            minus = '-';
        }
        i = Math.abs(i);
        var n = new String(i);
        var a = [];
        while(n.length > 3)
        {
            var nn = n.substr(n.length-3);
            a.unshift(nn);
            n = n.substr(0,n.length-3);
        }
        if(n.length > 0)
        {
            a.unshift(n);
        }
        n = a.join(delimiter);
        if(d.length < 1)
        {
            amount = n;
        }
        else
        {
            amount = n + ',' + d;
        }
        amount = minus + amount;
        return amount;
    }

