var cart_product_added = "Товар добавлен в корзину";

function buy_product(item, quantity)
{
 show($("cart_link"));
 show($("mini_cart"));
 if (!quantity || quantity < 1) quantity = 1;
 load_url("cart.php?action=add&item=" + item + "&quantity=" + quantity + "&show=price", "", $("mini_cart"));
 alert(cart_product_added);
}

function format_price(x)
{
 x = x.toString().split(".");
 if (x.length < 2) x[1] = "00"; else
 if (x[1].length < 2) x[1] += "0";
 return x.join(".");
}

function update_quantity(item)
{
 var price = parseFloat($("cart_price_" + item).innerHTML), quantity = parseInt($("cart_quantity_" + item).value);
 load_url("cart.php?action=update&item=" + item + "&quantity=" + quantity + "&show=totals", "", $("cart_totals"), function (request) {
  if (!isNaN(price) && !isNaN(quantity) && (quantity > 0))
   $("cart_total_" + item).innerHTML = format_price(Math.round(price * quantity * 100) / 100);
  else
  {
   $("cart_quantity_" + item).value = 1;
   if (!isNaN(price)) $("cart_total_" + item).innerHTML = format_price(price);
  };
  load_url("cart.php?show=price", "", $("mini_cart"));
 });
}

function delete_table_row(element)
{
 element.parentNode.parentNode.tBodies[0].deleteRow(element.parentNode.parentNode.sectionRowIndex);
}

function remove_from_cart(item)
{
 load_url("cart.php?action=delete&item=" + item + "&show=totals", "", $("cart_totals"), function(request) {
  var element = $("cart_item_" + item); element.parentNode.removeChild(element);
  load_url("cart.php?show=price", "", $("mini_cart"));
 });
}

function empty_cart()
{
 load_url("cart.php?action=empty&show=full", "", $("cart_container"), function(request) {
  load_url("cart.php?&show=totals", "", $("cart_totals"));
  load_url("cart.php?show=price", "", $("mini_cart"));
 });
}

function buy(form)
{
 load_url("cart.php?action=buy", serialize_form(form), $("cart_container"), function(request) {
  load_url("cart.php?&show=totals", "", $("cart_totals"));
  load_url("cart.php?show=price", "", $("mini_cart"));
 });
}