<?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');
  tep_get_last_access_file();require(DIR_WS_CLASSES . '/currencies.php');
  $currencies=new currencies;  
  // get input entries to find type of entry
  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
  $command=(isset($HTTP_GET_VARS['command']) ? $HTTP_GET_VARS['command'] : '');	
  $msg_id = (isset($HTTP_GET_VARS['msg_id']) ?$HTTP_GET_VARS['msg_id'] : 'WFU');

	$message_subscription=array();
	$message_subscription=array(
							array('id'=>'WFU','text'=>TEXT_TYPE_WALLET_FUC,'mode'=>1),
							array('id'=>'WBW','text'=>TEXT_TYPE_WALLET_BW,'mode'=>1)
								);
	$fields_type['WFU']='P_W';
	$fields_type['WBW']='P_W';

	$mes_array=array('message_id'=>'',
					'message_type'=>'',
					'message_send'=>'B',
					'message_send1'=>'0',
					'message_subject'=>'',
					'message_reply_to'=>STORE_OWNER_EMAIL_ADDRESS,
					'message_text'=>'',
					'message_format'=>'');
	$format_array=array(array('id'=>'T','text'=>TEXT_FORMAT_TEXT),
						array('id'=>'H','text'=>TEXT_FORMAT_HTML),
						array('id'=>'B','text'=>TEXT_FORMAT_BOTH)
						);

	// customer details
	$fields_details['P']=array(array('id'=>'TITLE_P','text'=>TEXT_TITLE_P),
							array('id'=>'FN','text'=>'&nbsp;&nbsp;' . TEXT_FN),
							array('id'=>'LN','text'=>'&nbsp;&nbsp;' . TEXT_LN),
							array('id'=>'DF','text'=>'&nbsp;&nbsp;' . TEXT_DF),
							array('id'=>'EM','text'=>'&nbsp;&nbsp;' . TEXT_EM),
							array('id'=>'TN','text'=>'&nbsp;&nbsp;' . TEXT_TN),
							array('id'=>'FX','text'=>'&nbsp;&nbsp;' . TEXT_FX),
							array('id'=>'SA','text'=>'&nbsp;&nbsp;' . TEXT_SA),
							array('id'=>'CT','text'=>'&nbsp;&nbsp;' . TEXT_CT),
							array('id'=>'SU','text'=>'&nbsp;&nbsp;' . TEXT_SU),
							array('id'=>'ST','text'=>'&nbsp;&nbsp;' . TEXT_ST),
							array('id'=>'PC','text'=>'&nbsp;&nbsp;' . TEXT_PC),
							array('id'=>'CY','text'=>'&nbsp;&nbsp;' . TEXT_CY),
							array('id'=>'IV','text'=>'&nbsp;&nbsp;' . TEXT_IV),
							array('id'=>'RE','text'=>'&nbsp;&nbsp;' . TEXT_RE),
							array('id'=>'UN','text'=>'&nbsp;&nbsp;' . TEXT_UN));
	
	$fields_details['W']=array(array('id'=>'TITLE_W','text'=>TEXT_TITLE_W),
							array('id'=>'AD','text'=>'&nbsp;&nbsp;' . TEXT_WAD),
							array('id'=>'CB','text'=>'&nbsp;&nbsp;' . TEXT_WCB),
							array('id'=>'PT','text'=>'&nbsp;&nbsp;' . TEXT_PT),
							array('id'=>'DD','text'=>'&nbsp;&nbsp;' . TEXT_DD));

	if ($action=='insert_message' || $action=='update_message'){
			$message_text=($HTTP_POST_VARS['message_format']!='T'?$HTTP_POST_VARS['message_text']:strip_tags($HTTP_POST_VARS['message_text'],'<br>'));
			$sql_array=array('message_type'=>tep_db_prepare_input($HTTP_POST_VARS['message_type']),
							'message_subject'=>tep_db_prepare_input($HTTP_POST_VARS['message_subject']),
							'message_reply_to'=>tep_db_prepare_input($HTTP_POST_VARS['message_reply_to']),
							'message_text'=>tep_db_prepare_input($message_text),
							'message_format'=>tep_db_prepare_input($HTTP_POST_VARS['message_format']));
			if (isset($HTTP_POST_VARS["message_send"])){
				$sql_array["message_send"]=$HTTP_POST_VARS["message_send"];
				$sql_array["message_send1"]=$HTTP_POST_VARS["message_send1"];
			}     
			if ($action=='insert_message'){
				tep_db_perform(TABLE_EMAIL_MESSAGES,$sql_array);
			} else {
				$update_id=(int)$HTTP_GET_VARS['id'];
	            tep_db_perform(TABLE_EMAIL_MESSAGES,$sql_array,'update',"message_id='" . (int)$update_id . "'");
			}
   			$msg_id=$HTTP_POST_VARS['message_type'];
			$command='fetch_message_details';
	} else if ($action=='delete_message'){
		$delete_id=(int)$HTTP_GET_VARS['id'];
		tep_db_query("delete from " . TABLE_EMAIL_MESSAGES . " where message_id='" . (int)$delete_id . "'");
		$msg_id=$HTTP_GET_VARS['msg_type'];
		$command='fetch_delete_message';
	} else if ($action=='test_send'){
		tep_send_default_test_email($HTTP_GET_VARS['msg_type']);
		$msg_id=$HTTP_GET_VARS['msg_type'];
		$command='fetch_message_details';
		$alert_message=TEST_EMAIL_SENT_TO;
	} else if ($action=='change_send'){
    	$new_type=$HTTP_POST_VARS['message_send'];
		if ($new_type!='')
		tep_db_query("UPDATE " . TABLE_EMAIL_MESSAGES . " set message_send='" . $new_type . "' where message_id='" . (int)$HTTP_POST_VARS['id'] . "'");
		$command='fetch_message_details';
		$msg_id=$HTTP_POST_VARS['msg_type'];
	}
	
	//==========initial parameters to perform ajax actions====
