function getAmountField(productId)  {
  return $("amount_" + productId);
}

function increaseAmount(productId)  {
  var field = getAmountField(productId);
  field.value = field.value * 1 + 1;
}

function decreaseAmount(productId)  {
  var field = getAmountField(productId);
  if (field.value > 1)  {
    field.value = field.value * 1 - 1;
  }
}

function addToBasket(productId)  {
  if (getAmountField(productId).value > 0)  {
    var products = getProductsFromCookie();
    products[productId] = getAmountField(productId).value;
    updateCookie(products);
    $("add_" + productId).style.display = 'none';
    $("remove_" + productId).style.display = 'inline';
  }
}

function removeFromBasket(productId)  {
  var products = getProductsFromCookie();
  delete products[productId];
  updateCookie(products);
  $("add_" + productId).style.display = 'inline';
  $("remove_" + productId).style.display = 'none';
}

function getProductsFromCookie()  {
  var cookie = document.cookie.split(";");
  var basket = cookie.filter(function (elem)  {return elem.startsWith("basket=")});
  return basket.length > 0 ? decodeURIComponent(basket[0].split("=")[1]).evalJSON() : {};
}

function updateCookie(products)  {
  var date = new Date();
  date.setDate(date.getDate() + 1);
  document.cookie = "basket=" + encodeURIComponent(Object.toJSON(products)) + "; expires=" + date.toUTCString();
}

function moveLeftMenu()  {
  $("leftMenu").style.top = document.documentElement.scrollTop + 10 + "px";
}

function checkInputs()  {
  var notFilled = $("mainForm").getInputs().filter(
    function (e)  {
      return !e.value;
    }
  );
  if (notFilled.size() > 0)  {
    alert(formNotFilledLabel);
    return false;
  }
}

Event.observe(window, "scroll", moveLeftMenu);
