<!-- #INCLUDE FILE="bkitadovbs.inc" -->
<!-- #INCLUDE FILE="bkitutillib.asp" -->

<%
' if selected 'remember' then save as a cookie
' cookies must come before any headers!
if trim(request("cremember")) = "ON" then
	response.cookies("ctype") = trim(request("ccardtype"))
	response.cookies("ctype").expires = "January 1, 2020"
	response.cookies("cno") = trim(request("ccardno"))
	response.cookies("cno").expires = "January 1, 2020"
	response.cookies("cyexp") = trim(request("cyexpires"))
	response.cookies("cyexp").expires = "January 1, 2020"
	response.cookies("cmexp") = trim(request("cmexpires"))
	response.cookies("cmexp").expires = "January 1, 2020"
	response.cookies("cyfrom") = trim(request("cyfrom"))
	response.cookies("cyfrom").expires = "January 1, 2020"
	response.cookies("cmfrom") = trim(request("cmfrom"))
	response.cookies("cmfrom").expires = "January 1, 2020"
	response.cookies("cissue") = trim(request("cissue"))
	response.cookies("cissue").expires = "January 1, 2020"
	response.cookies("ccardname") = trim(request("ccardname"))
	response.cookies("ccardname").expires = "January 1, 2020"
end if
%>

<!-- #include file="bkitonline.asp" -->


<%
' If member ref not already present then call login
' page and stop rest of this page
ref = trim(request("ref"))
if ref = "" then
%>
	<!-- #include file="bkitlogin.asp" -->
<%
response.end
end if
%>

<%
' bring form fields through from checkout form
cremember = trim( request( "cremember" ) )
namttoday 	= trim( request( "namttoday" ) )
ccardtype 	= trim( request( "ccardtype" ) )
ccardno 	= trim( request( "ccardno" ) )
cmexpires 	= trim( request( "cmexpires" ) )
cyexpires 	= trim( request( "cyexpires" ) )
cissue 	= trim( request( "cissue" ) )
cmfrom 	= trim( request( "cmfrom" ) )
cyfrom 	= trim( request( "cyfrom" ) )
ccardname	= trim( request( "ccardname" ) )
ccredit	= trim( request( "ccredit" ) )
ccreditdets = trim( request( "ccreditdets" ) )

' validate previous form values
if not ccredit = "" then
	if isnumeric(ccredit) = False then 
		errorform "Only enter valid numeric amounts in the credit note / voucher amount field if relevant", "bkitcheckout.asp"
	else
		if csng(ccredit) > 0 then
			if ccreditdets = "" then
				errorform "When entering a credit / voucher amount you must include further details such as a reference number", "bkitcheckout.asp"
			end if
		end if
	end if
end if
	
if ccardno="" then errorform "You must enter your card number","bkitcheckout.asp"
if not validccexpires(cmexpires, cyexpires) then errorform "The card expiry date is not valid", "bkitcheckout.asp"

if ccardtype = "Switch" then
	if not validswitchno(ccardno) then errorform "This is not a valid card number", "bkitcheckout.asp"
	if cissue = "" and (cmfrom = "" or cyfrom = "") then errorform "An issue number or valid from date is required for Switch cards", "bkitcheckout.asp"
	if not cissue = "" and not validissue(cissue) then errorform "The card issue number is not valid", "bkitcheckout.asp"
	if not cmfrom & cyfrom = "" and not validccfrom(cmfrom, cyfrom) then errorform "The card from date is not valid", "bkitcheckout.asp"
else
	if not validccnumber(ccardno) then errorform "This is not a valid credit card number", "bkitcheckout.asp"
	if not cmfrom & cyfrom = "" then errorform "A from date is not required for this card type", "bkitcheckout.asp"
	if not cissue = "" then errorform "An issue number is not required for this card type", "bkitcheckout.asp"
end if

if ccardname="" then errorform "You must enter your name as displayed on your card","bkitcheckout.asp"

%>

<%
' Open Database Connection
Set Con = Server.CreateObject( "ADODB.Connection" )
%>

<!-- #INCLUDE FILE="connection.inc" -->

<%
Set memberrs = Server.CreateObject( "ADODB.Recordset" )
memberrs.ActiveConnection = Con
sqlString = _
"SELECT 		ref, caddressee, cwebpass, cemail1, cemail2, homephone, workphone, fax, mobile " &_
"from 			bkitmembers " &_
"where 		ref = '" & request("ref") & "'"
memberrs.Open sqlString


