<%'Include Data Connection%>
<!--#INCLUDE VIRTUAL="/includes/dataconn.asp"-->

<%'Include ADO constants include file for VBScript%>
<!--#INCLUDE VIRTUAL="/includes/adovbs.inc"-->

<%
'open data connection
OpenConn()
%>

<%'Include Utility Scripts%>
<!--#INCLUDE VIRTUAL="/admin/includes/email_utils.asp"-->

<%
'On Error Resume Next
dim strDelGroupIds
dim intSendToLiveSubscribers
dim intLastSubscriberInPreviousRecipientList
dim intPostingId
dim intSubscriberCount
dim strLogFileEntry


'get the number of recipients so far for the current email:
'if the sub_count querystring is equal to an empty string then...
If Trim(Request.QueryString("sub_count")) = "" Then
	'set the subscription counter variable to 0
	intSubscriberCount = 0
'if the sub_count querystring is not equal to an empty string then...
Else
	'set the subscription counter variable to 0
	intSubscriberCount = CInt(Request.QueryString("sub_count"))
End If

'get the delimited list of contact type recipients into the strDelContactTypes variable
strDelGroupIds = Request.QueryString("selectedGroups")

'get the subscriber status (live:1 / test:0)
intSendToLiveSubscribers = CBool(Request.QueryString("is_live"))

'if the subscriber is a test then...
If Not intSendToLiveSubscribers And strDelGroupIds = "" Then
	strDelGroupIds = "1"
End If

'get the last subscriber's id from the recipient list of the previous batch
intLastSubscriberInPreviousRecipientList = CInt(Request.QueryString("last_sub"))

'get the posting id from the posting that instigated this email campaign
intPostingId = CInt(Request.QueryString("posting_id"))

'send email to subscribers
Call SendPostingEmail(CInt(intSubscriberCount), strLogFileEntry, intSendToLiveSubscribers, CInt(intLastSubscriberInPreviousRecipientList), CInt(intPostingId), strDelGroupIds)

'On Error GoTo 0

'set the PostingEmailLogFileEntry variable to an empty string
Session("PostingEmailLogFileEntry") = ""
%>
<%'Include Modal Window Fipt%>
<script language="JavaScript" src="/admin/includes/modalWindow.js"></script>
<script language="Javascript">
	function getContactTypes(){
		openDialog('/admin/group_type_selector.asp', '660', '450', 'no');
	}
	
	function sendPostings(delGroupids){
		//var sendPostingURL = '/admin/send_email.asp?sub_count=0&is_live=1&last_sub=0&posting_id=<%=CInt(intPostingId)%>&ct=' + contactTypes;
		var sendPostingURL = '/admin/send_email.asp?sub_count=0&is_live=1&last_sub=0&posting_id=<%=CInt(intPostingId)%>&selectedGroups=' + delGroupids;
		window.location.href=sendPostingURL;
	}
</script>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td align="left" valign="top">
		</td>
	</tr>
	<tr>
		<td align="left" valign="top">
		<%
		'if the email was sent to the test subscribers then...
		If Not CBool(intSendToLiveSubscribers) Then
			'if an error occurred then...
			If Not (InStr(strLogFileEntry, "Mail failure occured") <> 0) Then
			%>
			The Test Email was successfully sent to the following recipient(s):
			<p><%=strLogFileEntry%></p>
			Once you've received the Test Email and are satisfied with it's format, <a href="javascript:getContactTypes();">click here</a> to send the Live Email.
			<%
			Else
			%>
			An Error occurred while sending the Test Email, see the following log for details:
			<p><%=strLogFileEntry%></p>
			<%
			End If
		'if the email was sent to the live subscribers then...
		Else
			'if an error occurred then...
			If Not (InStr(strLogFileEntry, "Mail failure occured") <> 0) Then
			%>
			The Live Email was successfully sent to the following recipient(s):
			<p><%=strLogFileEntry%></p>
			<%
			Else
			%>
			An Error occurred while sending the Live Email, see the following log for details:
			<p><%=strLogFileEntry%></p>
			<%
			End If
		End If
		%>
		</td>
	</tr>
</table>
