

var cart = {

    add: function (el, product_id, product_version) {
        var qtd_input = app.get('cart_quantity_' + product_id + '_' + product_version);
        var product_quantity = qtd_input.value;
        if (isNaN(product_quantity)) {
            qtd_input.value = 0;
            return alert('Please enter a number!');
        } else if (product_quantity == 0) {
            qtd_input.value = 1;
            return alert('Please enter a product quantity!');
        }
        var info = 'cart_response_' + product_id + '_' + product_version;
        $('#' + info).fadeTo(0, 1); // jQuery
        el.disabled = true;
        var res = app.get(info);
        res.style.display = 'block';
        res.style.marginLeft = '10px';
        res.innerHTML = 'Wait a moment...';
        var ret = ajax.exec('POST', '/application/api/cart.add.php', 'product_id=' + product_id + '&product_version=' + product_version + '&qtd=' + product_quantity);
        qtd_input.value = 1;
        res.innerHTML = '<b>This product was added to <a href="/cart/">your shopping cart</a></b>';
        $('#' + info).fadeTo(5000, 0.33); // jQuery

        var msg = app.get('cart_message');
        msg.innerHTML = ret;
        el.disabled = false;
    },
    add_deposit: function (el, product_id, product_deposit) {
        var qtd_input = app.get('cart_quantity_' + product_id + '_' + product_deposit);
        var product_quantity = qtd_input.value;
        if (isNaN(product_quantity)) {
            qtd_input.value = 0;
            return alert('Please enter a number!');
        } else if (product_quantity == 0) {
            qtd_input.value = 1;
            return alert('Please enter a product quantity!');
        }
        var info = 'cart_response_' + product_id + '_' + product_deposit;
        $('#' + info).fadeTo(0, 1); // jQuery
        el.disabled = true;
        var res = app.get(info);
        res.style.display = 'block';
        res.style.marginLeft = '10px';
        res.innerHTML = 'Wait a moment...';
        var ret = ajax.exec('POST', '/application/api/cart.add.deposit.php', 'product_id=' + product_id + '&product_deposit=' + product_deposit + '&qtd=' + product_quantity);
        qtd_input.value = 1;
        res.innerHTML = '<b>Added to <a href="/cart/">shopping cart</a></b>';
        $('#' + info).fadeTo(5000, 0.33); // jQuery

        var msg = app.get('cart_message');
        msg.innerHTML = ret;
        el.disabled = false;
    },
    empty: function() {
        var c = confirm('Remove all items from your shopping cart?');
        if (!c) return false;
        var form = app.get('Form1');
        var qtds = form.getElementsByTagName('input');
        for (var i = 0; i < qtds.length; i++) {
            var qtd = qtds[i];
            if (qtd.type == 'text') {
                if (qtd.name.substr(0, 3) == 'qtd') {
                    qtd.value = 0;
                }
            }
        }
        form.submit();
        return false;
    },
    remove: function(product_id, product_version) {
        var c = confirm('Remove this item from your shopping cart?');
        if (!c) return false;
        var form = app.get('Form1');
        var qtds = form.getElementsByTagName('input');
        for (var i = 0; i < qtds.length; i++) {
            var qtd = qtds[i];
            if (qtd.name == 'qtd[' + product_id + '][' + product_version + ']') {
                qtd.value = 0;
            }
        }
        form.submit();
        return false;
    }
}

