<!--#include file="../owFunctions/I_GeneralFunctions.asp"-->
<!--#include file="../owFunctions/I_Shipping.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										*
	'*				htp://strategis.ic.gc.ca/											*
	'*																					*
	'************************************************************************************

	Dim sOWShipWeightType, sOWShipDimensionType, shipmentsArray
	Dim shipmentIndex, sCartQuantity, sCartTotalWeight
	
	
	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Sub ActionSubroutine()
	
		ReDim shipmentsArray(100,7)

		sShippingCountryCode		= Trim(Request("sCountryCode"))
		sShippingZip				= Trim(Request("sZip"))
		sShippingStateCode			= Trim(Request("sShippingStateCode"))	' required for Canada Post shipping (it's can't use just the postal code/country
		bCheckout					= Trim(Request("bCheckout"))
		
		If ( sShippingStateCode = "" ) Then
			' if the state is blank, try getting it from the zip
			sShippingStateCode = STRUCTURE_ReturnState(sShippingCountryCode, sShippingZip)
		End If
		
		shipmentIndex 				= 0
		sOWShipWeightType			= owCONFIG_ReturnConfigValue("sOWShipWeightType")
		sOWShipDimensionType		= owCONFIG_ReturnConfigValue("sOWShipDimensionType")
		dMaxLength					= 0
		dMaxHeight					= 0
		dWidthSummation				= 0
		sSubTotal  					= CART_CalcTotal()
		sCartTotalWeight 			= CART_CalcWeight()
		sCartQuantity  				= CART_CalcQty()
		sCartFreeShippingWeight 	= CART_CalcFreeShipWeight()

		Call CART_CalcVolume(dMaxLength, dMaxHeight, dWidthSummation)
		
		If ( sOWCurrencyCode <> "" ) Then sTempCurrencyCode = "(" & sOWCurrencyCode & ")" End If
		
		If ( sShippingCountryCode <> "" ) Then
			sDestTerritory		= STRUCTURE_TerritoryLabel(sShippingCountryCode)
			sSourceTerritory	= STRUCTURE_TerritoryLabel(sOriginatingCountry)
		Else
			sDestTerritory		= "State"
			sSourceTerritory	= "State"
		End If
		sDestCity	= STRUCTURE_ReturnCity(sShippingCountryCode, sShippingZip, sDestFullStateName)	' can't use sFullStateName in the UPS info

		If ( ( (sCartTotalWeight - sCartFreeShippingWeight) <= 0 ) And ( sCartQuantity > 0 ) ) Then
			sFreeShippingText = LANG_DT( "SHIPQUERYEXEC_FREESHIP", "Congradulations! You are eligable for FREE shipping with this order." )
		Else
			sNRTError = RetrieveNonRealTimeShipping(sShippingZip, sShippingCountryCode, sShippingStateCode)
		End If

		If ( sError = "" ) Then
			%>
			<table width=<%=sOWStoreTableWidth%> border=0 style='border: 1px solid #000000;' cellspacing=0 cellpadding=5>
				<%If ( bCheckout = "T" ) Then%>
				<form METHOD='POST' name='shippingform' action='owSelectPayment.asp'>
				<%End If%>
				<tr bgcolor="#eeeeee"><td colspan=3 style='border-bottom: 1px solid #000000;'><b><%=LANG_DT( "SHIPQUERYEXEC_METHODS", "Available Shipping Methods" )%></b></td></tr>
				<tr>
					<td colspan=3>
						<table width=100%>
							<tr>
								<td width=50%>
								</td>
								
								<td style='border-right: 1px solid #000000;'>&nbsp;</td>
								
								<td width=50%>					
									<table width=100%>
										<tr>
											<td colspan=3 style='border-bottom: 1px solid #000000;' bgcolor=EEEEEE>
												<b><%=LANG_DT( "SHIPQUERYEXEC_DEST", "Destination" )%>:
											</td>
										</tr>
										<tr>
											<td style='border-bottom: 1px solid #EEEEEE;'>
												<%=LANG_DT( "SHIPQUERYEXEC_COUNTRY", "Country" )%>
											</td>
											<td style='border-bottom: 1px solid #EEEEEE;'>&nbsp;&nbsp;&nbsp;</td>
											<td style='border-bottom: 1px solid #EEEEEE;'>
												<%
												sSQLText = "SELECT CountryName FROM CountryCodes WHERE CountryCode = '" & SQLEncode(sShippingCountryCode) & "'"
												Call DB_getFromDatabase(sSQLText, rsStoreData, "")
												If ( NOT rsStoreData.EOF ) Then
													Response.Write rsStoreData("CountryName")
												Else
													Response.Write sShippingCountryCode
												End If
												Call DB_closeRecordset(rsStoreData)
												%>
											</td>
										</tr>
										<%If ( sDestFullStateName <> "" ) Then%>
										<tr>
											<td style='border-bottom: 1px solid #EEEEEE;'>
												<%=sDestTerritory%>
											</td>
											<td style='border-bottom: 1px solid #EEEEEE;'>&nbsp;&nbsp;&nbsp;</td>
											<td style='border-bottom: 1px solid #EEEEEE;'>
												<%=sDestFullStateName%>
											</td>
										</tr>
										<%End If%>
										<%If ( sDestCity <> "" ) Then%>
										<tr>
											<td style='border-bottom: 1px solid #EEEEEE;'>
												<%=LANG_DT( "SHIPQUERYEXEC_CITY", "City" )%>
											</td>
											<td style='border-bottom: 1px solid #EEEEEE;'>&nbsp;&nbsp;&nbsp;</td>
											<td style='border-bottom: 1px solid #EEEEEE;'>
												<%=sDestCity%>
											</td>
										</tr>
										<%End If%>
										<tr>
											<td style='border-bottom: 1px solid #EEEEEE;'>
												<%=STRUCTURE_PostalCodeLabel(sShippingCountryCode)%>
											</td>
											<td style='border-bottom: 1px solid #EEEEEE;'>&nbsp;&nbsp;&nbsp;</td>
											<td style='border-bottom: 1px solid #EEEEEE;'>
												<%=sShippingZip%>
											</td>
										</tr>
									</table>
								</td>
							</tr>
						</table>
					</td>
				</tr>
				<%If ( sFreeShippingText <> "" ) Then%>
				<tr>
					<td colspan=3>
						<center><b><%=sFreeShippingText%></b><br><br><br>
					</td>
				</tr>
				<%End If%>
				<%
				If ( sNRTError <> "" ) Then
					Response.Write "<tr><td colspan=2><br><br>"
					Call owMessage(sNRTError, C_ERROR)
					Response.Write "</td></tr>"
				Else
					If ( shipmentIndex >  0 ) Then
						%>
						<TR>
							<TD>
								<b><%=LANG_DT( "SHIPQUERYEXEC_DESC", "Shipment Description" )%></b><br>
							</TD>
							<TD>
								<b><%=LANG_DT( "SHIPQUERYEXEC_COST", "Shipment Cost" )%></b>&nbsp;<%=sTempCurrencyCode%><br>
							</TD>	
							<td></td>
						</TR>
						<%
						for iX=0 to shipmentIndex-1%>
						<TR>
							<TD><%=shipmentsArray(iX,1)%></TD>
							<TD>
								<% 
								if shipmentsArray(iX,2)>0 or shipmentsArray(iX,3)>0 then 
									howMuch = shipmentsArray(iX,2) + (shipmentsArray(iX,3)*sSubTotal/100)                              
									response.write " " & CONVERSION_Currency(howMuch)
								else
									response.write " " & CONVERSION_Currency(0)
								End If
								%>
							</TD>
							<%If ( bCheckout = "T" ) Then%>
							<%If ( iX = 0 ) Then%>
							<td><input type='radio' name='sShipMethod' value='<%=howMuch%>|<%=shipmentsArray(iX,0)%>' checked></td>
							<%Else%>
							<td><input type='radio' name='sShipMethod' value='<%=howMuch%>|<%=shipmentsArray(iX,0)%>' ></td>
							<%End If%>
							<%Else%>
							<td></td>
							<%End If%>
						</TR>
						<%
						next
					End If
				End If
				%>
				<TR>
					<TD colspan=3 style='border-top: 1px solid #000000;'>
						<table width=100%>
							<tr>
								<%If ( bCheckout = "T" ) Then%>
								<td align=right><input type="submit" value=" <%=LANG_DT( "SELECTSHIPPING_SELECT", "Select Shipping Method and Continue" )%> &gt; "></td>
								<%Else%>
								<td>
									<input type='button' value=' <%=LANG_DT( "SHIPQUERYEXEC_RETURN", "Return to Cart" )%> ' onClick='document.location="owShowCart.asp";'>
								</TD>
								<TD align=right>
									<%if bHasAccount then%>
									<input type='button' value=' <%=LANG_DT( "SHIPQUERYEXEC_AGAIN", "Try Again" )%> ' onClick='document.location="owShippingCostQuery.asp";'>
									<%End If%>
								</TD>
								<%End If%>
							</tr>
						</table>
					</td>
				</TR>
			</table>
			<%
		Else
			Call owMessage(sError, C_ERROR)
		End If
		
	End Sub
	'************************************************************************************
	
	
	'************************************************************************************
	'*																					*
	'*																					*
	'*																					*
	'************************************************************************************
	Function RetrieveNonRealTimeShipping(sShippingZip, sShippingCountryCode, sShippingStateCode)
	
		sFilterZip			= left(sShippingZip,10)
		sFilterCountryCode	= left(sShippingCountryCode,4)
		sFilterStateCode	= left(sShippingStateCode,4)
		
		sSQLText = "SELECT * FROM shipments WHERE ((stateCode='" &sFilterStateCode& "' AND stateCodeEq=-1) OR (stateCode IS NULL) OR (stateCode<>'" &sFilterStateCode& "' AND stateCodeEq=0)) AND ((countryCode='"&sFilterCountryCode&"' AND countryCodeEq=-1) OR (countryCode IS NULL) OR (countryCode<>'" &sFilterCountryCode& "' AND countryCodeEq=0)) AND ((zip='" &sFilterZip& "' AND zipEq=-1) OR (zip IS NULL) OR (zip<>'" &sFilterZip& "' AND zipEq=0))"
		Call DB_getFromDatabase(sSQLText, rsStoreData, "")    
		if not rsStoreData.eof then 
			While ( NOT rsStoreData.EOF )
				' insert if all rules are ok
				if sCartQuantity >= rsStoreData("quantityFrom") and sCartQuantity <= rsStoreData("quantityUntil") and sCartTotalWeight >= rsStoreData("weightFrom") and sCartTotalWeight <= rsStoreData("weightUntil") and sSubTotal >= Cdbl(rsStoreData("priceFrom")) and sSubTotal <= Cdbl(rsStoreData("priceUntil")) then
					shipmentsArray(shipmentIndex,0)	= rsStoreData("idShipment")
					shipmentsArray(shipmentIndex,1)	= rsStoreData("shipmentDesc")
					shipmentsArray(shipmentIndex,2)	= Cdbl(rsStoreData("priceToAdd"))
					shipmentsArray(shipmentIndex,3)	= rsStoreData("percentageToAdd")
					shipmentIndex					= shipmentIndex+1
				End If
				rsStoreData.movenext
			Wend
		End If
		Call DB_closeRecordset(rsStoreData)
		
		If ( shipmentIndex = 0 ) Then
			RetrieveNonRealTimeShipping = LANG_DT( "SHIPQUERYEXEC_NOSHIPMENTS", "There are no shipments for your selection of State/Country/Zip and cart content." )
		End If

	End Function
	'************************************************************************************
	%>