<?php
/*

  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd 
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
  require('includes/application_top.php');
  require(DIR_WS_CLASSES . 'currencies.php');
  
	$currencies=new currencies;
	//mode params 0->default from emarketing emails 1->from search reservation
  
	$action=(isset($HTTP_GET_VARS['action'])?$HTTP_GET_VARS['action']:'');
	$subscription_id=(isset($HTTP_GET_VARS['subscription_id'])?$HTTP_GET_VARS['subscription_id']:0);

	// check for valid details
	if ($action=='new_message' || $action=='sendall'){
		if ($subscription_id<=0){
			tep_redirect(tep_href_link(FILENAME_SUBSCRIPTION_MESSAGES));
		} 
	}
	$back_url=tep_href_link(FILENAME_SUBSCRIPTION_MESSAGES,'subscription_id=' . $subscription_id);
	switch($action)
	{
		case 'sendall':
			$from = tep_db_prepare_input($HTTP_POST_VARS['from']);
			$subject = (isset($HTTP_POST_VARS['subject'])?tep_db_prepare_input($HTTP_POST_VARS['subject']):'');
			$message = tep_db_prepare_input($HTTP_POST_VARS['message']);
			$mimemessage = new email(array('X-Mailer: osCommRes'));
			if ($message!=""){
				$message_text=strip_tags($message,'<br>');
				$message_text=str_replace('<br>',chr(13) . chr(10),$message_text);
				$message_text=str_replace('<BR>',chr(13) . chr(10),$message_text);

				if (HTML_AREA_WYSIWYG_DISABLE_EMAIL == 'Disable') {
					$mimemessage->add_text($message_text);
				} else {
					$mimemessage->add_html($message,$message_text);
				}
				$mimemessage->build_message();

				$customer_query=tep_db_query("SELECT c.customers_id,c.customers_email_address,c.customers_firstname,c.customers_lastname,op.products_id from " . TABLE_CUSTOMERS . " c, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o where op.orders_id=o.orders_id and o.customers_id=c.customers_id and c.customers_subscription_newsletter='1'" . 
								" and op.products_type='S'  group by o.customers_id");
				while($mail=tep_db_fetch_array($customer_query)){
					if ($mail['customers_email_address']!=""){
						$mimemessage->send($mail['customers_firstname'] . ' ' . $mail['customers_lastname'], $mail['customers_email_address'], STORE_OWNER, $from, $subject);
					}
				}
				$messageStack->add_session(TEXT_SEND_MAILS,'success');
			}
			tep_redirect($back_url);
			break;
		}
		$from_email=$HTTP_SESSION_VARS["login_email"];
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script type="text/javascript" src="htmlarea/htmlarea.js"></script>
<script type="text/javascript" src="htmlarea/editor.js"></script>
       <script language="JavaScript" src="htmlarea/validation.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
	<!-- header //-->
	<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
	<!-- header_eof //-->
	<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
	<tr> 
		<!-- body_text //-->	  
		<td width=100% align=left valign="top">
		<table border="0" width="100%" cellspacing="0" cellpadding="2">
		<?php if ($action=='success'){ ?>
			<tr>
				<td>
				<table border="0" width="100%" cellspacing="0" cellpadding="2">
					<tr>
						<td align=left class='pageHeading' colspan='2'><?php echo HEADING_SUCCESS; ?></td>
					</tr>
				</table>
				</td>
			</tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','5'); ?></td></tr>
			<tr>
				<td class=main><?php echo TEXT_MAILS_SEND; ?></td>
			</tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','5'); ?></td></tr>
			<tr>
				<td class=main><?php echo '<a href="' . tep_href_link(FILENAME_SUBSCRIPTION_MESSAGES,'subscription_id=' . $subscription_id) . '">' . tep_image_button('button_continue.gif',IMAGE_CONTINUE) . '</a>'; ?></td>
			</tr>
		<?php } else if ($action=='new_message') { ?>
		<tr>
			<td>
			<table border="0" width="100%" cellspacing="0" cellpadding="2">
				<tr>
					<td align=left class='pageHeading' colspan='2'><?php echo HEADING_NEW_MESSAGE; ?></td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<tr>
				<td><?php echo tep_draw_separator('pixel_trans.gif','1','3'); ?></td>
			</tr>
			<?php 
					echo tep_draw_form('mail', FILENAME_SEND_SUBSCRIPTION_EMAIL, 'action=sendall&subscription_id=' .$subscription_id);
			?>
			<tr>
            	<td>
				<table border="0" cellpadding="2" cellspacing="0" >
					<tr>
						<td class="main" width=100><b><?php echo TEXT_FROM; ?></b></td>
						<td><?php echo tep_draw_input_field('from', $from_email,'size=30'); ?></td>
					</tr>
					<tr>
						<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
					</tr>
					<tr>
						<td class="main" width=100 valign="top"><b><?php echo TEXT_TO; ?></b></td>
						<td class="main" valign="top"><?php echo TEXT_SUBSCRIPTION_HOLDERS; ?>
						</td>
					</tr>
					<tr>
						<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
					</tr>
					<tr>
						<td class="main"><b><?php echo TEXT_SUBJECT; ?></b></td>
						<td class="main"><?php echo tep_draw_input_field('subject','','size=60'); ?></td>
					</tr>
					<tr>
						<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
					</tr>
					<tr>
						<td valign="top" class="main"><b><?php echo TEXT_MESSAGE; ?></b></td>
						<td><?php echo tep_draw_textarea_field('message', 'soft', '80', '24',$content_text,	'id="message"'); ?></td>
						<?php if ($mode<=1) {
								if (HTML_AREA_WYSIWYG_DISABLE_EMAIL == 'Enable') { ?>
								   <script>initEditor('message');</script>																			
						<?php }
							}
						   ?>
						
					</tr>
						
					<tr><td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td></tr>
						
					<tr><td colspan="2" align="right">
						<?php
							echo '<a href="' . $back_url . '">' . tep_image_button('button_back.gif',IMAGE_BACK) . '</a>&nbsp;';
							echo tep_image_submit('button_send.gif', IMAGE_SEND_EMAIL);?>
						</td>
					</tr>
				</table>
				</td>
			</tr>
			<?php } ?>
		</table>
		</td>
	</tr>
</table>
<!-- body_text_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
