<!--#include file="../../owFunctions/I_GeneralFunctions.asp"-->
<!--#INCLUDE file="../../owFunctions/clsUpload.asp"-->
<!--#INCLUDE file="../Functions/I_Upload.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/											*
	'*																					*
	'************************************************************************************

	Dim sError
	Dim sMessage, ctArray, sTemp, objUpload, sFileName

	sError		= ""

	If ( NOT bOWBackOfficeDemoMode ) Then	' just do a cursory check in case hackers try to replicate the forms on their computers (return no error)
		Server.ScriptTimeout = 5400
	
		ctArray = Split(Request.ServerVariables( "HTTP_CONTENT_TYPE"), ";")
		If ( UBound(ctArray) > 0 ) Then
			sTemp = Trim(UCase(ctArray(0)))
		End If
		
		If ( sTemp = "MULTIPART/FORM-DATA" ) Then
			Set objUpload			= New clsUpload
			sSort					= Trim(objUpload("sSort").Value)
			sSearch					= Trim(objUpload("sSearch").Value)
			sSearchText				= Trim(objUpload("sSearchText").Value)
			sIDProduct				= CONVERSION_SQLEncode2(objUpload("idProduct").Value,10)
			sSKU					= CONVERSION_SQLEncode2(objUpload("sku").Value,100)
			sDescription			= CONVERSION_SQLEncode2(objUpload("description").Value,100)
			sDetails				= objUpload("details").Value
			sIDCategory1			= CONVERSION_Numeric(objUpload("idCategory1").Value, C_INTEGER)
			sIDCategory2			= CONVERSION_Numeric(objUpload("idCategory2").Value, C_INTEGER)
			sIDCategory3			= CONVERSION_Numeric(objUpload("idCategory3").Value, C_INTEGER)
			sPrice					= CONVERSION_Numeric(objUpload("price").Value, C_DOUBLE)
			pListPrice				= CONVERSION_Numeric(objUpload("listPrice").Value, C_DOUBLE)
			pBtoBPrice				= CONVERSION_Numeric(objUpload("btoBPrice").Value, C_DOUBLE)
			pCost					= CONVERSION_Numeric(objUpload("cost").Value, C_DOUBLE)
			pListHidden				= CONVERSION_SQLEncode2(objUpload("listHidden").Value,10)
			pHotDeal				= CONVERSION_SQLEncode2(objUpload("hotDeal").Value,10)
			pWeight					= CONVERSION_Numeric(objUpload("sweight").Value, C_DOUBLE)
			pLength					= CONVERSION_Numeric(objUpload("slength").Value, C_DOUBLE)
			pWidth 					= CONVERSION_Numeric(objUpload("swidth").Value, C_DOUBLE)
			pHeight 				= CONVERSION_Numeric(objUpload("sheight").Value, C_DOUBLE)
			pShowInHome				= CONVERSION_SQLEncode2(objUpload("showInHome").Value,10)
			pActive					= CONVERSION_SQLEncode2(objUpload("active").Value,10)
			pFormQuantity			= CONVERSION_Numeric(objUpload("formQuantity").Value, C_INTEGER)
			aIDOptionGroup			= objUpload.Collection("sIDOptionGroup")
			
			If ( NOT bOWAdvancedImages ) Or ( sOWImageComponent = "" ) Then
				If ( objUpload("smallImageUrl").Length > 0 ) Then
					sSmallFileName = objUpload("smallImageUrl").FileName
				Else
					sSmallFileName = ""
				End If
			End If
			If ( objUpload("imageUrl").Length > 0 ) Then
				sFileName = objUpload("imageUrl").FileName
			Else
				sFileName = ""
			End If
			If ( objUpload("sDigitalDownload").Length > 0 ) Then
				sDigitalDownload = objUpload("sDigitalDownload").FileName
			Else
				sDigitalDownload = ""
			End If
		End If

		' replace comma
		sPrice				= replace(sPrice,",",".")
		pListPrice			= replace(pListPrice,",",".")
		pBtoBPrice			= replace(pBToBPrice,",",".")
		pCost				= replace(pCost,",",".")
		pWeight				= replace(pWeight,",",".")
		pLength				= replace(pLength,",",".")
		pWidth				= replace(pWidth,",",".")
		pHeight				= replace(pHeight,",",".")
		sDetails			= SQLEncode(sDetails)
		sDescription		= SQLEncode(sDescription)

		If ( pFormQuantity = "" ) Then pFormQuantity = 0 End If
		If ( pWeight = "" ) Then pWeight = 0 End If
		If ( plength = "" ) Then plength = 0 End If
		If ( pWidth = "" ) Then pWidth = 0 End If
		If ( pHeight = "" ) Then pHeight = 0 End If
		If ( plistPrice = "" ) Then plistPrice = "0.00" End If
		If ( pbToBPrice = "" ) Then pbToBPrice = "0.00" End If
		If ( pcost = "" ) Then pcost = "0.00" End If
		
		If ( objUpload("imageUrl").Length > 0 ) Then
			sSku = Replace(sSKU, "/", "-")
			sSku = Replace(sSku, "\", "-")
			sSku = Replace(sSku, "&", "-")
			sSku = Replace(sSku, "'", "-")
			sSku = Replace(sSku, """", "-")
			sSku = Replace(sSku, ",", "-")
			sSku = Replace(sSku, "|", "-")
			sDestFilename = sSku & ".jpg"
	
			sMessage = G_UPLOAD_SaveFile("imageUrl", sOWImagesDir, "", sDestFilename)
			If ( NOT bOWAdvancedImages ) Or ( sOWImageComponent = "" ) Then	' Advanced images are turned off
				sMessage = sMessage & G_UPLOAD_SaveFile("smallImageUrl", sOWImagesDir, "/small", sDestFilename)
			Else
				sMessage = sMessage & IMAGE_ResizeImage(sDestFilename, C_UPLOAD_PRIMARY)
			End If
		End If

		sSQLText = "INSERT INTO products (idSupplier, Description, Details, Price, ListPrice, BToBPrice, ImageUrl, SmallImageUrl, SKU, Stock, ListHidden, Weight, DeliveryTime, Active, HotDeal, Cost, Visits, Sales, formQuantity, ShowInHome, DateAdded, FreeShipping, SearchKeywords, Length, Width, Height, StockSecurity, StockCostOrder, StockDelay, StockMinimumOrder, FreeShipStartDate, FreeShipEndDate, FreeShipQty, DigitalDownload, ExpireDays, AllowedDownloads, AllowReviews, ModerateReviews, NumRanking, Ranking, DistributorUnq, TaxExempt) VALUES ( 0, '" & sDescription & "', '" & sDetails & "', " & sPrice & ", " & pListPrice & ", " & pBtoBPrice & ", '" & sDestFilename & "', '" & sDestFilename & "', '" &sSKU& "', 0, " & pListhidden & ", " & pWeight & ", 0, " & pActive & ", " & pHotDeal & ", " & pCost & ", 0, 0, " & pFormQuantity & ", " & pShowInHome & ", #" & CONVERSION_FormatDate(Now(), "S") & "#, 0, '', " & plength & ", " & pWidth & ", " & pHeight & ", 0, 0, 0, 0, '', '', 0, '', 0, 0, 'N', 'N', 0, 0, 0, 'N')"
		Call DB_updateDatabase(sSQLText)
		
		sSQLText = "SELECT idProduct FROM products WHERE sku='" & sSKU & "' AND price=" & sPrice & " ORDER by idProduct DESC"
		Call DB_getFromDatabase(sSQLText, rsStoreData, "") 
		if rsStoreData.eof then
			sMessage = LANG_DT( "PRODUCT_CANTFINDNEW", "Can't find inserted item" )
		Else
			sIDProduct = rsStoreData("idProduct")
		
			If ( sIDCategory1 <> "1" ) Then		' don't want to add to the root
				sSQLText = "INSERT INTO CatProdMap (idProduct, idCategory) VALUES (" &sIDProduct& "," &sIDCategory1& ")"
				Call DB_updateDatabase(sSQLText)
			end if
			
			if sIDCategory2<>"" And sIDCategory2 <> sIDCategory1 And ( sIDCategory2 <> "1" ) then
				sSQLText = "INSERT INTO CatProdMap (idProduct, idCategory) VALUES (" &sIDProduct& "," &sIDCategory2& ")"
				Call DB_updateDatabase(sSQLText)
			end if
			
			if sIDCategory3<>"" And sIDCategory3 <> sIDCategory1 And sIDCategory3 <> sIDCategory2 And ( sIDCategory3 <> "1" ) then
				sSQLText = "INSERT INTO CatProdMap (idProduct, idCategory) VALUES (" &sIDProduct& "," &sIDCategory3& ")" 
				Call DB_updateDatabase(sSQLText)
			end if
			
			' update product to option group assignments
			Call DB_updateDatabase("DELETE FROM OptionsGroups_Products WHERE idproduct = " & sIDProduct)
			For Each ProdOption In aIDOptionGroup
			    Call DB_updateDatabase("INSERT INTO OptionsGroups_Products (idProduct, idOptionGroup) VALUES (" & sIDProduct & ", " & ProdOption.Value & ")")
			Next
			
			sMessage = LANG_DT( "PRODUCT_ADDED", "** Product added. **" )
		end if
		Call DB_closeRecordset(rsStoreData)

	End If
	'************************************************************************************
	%>
	<script language = "javascript">
		parent.owBodyTopNav.location="owBodyTopNav.asp?sMessage=<%=Server.URLEncode(sMessage)%>";
		location="owListProducts.asp?sSort=<%=sSort%>&sSearch=<%=sSearch%>&sSearchText=<%=sSearchText%>";
	</script>