<!--#include file="../owFunctions/I_GeneralFunctions.asp"-->
<!--#include file="../owTemplates/Template_AllLeft.asp"-->
<%
	'************************************************************************************
	'*																					*
	'*	MODIFICATION TO THIS SCRIPT OR ANY FILES INCLUDED WITH 							*
	'*		ONEWORLDSTORE IS DONE AT YOUR OWN RISK!										*
	'*	Product  : OneWorldStore														*
	'*	Version  : 9.0000																*
	'*	Modified : June 2004															*
	'*	Copyright:	Copyright (C) 2004 OneWorldStore.Com								*
	'*				Registration Number: 1024507										*
	'*				http://strategis.ic.gc.ca/											*
	'*																					*
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Sub ActionSubroutine()

		Dim aOptions(), aRequired()
		
		ReDim aRequired(50, 2)	' we are assuming no more than 50 option groups coming from any one product
		
		iNumOptionsSelected	= 1
		iNumOptions 		= 1
		pClosingCounter		= 0
		sIDCartRow			= 0
		sIDProduct			= CONVERSION_Trim(request("idProduct"))
		sQuantity	 		= CONVERSION_Trim(request("quantity"))

		If ( sQuantity = "" ) Then
			sQuantity = 1
		Else
			sQuantity = CDbl(sQuantity)
		End If

		sSQLText = "SELECT * FROM optionsGroups_products WHERE idProduct = " & sIDProduct
		Call DB_getFromDatabase(sSQLText, rsStoreData, "")
		While ( NOT rsStoreData.EOF )
			aRequired(iNumOptions ,0) = rsStoreData("Required")			' this is the required flag (Y is yes)
			aRequired(iNumOptions ,1) = rsStoreData("idOptionGroup")	' this is the idOptionGroup
			iNumOptions = iNumOptions + 1
			rsStoreData.MoveNext
		Wend
		Call DB_closeRecordset(rsStoreData)
		
		ReDim aOptions(iNumOptions, 4)

		For Each sOptionSelect In Request.Form
			If ( InStr(sOptionSelect, "idOption_Dropdown_") > 0 ) Then
				If ( CONVERSION_Trim(Request(sOptionSelect)) <> "" ) Then
					aOptions(iNumOptionsSelected, 0) = Replace(sOptionSelect, "idOption_Dropdown_", "")	' this is the idOptionGroup
					aOptions(iNumOptionsSelected, 1) = CONVERSION_Trim(Request(sOptionSelect))			' this is the idOption
					aOptions(iNumOptionsSelected, 2) = "Dropdown"										' this is the data type
					iNumOptionsSelected = iNumOptionsSelected + 1
				End If
			End If
			If ( InStr(sOptionSelect, "idOption_Radio_") > 0 ) Then
				If ( CONVERSION_Trim(Request(sOptionSelect)) <> "" ) Then
					aOptions(iNumOptionsSelected, 0) = Replace(sOptionSelect, "idOption_Radio_", "")	' this is the idOptionGroup
					aOptions(iNumOptionsSelected, 1) = CONVERSION_Trim(Request(sOptionSelect))			' this is the idOption
					aOptions(iNumOptionsSelected, 2) = "Radio"											' this is the data type
					iNumOptionsSelected = iNumOptionsSelected + 1
				End If
			End If
			If ( InStr(sOptionSelect, "idOption_Checkbox_") > 0 ) Then
				If ( CONVERSION_Trim(Request(sOptionSelect)) <> "" ) Then
					aOptions(iNumOptionsSelected, 0) = Replace(sOptionSelect, "idOption_Checkbox_", "")	' this is the idOptionGroup
					aOptions(iNumOptionsSelected, 1) = CONVERSION_Trim(Request(sOptionSelect))			' this is the idOption
					aOptions(iNumOptionsSelected, 2) = "Checkbox"										' this is the data type
					iNumOptionsSelected = iNumOptionsSelected + 1
				End If
			End If
			If ( InStr(sOptionSelect, "idOption_Text_") > 0 ) Then
				If ( CONVERSION_Trim(Request(sOptionSelect)) <> "" ) Then
					aOptions(iNumOptionsSelected, 0) = Replace(sOptionSelect, "idOption_Text_", "")		' this is the idOptionGroup
					iPos = InStr(aOptions(iNumOptionsSelected, 0), "_")
					aOptions(iNumOptionsSelected, 1) = Right(aOptions(iNumOptionsSelected, 0), Len(aOptions(iNumOptionsSelected, 0))-iPos)
					aOptions(iNumOptionsSelected, 0) = Left(aOptions(iNumOptionsSelected, 0), iPos-1)	' must do in this order
					aOptions(iNumOptionsSelected, 2) = "Text"											' this is the data type
					aOptions(iNumOptionsSelected, 3) = Trim(Request(sOptionSelect))						' this is the text data's string
					iNumOptionsSelected = iNumOptionsSelected + 1
				End If
			End If
		Next
		iNumOptionsSelected = iNumOptionsSelected - 1

		If ( ( bOWForceToSelectOption ) And ( iNumOptionsSelected = 0 ) ) Then
			' the customer is coming from a product listing page and the product they are choosing to add to their cart requires that they add at least one option
			response.redirect "owProductDetail.asp?idProduct=" & sIDProduct & "&bSelectOption=T"
		Else
			If ( ( bOWForceToSelectOption ) And ( iNumOptionsSelected < iNumOptions ) ) Then
				' this product requires that ALL options are selected.
				Call owMessage(LANG_DT( "ADDITEM_REQUIREDOPTIONS", "Please return and choose a selection for each option this product has." ), C_ERROR)
			Else
				sSQLText = "SELECT * FROM Products WHERE idProduct = " & sIDProduct & " AND Active = -1"
				Call DB_getFromDatabase(sSQLText, rsStoreData, "")
				If ( rsStoreData.EOF ) Then
					Call owMessage(LANG_DT( "ADDITEM_MISSINGITEM", "The product you are adding to your cart is no longer available. Please return and choose another product." ), C_ERROR)
				Else
					sUnitPrice		= rsStoreData("price")
					sUnitBtoBPrice	= rsStoreData("bToBPrice")
					sWeight			= rsStoreData("weight")
					sCost			= rsStoreData("cost")
					Call DB_closeRecordset(rsStoreData)
					
					If ( bOWChangeDecimalPoint ) Then
						sUnitPrice		= Replace(sUnitPrice, ",", ".")
						sUnitBtoBPrice	= Replace(sUnitBtoBPrice, ",", ".")
						sWeight			= Replace(sWeight, ",", ".")
						sCost			= Replace(sCost, ",", ".")
					End If
			
					' ***see if the product is in the cart
						' check for the product w/ all the options the current product has - no more, no less (should also take into consideration prods w/ no options)
					If ( iNumOptionsSelected > 0 ) Then
						sSQLText = "SELECT idCartRow FROM CartProducts WHERE idProduct = " & sIDProduct & " AND idCart = " & sIDCart
