	function bg(e, color)
	{
		e.style.backgroundColor = color;
	}

	function validate_field(field, pattern)
	{
		field.className = '';
		if (field.value.search(pattern) == -1)
		{
			field.className = 'error';
			valid = 0;
		}
	}

	function disableButtons(frm)
	{
		var buttons = frm.getElementsByTagName('button');
		for (var i = 0; i < buttons.length; i++)
		{
			buttons[i].disabled = true;
		}
	}

	function getPath(v, id, host)
	{
		ID(id).innerHTML = host + '/' + v;
	}

	function loadSelect(value, select, hash)
	{
		select = ID(select);
		while(select.length > 0)
		{
			select.remove(0);
		}
		for (var v in hash[value])
		{
			var item = document.createElement('option');
			item.value = v;
			item.text = hash[value][v];
			try
			{
				select.add(item, null);
			}
			catch(ex)
			{
				select.add(item);
			}
		}
	}

	function copyFields(btn, count)
	{
		var dt = document.getElementsByTagName("DT");
		var dd = document.getElementsByTagName("DD");
		var dl = btn.parentNode.parentNode;
		var insertPoint = btn.parentNode.previousSibling;
		var i = 0;
		while(insertPoint != dt[i])
		{
			i++;
		}
		var counter = btn.previousSibling;
		counter.value++;
		for (var j = i - count; j < i; j++)
		{
			dl.insertBefore(dt[j].cloneNode(1), insertPoint);
			var newdd = dd[j].cloneNode(1);
			newdd.innerHTML = newdd.innerHTML.replace(/name="([\w\_]+)\_([0-9]+)"/ig, 'name="$1_' + counter.value + '"');
			newdd.innerHTML = newdd.innerHTML.replace(/show_calendar\(\'frm\.([\w\_]+)\_([0-9]+)\'\)/ig, 'show_calendar(\'frm.$1_' + counter.value + '\')');
			dl.insertBefore(newdd, insertPoint);
		}
	}

	var image = 0;
	function addImage(el, name)
	{
		image++;
		var div = document.createElement('DIV');
		var img = document.createElement('INPUT');
		img.className = 'image';
		img.type = 'file';
		img.name = name + '[' + image + ']';
		div.appendChild(img);
		var a = document.createElement('A');
		a.href = '#delete';
		a.innerHTML = 'delete';
		a.onclick = function(){ deleteImage(this.parentNode) };
		div.appendChild(a);
		el.parentNode.insertBefore(div, el);
	}

	function deleteImage(el)
	{
		el.parentNode.removeChild(el);
	}

	function refreshCaptcha(id)
	{
		ID(id).src = '/captcha.php?rnd=' + Math.random();
	}

	function toCart(e, id, amount)
	{
		if (amount > 0)
		{
			if (e.value > 0)
			{
				if (amount >= e.value)
				{
					document.location.href = '/cart/add/' + id + '/' + parseInt(e.value);
				}
				else
				{
					alert('На складе нет такого количества данного товара. Вы можете заказать максимум ' + amount + ' шт.');
				}
			}
			else
			{
				alert('Неверное количество товара');
			}
		}
		else
		{
			alert('К сожалению на складе не осталось данного товара');
		}
	}

	function changeTotal(e, amount, weight, volume, price)
	{
		op = (e.checked) ? 1 : -1;
		ID('amount').innerHTML = parseInt(ID('amount').innerHTML) + op * amount;
		ID('weight').innerHTML = parseFloat(ID('weight').innerHTML) + op * weight;
		ID('volume').innerHTML = parseFloat(ID('volume').innerHTML) + op * volume;
		ID('subtotal').innerHTML = Number(parseFloat(ID('subtotal').innerHTML) + op * price).toFixed(2);
		ID('total').innerHTML = Number(parseFloat(ID('total').innerHTML) + op * price).toFixed(2);
	}

	function cartSubmit(action)
	{
		ID('cart-frm').action = action;
		ID('cart-frm').submit();
	}

	function cartEdit(id)
	{
		var amount = ID('amount_' + id).value;
		if (amount > 0)
		{
			document.location.href = '/cart/edit/' + id + '/' + amount;
		}
	}

	function cartRemove(id)
	{
		if (confirm('Удалить?'))
		{
			document.location.href = '/cart/remove/' + id;
		}
	}

	function shipping(showWarning)
	{
		price = 0;
		period = 0;
		address = 'Неизвестен';
		method = 'Самовывоз';
		if (ID('location').value > 0 && ID('transporter').value > 0)
		{
			if (t_price[ID('transporter').value][ID('location').value])
			{
				price = t_price[ID('transporter').value][ID('location').value];
				period = t_period[ID('transporter').value][ID('location').value];
				address = ID('location').options[ID('location').selectedIndex].text;
				method = ID('transporter').options[ID('transporter').selectedIndex].text;
			}
			else
			{
				if (showWarning)
				{
					alert('К сожалению нет возможности доставить товар в данный регион выбранной службой доставки. Вы можете попробовать выбрать другую службу.');
				}
			}
		}
		ID('_period').innerHTML = period;
		ID('shipping').innerHTML = ID('_shipping').innerHTML = Number(price).toFixed(2);
		ID('total').innerHTML = Number(parseFloat(ID('subtotal').innerHTML) + price).toFixed(2);
		ID('address').innerHTML = address;
		ID('method').innerHTML = method;
	}

	function selfDelivery()
	{
		ID('location').selectedIndex = 0;
		ID('transporter').selectedIndex = 0;
		shipping();
	}
