// JavaScript Document
function setCookie(name, value, time)
{
	if (!time) time = 60*60*24*365;
	
	var d = new Date();
	var t = new Date(d.getTime() + time);
	t = t.toGMTString();
	document.cookie = name + "=" + escape(value) + ";expires=" + t + ";path=/;domain=.zinmantex.com";
}
function addToCart(style, cat, subcat)
{
	var newItems = [];
	jQuery.each($("input:checkbox:checked"), 
						  	function() { newItems[newItems.length] = this.value; } );
	
	var oldItems = getCartCookie() ? getCartCookie().split(/@/) : [];
	var goodNewItems = [];
	
	for (var i = 0; i < newItems.length; i++)
	{
		if (arraySearch(oldItems, newItems[i], cookieComparator) == -1)
		{
			goodNewItems[goodNewItems.length] = newItems[i];
		}
	}
	
	var text = getCartCookie() == "" ? goodNewItems.join('@') : oldItems.concat(goodNewItems).join('@');

	if (text != '')
		setCookie('cart', text);
	
	document.location.href = '../orderform.php';
}
function getCartCookie()
{
	var cookie = unescape(document.cookie);
	if (typeof cookie == 'undefined' || !cookie)
		return null;
	
	var cart = cookie.match(/cart=([^;]+)/);
	if (cart)
		return cart[1];
	return '';
}
function updateQuantity(input)
{
	var cookie = getCartCookie();
	if (input.value == 0)
	{
		var crumbs = cookie.split(/,/);
		var index = arraySearch(crumbs, input.name);

		if (index == -1)
			var newValue = cookie;
		else
		{
			var newValue = [];
			for (var i = 0; i < crumbs.length; i++)
				if (index != i)
					newValue[newValue.length] = crumbs[i];
			newValue = newValue.join();
		}
	}
	else
		var newValue = cookie.replace(input.name, input.name.substring(0, input.name.indexOf(':') + 1) + input.value);
		
	setCookie('cart', newValue);
	
	if (input.value == 0)
		input.parentNode.parentNode.innerHTML = '&nbsp;';
}
function removeProduct(input, product)
{
	var cookie = getCartCookie();
	var crumbs = cookie.split(/@/);
	var index = arraySearch(crumbs, product, function (key, value) { return (key.search(value)>=0) ? true : false; });

	if (index == -1)
		var newValue = cookie;
	else
	{
		var newValue = [];
		for (var i = 0; i < crumbs.length; ++i)
			if (index != i)
				newValue[newValue.length] = crumbs[i];
		newValue = newValue.join('@');
	}

	setCookie('cart', newValue);

	input.parentNode.parentNode.innerHTML = '&nbsp;';
}
function arraySearch(array, value, comparator)
{
	for (var i = 0; i < array.length; i++)
	{
		if (array[i] == value || (comparator && comparator(array[i], value)))
			return i;
	}
	return -1;
}
function cookieComparator(a, b)
{
	return a.substring(0, a.indexOf(':')) == b.substring(0, b.indexOf(':'));
}

