//m.i.

if (navigator && navigator.cookieEnabled == false) {
    document.write(no_cookies_message)
}
function addListToCart(shop_id) {
    if (!isNaN(shop_id) && document.f.elements['product_ids[]']) {
        var but = document.getElementById('submit_button');
        if (but) {
            but.disabled = true
        }
        var total = readCookie('CART_TOTAL_' + shop_id);
        var cart = unescape(readCookie('CART_' + shop_id));
        var cart_split = cart.split(';');
        var cart_item_split;
        var cart_hash = {};
        var found = false;
        for (var i = 0; i < cart_split.length; i++) {
            cart_item_split = cart_split[i].split("=");
            cart_hash[cart_item_split[0]] = cart_item_split[1]
        }
        cart = "";
        if (document.f.elements['product_ids[]']) {
            if (document.f.elements['product_ids[]'].nodeName != "INPUT") {
                for (var i = 0; i < document.f.elements['product_ids[]'].length; i++) {
                    if (document.f.elements['product_ids[]'][i]) {
                        var product_id = document.f.elements['product_ids[]'][i].value;
                        var price = document.f.elements['prices[]'][i].value;
                        var amount = document.f.elements['amounts[]'][i].value;
                        if (amount != "" && amount > 0) {
                            total = Number(total) + Number(price * amount);
                            if (cart_hash[product_id]) {
                                cart_hash[product_id] = Number(cart_hash[product_id]) + Number(amount)
                            } else {
                                cart_hash[product_id] = Number(amount)
                            }
                        }
                    }
                }
            } else {
                var product_id = document.f.elements['product_ids[]'].value;
                var price = document.f.elements['prices[]'].value;
                var amount = document.f.elements['amounts[]'].value;
                if (amount != "" && amount > 0) {
                    total = Number(total) + Number(price * amount);
                    if (cart_hash[product_id]) {
                        cart_hash[product_id] = Number(cart_hash[product_id]) + Number(amount)
                    } else {
                        cart_hash[product_id] = Number(amount)
                    }
                }
            }
        }
        cart = "";
        for (var i in cart_hash) {
            if (cart_hash[i]) {
                if (cart != "") cart = cart + ";";
                cart = cart + i + "=" + cart_hash[i]
            }
        }
        createCookie('CART_' + shop_id, cart, 10);
        createCookie('CART_TOTAL_' + shop_id, total, 10);
        if (document.getElementById('cart_total')) document.getElementById('cart_total').innerHTML = total;
        resetOrderList();
        return true
    }
    return false
}
function addToCart(shop_id, product_id, price, amount) {
    product_id = Number(product_id);
    if (!isNaN(shop_id) && (shop_id > 0) && !isNaN(product_id) && (product_id > 0) && !isNaN(price) && !isNaN(amount) && (amount > 0)) {
        var total = readCookie('CART_TOTAL_' + shop_id);
        var cart = unescape(readCookie('CART_' + shop_id));
        var cart_split = cart.split(';');
        var cart_new = "";
        var cart_item_split;
        var found = false;
        if (total == "") total = 0;
        for (var i = 0; i < cart_split.length; i++) {
            cart_item_split = cart_split[i].split("=");
            if (cart_item_split.length == 2) {
                if (!found && cart_item_split[0] == product_id) {
                    total = Number(total) + Number(price * amount);
                    var total_amount = Number(amount) + Number(cart_item_split[1]);
                    found = true;
                    if (cart_new != "") cart_new = cart_new + ";";
                    cart_new = cart_new + product_id + "=" + total_amount
                } else {
                    if (cart_new != "") cart_new = cart_new + ";";
                    cart_new = cart_new + cart_item_split[0] + "=" + cart_item_split[1]
                }
            }
        }
        if (!found) {
            if (cart_new != "") cart_new = cart_new + ";";
            cart_new = cart_new + product_id + "=" + amount;
            total = Number(total) + Number(price * amount)
        }
        createCookie('CART_' + shop_id, cart_new, 10);
        createCookie('CART_TOTAL_' + shop_id, total, 10);
        if (document.getElementById('cart_total')) document.getElementById('cart_total').innerHTML = total;
        return true
    }
    return false
}
function getParent(obj, tagName) {
    if (obj) {
        var par = obj.parentNode;
        while (par && (par.nodeName != tagName)) {
            par = par.parentNode
        }
        return par
    }
    return null
}
function deleteRaw(o) {
    v = getParent(o, "TR");
    if (v) {
        v.parentNode.removeChild(v)
    }
}
var dot = true;
var ttt = "2.23";
if (isNaN(ttt)) {
    dot = false
}
function getEventTarget(e) {
    if (!e) e = window.event;
    if (e.target) {
        if (e.target.nodeType == 3) e.target = e.target.parentNode;
        return e.target
    } else if (e.srcElement) return e.srcElement
}
function inputOnlyRealNumber(obj, e) {
    var target = getEventTarget(e);
    if (target && target.nodeName == "INPUT" && target.type == "text") {
        var valueBefore = target.value;
        var value = "";
        if (dot) {
            value = valueBefore.replace(",", ".")
        } else {
            value = valueBefore.replace(".", ",")
        }
        value = value.replace(/[^\d\.,]+/, "");
        if (value.length > 1) value = value.replace(/[0]*(\d*[\.,]?\d*).*/, "$1");
        if (value != valueBefore) {
            target.value = value
        }
        if (value != "" && valueBefore == value) {
            return true
        }
    }
    return false
}
function recountCart(shop_id) {
    var total = 0;
    var cart = "";
    if (document.f.elements['product_ids[]']) {
        if (document.f.elements['product_ids[]'].nodeName != "INPUT") {
            for (var i = 0; i < document.f.elements['product_ids[]'].length; i++) {
                if (document.f.elements['product_ids[]'][i]) {
                    var product_id = document.f.elements['product_ids[]'][i].value;
                    var price = document.f.elements['prices[]'][i].value;
                    var amount = document.f.elements['amounts[]'][i].value;
                    total = total + price * amount;
                    if (amount > 0) {
                        if (cart != "") cart = cart + ";";
                        cart = cart + product_id + "=" + amount
                    }
                    document.getElementById('res_' + product_id).innerHTML = (price * amount)
                }
            }
        } else {
            var product_id = document.f.elements['product_ids[]'].value;
            var price = document.f.elements['prices[]'].value;
            var amount = document.f.elements['amounts[]'].value;
            total = total + price * amount;
            cart = product_id + "=" + amount;
            document.getElementById('res_' + product_id).innerHTML = (price * amount)
        }
    } else {
        if (document.getElementById('cart_div')) document.getElementById('cart_div').innerHTML = ""
    }
    createCookie('CART_' + shop_id, cart, 10);
    createCookie('CART_TOTAL_' + shop_id, total, 10);
    if (document.getElementById('total')) document.getElementById('total').innerHTML = total.toFixed(2);
    if (document.getElementById('cart_total')) document.getElementById('cart_total').innerHTML = total.toFixed(2);
}
function resetOrderList() {
    var but = document.getElementById('submit_button');
    if (but) {
        but.disabled = true
    }
    if (document.f.elements['amounts[]']) {
        if (document.f.elements['amounts[]'].nodeName != "INPUT") {

            for (var i = 0; i < document.f.elements['amounts[]'].length; i++) {
                if (document.f.elements['amounts[]'][i]) {
                    document.f.elements['amounts[]'][i].value = 0
                }
            }
        } else {
            document.f.elements['amounts[]'].value = 0
        }
    }
    var total_span = document.getElementById('total');
    if (total_span) total_span.innerHTML = 0
}
function recountOrderList() {
    var total = 0;
    if (document.f.elements['product_ids[]']) {
        if (document.f.elements['product_ids[]'].nodeName != "INPUT") {
            for (var i = 0; i < document.f.elements['product_ids[]'].length; i++) {
                if (document.f.elements['product_ids[]'][i]) {
                    var product_id = document.f.elements['product_ids[]'][i].value;
                    var price = document.f.elements['prices[]'][i].value;
                    var amount = document.f.elements['amounts[]'][i].value;
                    total = total + price * amount;
                    document.getElementById('res_' + product_id).innerHTML = (price * amount)
                }
            }
        } else {
            var product_id = document.f.elements['product_ids[]'].value;
            var price = document.f.elements['prices[]'].value;
            var amount = document.f.elements['amounts[]'].value;
            total = total + price * amount;
            document.getElementById('res_' + product_id).innerHTML = (price * amount)
        }
    }
    var total_span = document.getElementById('total');
    if (isNaN(total) || total == 0 || total == '') total = 0;
    if (total_span) total_span.innerHTML = total.toFixed(2);
    var but = document.getElementById('submit_button');
    if (but) {
        if (total == 0) but.disabled = true;
        else but.disabled = false
    }
}
function findPos(obj) {
    var result = {};
    result.x = 0;
    result.y = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            result.y += obj.offsetTop;
            result.x += obj.offsetLeft;
            obj = obj.offsetParent
        }
    } else {
        if (obj.x) result.x += obj.x;
        if (obj.y) result.y += obj.y
    }
    return result
}
function emptyInputBlur(obj, e) {
    var target = getEventTarget(e);
    if (target && target.nodeName == "INPUT" && target.type == "text") {
        if (target.value == "") target.value = 0;
        return true
    }
    return false
}
function showAddMessage(obj) {
    var pos = findPos(obj);
    var d = document.getElementById("shop-added");
    if (d) {
        d = d.cloneNode(true);
        d.style.display = 'block';
        d.style.left = (pos.x + 10) + 'px';
        d.style.top = (pos.y + obj.offsetHeight - d.offsetHeight) + 'px';
        document.body.appendChild(d);
        d.style.top = (parseInt(d.style.top) - d.offsetHeight - 10) + 'px';
        window.setTimeout(function() {
            if (d && d.parentNode) d.parentNode.removeChild(d);
            delete d
        },
        500)
    }
}
function addList(f, shop_id, func) {
    if (addListToCart(shop_id)) {
        if (func) func(f);
        else showAddMessage(f)
    }
    return false
}
function addProductForm(shop_id, product_id, product_price, f, func) {
    if (addToCart(shop_id, product_id, product_price, f.product_amount.value)) {
        if (func) func(f);
        else showAddMessage(f)
    }
    f.product_amount.value = "1";
    return false
}