$submit_pagename=FILENAME_WALLET_EMARKETING;
$variable_name="id";
$form_name="select_event";
$init_editor=true;
require('marketing_functions_ajax.php');
//==========================================================

	if($command=='fetch_message_list')
	{
		echo "message_list{}";
		tep_get_message_list($message_subscription,$message_type_array,$fields_type,'W',0);
		exit;
	}
	elseif($command=='fetch_delete_message')
	{
		echo "del_message{}";
		exit;
	}
	elseif($command=='fetch_message_details')
	{
		for($icnt=0;$icnt<sizeof($message_subscription);$icnt++)
		{
			if($message_subscription[$icnt]['id']==$msg_id)
			{
				tep_get_message_details($message_subscription[$icnt],$message_type_array[$msg_id],$fields_type[$msg_id],'W',0);
				exit;
			}
		}
	}

?>
<!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 type="text/javascript" src="htmlarea/htmlarea.js"></script>
<script type="text/javascript" src="htmlarea/editor.js"></script>
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script language="javascript" src="includes/http.js"></script>
<script language=javascript>
	function validateForm(){
		var error_result="";
		var mes_format;
		if (document.insert_message.message_type.value!='MIV'){
			mes_format=document.insert_message.message_format.value;
			obj=document.insert_message.message_subject.value;
			obj1=document.insert_message.message_reply_to.value;
			if (obj=="" || str_trim(obj)=="")
				error_result="<?php echo tep_db_input(ERR_EMPTY_SUBJECT); ?>";
			else if (obj1=="" || str_trim(obj1)=="")
				error_result="<?php echo tep_db_input(ERR_EMPTY_REPLY_TO); ?>";
			else if (document.insert_message.message_reply_to.value.indexOf("@")<1)
				error_result="<?php echo tep_db_input(ERR_INVALID_REPLY_TO); ?>";
			if (error_result!=""){
				alert(error_result);
				return false;
			}
		}
		return true;
	}
	function AddField(){
		var select_field;
		var select_id;
		if (document.insert_message.fields.selectedIndex<=-1) return;

		select_id=document.insert_message.fields.options[document.insert_message.fields.selectedIndex].value;
		if (select_id.length>3) return;
		select_field="%%" + document.insert_message.fields.options[document.insert_message.fields.selectedIndex].innerHTML + "%%";
		select_field=select_field.replace("&nbsp;&nbsp;","");
		editor.insertHTML(select_field);
	}
	function setAction(ptype){
		if (ptype==1)
			document.insert_message.action_type.value="test";
		else
			document.insert_message.action_type.value="create";
		}
	</script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'marketing_functions_js.php'); ?>
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<table border="0" width="100%" cellspacing="2" cellpadding="2">
	<tr> 
    <!-- body_text //-->
	<td width="100%" valign="top">
	<table border="0" width="100%" cellspacing="0" cellpadding="2">
		<tr>
			<td>
			<table border="0" width="100%" cellspacing="0" cellpadding="2">
			<?php if ($action==''){ ?>
				<tr>
					<td class="pageHeading"><?php //echo TEXT_HEADING_TITLE; ?></td>
				</tr>
				<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td></tr>
				<tr>
					<td id="message_list">
					<?php tep_get_message_list($message_subscription,$message_type_array,$fields_type,'W',0)?>
					</td>
				</tr>
					<?php } ?>
					</table>
					</td>
				</tr>
				<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td></tr>
				</table>
				</td>
<!-- body_text_eof //-->
			</tr>
		</table>
		</td>
	</tr>
</table>
<!-- body_eof //-->
<!-- footer //-->
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>