<!--#include file="../owFunctions/I_GeneralFunctions.asp"-->
<!--#include file="../owFunctions/I_StoreFront.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 sEmail, sZip, sCountryCode, sShipToState
	Dim bWhichForm, sAction, sError, sSuccess, bDisplayForm
	
	Call SF_OpenPage()	' call this to populate the cart information - this is also called in the template, but since we are doing this out of order, we need a special case here
	Call owMain()
	

	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Sub owMain()
	
		sAction			= Trim(Request("sAction"))
		sError			= ""
		sSuccess		= ""

		Select Case sAction
			Case "PerformRegistration"
				sName					= CONVERSION_SQLEncode2(Request("name"),50)
				sLastName				= CONVERSION_SQLEncode2(Request("lastName"),50)
				sCustomerCompany		= CONVERSION_SQLEncode2(Request("customerCompany"),50)
				sEmail					= CONVERSION_SQLEncode2(Request("email"),80)
				sPassword				= CONVERSION_SQLEncode2(EnCrypt(Request("sPassword"), sOWEncryptionPassword),50)
				sPassword2				= CONVERSION_SQLEncode2(EnCrypt(Request("sPassword2"), sOWEncryptionPassword),50)
				sPhone					= CONVERSION_SQLEncode2(Request("phone"),20)
				sAddress				= CONVERSION_SQLEncode2(Request("address"),100)
				sCity					= CONVERSION_SQLEncode2(Request("city"),30)
				sZip					= CONVERSION_SQLEncode2(Request("zip"),12)
				sState					= CONVERSION_SQLEncode2(Request("state"),30)
				sStateCode				= CONVERSION_SQLEncode2(Request("stateCode"),30)
				sCountryCode			= CONVERSION_SQLEncode2(Request("countryCode"),4)
				sShippingName			= CONVERSION_SQLEncode2(Request("sShippingName"),50)
				sShippingLastName		= CONVERSION_SQLEncode2(Request("sShippingLastName"),50)
				sShippingAddress		= CONVERSION_SQLEncode2(Request("shippingAddress"),100)
				sShippingCity			= CONVERSION_SQLEncode2(Request("shippingCity"),30)
				sShippingZip			= CONVERSION_SQLEncode2(Request("shippingZip"),12)
				sShippingState			= CONVERSION_SQLEncode2(Request("shippingState"),30)
				sShippingStateCode		= CONVERSION_SQLEncode2(Request("shippingStateCode"),30)
				sShippingCountryCode	= CONVERSION_SQLEncode2(Request("shippingCountryCode"),4)
				
				If ( sShippingAddress = "" ) Then
					sShipToState	= sStateCode
					sShipToZip		= sZip
					sShipToCountry	= sCountryCode
				Else
					sShipToState	= sShippingStateCode
					sShipToZip		= sShippingZip
					sShipToCountry	= sShippingCountryCode
				End If
		
				' verify password
		'response.write ":" & sPassword & ":<BR>"
		'response.write ":" & sPassword2 & ":<BR><BR>"
				if sPassword<>sPassword2 then
					sError = LANG_DT( "CHECKOUT_PWINCORRECT", "Password verification incorrect" )
				Else
					if sPassword="" and sPassword2="" then
						randomize
						sPassword = EnCrypt(int(rnd*99999999)+1, sOWEncryptionPassword)
					End If 
		
					if sState<>"" and sStateCode<>"" then
						sError = LANG_DT( "CHECKOUT_SELECTANOTHERSTATE", "Please select State or Another State, you cannot select both." )
					Else
						if sShippingAddress<>"" And (sShippingZip="" or sShippingCity="" or sShippingCountryCode="") then
							sError = LANG_DT( "CHECKOUT_ENTERALLSHIPPING", "To use a specified shipping address, please fill in all shipping address fields." )
						Else
		'response.write "here1<br>"
		'response.write "--:" & EmailExists(sEmail) & ":<BR>"
							If ( EmailExists(sEmail) ) Then
								sError = LANG_DT( "CHECKOUT_EMAILEXISTS", "That email has already been taken. Please choose another." )
		'response.write "here2<br>"
		'response.end
							Else
		'response.write "here3<br>"
								sSQLText = "INSERT INTO owAccounts ( Login, Password, AddDate, RemoveDate, HomeDomain, IgnoreGlobal, Authenticated, AuthID, ExpireDate ) VALUES ('" & sEmail & "', '" & sPassword & "', #" & CONVERSION_FormatDate(Now(), "L") & "#, null, 0, '', 'Y', 0, null)"
		'response.write sSQLText & "<Br>"
								Call DB_updateDatabase(sSQLText)
								
								sSQLText = "SELECT AccountUnq FROM owAccounts WHERE Login = '" & sEmail & "'"
								Call DB_getFromDatabase(sSQLText, rsStoreData2, "")
								If ( rsStoreData2.EOF ) Then
		'response.write "here4<br>"
									sError = LANG_DT( "CHECKOUT_MISSINGID", "Cannot locate customer ID inside" )
								Else
		'response.write "here5<br>"
									sAccountUnq = rsStoreData2("AccountUnq")
									Call ACCNT_WriteADV("OW_CUSTPHONE", sPhone, "V", sAccountUnq, sViewLvl)
									Call ACCNT_WriteADV("OW_CUSTEMAIL", sEmail, "V", sAccountUnq, sViewLvl)
									Call ACCNT_WriteADV("OW_CUSTCOMPANY", sCustomerCompany, "V", sAccountUnq, sViewLvl)
									Call ACCNT_WriteADV("OW_CUSTFNAME", sName, "V", sAccountUnq, sViewLvl)
									Call ACCNT_WriteADV("OW_CUSTLNAME", sLastName, "V", sAccountUnq, sViewLvl)
		'response.write "here6<br>"
									Session("Login")		= sEmail
									Session("Password")		= sPassword
									Session("AccountUnq")	= sAccountUnq
									Session("Auto")			= "N"	' for future use
		'response.write "here7<br>"
									' insert the shipping address - even if it's blank so we don't have to do outer joins and such
									sSQLText = "INSERT INTO CustAltShip (AccountUnq, ShippingName, ShippingLastName, ShippingAddress, ShippingCity, ShippingStateCode, ShippingState, ShippingCountryCode, ShippingZip, Position) VALUES (" & sAccountUnq & ", '" & sShippingName & "', '" & sShippingLastName & "', '" & sShippingAddress & "', '" & sShippingCity & "', '" & sShippingStateCode & "', '" & sShippingState & "', '" & sShippingCountryCode & "', '" & sShippingZip & "', 0)"
									call DB_updateDatabase(sSQLText)
		'response.write "here8<br>"
									sSQLText = "INSERT INTO CustAltBill (AccountUnq, BillingAddress, BillingCity, BillingStateCode, BillingState, BillingCountryCode, BillingZip, Position) VALUES (" & sAccountUnq & ", '" & sAddress & "', '" & sCity & "', '" & sStateCode & "', '" & sState & "', '" & sCountryCode & "', '" & sZip & "', 0)"
									call DB_updateDatabase(sSQLText)
									
									Call DB_updateDatabase("INSERT INTO owAGroupMap (GroupUnq, AccountUnq, IgnoreGlobal, DomainUnq) VALUES (" & C_GROUP_RETAIL & ", " & sAccountUnq & ", '', 0)")
									
									If ( sIDCart <> "" ) Then
										Call SESSION_SaveGlobals()
										Response.Redirect "owShippingCostQueryExec.asp?bCheckout=T&sShippingStateCode=" & sShipToState & "&sCountryCode=" & sShipToCountry & "&sZip=" & sShipToZip & "&sAction=ZipOnly"
									Else
										' figure something out for those who don't have anything in their cart
										sSuccess = Replace(LANG_DT( "CHECKOUT_REGTHANKS", "Thank you for registering!<br><br><a href='1:'>Please continue to shop by clicking here.</a>" ), "1:", sOWStoreUrlNonSSL)
									End If
									
								End If
		'response.write "here9<br>"
								Call DB_closeRecordset(rsStoreData2)
							End If
		'response.write "here10<br>"
						End If
		'response.write "here11<br>"
					End If
		'response.write "here12<br>"
				End If
		'response.write "here13<br>"

		End Select

'response.write sError		
	End Sub
	'************************************************************************************

	
	'************************************************************************************
	'*																					*
	'*	Just check for existing email address. Cust might have entered a bogus pw, so	*
	'*		we don't want to check against that.										*
	'*																					*
	'************************************************************************************
	Function EmailExists(sEmail)
		
		Dim rsStoreData
		
		Set rsStoreData	= CreateObject("ADODB.Recordset")
		
		sSQLText = "SELECT AccountUnq FROM owAccounts WHERE Login = '" & sEmail & "'"
'response.write sSQLText & "<BR>"
		Call DB_getFromDatabase(sSQLText, rsStoreData, "") 
		If ( rsStoreData.EOF ) Then
			EmailExists = False
		Else
			EmailExists = True
		End If
		Call DB_closeRecordset(rsStoreData)

		Set rsStoreData	= Nothing
		
	End Function
	'************************************************************************************
	%>