<?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
	$mode=(isset($HTTP_GET_VARS['mode'])?$HTTP_GET_VARS['mode']:0);
	$action=(isset($HTTP_GET_VARS['action'])?$HTTP_GET_VARS['action']:'');
	$events_id=(isset($HTTP_GET_VARS['events_id'])?$HTTP_GET_VARS['events_id']:0);
	$type=(isset($HTTP_GET_VARS['type'])?$HTTP_GET_VARS['type']:'');
	$search_emails=(isset($HTTP_POST_VARS["search_ids"])?tep_db_prepare_input($HTTP_POST_VARS["search_ids"]):'');
	
	// check for valid details
	if ($action=='new_message' || $action=='sendall'){
		if ($mode==0 && ($events_id<=0 || $type=='')){
			tep_redirect(tep_href_link(FILENAME_EVENTS_MESSAGES));
		} else if ($mode!=0 && ($search_emails=='')){
			tep_redirect(tep_href_link(FILENAME_SEARCH_RESERVATIONS));
		}
	}
	if ($mode==0){
		$back_url=tep_href_link(FILENAME_EVENTS_MESSAGES,'events_id=' . $events_id);
	} else {
		if (isset($search_params["tx_dated"]) && $search_params["tx_dated"]=="Y") $back_url=tep_href_link(FILENAME_SEARCH_RESERVATIONS_DATED,'action=search');
		else $back_url=tep_href_link(FILENAME_SEARCH_RESERVATIONS,'action=search');

		if ($search_emails!=""){
			$split_array=array_unique(split(",",$search_emails));
			$cust_emails=join("','",$split_array);
		}
	}

	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!="" && $mode!=0){
				//$search_params=tep_prepare_hidden_params($params,"post","get");
				
				$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);
				$customer_query = tep_db_query("select c.customers_firstname, c.customers_lastname, c.customers_email_address, c.customers_password,c.customers_dob,c.customers_telephone,c.customers_fax, " .
												"a.entry_street_address,a.entry_city,a.entry_suburb,a.entry_postcode,a.entry_state,a.entry_country_id,a.entry_zone_id from " . TABLE_CUSTOMERS . " c, " . TABLE_ADDRESS_BOOK . " a where c.customers_email_address in ('" . $cust_emails . "') and c.customers_default_address_id=a.address_book_id");
				while ($customer=tep_db_fetch_array($customer_query)){
				
					$country_name=tep_get_country_name($customer['entry_country_id']);
				
					if ($mode==1){
						if (HTML_AREA_WYSIWYG_DISABLE_EMAIL == 'Disable'){
							$mimemessage->add_text($message_text);
						} else {
							$mimemessage->add_html($message,$message_text);
						}
						$mimemessage->build_message();
						$mimemessage->send($customer['customers_firstname'] . ' ' . $customer['customers_lastname'], $customer['customers_email_address'], STORE_OWNER, $from, $subject);
					} else if ($customer["customers_fax"]!="") {
						send_sms($customer["customers_fax"],$from,$message_text,$country_name);
					}
				}
				if ($mode==1) $messageStack->add_session(TEXT_SEND_MAILS,'success');
				tep_redirect($back_url);
				break;
			} else if ($message!="" && $mode==0){
				$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, " . TABLE_EVENTS_SESSIONS . " s where op.orders_id=o.orders_id and o.customers_id=c.customers_id and c.customers_reserve_newsletter='1'" . 
								" and op.products_type='E' and op.events_type='" . $HTTP_GET_VARS['type'] . "' and op.products_id=s.sessions_id and s.events_id='" . $events_id . "' and s.start_date>curdate() group by o.customers_id");

				$type=($HTTP_GET_VARS['type']=='R'?'MEV':'MEW');
				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 language="javascript" src="includes/general.js"></script>
<script type="text/javascript" src="htmlarea/htmlarea.js"></script>
<script type="text/javascript" src="htmlarea/editor.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_EVENTS_MESSAGES,'events_id=' . $events_id) . '">' . tep_image_button('button_continue.gif',IMAGE_CONTINUE) . '</a>'; ?></td>
			</tr>
		<?php } else if ($action=='new_message') { 
					$show_template=false;
					$content_text="";
					if ($mode!=0){
						//$search_params=tep_prepare_hidden_params($params,"post","post");

						if ($mode==2 && $content_text!="") {
							$content_text=strip_tags($content_text,'<br>');
							$content_text=str_replace('<br>',chr(13) . chr(10),$content_text);
							$content_text=str_replace('<BR>',chr(13) . chr(10),$content_text);
						}
						$customer_query=tep_db_query("SELECT c.customers_firstname,c.customers_lastname,c.customers_email_address,c.customers_fax from " . TABLE_CUSTOMERS . " c where c.customers_email_address in ('" . $cust_emails . "')");
						$customer_cnt=tep_db_num_rows($customer_query);
					}
		?>
		<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 
				if ($mode==0) {
					echo tep_draw_form('mail', FILENAME_SEND_EMAIL, 'action=sendall&mode=0&type=' . $HTTP_GET_VARS['type'] . '&events_id=' . $events_id); 
				} else {
					echo tep_draw_form('mail', FILENAME_SEND_EMAIL, 'action=sendall&mode=' . $mode);
					//echo $search_params;
					echo '<input type="hidden" name="search_ids" value="' . htmlspecialchars($search_emails) . '">';
				}
			?>
			<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 
						if ($mode!=0){
							$icnt=1;
							while($customer=tep_db_fetch_array($customer_query)){
								echo $customer['customers_firstname'] . ' ' . $customer['customers_lastname'];
								if ($mode==1){
									if ($customer["customers_email_address"]!="") echo " (" . $customer["customers_email_address"] . ")";
								} else {
									if ($customer["customers_fax"]!="") echo " (" . $customer["customers_fax"] . ")";
								}
								echo "<br>";
								$icnt++;
							}
						} else {
							echo ($type=='R'?TEXT_RESERVE_HOLDERS:TEXT_WAITLIST_HOLDERS); 
						}		      
						?>
						</td>
					</tr>
					<tr>
						<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
					</tr>
					<?php if ($mode<=1) { ?>
					<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>
					<?php } ?>
					<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'); ?>