Set cartrs = Server.CreateObject( "ADODB.Recordset" )
cartrs.ActiveConnection = Con
sqlstring = _
"select 		bkitcart.*, " &_
"				bkitevents.desc, "&_
"				bkitevents.location, " &_
"				bkitevents.fromdate " &_
"from 			bkitcart " &_
"inner join	bkitevents on bkitevents.eventno == bkitcart.eventno " &_
"where 	ref='" & ref & "'"
cartrs.open sqlstring

Set defaultsrs = Server.CreateObject( "ADODB.Recordset" )
defaultsrs.ActiveConnection = Con
sqlstring = "select * from bkitdefaults"
defaultsrs.open sqlstring

Set checkoutrs = Server.CreateObject( "ADODB.Recordset" )
checkoutrs.ActiveConnection = Con

'now validate cart to ensure slots still free
while not cartrs.eof
	Set bookingsrs = Server.CreateObject( "ADODB.Recordset" )
	bookingsrs.ActiveConnection = Con
	sqlstring = _
	"select 		desc, location, fromdate, sum(nvl(slots1,00000000)) as slots1, sum(nvl(slots2,00000000)) as slots2, " &_
	"				sum(nvl(slots3,00000000)) as slots3, sum(nvl(slots4,00000000)) as slots4, " &_
	"				taken1, taken2, taken3, taken4, max1, max2, " &_
	"				max3, max4, maxall, book1name, book2name, book3name, book4name " &_
	"from 			bkitevents " &_
	"left join		bkitbookings on bkitbookings.eventno = bkitevents.eventno " &_
	"where 		bkitevents.eventno='" & cartrs("eventno") & "' " &_
	"group by 		bkitevents.eventno"
	bookingsrs.open sqlstring
	if clng(bookingsrs("max1")) > 0 and clng(bookingsrs("slots1")) + clng(bookingsrs("taken1")) > clng(bookingsrs("max1")) then
		errorform "Cannot accept booking as insufficient '" & trim(bookingsrs("book1name")) & "' places on " & trim(bookingsrs("desc")) & ", " & trim(bookingsrs("fromdate")), "bkitcheckout.asp"
	end if
	cartrs.MoveNext	
wend

if namttoday = "" then namttoday = 0
cnextno = nextno("BKITCHECKOUT")

lcemail1 = trim(memberrs("cemail1"))
lcemail2 = trim(memberrs("cemail2"))
lchomephone = trim(memberrs("homephone"))
lcworkphone = trim(memberrs("workphone"))
lcmobile = trim(memberrs("mobile"))
lcfax = trim(memberrs("fax"))

lcbody = ""
lcbody = lcbody & "Web Booking from " & trim(defaultsrs("cweb")) & chr(13) & chr(10)
lcbody = lcbody & chr(13) & chr(10)
lcbody = lcbody & "Web Ref No: " & cnextno & chr(13) & chr(10)
lcbody = lcbody & "Member No:  " & ref & chr(13) & chr(10)
lcbody = lcbody & "Name:       " & trim(memberrs("caddressee")) & chr(13) & chr(10)
if not lcemail1 = "" then
	lcbody = lcbody & "Email1:     " & lcemail1 & chr(13) & chr(10)
end if
if not lcemail2 = "" then
	lcbody = lcbody & "Email2:     " & lcemail2 & chr(13) & chr(10)
end if
if not lchomephone = "" then
	lcbody = lcbody & "Homephone:  " & lchomephone & chr(13) & chr(10)
end if
if not lcworkphone = "" then
	lcbody = lcbody & "Workphone:  " & lcworkphone & chr(13) & chr(10)
end if
if not lcmobile = "" then
	lcbody = lcbody & "Mobile:     " & lcmobile & chr(13) & chr(10)
end if
if not lcfax = "" then
	lcbody = lcbody & "Fax:        " & lcfax & chr(13) & chr(10)
end if
lcbody = lcbody & chr(13) & chr(10)

