<!--#include file="Includes\dbFunctions.asp" -->
 <%
 
 If UCase(Request.ServerVariables("REQUEST_METHOD")) = "POST" Then
 '********************
 'SETUP DB CONNECTION*
 '********************
 'Dim Conn
 'Set Conn = Server.CreateObject("ADODB.Connection")
 'Conn.Open Application("conn_string")
 'insert into blah (blah, moneycol) values('" & Request.Form("blah") & "', Convert(money, '" & Request.Form("blah") & "'))"
 '******************************************
 'DEFINE VARIABLES RETURNED BY AUTHORIZENET*
 '******************************************
 Dim x_response_code
 Dim x_response_subcode
 Dim x_response_reason_code
 Dim x_response_reason_text
 Dim x_auth_code
 Dim x_avs_code
 Dim x_trans_id
 Dim x_invoice_num
 Dim x_description
 Dim x_amount
 Dim x_method
 Dim x_type
 Dim x_cust_id
 Dim x_first_name
 Dim x_last_name
 Dim x_company
 Dim x_address
 Dim x_city
 Dim x_state
 Dim x_zip
 Dim x_country
 Dim x_phone
 Dim x_fax
 Dim x_email
 Dim x_ship_to_first_name
 Dim x_ship_to_last_name
 Dim x_ship_to_company
 Dim x_ship_to_address
 Dim x_ship_to_city
 Dim x_ship_to_state
 Dim x_ship_to_zip
 Dim x_ship_to_country
 Dim x_tax
 Dim x_duty
 Dim x_freight
 Dim x_tax_exempt
 Dim x_po_num
 Dim x_md5_hash
 Dim Customer_IP
 Dim Contact_ID
 Dim Merchant_ID
 Dim Transaction_ID
 Dim myMail
 
 '*****************************
 'BEGIN SQL FOR INQUIRY INSERT*
 '*****************************
 
 Set RS = Server.CreateObject("ADODB.Recordset")
 
 SQL = "Insert_Transaction '"& Request.Form("Merchant_ID") &"','"& Request.Form("Itinerary_ID") &"','"& Request.Form("Contact_ID") &"','"& Request.Form("Customer_IP") &"','"& Request.Form("x_Response_Code") &"','"& Request.Form("x_Response_SubCode") &"','"& Request.Form("x_Response_Reason_Code") &"','"& Request.Form("x_Response_Reason_Text") &"','"& Request.Form("Payment_Type") &"','"& Request.Form("x_Auth_Code") &"','"& Request.Form("x_AVS_Code") &"','"& Request.Form("x_Trans_ID") &"','"& Request.Form("x_Description") &"','"& Request.Form("x_Amount") &"', '"& Request.Form("CSR_ID") &"'"
 
 RS.Open SQL, Conn
 Transaction_ID = RS("Transaction_ID")
 Itinerary_ID = Request.Form("Itinerary_ID")
 
 
 '*************
 'EMAIL A COPY*
 '*************
 
     for i=1 to request.form.count
         If request.form.item(i) = "" Then
 			PLAIN = PLAIN & request.form.key(i) & ": None"  & vbCrLf
 		Else
 			PLAIN = PLAIN & request.form.key(i) & ": " & request.form.item(i) & vbCrLf
 		End If
     Next
 
     Set myMail = CreateObject("CDONTS.NewMail")
 
     myMail.Subject="VIF Transaction Summary"
     myMail.From="webmaster@vacationinflorida.com"
     myMail.Body=PLAIN
     myMail.BodyFormat=1
     myMail.MailFormat=1
     myMail.To = "webmaster@vacationinflorida.com;" & Request.Form("Email")
     myMail.Bcc = "monitor@emagineering.com"
     myMail.Send
     set mymail=nothing
 
 '*********************************
 'Update Inventory for Transaction*
 '*********************************
 
 If  Request.Form("x_Response_Code") = "1" Then
 Dim SQLInv
 SQLInv = "Update_Available_Inventory '" & Transaction_ID & "'"
 'Response.Write SQLInv
 Conn.Execute(SQLInv)
 
 SQLInv = "Update Avante.dbo.Itinerary Set Itinerary_Status = '4' Where Itinerary_ID = '" & Itinerary_ID & "'"
 'Response.Write SQLInv
 Conn.Execute(SQLInv)
 
 SQLInv = "Update Avante.dbo.Itinerary_Detail Set Product_Status = '1' Where Itinerary_ID = '" & Itinerary_ID & "' And Product_Type='1'"
 'Response.Write SQLInv
 Conn.Execute(SQLInv)
 
 SQLInv = "Update Avante.dbo.Itinerary_Detail Set Product_Status = '1' Where Itinerary_ID = '" & Itinerary_ID & "' And Product_Type='3' And Exists (Select Package_ID from Package_Detail Where Package_ID = Product_ID And Inventory_Type_ID=3)"
 'Response.Write SQLInv
 Conn.Execute(SQLInv)
 
 '*****************************
 'Send Confirmations to Hotels*
 '*****************************
 
 Dim tmpArray, itemArray, transArray, Itinerary_ID, User_ID, SQLTrans, SQLUser, SQLEntity, Entity, Guest
 Dim HTML, Text, Header, Body, Footer, RepHeader, Body_Header, Body_Detail_Desc, Body_Detail_Dates, SQLItems
 Dim RepBodyHeader, RepBodyDates, i, RepBodyDatesBlank, SQLItemID, Nightly_Total
 Itinerary_ID = Request("Itinerary_ID")
 Set RSUser = Server.CreateObject("ADODB.Recordset")
 Set RSTrans = Server.CreateObject("ADODB.Recordset")
 Set RSEntity = Server.CreateObject("ADODB.Recordset")
 
 Nightly_Total = 0
 
 SQLEntity = "Get_Entity_Detail_From_Itin '" & Itinerary_ID & "'"
 'Response.Write SQLEntity
 RSEntity.Open SQLEntity, Conn
 
 SQLTrans = "Select * From Transactions Where Itinerary_ID = '" & Itinerary_ID & "'"
 RSTrans.Open SQLTrans, Conn
 User_ID = RSTrans("Contact_ID")
 
 SQLUser = "Select CD.Name_First, CD.Name_Last, CD.Address_1, CD.City, CD.State, CD.Zip from Contact_Detail CD Inner Join Users U On U.Contact_ID=CD.Contact_ID Where U.User_ID = '" & User_ID & "'"
 'Response.Write SQLUser
 RSUser.Open SQLUser, Conn
 
 Guest = RSUser("Name_First") & " " & RSUser("Name_Last") & "<br>" & RSUser("Address_1") & "<br>" 
 Guest = Guest & RSUser("City") & ", " & RSUser("State") & " " & RSUser("Zip")
 
 Do while not RSEntity.EOF
 i=0
 
 
 set Text = Server.CreateObject("Scripting.FileSystemObject")
 Set Header = Text.OpenTextFile("d:\web sites\tropical_palms\documents\resi-header.htm", 1, False, 0)
 Set Body_Header_tmp = Text.OpenTextFile("d:\web sites\tropical_palms\documents\resi-body-header.htm", 1, False, 0)
 Set Body_Detail_Desc_tmp = Text.OpenTextFile("d:\web sites\tropical_palms\documents\resi-body-detail-desc.htm", 1, False, 0)
 Set Body_Detail_Dates_tmp = Text.OpenTextFile("d:\web sites\tropical_palms\documents\resi-body-detail-dates.htm", 1, False, 0)
 'Footer below
 Set Footer_Email = Text.OpenTextFile("d:\web sites\tropical_palms\documents\resi-footer.htm", 1, False, 0)
 Set Footer_Fax = Text.OpenTextFile("d:\web sites\tropical_palms\documents\resi-footer-fax.htm", 1, False, 0)
 
 RepHeader = Header.ReadAll
 Body_Header = Body_Header_tmp.ReadAll
 Body_Detail_Desc = Body_Detail_Desc_tmp.ReadAll
 Body_Detail_Dates = Body_Detail_Dates_tmp.ReadAll
 
 Entity = RSEntity("Name") & "<br>" & RSEntity("Address_1") & "<br>" & _
 	RSEntity("City") & ", " & RSEntity("State") & " " & RSEntity("Zip")
 
 RepHeader = Replace(RepHeader, "{Entity_Name}", Entity)
 RepHeader = Replace(RepHeader, "{Guest}", Guest)
 RepHeader = Replace(RepHeader, "{CC_Num}", Request.Form("CC_num"))
 RepHeader = Replace(RepHeader, "{Itinerary_ID}", Itinerary_ID)
 
 Set RSDirect = server.CreateObject("ADODB.Recordset")
 SQLDirect = "Get_Attribute_Values @GroupID='" & RSEntity("Group_ID") & "', @Att_ID = '607'"
 RSDirect.Open SQLDirect, Conn
 
 If Not RSDirect.EOF Then
 RepHeader = Replace(RepHeader, "{Direct_Bill}", RSDirect("Variable"))
 Else
 RepHeader = Replace(RepHeader, "{Direct_Bill}", "N/A")
 End If
 RSDirect.Close()
 
 Set RSItemID = Server.CreateObject("ADODB.Recordset")
 SQLItemID = "Get_Itin_Item_ID_By_Entity '" & Itinerary_ID & "', '" & RSEntity("Group_ID") & "'"
 RSItemID.Open SQLItemID, Conn
 
 
 'Response.Write RepHeader
 
 'Response.Write Entity
 HTML = HTML & RepHeader
 Do While Not RSItemID.EOF
 i=0
 SQLItems = "Get_Itin_Items_By_Entity '" & Itinerary_ID & "', '" & RSEntity("Group_ID") & "', '" & RSItemID("Item_ID") & "'"
 'Response.Write SQLItems
 Set RSItems = Server.CreateObject("ADODB.Recordset")
 RSItems.Open SQLItems, Conn
 RepBodyHeader = Replace(Body_Header, "{Nights}", RSItems("Stay"))
 RepBodyHeader = Replace(RepBodyHeader, "{Adults}", RSItems("AdultCount"))
 RepBodyHeader = Replace(RepBodyHeader, "{Children}", Int(RSItems("Guest_Count"))-Int(RSItems("AdultCount")))
 
 If IsNumeric(RSItems("Special_Request")) Or IsNull(RSItems("Special_Request")) Then
 RepBodyHeader = Replace(RepBodyHeader, "{Special_Request}", "None")
 Else
 RepBodyHeader = Replace(RepBodyHeader, "{Special_Request}", RSItems("Special_Request"))
 End If
 
 RepBodyHeader = Replace(RepBodyHeader, "{Room_Type}", RSItemID("Name"))
 'If Not RSItems("Smoking") Then
 'RepBodyHeader = Replace(RepBodyHeader, "{Smoking}", "No")
 'ElseIf RSItems("Smoking") Then
 'RepBodyHeader = Replace(RepBodyHeader, "{Smoking}", "Yes")
 'End If
 RepBodyHeader = Replace(RepBodyHeader, "{Smoking}", "No")
 
 'Set RSTickets = server.CreateObject("ADODB.Recordset")
 'SQLTickets = "CheckForTickets '" & Itinerary_ID & "'"
 'RSTickets.Open SQLTickets, Conn
 
 'If RSTickets("Tickets") = 1 Then
 'RepBodyHeader = Replace(RepBodyHeader, "{Tickets}", "Yes")
 'Else
 'RepBodyHeader = Replace(RepBodyHeader, "{Tickets}", "No")
 'End If
 'RSTickets.Close()
 RepBodyHeader = Replace(RepBodyHeader, "{Tickets}", "No")
 
 HTML = HTML & "<br>" & RepBodyHeader & "<table width=""575"" border=""0"" cellspacing=""0"" cellpadding=""0"" bgcolor=""#FFFFFF""><tr>"
 HTML = HTML & "<td align=left>" & Body_Detail_Desc & "</td>"
 Do While Not RSItems.EOF
 If i = 7 Then
 HTML = HTML & "</tr><tr><td align=left>" & Body_Detail_Desc & "</td>"
 i = 0
 End If
 RepBodyDates = Body_Detail_Dates
 
 
 
 RepBodyDates = Replace(RepBodyDates, "{Date}", Month(RSItems("Date")) & "/" & Day(RSItems("Date")) & "/" & Right(Year(RSItems("Date")), 2))
 RepBodyDates = Replace(RepBodyDates, "{Net_Rate}", FormatCurrency(RSItems("Unit_Price"), 2))
 
 Nightly_Total = Nightly_Total + RSItems("Unit_Price")
 
 	Set RSTax = Server.CreateObject("ADODB.Recordset")
 			SQLTax = "Get_Tax_Costs_Lodging '" & RSItems("Inventory_List_ID") & "', '" & CCur(RSItems("Unit_Price")) & "'"
 			RSTax.Open SQLTax, Conn
 RepBodyDates = Replace(RepBodyDates, "{Taxes}", FormatCurrency(RSTax("Total_Tax"), 2))
 Nightly_Total = Nightly_Total + RSTax("Total_Tax")
 			RSTax.Close()
 
 RepBodyDates = Replace(RepBodyDates, "{Nightly_Total}", FormatCurrency(Nightly_Total, 2))
 HTML = HTML & "<td align=left>" & RepBodyDates & "</td>"
 RSItems.MoveNext()
 i = i + 1
 Itin_Total = Itin_Total + Nightly_Total
 Loop
 If i < 7 Then
 RepBodyDatesBlank = Body_Detail_Dates
 RepBodyDatesBlank = Replace(RepBodyDatesBlank, "{Date}", "&nbsp;")
 RepBodyDatesBlank = Replace(RepBodyDatesBlank, "{Net_Rate}", "&nbsp;")
 RepBodyDatesBlank = Replace(RepBodyDatesBlank, "{Extra_Adult}", "&nbsp;")
 RepBodyDatesBlank = Replace(RepBodyDatesBlank, "{Rollaway}", "&nbsp;")
 RepBodyDatesBlank = Replace(RepBodyDatesBlank, "{Taxes}", "&nbsp;")
 RepBodyDatesBlank = Replace(RepBodyDatesBlank, "{Nightly_Total}", "&nbsp;")
 For j = 1 to (7 - i)
 HTML = HTML & "<td align=left>" & RepBodyDatesBlank & "</td>"
 Next
 End If
 HTML = HTML & "</td></tr></table>"
 RSItemID.MoveNext()
 Loop
 
 	SQL = "Get_Notification @Group_ID='" & RSEntity("Group_ID") & "'"
 	Set RSNotify = Conn.Execute(SQL)
 	Addresses = ""
 	
 	If Not RSNotify.EOF Then
 		Do While Not RSNotify.EOF
 
 			Addresses = RSNotify("Notify_Addr")
 			Notify_Type = RSNotify("Notify_Type")
 
 			If Trim(lcase(Notify_Type)) = "fax" Then
 				BodyText = HTML & Footer_Fax.ReadAll
 				BodyText = Replace(BodyText, "{Itinerary_Total}", FormatCurrency(Itin_Total, 2))
 			ElseIf Trim(lcase(Notify_Type)) = "email" Then
 				BodyText = HTML & Footer_Email.ReadAll
 			End If
 		
 			Set myMail = Server.CreateObject("CDONTS.NewMail")
 
 			  myMail.Subject="Tropical Palms Reservation Receipt"
 			  myMail.From="test@vacationinflorida.com"
 			  myMail.Body=BodyText
 			  myMail.BodyFormat=0
 			  myMail.MailFormat=0
 			  myMail.To = Addresses
 			  'myMail.To = "shadow@empnet.com"
 			myMail.Bcc = "monitor@emagineering.com"
 			myMail.Send
 			set mymail=nothing
 
 			RSNotify.MoveNext
 						
 		Loop
 	Else
 	
 		Domain = Split(Request.ServerVariables("SERVER_NAME"), ".")
 		Domain_Num = UBound(Domain)
 		Domain_Str = Domain(Domain_Num-1) & "." & Domain(Domain_Num)
 		Domain = Domain_Str
 
 		Addresses = "webmaster@" & Domain
 		Addresses = Addresses & ";shadow@empnet.com"
 
 		BodyText = "<font color=red><b>FAILED TO SEND NOTIFICATION TO THE HOTEL. COULD NOT APPLY AN E-MAIL ADDRESS. PLEASE NOTIFY THE HOTEL OF THIS TRANSACTION IMMEDIATELY!</b></font><br>" & vbCrLf & HTML & Footer_Email.ReadAll
 		
 		Set myMail = Server.CreateObject("CDONTS.NewMail")
 
 		myMail.Subject="Tropical Palms Reservation Receipt"
 
 		  myMail.From="Reservations@encorerv.com"
 		  myMail.Body=BodyText
 		  myMail.BodyFormat=0
 		  myMail.MailFormat=0
 		  myMail.To = Addresses
 		  'myMail.To = "shadow@empnet.com"
 		  
 		myMail.Bcc = "monitor@emagineering.com"
 		myMail.Send
 		set mymail=nothing
 		
 	End If
 
     RSEntity.MoveNext()
     
 Loop
 
 
 End If
 
 'Response.End
 
 '*************************************************
 'REDIRECT TO THANK YOU PAGE OR DISPLAY DEBUG INFO*
 '*************************************************
 Dim Redirect_URL 
 Redirect_URL = "http://tpr.emagineering.com/transact-complete.asp?Transaction_ID=" & Transaction_ID
 
 Else
 Redirect_URL = "http://tpr.emagineering.com/transact-complete.asp?Transaction_ID=" & Transaction_ID
 End If
 %>
 <html>
 <body onload="document.location.replace('<%=Redirect_URL%>')">
   </body>
 </html>
 
 