function openWindow(url, name, w, h, x, y) {
	screenw = screen.availWidth;
	screenh = screen.availHeight;
	if((x == null) || (y == null) || (x < 0) || (x > screenw - w - 10) || (y < 0) || (y > screenh - h - 40)) {
		x = (screenw - w) / 2;
		y = (screenh - h) / 2;
	}
	popupWin = window.open(url, name, 'width='+w+',height='+h+',left='+x+',top='+y+',titlebar=no,toolbars=no,location=no,menubar=no,status=no,statusbar=no,scrollbars=no,resizable=no');
	popupWin.focus();
}


function set_cookie(name, value, expire)
{
	var cur_date = new Date();
	var expires = '';

	if (null != expire)
	{
		var cookie_expire = new Date(Date.parse(cur_date.toUTCString()) + (expire * 24 * 3600 * 1000));
		expires = '; expires=' + cookie_expire.toUTCString();
	}

	document.cookie = name + '=' + escape(value) + expires + '; path=/';
}


function get_cookie(name)
{
	var search = name + '=';

	if (0 < document.cookie.length)
	{
		var offset = document.cookie.indexOf(search);
		if (-1 != offset)
		{
			offset += search.length;
			var end = document.cookie.indexOf(';', offset);
			if (-1 == end) end = document.cookie.length;

			return unescape(document.cookie.substring(offset, end));
		}
	}

	return false;
}

function cart(product_id)
{
	var cart_container = document.getElementById('cart_container');
	var adding_to_cart = document.getElementById('adding_to_cart');
	var already_in_cart = document.getElementById('already_in_cart');

	if (cart_container && adding_to_cart && already_in_cart)
	{
		cart_container.style.display = 'block';

		if (is_in_cart(product_id))
		{
			adding_to_cart.style.display = 'none';
			already_in_cart.style.display = 'block';
			return true;
		}

		adding_to_cart.style.display = 'block';
		already_in_cart.style.display = 'none';
		return true;
	}
}

function add_to_cart(product_id)
{
	if (!is_in_cart(product_id))
	{
		var cart_cookie = get_cookie('cart');
		cart_cookie = (cart_cookie) ? cart_cookie + ';' : '';
		set_cookie('cart', cart_cookie + product_id);
	}

	var adding_to_cart = document.getElementById('adding_to_cart');
	var already_in_cart = document.getElementById('already_in_cart');

	if (adding_to_cart && already_in_cart)
	{
		adding_to_cart.style.display = 'none';
		already_in_cart.style.display = 'block';
	}
}

function delete_from_cart(product_id)
{
	var cart_cookie = get_cookie('cart');
	if (cart_cookie)
	{
		var cart = cart_cookie.split(';');
		if (cart)
		{
			var new_cart_value = '';
			for (var i = 0; i < cart.length; i++)
			{
				if (cart[i] != product_id)
				{
					if (new_cart_value) new_cart_value += ';';
					new_cart_value += cart[i];
				}
			}
			set_cookie('cart', new_cart_value);
		}
	}
	return false;
}

function is_in_cart(product_id)
{
	var cart_cookie = get_cookie('cart');
	if (cart_cookie)
	{
		var cart = cart_cookie.split(';');
		if (cart)
		{
			for (var i = 0; i < cart.length; i++)
			{
				if (cart[i] == product_id) return true;
			}
		}
	}
	return false;
}

function recount_cart(obj, product_id)
{
	if (obj.value && !isNaN(obj.value) && obj.value > 0)
	{
		if (!is_in_cart(product_id)) add_to_cart(product_id);
	}
	else
	{
		delete_from_cart(product_id);
	}

	if (!obj.value || isNaN(obj.value) || obj.value == 0)
	{
		obj.value = '0';
		obj.className = 'none_value';
	}
	else
	{
		obj.className = 'positive_value';
	}
}
