<%
	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Function CART_CalculateTax()
	
		ReDim aTaxStateInfo(2, 200)
		ReDim aTaxCountryInfo(2, 200)
		
		sCartTax	= 0
		iCount		= 0

		If ( bHasAccount ) Then
			sSQLText = "SELECT BillingStateCode, BillingCountryCode, BillingZip, shippingStateCode, shippingCountryCode, shippingZip FROM CustAltBill CAB, CustAltShip CAS WHERE CAS.AccountUnq = CAB.AccountUnq AND CAB.AccountUnq = " & iLoginAccountUnq & " AND CAS.Position = 0"
			Call DB_getFromDatabase(sSQLText, rsStoreData, "") 
			If ( NOT rsStoreData.EOF ) Then
				sStateCode			= rsStoreData("BillingStateCode")
				sCountryCode		= rsStoreData("BillingCountryCode")
				sZip				= rsStoreData("BillingZip")
				if rsStoreData("shippingStateCode")<>"" then
					' use shipping for tax calculation
					sStateCode		= rsStoreData("shippingStateCode")
					sCountryCode	= rsStoreData("shippingCountryCode")
					sZip			= rsStoreData("shippingZip")
				End If
				
				' get State/Provencial Taxes
				sSQLText = "SELECT TaxName, Percentage FROM StateTax WHERE StateCode = '" & sStateCode & "'"
				Call DB_getFromDatabase(sSQLText, rsStoreData2, "")
				If ( NOT rsStoreData2.EOF ) Then
					While ( NOT rsStoreData2.EOF )
						sCartTax				= sCartTax + CDbl(Trim(rsStoreData2("Percentage")))
						aTaxStateInfo(0, iCount)	= Trim(rsStoreData2("TaxName"))
						aTaxStateInfo(1, iCount)	= Trim(rsStoreData2("Percentage"))
						iCount						= iCount + 1
						rsStoreData2.movenext
					Wend
				End If
				Call DB_closeRecordset(rsStoreData2)
				
				' get Country Taxes (do this seperate, and not within a UNION so that it's easy for us to seperate them in the display
				sSQLText = "SELECT TaxName, Percentage FROM CountryTax WHERE CountryCode = '" & sCountryCode & "'"
				Call DB_getFromDatabase(sSQLText, rsStoreData2, "")
				If ( NOT rsStoreData2.EOF ) Then
					iCount = 0
					While ( NOT rsStoreData2.EOF )
						sCartTax					= sCartTax + CDbl(Trim(rsStoreData2("Percentage")))
						aTaxCountryInfo(0, iCount)	= Trim(rsStoreData2("TaxName"))
						aTaxCountryInfo(1, iCount)	= Trim(rsStoreData2("Percentage"))
						iCount						= iCount + 1
						rsStoreData2.movenext
					Wend
				End If
				Call DB_closeRecordset(rsStoreData2)
			End If
			Call DB_closeRecordset(rsStoreData)
		End If

		CART_CalculateTax = sCartTax

	End Function
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	function CART_CountRows()
	
		dim sSQLText, rsStoreData
		
		set rsStoreData	= CreateObject("ADODB.Recordset")
		CART_CountRows	= 0

		If ( sIDCart <> "" ) Then
			' check if the product is inside the cart
			sSQLText = "SELECT COUNT(idCartRow) FROM CartProducts WHERE IDCart=" & sIDCart
			call DB_getFromDatabase(sSQLText, rsStoreData, "")  
			If ( NOT rsStoreData.EOF ) Then
				CART_CountRows = rsStoreData(0)
			End If
			DB_closeRecordset(rsStoreData)
		End If
		
		set rsStoreData	= Nothing
	
	End Function
	'************************************************************************************
	

	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	function CART_CalcTotal()       
	
		Dim sOptionsTotal, rsStoreData, rsStoreData2

		Set rsStoreData		= CreateObject("ADODB.Recordset")
		Set rsStoreData2	= CreateObject("ADODB.Recordset")
		sOptionsTotal		= 0
		CART_CalcTotal		= 0

		If ( sIDCart <> "" ) Then
			' iterate through cart rows
			sSQLText = "SELECT idCartRow, idProduct, quantity, unitPrice, unitBtoBPrice FROM CartProducts WHERE IDCart=" & sIDCart
			call DB_getFromDatabase(sSQLText, rsStoreData, "")  
			While ( NOT rsStoreData.EOF )
				sUnitPrice		= Cdbl(rsStoreData("unitPrice"))
				sUnitBtoBPrice	= Cdbl(rsStoreData("unitBtoBPrice"))
				sQuantity		= rsStoreData("quantity")
				sIDCartRow		= rsStoreData("idCartRow")
				sIDProduct		= rsStoreData("idProduct")
				sOptionsTotal	= 0
				
				' get Optionals value for that row    
				sSQLText = "SELECT SUM(CartProductsOptions.priceToAdd) AS optionsTotal FROM options, CartProductsOptions WHERE options.idOption=CartProductsOptions.idOption AND CartProductsOptions.idCartRow="&sIDCartRow     
				call DB_getFromDatabase(sSQLText, rsStoreData2, "cartFunctions")  
				If ( NOT rsStoreData2.EOF ) Then
					If ( NOT IsNull(rsStoreData2("optionsTotal")) ) Then
						sOptionsTotal = Cdbl(rsStoreData2("optionsTotal"))
					End If
				End If                
				Call DB_closeRecordset(rsStoreData2)
				
				If ( ( ACCNT_InGroup(iLoginAccountUnq, C_GROUP_WHOLESALE) ) And ( sUnitBtoBPrice > 0 ) ) Then
					CART_CalcTotal = CDbl(CART_CalcTotal + sQuantity * (sUnitBtoBPrice + sOptionsTotal))
				Else
					CART_CalcTotal = CDbl(CART_CalcTotal + sQuantity * (sUnitPrice + sOptionsTotal))
				End If

				rsStoreData.movenext
			Wend
			Call DB_closeRecordset(rsStoreData)
		End If
		
		Set rsStoreData		= Nothing
		Set rsStoreData2	= Nothing
	
	End Function
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*	This returns the monitary amount of product that's been set as tax exempt		*
	'*		- subtract this from the subtotal when calculating tax						*
	'*																					*
	'************************************************************************************
	function CART_CalcTotalTaxExempt()       
	
		Dim sOptionsTotal, rsStoreData, rsStoreData2

		Set rsStoreData			= CreateObject("ADODB.Recordset")
		Set rsStoreData2		= CreateObject("ADODB.Recordset")
		sOptionsTotal			= 0
		CART_CalcTotalTaxExempt	= 0

		If ( sIDCart <> "" ) Then
			' iterate through cart rows
			sSQLText = "SELECT CP.idCartRow, CP.idProduct, CP.quantity, CP.unitPrice, CP.unitBtoBPrice FROM CartProducts CP, Products P WHERE P.idProduct = CP.idProduct AND P.TaxExempt = 'Y' AND CP.IDCart=" & sIDCart
			call DB_getFromDatabase(sSQLText, rsStoreData, "")  
			While ( NOT rsStoreData.EOF )
				sUnitPrice		= Cdbl(rsStoreData("unitPrice"))
				sUnitBtoBPrice	= Cdbl(rsStoreData("unitBtoBPrice"))
				sQuantity		= rsStoreData("quantity")
				sIDCartRow		= rsStoreData("idCartRow")
				sIDProduct		= rsStoreData("idProduct")
				sOptionsTotal	= 0
				
				' get Optionals value for that row
				sSQLText = "SELECT SUM(CartProductsOptions.priceToAdd) AS optionsTotal FROM options, CartProductsOptions WHERE options.idOption=CartProductsOptions.idOption AND CartProductsOptions.idCartRow="&sIDCartRow     
				call DB_getFromDatabase(sSQLText, rsStoreData2, "cartFunctions")  
				If ( NOT rsStoreData2.EOF ) Then
					If ( NOT IsNull(rsStoreData2("optionsTotal")) ) Then
						sOptionsTotal = Cdbl(rsStoreData2("optionsTotal"))
					End If
				End If                
				Call DB_closeRecordset(rsStoreData2)
				
				If ( ( ACCNT_InGroup(iLoginAccountUnq, C_GROUP_WHOLESALE) ) And ( sUnitBtoBPrice > 0 ) ) Then
					CART_CalcTotalTaxExempt = CDbl(CART_CalcTotalTaxExempt + sQuantity * (sUnitBtoBPrice + sOptionsTotal))
				Else
					CART_CalcTotalTaxExempt = CDbl(CART_CalcTotalTaxExempt + sQuantity * (sUnitPrice + sOptionsTotal))
				End If

				rsStoreData.movenext
			Wend
			Call DB_closeRecordset(rsStoreData)
		End If
		
		Set rsStoreData		= Nothing
		Set rsStoreData2	= Nothing
	
	End Function
	'************************************************************************************
	
	

	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Function CART_CalcWeight()
	
		Dim rsStoreData
		
		Set rsStoreData = CreateObject("ADODB.Recordset")
		CART_CalcWeight = 0
		
		If ( sIDCart <> "" ) Then
			' sum the total weight of the cart
			sSQLText = "SELECT SUM(quantity*unitWeight) AS total FROM CartProducts WHERE IDCart="&sIDCart
			call DB_getFromDatabase(sSQLText, rsStoreData, "cartFunctions")    
			If ( NOT rsStoreData.EOF ) Then
				if not isNull(rsStoreData("total")) then
					CART_CalcWeight = Cdbl(rsStoreData("total"))
				End If
			End If
			DB_closeRecordset(rsStoreData)
	
			' sum the weight of the optionals, if any
			sSQLText = "SELECT SUM(CartProducts.quantity*options.weight) AS total FROM CartProducts, CartProductsOptions, options WHERE CartProducts.idCartRow=CartProductsOptions.idCartRow AND CartProductsOptions.idOption=options.idOption AND IDCart=" & sIDCart
			call DB_getFromDatabase(sSQLText, rsStoreData, "cartFunctions")    
			if not rsStoreData.eof and not isNull(rsStoreData("total")) then
				CART_CalcWeight	= CDbl(CART_CalcWeight + Cdbl(rsStoreData("total")))
			End If  
			DB_closeRecordset(rsStoreData)
		End If
		
		Set rsStoreData = Nothing
	
	End Function
	'************************************************************************************


	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Function CART_CalcQty()
	
		Dim rsStoreData
		
		Set rsStoreData = CreateObject("ADODB.Recordset")
		CART_CalcQty	= 0

		If ( sIDCart <> "" ) Then
			sSQLText = "SELECT SUM(quantity) AS qTotal FROM CartProducts WHERE IDCart=" & sIDCart 
			call DB_getFromDatabase(sSQLText, rsStoreData, "")        
			If ( NOT rsStoreData.EOF ) Then
				if NOT isNull(rsStoreData("qTotal")) then
					CART_CalcQty = Cdbl(rsStoreData("qTotal"))
				End If
			End If     
			DB_closeRecordset(rsStoreData)
		End If
		
		set rsStoreData = Nothing
	
	End Function
	'************************************************************************************



	'************************************************************************************
	'*																					*
	'*	This returns the total weight of all products in the current cart who have 		*
	'*		free shipping. This number can be used to figure the total weight to use	*
	'*		when calculating shipping price. Subtract this number from the overall		*
	'*		weight to find the weight to use when calculating the cost of shipping.		*
	'*																					*
	'*	This is seperate from the CART_CalcWeight() because there are times when we 	*
	'*		require the actual weight of the products within the cart.					*
	'*																					*
	'************************************************************************************
	function CART_CalcFreeShipWeight()
	
		Dim rsStoreData
		
		Set rsStoreData			= CreateObject("ADODB.Recordset")
		CART_CalcFreeShipWeight = 0
	
		If ( sIDCart <> "" ) Then
			' sum the total weight
			sSQLText = "SELECT CR.idCartRow, CR.UnitWeight, CR.Quantity, P.FreeShipStartDate, P.FreeShipEndDate, P.FreeShipQty FROM CartProducts CR, Products P WHERE (#" & CONVERSION_FormatDate(Now(), "L") & "# BETWEEN P.FreeShipStartDate AND P.FreeShipEndDate OR ((P.FreeShipStartDate='') AND (P.FreeShipEndDate=''))) AND CR.idCart = " & sIDCart & " AND CR.idProduct = P.idProduct AND P.FreeShipping = -1 AND (CR.Quantity >= P.FreeShipQty OR P.FreeShipQty = 0)"
			call DB_getFromDatabase(sSQLText, rsStoreData, "")
			While ( NOT rsStoreData.EOF )
				CART_CalcFreeShipWeight = CART_CalcFreeShipWeight + (CDbl(rsStoreData("UnitWeight")) * Cdbl(rsStoreData("Quantity")))
				' now get the weight added by any options associated w/ this (if there are any in the cart)
				sSQLText = "SELECT Sum(O.Weight) FROM Options O, CartProductsOptions CRO WHERE CRO.idCartRow = " & rsStoreData("idCartRow") & " AND CRO.idOption = O.idOption"
				call DB_getFromDatabase(sSQLText, rsStoreData2, "")
				If ( NOT rsStoreData2.EOF ) Then
					If ( NOT IsNull(rsStoreData2(0)) ) Then
						CART_CalcFreeShipWeight = CART_CalcFreeShipWeight + (CDbl(rsStoreData2(0)) * Cdbl(rsStoreData("Quantity")))
					End If
				End If
				DB_closeRecordset(rsStoreData2)
				rsStoreData.MoveNext
			Wend
			DB_closeRecordset(rsStoreData)
		End If
		
		set rsStoreData	= nothing
		
	End Function
	'************************************************************************************



	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	function CART_CalcFreeShipQty()
	
		Dim rsStoreData
	
		Set rsStoreData			= CreateObject("ADODB.Recordset")
		CART_CalcFreeShipQty	= 0
	
		If ( sIDCart <> "" ) Then
			sSQLText = "SELECT SUM(quantity) FROM CartProducts, products WHERE IDCart=" & sIDCart & " AND CartProducts.idProduct=products.idProduct AND freeShipping=-1" 
			call DB_getFromDatabase(sSQLText, rsStoreData, "cartFunctions")        
			If ( NOT rsStoreData.EOF ) Then
				if NOT isNull(rsStoreData(0)) then
					CART_CalcFreeShipQty = Cdbl(rsStoreData(0))
				End If
			End If     
			DB_closeRecordset(rsStoreData)
		End If
		
		set rsStoreData = nothing
	
	End Function
	'************************************************************************************


	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	function CART_CalcFreeShipTtl()       
	
		Dim rsStoreData2, sOptionsTotal, rsStoreData
		
		set rsStoreData		= CreateObject("ADODB.Recordset")
		set rsStoreData2	= CreateObject("ADODB.Recordset")
	
		If ( sIDCart <> "" ) Then
			' iterate through FS cart rows
			sSQLText = "SELECT idCartRow, idProduct, quantity, unitPrice, unitBtoBPrice FROM CartProducts, products WHERE IDCart=" & sIDCart & " AND CartProducts.idProduct=products.idProduct AND freeShipping=-1"   
			call DB_getFromDatabase(sSQLText, rsStoreData, "cartFunctions")  
			do while not rsStoreData.eof
				sUnitPrice		= Cdbl(rsStoreData("unitPrice"))
				sUnitBtoBPrice	= Cdbl(rsStoreData("unitBtoBPrice"))
				sQuantity		= rsStoreData("quantity")
				sIDCartRow		= rsStoreData("idCartRow")
				sIDProduct		= rsStoreData("idProduct")
				sOptionsTotal	= 0
		
				' get Optionals value for that row    
				sSQLText = "SELECT SUM(CartProductsOptions.priceToAdd) AS optionsTotal FROM options, CartProductsOptions WHERE options.idOption=CartProductsOptions.idOption AND CartProductsOptions.idCartRow="&sIDCartRow     
				call DB_getFromDatabase(sSQLText, rsStoreData2, "cartFunctions")  
				If ( NOT rsStoreData2.EOF ) Then
					if NOT isNull(rsStoreData2("optionsTotal")) then
						sOptionsTotal = Cdbl(rsStoreData2("optionsTotal"))
					End If
				End If        
				DB_closeRecordset(rsStoreData2)
	
				
				If ( ( ACCNT_InGroup(iLoginAccountUnq, C_GROUP_WHOLESALE) ) And ( sUnitBtoBPrice > 0 ) ) Then
					CART_CalcFreeShipTtl = CDbl(CART_CalcFreeShipTtl + sQuantity * (sUnitBtoBPrice + sOptionsTotal))
				Else
					CART_CalcFreeShipTtl = CDbl(CART_CalcFreeShipTtl + sQuantity * (sUnitPrice + sOptionsTotal))
				End If
				rsStoreData.movenext
			loop   
			DB_closeRecordset(rsStoreData)
		End If

		set rsStoreData	= nothing
		set rsStoreData2	= nothing
	
	End Function
	'************************************************************************************


	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	sub CART_CalcVolume(sMaxLength, sMaxHeight, sSumWidth)
	
		dim sSQLText, rsStoreData2, iOWInterWidth
		
		iOWInterWidth		= owCONFIG_ReturnConfigValue("iOWInterWidth")
		sSumWidth			= 0
		sMaxLength			= 0
		sMaxHeight			= 0
		Set rsStoreData2	= CreateObject("ADODB.Recordset")
		
		If ( sIDCart <> "" ) Then
			' get max length
			sSQLText = "SELECT MAX(length) AS maxLength FROM CartProducts, products WHERE IDCart="&sIDCart& " AND CartProducts.idProduct=products.idProduct" 
			call DB_getFromDatabase(sSQLText, rsStoreData2, "cartFunctions")        
			If ( NOT rsStoreData2.eof ) Then
				if NOT isNull(rsStoreData2("maxLength")) then
					sMaxLength = Cdbl(rsStoreData2("maxLength"))
				End If
			End If     
			DB_closeRecordset(rsStoreData2)
			
			' get max height
			sSQLText = "SELECT MAX(height) AS maxHeight FROM CartProducts, products WHERE IDCart="&sIDCart& " AND CartProducts.idProduct=products.idProduct" 
			call DB_getFromDatabase(sSQLText, rsStoreData2, "cartFunctions")        
			If ( NOT rsStoreData2.eof ) Then
				if NOT isNull(rsStoreData2("maxHeight")) then
					sMaxHeight = Cdbl(rsStoreData2("maxHeight"))
				End If
			End If     
			DB_closeRecordset(rsStoreData2)
	
			' get sum width
			' sum width and sum inter space from settings
			sSQLText = "SELECT width, quantity FROM CartProducts, products WHERE IDCart="&sIDCart& " AND CartProducts.idProduct=products.idProduct" 
			call DB_getFromDatabase(sSQLText, rsStoreData2, "cartFunctions")        
			do while not rsStoreData2.eof   
				sSumWidth = CDbl(sSumWidth + (rsStoreData2("quantity")*rsStoreData2("width")) + (rsStoreData2("quantity")*Cdbl(iOWInterWidth)))
				rsStoreData2.movenext
			loop
			DB_closeRecordset(rsStoreData2)
		End If
		
		set rsStoreData2 = nothing
	
	End Sub
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Sub CART_PaymentMethodInfo(iPaymentID, bPaymentActive, sPaymentDescription, sPaymentPriceToAdd, sPaymentPercentageToAdd)
		
		Select Case iPaymentID
			Case C_PM_MAILIN
				sPaymentDescription		= LANG_DT( "CART_PM_MAILIN", "Mail-In" )
				bPaymentActive			= CONVERSION_Numeric(owCONFIG_ReturnConfigValue("sOWMailInActive"), C_INTEGER)
				sPaymentPriceToAdd		= CONVERSION_Numeric(owCONFIG_ReturnConfigValue("sOWMailInPriceToAdd"), C_DOUBLE)
				sPaymentPercentageToAdd	= CONVERSION_Numeric(owCONFIG_ReturnConfigValue("sOWMailInPercentToAdd"), C_DOUBLE)
				
			Case C_PM_FAXIN
				sPaymentDescription		= LANG_DT( "CART_PM_FAXIN", "Fax-In" )
				bPaymentActive			= CONVERSION_Numeric(owCONFIG_ReturnConfigValue("sOWFaxInActive"), C_INTEGER)
				sPaymentPriceToAdd		= CONVERSION_Numeric(owCONFIG_ReturnConfigValue("sOWFaxInPriceToAdd"), C_DOUBLE)
				sPaymentPercentageToAdd	= CONVERSION_Numeric(owCONFIG_ReturnConfigValue("sOWFaxInPercentToAdd"), C_DOUBLE)
			
			Case C_PM_PHONEIN
				sPaymentDescription		= LANG_DT( "CART_PM_PHONEIN", "Phone-In" )
				bPaymentActive			= CONVERSION_Numeric(owCONFIG_ReturnConfigValue("sOWPhoneInActive"), C_INTEGER)
				sPaymentPriceToAdd		= CONVERSION_Numeric(owCONFIG_ReturnConfigValue("sOWPhoneInPriceToAdd"), C_DOUBLE)
				sPaymentPercentageToAdd	= CONVERSION_Numeric(owCONFIG_ReturnConfigValue("sOWPhoneInPercentToAdd"), C_DOUBLE)
				
			Case Else
		End Select
		
		If ( bPaymentActive = "" ) Then
			' this is in case that payment method hasn't yet been added to the database. the owCONFIG_ReturnConfigValue will return a blank and we
			'	want to take that and "turn off" that payment method's code throughout the store
			bPaymentActive = False
		End If
		
	End Sub
	'************************************************************************************
%>