<!--#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 aIDOptionGroup
	Dim sMessage, ctArray, sTemp, objUpload

	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
			sIDProduct				= CONVERSION_SQLEncode2(objUpload("idProduct").Value,10)
			pSku					= CONVERSION_SQLEncode2(objUpload("sku").Value,100)
			pDescription			= CONVERSION_SQLEncode2(objUpload("description").Value,100)
			pDetails				= objUpload("details").Value
			pIdCategory1			= CONVERSION_Numeric(objUpload("idCategory1").Value, C_INTEGER)
			pIdCategory2			= CONVERSION_Numeric(objUpload("idCategory2").Value, C_INTEGER)
			pIdCategory3			= CONVERSION_Numeric(objUpload("idCategory3").Value, C_INTEGER)
			sOriginalidCategory1	= CONVERSION_Numeric(objUpload("OriginalidCategory1").Value, C_INTEGER)
			sOriginalidCategory2	= CONVERSION_Numeric(objUpload("OriginalidCategory2").Value, C_INTEGER)
			sOriginalidCategory3	= CONVERSION_Numeric(objUpload("OriginalidCategory3").Value, C_INTEGER)
			pPrice					= 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")
			sDelCurImage			= objUpload("sDelCurImage").Value
			sOriginalImageName		= objUpload("sOriginalImageName").Value
			sDelCurImageSmall		= objUpload("sDelCurImageSmall").Value
			sOriginalImageNameSmall	= objUpload("sOriginalImageNameSmall").Value
		End If
		
		pDetails			= SQLEncode(pdetails)
		pDescription		= SQLEncode(pdescription)
		
		sSku = Replace(pSku, "/", "-")
		sSku = Replace(sSku, "\", "-")
		sSku = Replace(sSku, "&", "-")
		sSku = Replace(sSku, "'", "-")
		sSku = Replace(sSku, """", "-")
		sSku = Replace(sSku, ",", "-")
		sSku = Replace(sSku, "|", "-")
		sDestFilename = sSku & ".jpg"

		' they swapped out images so delete the original one, or they are asking to delete the image outright
		If ( NOT bOWAdvancedImages ) Or ( sOWImageComponent = "" ) Then
			If ( sDelCurImage = "Y" ) Then
				' they swapped out images so delete the original one, or they are asking to delete the image outright
				Call DeleteProductImage(sOriginalImageName, "")
				sDestFilename = ""
			End If
			If ( sDelCurImageSmall = "Y" ) Then
				Call DeleteProductImage(sOriginalImageNameSmall, "Small")
				sOriginalImageNameSmall = ""
			End If
		Else	' advanced images
			If ( sDelCurImage = "Y" ) Then
				DeleteAllImages(sDestFilename)
				sDestFilename = ""
			End If
			If ( objUpload("imageUrl").Length > 0 ) Then
				DeleteAllImages(sDestFilename)
			End If
			' if the len = 0 and they aren't deleting the image, then they just want it to stay the same
		End If

		If ( UCase(sDelCurImage) <> "Y" ) Then
			sMessage = G_UPLOAD_SaveFile("imageUrl", sOWImagesDir, "", sDestFilename)
			If ( NOT bOWAdvancedImages ) Or ( sOWImageComponent = "" ) Then	' Advanced images are turned off so check for an uploaded thumbnail
				If ( objUpload("smallImageUrl").Length > 0 ) Then
					sMessage = sMessage & G_UPLOAD_SaveFile("smallImageUrl", sOWImagesDir, "/small", sDestFilename)
					sOriginalImageNameSmall = sDestFilename
				End If
			Else	' advanced images
				If ( objUpload("imageUrl").Length > 0 ) Then
					sMessage = sMessage & IMAGE_ResizeImage(sDestFilename, C_UPLOAD_PRIMARY)
				End If
			End If
		End If
		
		If ( sMessage = "" ) Then
			If ( objUpload("imageUrl").Length > 0 ) Or ( sDelCurImage = "Y" ) Then
				Call DB_updateDatabase("UPDATE Products SET imageUrl='" & sDestFilename & "' WHERE  idProduct=" & sIDProduct)
			End If
			' if the smallimage size is > 0 or they are deleting the small image, OR they are using advanced images (the small image name
			'	then defaults to the large image's name), then set or reset the small image file name

			If ( objUpload("smallImageUrl").Length > 0 ) Or ( sDelCurImageSmall = "Y" ) Or ( ( bOWAdvancedImages AND sOWImageComponent <> "" ) And ( objUpload("ImageUrl").Length > 0 ) ) Then
				If ( sOriginalImageNameSmall = "" ) And ( sDelCurImageSmall <> "Y" ) Then
					sOriginalImageNameSmall = sDestFilename
				End If
				Call DB_updateDatabase("UPDATE Products SET smallImageUrl='" & sOriginalImageNameSmall & "' WHERE idProduct=" & sIDProduct)
			End If

			sSQLText = "UPDATE products SET sku='" &pSku& "', description='" &pDescription& "', details='" &pDetails& "',price=" &pPrice& ", listPrice=" &pListPrice& ", cost=" &pCost& ", weight=" &pWeight& ", length=" &pLength& ", width=" &pWidth& ", height=" &pHeight& ", listHidden=" &plistHidden& ", hotDeal=" &pHotDeal& ",active=" &pActive& ", showInHome=" &pShowInHome& ", bToBPrice=" &pBToBPrice& ", formQuantity=" &pFormQuantity&" WHERE idProduct=" &sIDProduct
			Call DB_updateDatabase(sSQLText)
			
			' 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
			
			if ( pUseUnlimitedCategsAssignment = 0 ) then
				sSQLText = "DELETE FROM CatProdMap WHERE idProduct=" &sIDProduct
				Call DB_updateDatabase(sSQLText) 

				' must delete the previous entry for this product or else there will be duplicates in the table
				'	must also make sure that category 2 and 3 aren't duplicates
				If ( sOriginalidCategory1 <> "" ) Then
					Call DB_updateDatabase("DELETE FROM CatProdMap WHERE idProduct = " & sIDProduct & " AND idCategory = " & sOriginalidCategory1)
				End If
				If ( pIdCategory1 <> "1" ) Then		' don't want to add to the root
					sSQLText = "INSERT INTO CatProdMap (idProduct, idCategory) VALUES (" &sIDProduct& "," &pIdCategory1& ")"
				End If
				Call DB_updateDatabase(sSQLText)

				if pIdCategory2<>"" And pIdCategory2 <> pidCategory1 And ( pIdCategory2 <> "1" ) then
					If ( sOriginalidCategory2 <> "" ) Then
						Call DB_updateDatabase("DELETE FROM CatProdMap WHERE idProduct = " & sIDProduct & " AND idCategory = " & sOriginalidCategory2)
					End If
					sSQLText = "INSERT INTO CatProdMap (idProduct, idCategory) VALUES (" &sIDProduct& "," &pIdCategory2& ")"
					Call DB_updateDatabase(sSQLText)
				end if

				if pIdCategory3<>"" And pIdCategory3 <> pidCategory1 And pIdCategory3 <> pidCategory2 And ( pIdCategory3 <> "1" ) then
					If ( sOriginalidCategory3 <> "" ) Then
						Call DB_updateDatabase("DELETE FROM CatProdMap WHERE idProduct = " & sIDProduct & " AND idCategory = " & sOriginalidCategory3)
					End If
					sSQLText = "INSERT INTO CatProdMap (idProduct, idCategory) VALUES (" &sIDProduct& "," &pIdCategory3& ")" 
					Call DB_updateDatabase(sSQLText)
				end if
			end if
		End If

		%>
		<script language = "javascript">
			location="owModifyProductForm.asp?bRefresh=TRUE&sSort=<%=sSort%>&sSearch=<%=sSearch%>&sSearchText=<%=sSearchText%>&IdProduct=<%=sIDProduct%>";
		</script>
		<%
	End If
	'************************************************************************************

%>