cartrs.movefirst
WHILE NOT cartrs.EOF 
	sqlString = _
	"insert into bkitbookings (" &_
		"cbookref, ref, eventno, slots1, slots2, slots3, slots4, cpaytype, transport, " &_
		"amtdue1, amtdue2, amtdue3, amtdue4, depdue1, depdue2, depdue3, depdue4, notes ) " &_
		"values (" &_
		"'" & cnextno & "','" & cartrs("ref") & "','" & cartrs("eventno") & "'," & cartrs("slots1") & "," & cartrs("slots2") & "," &_
		 cartrs("slots3") & "," & cartrs("slots4") & ",'" & cartrs("cpaytype") & "','" & cartrs("transport") & "', " & cartrs("amtdue1") & "," & cartrs("amtdue2") & "," & cartrs("amtdue3") & "," &_
		 cartrs("amtdue4") & "," & cartrs("depdue1") & "," & cartrs("depdue2") & "," & cartrs("depdue3") & "," & cartrs("depdue4") & ",'" & cartrs("notes") & "')"
	checkoutrs.Open sqlString
	lcbody = lcbody & "Event No:   " & cartrs("eventno") & chr(13) & chr(10)
	lcbody = lcbody & "Desc:       " & trim(cartrs("desc")) & ", " & trim(cartrs("location")) & chr(13) & chr(10)
	lcbody = lcbody & "Date:       " & cartrs("fromdate") & chr(13) & chr(10)
	if not cartrs("slots1") = "0" then
		lcbody = lcbody & "Slots1:     " & cartrs("slots1") & chr(13) & chr(10)
		lcbody = lcbody & "AmtDue1:    " & formatcurrency(csng(cartrs("amtdue1")) * csng(cartrs("slots1"))) & chr(13) & chr(10)
		lcbody = lcbody & "DepDue1:    " & formatcurrency(csng(cartrs("depdue1")) * csng(cartrs("slots1"))) & chr(13) & chr(10)
	end if
	if not cartrs("slots2") = "0" then
		lcbody = lcbody & "Slots2:     " & cartrs("slots2") & chr(13) & chr(10)
		lcbody = lcbody & "AmtDue2:    " & formatcurrency(csng(cartrs("amtdue2")) * csng(cartrs("slots2"))) & chr(13) & chr(10)
		lcbody = lcbody & "DepDue2:    " & formatcurrency(csng(cartrs("depdue2")) * csng(cartrs("slots2"))) & chr(13) & chr(10)
	end if
	if not cartrs("slots3") = "0" then
		lcbody = lcbody & "Slots3:     " & cartrs("slots3") & chr(13) & chr(10)
		lcbody = lcbody & "AmtDue3:    " & formatcurrency(csng(cartrs("amtdue3")) * csng(cartrs("slots3"))) & chr(13) & chr(10)
		lcbody = lcbody & "DepDue3:    " & formatcurrency(csng(cartrs("depdue3")) * csng(cartrs("slots3"))) & chr(13) & chr(10)
	end if
	if not cartrs("slots4") = "0" then
		lcbody = lcbody & "Slots4:     " & cartrs("slots4") & chr(13) & chr(10)	
		lcbody = lcbody & "AmtDue4:    " & formatcurrency(csng(cartrs("amtdue4")) * csng(cartrs("slots4"))) & chr(13) & chr(10)
		lcbody = lcbody & "DepDue4:    " & formatcurrency(csng(cartrs("depdue4")) * csng(cartrs("slots4"))) & chr(13) & chr(10)
	end if
	if cartrs("cpaytype") = "F" then 
		lnevepay = (csng(cartrs("amtdue1"))*clng(cartrs("slots1"))) +_
					 (csng(cartrs("amtdue2"))*clng(cartrs("slots2"))) +_
					 (csng(cartrs("amtdue3"))*clng(cartrs("slots3"))) +_
					 (csng(cartrs("amtdue4"))*clng(cartrs("slots4")))
	else
		lnevepay = (csng(cartrs("depdue1"))*clng(cartrs("slots1"))) +_
					 (csng(cartrs("depdue2"))*clng(cartrs("slots2"))) +_
					 (csng(cartrs("depdue3"))*clng(cartrs("slots3"))) +_
					 (csng(cartrs("depdue4"))*clng(cartrs("slots4")))
	end if	
	lcbody = lcbody & "Paying:     " & formatcurrency(lnevepay) & chr(13) & chr(10)
	lcbody = lcbody & "Transport:  " & cartrs("transport") & chr(13) & chr(10)
	
	lcnotes = trim(cartrs("notes")) 
	if not lcnotes = "" then
		lcbody = lcbody & "Notes:      " & lcnotes & chr(13) & chr(10)
	end if
	lcbody = lcbody & chr(13) & chr(10)
	
	cartrs.MoveNext
WEND

lnbalance = namttoday
if isnumeric(ccredit) = True then
	if csng(ccredit) > 0 then
		lcbody = lcbody & "Credit Note:" & formatcurrency(ccredit) & chr(13) & chr(10)
		lcbody = lcbody & "Credit Dets:" & ccreditdets & chr(13) & chr(10)
		lnbalance = lnbalance - csng(ccredit)
	end if
