// cart.js - functions for james millar's wonder shop
// version 1.1
// ----
// 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)
//		while (product.elements[index].name!=selection)
		//changed by ayaz on 26 th aug to overcome options problem and change the 
		//form name from product to cart 
		while (document.cart.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
//ayaz
document.cart.elements[index].options[document.cart.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
	SetCookie("cart",adjustQuantity(compileProduct(),quantity),"","/","","")
//	alert("Thank you, product has been added to your cart.")
//	alert(GetCookie("cart"))
	window.location.href = "/shop/checkout.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 = "/shop/checkout.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)
{
	// 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()
}