'response.write "1:" & sSQLText & "<Br>"
						Call DB_getFromDatabase(sSQLText, rsStoreData, "")
						If ( NOT rsStoreData.EOF ) Then
							Do While ( NOT rsStoreData.EOF )
								sSQLText = "SELECT Count(*) FROM CartPRoductsOptions WHERE idCartRow = " & rsStoreData("idCartRow") & " AND ("
'response.write "2:" & sSQLText & "<Br>"
								For iX = 1 To iNumOptionsSelected
									sSQLText = sSQLText & "(idOption = " & aOptions(iX, 1) & " AND idOptionGroup = " & aOptions(iX, 0) & ")"
									If ( iX < iNumOptionsSelected ) Then
										sSQLText = sSQLText & "OR"
									End If
								Next
								sSQLText = sSQLText & ")"
'response.write "3:" & sSQLText & "<Br>"
								Call DB_getFromDatabase(sSQLText, rsStoreData2, "")

								If ( NOT rsStoreData2.EOF ) Then
									If ( rsStoreData2(0) = iNumOptionsSelected ) Then
										sIDCartRow = rsStoreData("idCartRow")
										Exit Do
									End If
								End If
								Call DB_closeRecordset(rsStoreData2)
								rsStoreData.MoveNext
							Loop
						End If
						Call DB_closeRecordset(rsStoreData)
					Else
						' since there are no options, search for a matching product with no options also
						sSQLText = "SELECT idCartRow FROM CartProducts WHERE idProduct = " & sIDProduct & " AND idCart = " & sIDCart
						Call DB_getFromDatabase(sSQLText, rsStoreData, "")
						If ( NOT rsStoreData.EOF ) Then
							sIDCartRow = rsStoreData("idCartRow")
						End If
						Call DB_closeRecordset(rsStoreData)
					End If
					' ***end checking for product within current cart

					If ( sIDCartRow = 0 ) Then
						' no matching products were found within the cart
						sSQLText = "INSERT INTO CartProducts (idProduct,quantity,unitprice,unitcost,unitweight,unitbtobprice,IDCart) VALUES (" & sIDProduct & "," & sQuantity & "," & sUnitPrice & "," & sCost & "," & sWeight & "," & sUnitBtoBPrice & "," & sIDCart & ")"