end if

' If some of balance by credit/debit card, add in card handling fee if present
if lnbalance > 0 then
	lnbalance = lnbalance + csng(defaultsrs("ycardfee"))
end if


sqlString = "insert into bkitcheckout (cbookref, ref, namttoday, ccardtype, ccardno, cmexpires, cyexpires, cissue, " &_
	"cmfrom, cyfrom, lprocessed) " &_
	"values ('" & cnextno & "','" & ref & "'," & namttoday & ",'" & ccardtype & "',' ',' '," &_
	"' ','" & cissue & "','" & cmfrom & "','" & cyfrom & "', .f.)"
checkoutrs.Open sqlString




lccard = ""
if lnbalance > 0 and csng(defaultsrs("ycardfee")) > 0 then
	lccard = lccard & "Card Fee:   " & formatcurrency(defaultsrs("ycardfee")) & chr(13) & chr(10)
end if
lccard = lccard & "Card Type:  " & ccardtype & chr(13) & chr(10)
lccard = lccard & "Issue No:   " & cissue & chr(13) & chr(10)
lccard = lccard & "Valid From: " & cmfrom & "/" & cyfrom & chr(13) & chr(10)
lccard = lccard & "Card Name:  " & ccardname & chr(13) & chr(10)
lccard = lccard & "Tot Amount: " & formatcurrency(lnbalance)



' for office take standard body, add card number and standard card details
lcbodyo = ""
lcbodyo = lcbodyo & lcbody
lcbodyo = lcbodyo & "Card No:    " & ccardno & chr(13) & chr(10)
lcbodyo = lcbodyo & "Expires:    " & cmexpires & "/" & cyexpires & chr(13) & chr(10)
lcbodyo = lcbodyo & lccard

' for member take standard body, * out some card details and add remainder standard card details
lcbodym = ""
lcbodym = lcbodym & "Here is a copy of the information that has been sent by completing our " & chr(13) & chr(10)
lcbodym = lcbodym & "web form. Please note that this is not confirmation that your booking " & chr(13) & chr(10)
lcbodym = lcbodym & "has been received and processed but simply that it has been sent. " & chr(13) & chr(10)
lcbodym = lcbodym & chr(13) & chr(10)
lcbodym = lcbodym & lcbody
lcbodym = lcbodym & "Card No:    " & string(len(trim(ccardno))-4,"*") & right(trim(ccardno),4) & chr(13) & chr(10)
'cint(len(trim(ccardno)))-4
lcbodym = lcbodym & "Expires:    " & "**/**" & chr(13) & chr(10)
lcbodym = lcbodym & lccard

cartrs.close
sqlString = _
"delete 	from bkitcart " &_
"where 	ref = '" & ref & "'"
cartrs.Open sqlString

%>

<head>
<title>Checkout Confirmation </title>
<style><% defstyles %></style>
</head>

<center>
<h1>Checkout Confirmation</h1>

<% if fileexists("local.txt") = True then %>
		<p><font color="#FF0000">An email has not been sent as testing locally</font><p>
<% else 

		lcemailtob = trim(defaultsrs("cbookemail"))
		lcsubject = "Web Booking (Automated)"

		' blank from email address CDONTS email just disappears
		if lcemail1 = "" then
			lcfrom = "bookings@spiceuk.com"
		else
			lcfrom = lcemail1
		end if
		
		' --- Office Copy ---
		Sendemail lcemailtob, lcsubject, lcfrom, lcbodyo
		' --- Member Copy First Email Address ---
		if not lcemail1 = "" then
			Sendemail lcfrom, lcsubject, lcemailtob, lcbodym
		end if
		' --- Member Copy Second Email Address ---
		if not lcemail2 = "" then
			Sendemail lcemail2, lcsubject, lcemailtob, lcbodym
		end if
				
		Sendemail "paul@instinctive.co.uk", "Copy "+lcsubject, lcfrom, lcbodym
   end if
%>

<p>
Your reference is&nbsp; <b><font color="#FF0000" size="4">W / <%= cnextno %></font></b> 

<p>
<%= defaultsrs("mcheckconf") %>
<p>

<table border="0" width="35%">
  <tr>
    <td width="50%"><% showbutton "Events",	getpath("web") + "/bkiteventavail.asp" %></td>
    <td width="50%"><% showbutton "Home",  	getpath("home")  %></td>
  </tr>
</table>
</center>


<% pagefooter %>
<% Con.Close %>