function getCookieVal (offset)
{
	var endstr = document.cookie.indexOf (";", offset);

	if (endstr == -1)
		endstr = document.cookie.length;

	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
	  		return getCookieVal (j);

		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
  	}
	return null;
}

function SetCookie (name,value,expires,path,domain,secure)
{
	document.cookie = name + "=" + escape (value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function DeleteCookie (name,path,domain)
{
	if (GetCookie(name))
	{
		document.cookie = name + "=" +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}


// shopping cart 
// ----
// update 20020108 - added update function

function getProduct(productID)
// returns the product record from the cart cookies
// as specified by productID
{
	var cart	= GetCookie("cart")

	var start	= cart.indexOf("^|"+productID)
	var end		= cart.indexOf("^",cart.indexOf("^|"+productID)+1)

	return cart.substring(start,end)
}


function getQuantity(product)
// return the quantity of an item in a cart as determined by
// product, product consists of the productID and all optiongroupID's
// delimited by pipes
// eg "|12|3|5|2|"
{
	var cart	= GetCookie("cart")
	if (cart!=null)
	{
		var start	= cart.indexOf("~",cart.indexOf("^"+product)) + 1
		var end		= cart.indexOf("|",cart.indexOf("~",cart.indexOf("^"+product)))

		if (cart.indexOf("^"+product)==-1)
		{
			return "0"
		}
		else
		{
			return cart.substring(start,end)
		}
	}
	else
	{
		return "0"
	}
}


function otherProducts(product)
// returns all other products in the cart
// excluding product
{
	var cart	= GetCookie("cart")

	if (cart!=null)
	{
		var start	= cart.indexOf("^"+product)
		var end		= cart.indexOf("^",(cart.indexOf("~",(cart.indexOf("^"+product)))))

		if (start==-1)
		{
			return cart
		}
		else
		{
			return (cart.substring(0,start) + cart.substring(end,cart.length))
		}
	}
	else
	{
		return "^"
	}
}


function adjustQuantity(product,amount)
// returns all products in the cart with the
// quantity of product altered by amount,
// if the quantity falls to zero or less
// the product is removed
{
	var oldQuantity = getQuantity(product)
	var newQuantity = parseInt(oldQuantity) + parseInt(amount)

	if (newQuantity<=0)
	{
		return otherProducts(product)
	}
	else
	{
		return (otherProducts(product) + product + "~" + newQuantity +"|^")
	}
}

function compileProduct()
// this function compiles the product by pulling
// the productid and various options from the form
{
	var product = "|" + document.product.productid.value
	var options = document.product.options.value

	while (options.length>1)
	{
		var index = 0
		optiongroupid = (options.substring(options.indexOf("|")+1,options.indexOf("|",options.indexOf("|")+1)))
		selection = "optiongroupid" + optiongroupid
		options = options.substring(options.indexOf("|",1),options.length)
		while (document.product.elements[index].name!=selection)
		{
			index = index + 1
		}
//		product = product + "|" + document.product.elements[index].value
		product = product + "|" + document.product.elements[index].options[document.product.elements[index].options.selectedIndex].value

	}
	product = product + "|"
	return product
}

function compileProductParam(productField, optionsField)
{
	var product = "|" + productField.value
	var options = optionsField.value

	while (options.length>1)
	{
		var index = 0
		optiongroupid = (options.substring(options.indexOf("|")+1,options.indexOf("|",options.indexOf("|")+1)))
		selection = "optiongroupid" + optiongroupid
		options = options.substring(options.indexOf("|",1),options.length)

// ---- start change ----
//  - changed to allow the processing of radio buttons and checkboxes
// ----
		while (document.cart.elements[index].name!=selection||
				(document.cart.elements[index].type=="radio" && 
				document.cart.elements[index].checked!=true))
		{
			index = index + 1
		}
		
		if (document.cart.elements[index].type == "checkbox")
		{
			if (document.cart.elements[index].checked==true)
			{
				product = product + "|" + document.cart.elements[index].value
			}
		}
		else
		{
			product = product + "|" + document.cart.elements[index].value
		}

// ---- end change 16/10/2003 ----
	}
	product = product + "|"
	return product
}

function compileProductParamList(productField, optionsField, productForm)
{
	var product = "|" + productField.value
	var options = optionsField.value

	while (options.length>1)
	{
		

		var index = 0
		optiongroupid = (options.substring(options.indexOf("|")+1,options.indexOf("|",options.indexOf("|")+1)))
		selection = "optiongroupid" + optiongroupid
		options = options.substring(options.indexOf("|",1),options.length)

		while (productForm.elements[index].name!=selection||
				(productForm.elements[index].type=="radio" && 
				productForm.elements[index].checked!=true))
		{
			index = index + 1
		}
		
		if (productForm.elements[index].type == "checkbox")
		{
			if (productForm.elements[index].checked==true)
			{
				product = product + "|" + productForm.elements[index].value
			}
		}
		else
		{
			product = product + "|" + productForm.elements[index].value
		}
	}
	product = product + "|"
	return product
}

function head(thelist,delimiter)
{
	return thelist.substring(1,thelist.indexOf(delimiter,1))
}

function tail(thelist,delimiter)
{
	return thelist.substring(thelist.indexOf(delimiter,1),thelist.length)
}

function itemCount(thelist,delimiter)
{
	var index = 0
	while (thelist!=delimiter)
	{
		thelist = tail(thelist,delimiter)
		index = index + 1
	}
	return index
}

function updateCart()
{
	var quantity = document.product.quantity.value
	SetCookie("cart",adjustQuantity(compileProduct(),quantity),"","/","","")
//	alert("Thank you, product has been added to your cart.")
//	alert(GetCookie("cart"))
	window.location.href = "http://www.freshflowersandgifts.com.au/cart.asp"
}

function updateCartParam(product,options,quantity)
{
	SetCookie("cart",adjustQuantity(compileProductParam(product,options),quantity.value),"","/","","")
//	alert("Thank you, product has been added to your cart.")
//	alert(GetCookie("cart"))
	window.location.href = "http://www.freshflowersandgifts.com.au/cart.asp"
}

function updateCartParamList(product,options,quantity,productForm)
{
	SetCookie("cart",adjustQuantity(compileProductParamList(product,options,productForm),quantity.value),"","/","","")
//	alert("Thank you, product has been added to your cart.")
//	alert(GetCookie("cart"))
	window.location.href = "http://www.freshflowersandgifts.com.au/cart.asp"
}

function add(product)
{
	SetCookie("cart",adjustQuantity(product,1),"","/","","")
	location.reload()
}

function minus(product)
{
	SetCookie("cart",adjustQuantity(product,-1),"","/","","")
	location.reload()
}

function update(product,newAmount)
{
	// purpose - provides a way to directly assign the quantity of a product in the cart

	var adjustment = newAmount - getQuantity(product)
	SetCookie("cart",adjustQuantity(product,adjustment),"","/","","")
	location.reload()
}

function deleteproduct(product)
{
	// purpose - delete a product in the cart

	var newQuantity = getQuantity(product)
	SetCookie("cart",adjustQuantity(product,-newQuantity),"","/","","")
	location.reload()
}

