<!--#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/											*
	'*																					*
	'************************************************************************************

	Dim sEmail, sZip, sCountryCode, sShipToState
	Dim bWhichForm, sAction, sError
	
	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Sub ActionSubroutine()

		sAction			= Trim(Request("sAction"))
		sEmail 			= Trim(Request("sEmail"))
		sPassword 		= Trim(Request("sPassword"))
		sError			= ""

		Select Case sAction
			Case "LoggedIn"		' the user is logged in, has checked their ship/bill info, and clicked the "next" button. now we have to save their info and redirect them as required
				sName					= Request("name")
				sLastName				= Request("lastName")
				sCustomerCompany		= Request("customerCompany")
				sPhone					= Request("phone")
				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
					sShipToCountryCode	= sCountryCode
				Else
					sShipToState		= sShippingStateCode
					sShipToZip			= sShippingZip
					sShipToCountryCode	= sShippingCountryCode
				End If
				
				If ( ( sState <> "" ) And  ( sStateCode <> "" ) ) Then
					sError = LANG_DT( "MODCHECKOUT_SELECTSTATE", "Please select State/Provence or Another State/Provence, you cannot select both." )
				Else
					' check if the entire shipping address was entered
					If ( sShippingAddress <> "" ) Then
						If ( ( sShippingZip = "" ) Or ( sShippingCity = "" ) Or ( sShippingCountryCode = "" ) ) Then
							sError = LANG_DT( "MODCHECKOUT_ALLSHIP", "If you want to enter shipping address, please complete all the fields." )
						End If
					End If
		
					If ( sError = "" ) Then
						if sShippingAddress <> "" then
							sSQLText = "UPDATE CustAltShip SET ShippingLastName = '" & sShippingLastName & "', ShippingName = '" & sShippingName & "', shippingAddress='" & sShippingAddress & "', shippingZip='" & sShippingZip & "', shippingStateCode='" & sShippingStateCode & "', shippingState='" & sShippingState & "', shippingCity='" & sShippingCity &"', shippingCountryCode='" & sShippingCountryCode & "' WHERE AccountUnq = " & iLoginAccountUnq & " AND Position = 0"
							call DB_updateDatabase(sSQLText)
						End If
						
						sSQLText = "UPDATE CustAltBill SET BillingAddress='" & sAddress & "', BillingZip='" & sZip & "', BillingStateCode='" & sStateCode & "', BillingState='" & sState & "', BillingCity='" & sCity &"', BillingCountryCode='" & sCountryCode & "' WHERE AccountUnq = " & iLoginAccountUnq & " AND Position = 0"
						call DB_updateDatabase(sSQLText)
						
						Call ACCNT_WriteADV("OW_CUSTPHONE", sPhone, "V", iLoginAccountUnq, sViewLvl)
						Call ACCNT_WriteADV("OW_CUSTCOMPANY", sCustomerCompany, "V", iLoginAccountUnq, sViewLvl)
						Call ACCNT_WriteADV("OW_CUSTFNAME", sName, "V", iLoginAccountUnq, sViewLvl)
						Call ACCNT_WriteADV("OW_CUSTLNAME", sLastName, "V", iLoginAccountUnq, sViewLvl)
		
						Response.Redirect "owShippingCostQueryExec.asp?sShippingStateCode=" & sShipToState & "&bCheckout=T&sCountryCode=" & sShipToCountryCode & "&sZip=" & sShipToZip & "&sAction=ZipOnly"
					End If
				End If
			
			Case Else
				if CART_CountRows() = 0 then
					sError = LANG_DT( "CHECKOUT_EMPTYCART", "Your cart is empty. Please add at least one product to your cart before proceeding to check out." )
				Else
					' check if user already logged in
					If ( iLoginAccountUnq > 0 ) Then
						sAction = "LoggedIn"
					Else
						Response.Redirect "owCustomerAuthenticate.asp?sRedirectUrl=1"	' sRedirectUrl of 1 means to redirect back to here - when we redirect back here logged in, it'll display the customer billing/shipping information
					End If
				End If
		End Select
		Call DispForm()
		
	End Sub
	'************************************************************************************
		

	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Sub DispForm()
	
		Dim sURL, sTitle, sButtonText

			If ( sError <> "" ) Then
				Call owMessage(sError, C_ERROR)
				Response.Write "<br><br>"
			End If
			
			If ( sAction = "LoggedIn" ) Then
				sTitle		= LANG_DT( "CHECKOUT_CHECKOUTINFO", "Checkout Information" )
				sButtonText	= LANG_DT( "CHECKOUT_CONTCHECK", "Continue with Checkout &gt;" )
				
				If ( iLoginAccountUnq > 0 ) Then
					' we know their AccountUnq
					sSQLText = "SELECT * FROM CustAltBill CAB, CustAltShip CAS, owAccounts A WHERE CAS.AccountUnq = CAB.AccountUnq AND CAB.AccountUnq = " & iLoginAccountUnq & " AND A.AccountUnq = CAB.AccountUnq AND ( A.AddDate > A.RemoveDate OR A.RemoveDate IS NULL ) AND A.Authenticated = 'Y' AND CAS.Position = 0"
				Else
					' they just logged in and we don't know their AccountUnq, so use their login/pw instead
					sSQLText = "SELECT * FROM CustAltBill CAB, CustAltShip CAS, owAccounts A WHERE CAS.AccountUnq = CAB.AccountUnq AND A.Password = '" & EnCrypt(sPassword, sOWEncryptionPassword) & "' AND A.Login = '" & SQLEncode(sEmail) & "' AND CAB.AccountUnq = A.AccountUnq AND ( A.AddDate > A.RemoveDate OR A.RemoveDate IS NULL ) AND A.Authenticated = 'Y' AND CAS.Position = 0"
				End If
				Call DB_getFromDatabase(sSQLText, rsStoreData, "")
				If ( rsStoreData.EOF ) Then
					Call owMessage(LANG_DT( "CHECKOUT_NONMATCHINGPW", "Your email and password did not match up, or was not found in our store. Please try a different email and/or password." ), C_ERROR)
				Else
					If ( iLoginAccountUnq <= 0 ) Then
						iLoginAccountUnq = rsStoreData("AccountUnq")
					End If
					sName					= ACCNT_ReturnADV("OW_CUSTFNAME", "V", iLoginAccountUnq, C_OVERRIDE_ADV)
					sLastName				= ACCNT_ReturnADV("OW_CUSTLNAME", "V", iLoginAccountUnq, C_OVERRIDE_ADV)
					sPhone					= ACCNT_ReturnADV("OW_CUSTPHONE", "V", iLoginAccountUnq, C_OVERRIDE_ADV)
					sEmail					= ACCNT_ReturnADV("OW_CUSTEMAIL", "V", iLoginAccountUnq, C_OVERRIDE_ADV)
					sCustomerCompany		= ACCNT_ReturnADV("OW_CUSTCOMPANY", "V", iLoginAccountUnq, C_OVERRIDE_ADV)
				
					sAddress				= rsStoreData("BillingAddress")
					sZip					= rsStoreData("BillingZip")
					sStateCode				= rsStoreData("BillingStateCode")
					sState					= rsStoreData("BillingState")
					sCity					= rsStoreData("BillingCity")
					sCountryCode			= rsStoreData("BillingCountryCode")
					
					sShippingName			= rsStoreData("ShippingName")
					sShippingLastName		= rsStoreData("ShippingLastName")
					sShippingAddress		= rsStoreData("ShippingAddress")
					sShippingZip			= rsStoreData("ShippingZip")
					sShippingStateCode		= rsStoreData("ShippingStateCode")
					sShippingState			= rsStoreData("ShippingState")
					sShippingCity			= rsStoreData("ShippingCity")
					sShippingCountryCode	= rsStoreData("ShippingCountryCode")
	
					Call SESSION_SaveGlobals()	' saves this information to their cart
				End If
				Call DB_closeRecordset(rsStoreData)
			Else
				sTitle		= LANG_DT( "CHECKOUT_REG", "Please Register" )
				sButtonText	= LANG_DT( "CHECKOUT_REGFINALIZE", "Finalize Registration &gt;" )
			End If
	
			%>
			<script type="text/javascript" src="../owManager/Script/fValConfig.js"></script>
			<script type="text/javascript" src="../owManager/Script/fValidate.js"></script>
			
			<script language='JavaScript1.2' type='text/javascript'>
		
				function SubmitForm(objForm, sAction){
				
					if ( validateForm( objForm, false, false, false, true, 17 ) )
					{
						objForm.sAction.value = sAction;
						objForm.submit();
					}else{
						return false;
					}
				}
				
			</script>
			
			<center>
			<table width=<%=sOWStoreTableWidth%> border=0 style='border: 1px solid #000000;' cellspacing=0 cellpadding=5>
				<form action="owCheckOut.asp" METHOD="POST" name="orderform" onSubmit='return SubmitForm(this, "<%=sAction%>");'>
				<input type='hidden' name='sAction' value='<%=sAction%>'>
				<tr bgcolor="#eeeeee"><td COLSPAN=2 style='border-bottom: 1px solid #000000;'><b><%=sTitle%></b></td></tr>
				<TR><TD COLSPAN=2><b><%=LANG_DT( "CHECKOUT_CUSTOMER", "Customer" )%></b></TD></TR>    
				<TR> 
					<TD>*<%=LANG_DT( "CHECKOUT_FNAME", "First Name" )%></TD>
					<TD><%VALIDATE_textbox "name", sName, 30, "textbox", "blank"%></TD>
				</TR>
				<TR> 
					<TD>*<%=LANG_DT( "CHECKOUT_LNAME", "Last Name" )%></TD>
					<TD><%VALIDATE_textbox "lastName", sLastName, 30, "textbox", "blank"%></TD>
				</TR>   
				<TR> 
					<TD><%=LANG_DT( "CHECKOUT_COMPANY", "Company Name" )%></TD>
					<TD><%VALIDATE_textbox "customerCompany", sCustomerCompany, 30, "textbox", ""%></TD>
				</TR>   
				<TR> 
					<TD>*<%=LANG_DT( "CHECKOUT_PNUM", "Phone Number" )%></TD>
					<TD><%VALIDATE_textbox "phone", sPhone, 30, "textbox", "blank"%></TD>
				</TR>
				<TR> 
					<TD>*<%=LANG_DT( "CHECKOUT_EMAILMSG", "Email (use this to log in)" )%></TD>
					<TD>
						<%If ( sAction = "LoggedIn" ) Then%>
						<%=sEmail%>
						<%Else%>
						<%VALIDATE_textbox "email", "", 40, "textbox", "blank"%>
						<%End If%>
					</TD>
				</TR>
				<%If ( sAction <> "LoggedIn" ) Then ' have them change their login/pw elsewhere. it gets too messy for the customer to do it here %>
				<tr>
					<td><%=LANG_DT( "CHECKOUT_PW", "Password" )%></td>
					<td><%VALIDATE_textbox "sPassword", sPassword, 30, "password", "blank"%></td>
				</tr>
				<tr>
					<td><%=LANG_DT( "CHECKOUT_PW2", "Re-Enter Password" )%></td>
					<td><%VALIDATE_textbox "sPassword2", sPassword, 30, "password", "blank"%></td>
				</tr>
				<%End If%>
				<TR><TD colspan="2"><br><b><%=LANG_DT( "CHECKOUT_BILLINGADD", "Billing address" )%></b></TD></TR>
				<tr>
					<TD>*<%=LANG_DT( "CHECKOUT_ADDRESS", "Address" )%></TD>
					<TD><%VALIDATE_textbox "address", sAddress, 30, "textbox", "blank"%></TD>
				</TR>
				<TR> 
					<td>*<%=LANG_DT( "CHECKOUT_CITY", "City" )%></TD>
					<td><%VALIDATE_textbox "city", sCity, 30, "textbox", "blank"%></TD>
				</TR>
				<TR> 
					<TD>*<%=LANG_DT( "CHECKOUT_STATE", "State/Provence" )%></TD>
					<TD>
						<%
						sSQLText = "SELECT S.StateCode, S.StateName FROM StateCodes S, CountryCodes C WHERE S.Active = 'Y' AND S.CountryCode = C.CountryCode AND C.Active = 'Y' ORDER BY S.StateName"
						Call DB_getFromDatabase(sSQLText, rsStoreData2, "")      
						%>
						<SELECT name=stateCode size=1>
							<%
							While ( NOT rsStoreData2.EOF )
								sStateCode2 = rsStoreData2("stateCode")
								%>
								<option value="<%=sStateCode2%>" <%if (request("StateCode") = "" and sStateCode2 = sStateCode) Or (request("StateCode") = sStateCode2) then response.write "selected" End If%>><%=rsStoreData2("stateName")%></OPTION>
								<%
								rsStoreData2.MoveNext
							Wend
							Call DB_closeRecordset(rsStoreData2)
							%>
						</SELECT>
					</TD>
				</TR>
				<TR>  
					<TD><%=LANG_DT( "CHECKOUT_ANOTHERSTATE", "Another State/Provence" )%></TD>
					<TD><%VALIDATE_textbox "state", sState, 30, "textbox", ""%></TD>
				</TR>
				
				<TR> 
					<TD>*<%=LANG_DT( "CHECKOUT_ZIP", "Zip/Postal Code" )%></TD>
					<TD><%VALIDATE_textbox "zip", sZip, 30, "textbox", "blank"%></TD>
				</TR>
				<TR> 
					<TD>*<%=LANG_DT( "CHECKOUT_COUNTRY", "Country" )%></TD>
					<TD>  
						<%
						sSQLText = "SELECT countryCode, countryName FROM countryCodes WHERE Active = 'Y' ORDER BY Position"
						Call DB_getFromDatabase(sSQLText, rsStoreData2, "")
						%>
						<SELECT name="countryCode">
							<%
							While ( NOT rsStoreData2.EOF )
								sCountryCode2 = rsStoreData2("countryCode")
								%>
								<option value="<%=sCountryCode2%>" <%if (request("countryCode")="" and sCountryCode=sCountryCode2) Or (request("countryCode")=sCountryCode2) then response.write "selected" End If%>><%=rsStoreData2("countryName")%></OPTION>
								<%
								rsStoreData2.MoveNext
							Wend
							Call DB_closeRecordset(rsStoreData2)
							%>
						</SELECT>
					</TD>
				</TR>    
				<%if bOWAllowShippingAddress then%>
				<tr><td colspan="2"><br><b><%=LANG_DT( "CHECKOUT_SHIPPINGADDRESS", "Shipping address (optional)" )%></b></td></tr>
				<TR> 
					<TD><%=LANG_DT( "CHECKOUT_FNAME", "First Name" )%></TD>
					<TD><%VALIDATE_textbox "sShippingName", sShippingName, 30, "textbox", ""%></TD>
				</TR>
				<TR> 
					<TD><%=LANG_DT( "CHECKOUT_LNAME", "Last Name" )%></TD>
					<TD><%VALIDATE_textbox "sShippingLastName", sShippingLastName, 30, "textbox", ""%></TD>
				</TR>
				<tr>
					<td><%=LANG_DT( "CHECKOUT_ADDRESS", "Address" )%></td>
					<td><%VALIDATE_textbox "shippingAddress", sShippingAddress, 50, "textbox", ""%></td>
				</tr>
				<tr> 
					<td><%=LANG_DT( "CHECKOUT_CITY", "City" )%></td>
					<td><%VALIDATE_textbox "shippingCity", sShippingCity, 50, "textbox", ""%></td>
				</tr>
				<tr> 
					<td><%=LANG_DT( "CHECKOUT_STATE", "State/Provence" )%></td>
					<td>
						<%
						sSQLText = "SELECT S.StateCode, S.StateName FROM StateCodes S, CountryCodes C WHERE S.Active = 'Y' AND S.CountryCode = C.CountryCode AND C.Active = 'Y' ORDER BY S.StateName"
						Call DB_getFromDatabase(sSQLText, rsStoreData2, "") 
						%>
						<SELECT name="shippingStateCode" size=1>
							<%
							While ( NOT rsStoreData2.EOF )
								sStateCode2 = rsStoreData2("stateCode")
								%>
								<option value="<%=sStateCode2%>" <%if sStateCode2=sShippingStateCode then response.write "selected" End If%>><%=rsStoreData2("stateName")%></OPTION>
								<%
								rsStoreData2.movenext
							Wend
							Call DB_closeRecordset(rsStoreData2)
							%>							
						</SELECT>
					</td>
				</tr>
			
				<tr> 
					<td><%=LANG_DT( "CHECKOUT_ANOTHERSTATE", "Another State/Provence" )%></td>
					<td><%VALIDATE_textbox "shippingState", sShippingCity, 30, "textbox", ""%></td>
				</tr>
				
				<tr> 
					<td><%=LANG_DT( "CHECKOUT_ZIP", "Zip/Postal Code" )%></td>
					<td><%VALIDATE_textbox "shippingZip", sShippingZip, 10, "textbox", ""%></td>
				</tr>
				<tr> 
					<td><%=LANG_DT( "CHECKOUT_COUNTRY", "Country" )%></td>
					<td>                 
						<%
						sSQLText = "SELECT countryCode, countryName FROM countryCodes WHERE Active = 'Y' ORDER BY Position"
						Call DB_getFromDatabase(sSQLText, rsStoreData2, "") 
						%>
						<SELECT name="shippingCountryCode" size=1>
							<%
							While ( NOT rsStoreData2.EOF )
								sShippingCountryCode2 = rsStoreData2("countryCode")
								%>
								<option value="<%=sShippingCountryCode2%>" <%if sShippingCountryCode2=sShippingCountryCode then response.write "selected" End If%>><%=rsStoreData2("countryName")%></OPTION>
								<%
								rsStoreData2.movenext
							Wend
							Call DB_closeRecordset(rsStoreData2)
							%>
						</SELECT>
					</td>
				</tr>
				<%End If%>
				<TR> 
					<td style='border-top: 1px solid #000000;'>
						* <%=LANG_DT( "CHECKOUT_REQUIRED", "required field" )%>
					</td>
					<td align=right style='border-top: 1px solid #000000;'>
						<input type="submit" name="save" value=" <%=sButtonText%> ">
					</td>
				</TR>
				</form>
			</table>
			<%

	End Sub
	'************************************************************************************
	%>