'response.write "4:" & sSQLText & "<Br>"
'response.write "a:" & sQuantity & ":<br>"
'response.write "a:" & sSQLText & ":<br>"
'response.end
						dbStoreConnection.Execute sSQLText
			
						sSQLText ="SELECT Max(CP.idCartRow) FROM CartProducts CP, Cart C WHERE C.idCart = " & sIDCart & " AND C.idCart = CP.idCart"
'response.write "5:" & sSQLText & "<Br>"
						Call DB_getFromDatabase(sSQLText, rsStoreData, "")
						If ( NOT rsStoreData.EOF ) Then
							sIDCartRow = rsStoreData(0)
						End If
						Call DB_closeRecordset(rsStoreData)
						
						For iX = 1 To iNumOptionsSelected
							pOptionDescrip	= ""
							pPriceToAdd		= 0
							sSQLText = "SELECT PriceToAdd, OptionDescrip FROM Options WHERE idOption = " & aOptions(iX, 1)
							Call DB_getFromDatabase(sSQLText, rsStoreData, "")      
							if not rsStoreData.eof then
								pOptionDescrip	= rsStoreData("optionDescrip")
								pPriceToAdd		= rsStoreData("priceToAdd")
							End If
							Call DB_closeRecordset(rsStoreData)
							
							sSQLText = "INSERT INTO CartProductsOptions (idCartRow, idOption, optionDescrip, priceToAdd, idOptionGroup, TextDetail) VALUES (" & sIDCartRow & "," & aOptions(iX, 1) & ",'" & pOptionDescrip & "',"  & pPriceToAdd & ", " & aOptions(iX, 0) & ", '" & SQLEncode(aOptions(iX, 3)) & "')"
							call DB_updateDatabase(sSQLText)
						Next
					Else
'response.write "b:" & sQuantity & ":<br>"
'response.write "b:" & sIDCartRow & ":<br>"
'response.end
						' found a match! increase the qty by one
						sSQLText = "SELECT Quantity FROM CartProducts WHERE idCartRow = " & sIDCartRow
'response.write "6:" & sSQLText & "<Br>"
						Call DB_getFromDatabase(sSQLText, rsStoreData, "")
						if not rsStoreData.eof then
							sQuantity = sQuantity + rsStoreData("Quantity")
						End If
						Call DB_closeRecordset(rsStoreData)
'response.write "q:" & sQuantity & "<Br>"

						sSQLText = "UPDATE CartProducts SET quantity = " & sQuantity & ", unitPrice = " & replace(sUnitPrice,",",".") & ", unitBToBPrice = " & replace(sUnitBtoBPrice,",",".") & " WHERE idCartRow = " & sIDCartRow
'response.write "7:" & sSQLText & "<Br>"
						call DB_updateDatabase(sSQLText)
					End If
			
					SF_ClosePage()
'response.end
					response.redirect "owShowCart.asp"
				End If
			End If
		End If
		
	End Sub
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Function QtySummation()
	
		QtySummation = 0
		
		sSQLText = "SELECT Sum(Quantity) FROM CartProducts WHERE idCart = " & sIDCart
		Call DB_getFromDatabase(sSQLText, rsStoreData, "")
		If ( NOT rsStoreData.EOF ) Then
			QtySummation = rsStoreData(0)
		End If
		Call DB_closeRecordset(rsStoreData)
		
	End Function
	'************************************************************************************
%>