Public ShoppingCartID()
Public ShoppingCartQty()

Function StartCart()
Redim ShoppingCartID(0)
Redim ShoppingCartQty(0)
StringToArray CleanCookieData(GetCookie("ShoppingCart"))
End Function

Function CloseCart()
SetCookie "ShoppingCart",ArrayToString
End Function

Function EmptyCart()
Redim ShoppingCartID(0)
Redim ShoppingCartQty(0)
CloseCart
End Function

Function AddItem(id,qty)
If ItemQty(id) = 0 then
    if qty = 0 then exit function
    Redim Preserve ShoppingCartID(Ubound(ShoppingCartID) + 1)
    Redim Preserve ShoppingCartQty(Ubound(ShoppingCartQty) + 1)
    ShoppingCartID(Ubound(ShoppingCartID)) = id
    ShoppingCartQty(Ubound(ShoppingCartQty)) = qty
Else
    ChangeItem id, CInt(ItemQty(id)) + CInt(qty)
End If
End Function

Function ItemQty(id)
Dim x
x = 1
ItemQty = 0
Do Until x = Ubound(ShoppingCartID) + 1
    If ShoppingCartID(x) = id then
	ItemQty = ShoppingCartQty(x) 
    End If
    x = x + 1
Loop
End Function

Function ChangeItem(id,qty)
Dim x
If qty = 0 then
    RemoveItem id
Else
    Do Until x = Ubound(ShoppingCartID) + 1
	If ShoppingCartID(x) = id then
	    ShoppingCartQty(x) = qty
	End If
	x = x + 1
    Loop
End If
End Function

Function RemoveItem(id)
Dim tmpShoppingCartID()
Dim tmpShoppingCartQty()
Dim x
Redim tmpShoppingCartID(0)
Redim tmpShoppingCartQty(0)
x = 1
Do Until x = Ubound(ShoppingCartID) + 1
    If ShoppingCartID(x) <> id then
	Redim Preserve tmpShoppingCartID(Ubound(tmpShoppingCartID) + 1)
	Redim Preserve tmpShoppingCartQty(Ubound(tmpShoppingCartQty) + 1)
	tmpShoppingCartID(Ubound(tmpShoppingCartID)) = ShoppingCartID(x)
	tmpShoppingCartQty(Ubound(tmpShoppingCartQty)) = ShoppingCartQty(x)
    End If
    x = x + 1
Loop
Redim ShoppingCartID(0)
Redim ShoppingCartQty(0)
x = 1
Do Until x = Ubound(tmpShoppingCartID) + 1
    Redim Preserve ShoppingCartID(Ubound(ShoppingCartID) + 1)
    Redim Preserve ShoppingCartQty(Ubound(ShoppingCartQty) + 1)
    ShoppingCartID(Ubound(ShoppingCartID)) = tmpShoppingCartID(x)
    ShoppingCartQty(Ubound(ShoppingCartQty)) = tmpShoppingCartQty(x)
    x = x + 1
Loop
Redim tmpShoppingCartID(0)
Redim tmpShoppingCartQty(0)
End Function

Function ArrayToString()
Dim x, data
data = "$Start$"
x = 1
Do Until x = Ubound(ShoppingCartID) + 1
    If data = "$Start$" Then
	data = data & ShoppingCartID(x) & "," & ShoppingCartQty(x)
    Else
	data = data & ":" & ShoppingCartID(x) & "," & ShoppingCartQty(x)
    End If
    x = x + 1
Loop
data = data & "$End$"
ArrayToString = data
End Function

Function CleanCookieData(str)
Dim x
x = 1
If trim(str) = "" then exit function
Do Until Mid(str, x, Len("$Start$")) = "$Start$" Or x > Len(str)
    x = x + 1
Loop
str = Mid(str, x + Len("$Start$"), Len(str) - x - Len("$Start$") + 1)
Do Until Mid(str, x, Len("$End$")) = "$End$" Or x > Len(str)
    x = x + 1
Loop
str = Mid(str, 1, x - 1)
CleanCookieData = str
End Function

Function StringToArray(str)
Dim x, y, z
x = 1
If trim(str) = "" then exit function
Do Until x > Len(str)
    y = x
    Do Until Mid(str, x, 1) = ":" Or x > Len(str)
        x = x + 1
    Loop
    z = 1
    Do Until Mid(Mid(str, y, x - y), z, 1) = "," Or z > Len(str)
        z = z + 1
    Loop
    Redim Preserve ShoppingCartID(Ubound(ShoppingCartID) + 1)
    Redim Preserve ShoppingCartQty(Ubound(ShoppingCartQty) + 1)
    ShoppingCartID(Ubound(ShoppingCartID)) = Mid(Mid(str, y, x - y), 1, z - 1)
    ShoppingCartQty(Ubound(ShoppingCartQty)) = Mid(Mid(str, y, x - y), z + 1, Len(Mid(str, y, x - y)) - z)
    x = x + 1
Loop
End Function