window.onload = function()
{
	document.forms[0].onkeydown = function (){setTimeout(formCount, 10)};
}

function formCount()
{
	var form = document.forms[0];

	var qty_total = 0;
	var sum_total = 0;

	for (var i = 0; i < form.length; i++)
	{
		var elem = form.elements[i];

		if (elem.type == "text")
		{
			var id = elem.getAttribute("good");
			var price = form.elements["price_" + id].value;

			var tdSum = document.getElementById("sum_" + id);
			var tdArt = document.getElementById("art_" + id);

			var qty = elem.value.match(/^[0-9]+$/) ? parseInt(elem.value) : 0;

			tdSum.innerHTML = (qty * price) + " руб.";
			tdArt.style.textDecoration = qty == 0 ? "line-through" : "none";

			qty_total += qty;
			sum_total += qty * price;

			var btn = document.getElementById("btn");
			if (qty_total == 0)
			{
				btn.value = BTN_CLOSE;
				btn.onclick = function ()
				{
					window.close();
					return false;
				}
			}
			else
			{
				btn.value = BTN_CONTINUE;
				btn.onclick = null;
			}
		}
	}

	document.getElementById("qty_total").innerHTML = qty_total;
	document.getElementById("sum_total").innerHTML = sum_total + " руб." ;
}