// cart.js - functions for james millar's wonder shop
// version 1.1
// ----
// update 20020108 - added update function
// update 20072811 - renamed cart to basket for consistency
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 compileProduct2(form_name)
{
	var product = "|" + form_name.productid.value
	var options = form_name.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 (form_name.elements[index].name!=selection)
		{
			index = index + 1
		}
		product = product + "|" + form_name.elements[index].options[form_name.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)
		while (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 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
	
	if (quantity == "" || isNaN(quantity)) {
		alert("Please enter number.");
		return;
	}
	
	SetCookie("cart",adjustQuantity(compileProduct(),quantity),"","/","","")
	alert("Thank you, product has been added to your basket.")
//	alert(GetCookie("cart"))
}

function updateCart2(formname)
{
	
	var quantity = formname.quantity.value

	if (quantity == "" || isNaN(quantity)) {
		alert("Please enter number.");
		return;
	}

	SetCookie("cart",adjustQuantity(compileProduct2(formname),quantity),"","/","","")
	alert("Thank you, product has been added to your basket.")
}


function updateCart3() {
	if (document.getElementById("cart3Added").value == "True") {
		alert("Your selection has already been added.");
	}
	else {
	
		countCart3Items = parseInt(document.getElementById("cart3Count").value)
		
		if (countCart3Items > 0 && countCart3Items < 6) {
			alert("Please select 6 items.")
		}
		else {
			cart3Products = document.getElementById("cart3Products").value;
			cart3Products = cart3Products.replace("||","|");
			cart3Products = cart3Products.replace("|||","|");
			quantity = 1;
			var productsString = cart3Products;
			var productsList = productsString.split("|");
			
			for(productItemNum = 0; productItemNum < productsList.length; productItemNum++){
				if (productsList[productItemNum] != "") {
					formname = document.getElementById("form_product"+productsList[productItemNum]);
					SetCookie("cart",adjustQuantity(compileProduct2(formname),quantity),"","/","","");
				}
			}
			
			document.getElementById("cart3Added").value = "True"
			alert("Thank you, Adrian's Selection has been added to your basket.");
		}
	}
}


function cart3Select() {
	if (document.getElementById("cart3Added").value == "True") {
		alert("Your selection has already been added.");
	}
	else {
		cart3Products = document.cart3ProductsForm.cart3Products.value;
		cart3Array = cart3Products.split(",");
		var cart3Selection = "";
		quantity = 1;
		for (cart3Item = 0; cart3Item < 6; cart3Item++) {
			cart3Selection = cart3Array[cart3Item] + " " + cart3Selection
			var randomItem = Math.floor(Math.random()*cart3Array.length)
			//cart3Array.splice(randomItem,1)
			formname = document.getElementById("form_product"+cart3Array[cart3Item]);
			formname.quantitytick.checked = true;
			SetCookie("cart",adjustQuantity(compileProduct2(formname),quantity),"","/","","");	
		}
		
		document.getElementById("cart3Added").value = "True"
		alert("Thank you, Adrian's Selection has been added to your basket.");
	}
}


function updateCart3Products(formname,product) {
	
	//count number of selected items
	countCart3 = document.getElementById("cart3Count")
	countCart3Items = document.getElementById("cart3Count").value
	cart3Products = document.getElementById("cart3Products").value
	cart3Products = cart3Products.replace("||","|")
	cart3Products = cart3Products.replace("|||","|")
	
	//set to 0 if none selected
	if (countCart3Items == "" || isNaN(countCart3Items)) {
		countCart3Items = 0
		document.getElementById("cart3Count").value = countCart3Items
	}
		
	if (formname.quantitytick.checked == true) {
		if (countCart3Items < 6 ) {
			countCart3Items = parseInt(countCart3Items) +1
			document.getElementById("cart3Count").value = countCart3Items
			document.getElementById("cart3Products").value = cart3Products + "|" + product + "|"
			
			
		}
		else {
			formname.quantitytick.checked = false
			alert("You have selected 6 products.")
		}
	}
	else {
		if (countCart3Items > 0) {
			countCart3Items = parseInt(countCart3Items) -1
			document.getElementById("cart3Count").value = countCart3Items
			document.getElementById("cart3Products").value = cart3Products.replace(product + "|","")
		}
	}
}

function updateCartParam(product,options,quantity)
{
	SetCookie("cart",adjustQuantity(compileProductParam(product,options),quantity.value),"","/","","")
	alert("Thank you, product has been added to your basket.")
//	alert(GetCookie("cart"))
}

function add(product)
{
	SetCookie("cart",adjustQuantity(product,1),"","/","","")
	location.reload()
}

function minus(product)
{
	SetCookie("cart",adjustQuantity(product,-1),"","/","","")
	location.reload()
}

function update(product,newAmount)
{
	// created - 20020108 by james millar
	// 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)
{
	// created - 20020108 by james millar
	// purpose - delete a product in the cart

	var newQuantity = getQuantity(product)
	SetCookie("cart",adjustQuantity(product,-newQuantity),"","/","","")
	location.reload()
}


