<?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;  
  $alert_message="";


  $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'] : '');
  // get input entries to find type of entry
 $message=array();
	$message=array(
				array('id'=>'PRD','text'=>TEXT_TYPE_PRD,'mode'=>1),
				array('id'=>'REM','text'=>TEXT_TYPE_REM,'mode'=>4),
				array('id'=>'PST','text'=>TEXT_TYPE_PST,'mode'=>1),
				array('id'=>'PRS','text'=>TEXT_TYPE_PRS,'mode'=>1),
				array('id'=>'PRE','text'=>TEXT_TYPE_PRE,'mode'=>1),
				);
	$mes_array=array('message_id'=>'',
					'message_type'=>'',
					'message_send'=>'',
					'message_subject'=>'',
					'message_reply_to'=>STORE_OWNER_EMAIL_ADDRESS,
					'message_text'=>'',
					'message_format'=>'');					
	$fields_type['PRD']='U_D_B_O';
	$fields_type['REM']='P';
	$fields_type['PST']='U_S_O';
	//$fields_type['PSP']='U_S_O';
	$fields_type['PRS']='C_T';
	$fields_type['PRE']='C_T'; 
	$format_array=array(array('id'=>'T','text'=>TEXT_FORMAT_TEXT),
						array('id'=>'H','text'=>TEXT_FORMAT_HTML),
						array('id'=>'B','text'=>TEXT_FORMAT_BOTH));
	
	$fields_details['C']=array(
							array('id'=>'TITLE_C','text'=>TEXT_TITLE_C),	
							array('id'=>'CF','text'=>'&nbsp;&nbsp;' . 'ÂÂÂ'),
							array('id'=>'CL','text'=>'&nbsp;&nbsp;' . CUST_CL),
							array('id'=>'NO','text'=>'&nbsp;&nbsp;' . ORDR_NO),
							array('id'=>'OP','text'=>'&nbsp;&nbsp;' . ORDR_OP),
							array('id'=>'PD','text'=>'&nbsp;&nbsp;' . ORDR_PD),
							array('id'=>'PS','text'=>'&nbsp;&nbsp;' . TEXT_P_STATUS),
							array('id'=>'OC','text'=>'&nbsp;&nbsp;' . TEXT_OC),
							);				
	$fields_details['T']=array(
							array('id'=>'TITLE_T','text'=>TEXT_TITLE_T),
							array('id'=>'SN','text'=>'&nbsp;&nbsp;' . TEXT_SN),
							array('id'=>'SM','text'=>'&nbsp;&nbsp;' . TEXT_SM),
							array('id'=>'SE','text'=>'&nbsp;&nbsp;' . TEXT_SE),
							);
											
	$fields_details['P']=array(
							array('id'=>'TITLE_P','text'=>TEXT_TITLE_P),
							array('id'=>'PN','text'=>'&nbsp;&nbsp;' . TEXT_PN),
							array('id'=>'PP','text'=>'&nbsp;&nbsp;' . TEXT_PP),
							array('id'=>'PA','text'=>'&nbsp;&nbsp;' . TEXT_PA),
							array('id'=>'MO','text'=>'&nbsp;&nbsp;' . TEXT_P_M),
							array('id'=>'DA','text'=>'&nbsp;&nbsp;' . TEXT_P_DA),
							array('id'=>'PW','text'=>'&nbsp;&nbsp;' . TEXT_P_W),
							array('id'=>'UP','text'=>'&nbsp;&nbsp;' . TEXT_P_A_UP),
							array('id'=>'PU','text'=>'&nbsp;&nbsp;' . TEXT_P_U),
							array('id'=>'PQ','text'=>'&nbsp;&nbsp;' . TEXT_P_Q),
							array('id'=>'TITLE_U','text'=>TEXT_TITLE_U),
							array('id'=>'SN','text'=>'&nbsp;&nbsp;' . TEXT_SN),
							array('id'=>'SM','text'=>'&nbsp;&nbsp;' . TEXT_SM),
							array('id'=>'SE','text'=>'&nbsp;&nbsp;' . TEXT_SE),
							);
	$fields_details['S']=array(
							array('id'=>'TITLE_P','text'=>TEXT_TITLE_P),
							array('id'=>'PN','text'=>'&nbsp;&nbsp;' . TEXT_PN),
							array('id'=>'PP','text'=>'&nbsp;&nbsp;' . TEXT_PP),
							array('id'=>'PQ','text'=>'&nbsp;&nbsp;' . TEXT_P_Q),
							array('id'=>'PS','text'=>'&nbsp;&nbsp;' . TEXT_P_STATUS),
							);						
	$fields_details['O']=array(
							array('id'=>'TITLE_O','text'=>TEXT_TITLE_O),
							array('id'=>'NO','text'=>'&nbsp;&nbsp;' . ORDR_NO),
							array('id'=>'OP','text'=>'&nbsp;&nbsp;' . ORDR_OP),
							array('id'=>'OL','text'=>'&nbsp;&nbsp;' . ORDR_OL),
							array('id'=>'PO','text'=>'&nbsp;&nbsp;' . ORDR_PO),
							array('id'=>'OM','text'=>'&nbsp;&nbsp;' . ORDR_OM),
							array('id'=>'OT','text'=>'&nbsp;&nbsp;' . ORDR_OT),
							array('id'=>'PM','text'=>'&nbsp;&nbsp;' . ORDR_PM),
							array('id'=>'DD','text'=>'&nbsp;&nbsp;' . ORDR_DD),
							array('id'=>'DL','text'=>'&nbsp;&nbsp;' . TEXT_DL),
							array('id'=>'PF','text'=>'&nbsp;&nbsp;' . ORDR_PF),
							);
	$fields_details['U']=array(
							array('id'=>'TITLE_U','text'=>TEXT_TITLE_U),
							array('id'=>'CF','text'=>'&nbsp;&nbsp;' . CUST_CF),
							array('id'=>'CL','text'=>'&nbsp;&nbsp;' . CUST_CL),
							array('id'=>'CM','text'=>'&nbsp;&nbsp;' . CUST_CM),
							array('id'=>'SN','text'=>'&nbsp;&nbsp;' . TEXT_SN),
							array('id'=>'SM','text'=>'&nbsp;&nbsp;' . TEXT_SM),
							array('id'=>'SE','text'=>'&nbsp;&nbsp;' . TEXT_SE),
							array('id'=>'AL','text'=>'&nbsp;&nbsp;' . TEXT_AL),
							array('id'=>'CT','text'=>'&nbsp;&nbsp;' . CUST_CT),
							array('id'=>'CP','text'=>'&nbsp;&nbsp;' . CUST_CP),
							array('id'=>'CC','text'=>'&nbsp;&nbsp;' . CUST_CC),
							array('id'=>'CS','text'=>'&nbsp;&nbsp;' . CUST_CS),
							array('id'=>'CE','text'=>'&nbsp;&nbsp;' . CUST_CE),
							array('id'=>'CU','text'=>'&nbsp;&nbsp;' . CUST_CU),
							array('id'=>'CO','text'=>'&nbsp;&nbsp;' . CUST_CO),
							array('id'=>'CA','text'=>'&nbsp;&nbsp;' . CUST_CA),
							);
	$fields_details['B']=array(
							array('id'=>'TITLE_B','text'=>TEXT_TITLE_B),
							array('id'=>'NA','text'=>'&nbsp;&nbsp;' . BILL_NA),
							array('id'=>'CM','text'=>'&nbsp;&nbsp;' . BILL_CM),
							array('id'=>'CT','text'=>'&nbsp;&nbsp;' . BILL_CT),
							array('id'=>'CP','text'=>'&nbsp;&nbsp;' . BILL_CP),
							array('id'=>'CC','text'=>'&nbsp;&nbsp;' . BILL_CC),
							array('id'=>'CS','text'=>'&nbsp;&nbsp;' . BILL_CS),
							array('id'=>'CE','text'=>'&nbsp;&nbsp;' . BILL_CE),
							array('id'=>'CU','text'=>'&nbsp;&nbsp;' . BILL_CU),
							);
	$fields_details['D']=array(
							array('id'=>'TITLE_D','text'=>TEXT_TITLE_D),
							array('id'=>'NA','text'=>'&nbsp;&nbsp;' . DELI_NA),
							array('id'=>'CM','text'=>'&nbsp;&nbsp;' . DELI_CM),
							array('id'=>'CT','text'=>'&nbsp;&nbsp;' . DELI_CT),
							array('id'=>'CP','text'=>'&nbsp;&nbsp;' . DELI_CP),
							array('id'=>'CC','text'=>'&nbsp;&nbsp;' . DELI_CC),
							array('id'=>'CS','text'=>'&nbsp;&nbsp;' . DELI_CS),
							array('id'=>'CE','text'=>'&nbsp;&nbsp;' . DELI_CE),
							array('id'=>'CU','text'=>'&nbsp;&nbsp;' . DELI_CU),
							);
	$fields_details['I']=array(array('id'=>'TITLE_I','text'=>TEXT_TITLE_O),
							array('id'=>'FN','text'=>'&nbsp;&nbsp;' . TEXT_FN),
							array('id'=>'LN','text'=>'&nbsp;&nbsp;' . TEXT_LN),
							array('id'=>'NO','text'=>'&nbsp;&nbsp;' . TEXT_NO),
							array('id'=>'OP','text'=>'&nbsp;&nbsp;' . TEXT_OP),
							array('id'=>'OL','text'=>'&nbsp;&nbsp;' . TEXT_OL),
							array('id'=>'OL','text'=>'&nbsp;&nbsp;' . TEXT_OM),
							array('id'=>'PO','text'=>'&nbsp;&nbsp;' . TEXT_PO),
							array('id'=>'PM','text'=>'&nbsp;&nbsp;' . TEXT_PM),
							array('id'=>'DT','text'=>'&nbsp;&nbsp;' . PAYMT_DT),
							array('id'=>'BA','text'=>'&nbsp;&nbsp;' . ORDR_BA),
							array('id'=>'SA','text'=>'&nbsp;&nbsp;' . ORDR_SA),
							array('id'=>'SM','text'=>'&nbsp;&nbsp;' . TEXT_SM),
							array('id'=>'AD','text'=>'&nbsp;&nbsp;' . STORE_AD)
							);
	  $pid=$HTTP_GET_VARS['pID'];
	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 ($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 . "'");
		$command='fetch_delete_message';
		$msg_id=$HTTP_GET_VARS['msg_type'];
	} else if ($action=='test_send'){ 
			tep_send_default_test_email($HTTP_GET_VARS['msg_type']);
			$command='fetch_message_details';
			$msg_id=$HTTP_GET_VARS['msg_type'];
			$alert_message=TEST_EMAIL_SENT_TO;
	} 
	else if($action=='send_message') {
			//$product_query = tep_db_query("Select p.products_id,p.products_price,pd.products_name,pr.email_address from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION ." pd," .TABLE_PRODUCTS_REMINDERS. " pr where pd.products_id=p.products_id and pr.products_id=p.products_id  and pr.products_id='".$pid."' group by pr.attributes_id,pr.email_address" );
			if(isset($HTTP_GET_VARS['attribute_value'])){
			$attribute_value=(isset($HTTP_GET_VARS['attribute_value']) ? $HTTP_GET_VARS['attribute_value'] : '');
			}
			if($attribute_value!="")
			{
				$attribute_value=tep_get_sorted_attribute_ids($attribute_value);
				$id_splt=split("-",$attribute_value);
				for($inc=0;$inc<count($id_splt);$inc++)
				{
				  $option_values=split('{',$id_splt[$inc]);
				  $att_value=tep_db_query("select po.products_options_name,po.products_options_id,pov.products_options_values_name,pov.products_options_values_id from " . TABLE_PRODUCTS_OPTIONS ." po ," . TABLE_PRODUCTS_OPTIONS_VALUES . " pov ," . TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS . " povp, " . TABLE_PRODUCTS_ATTRIBUTES . " pa where po.products_options_id=povp.products_options_id and pov.products_options_values_id=povp.products_options_values_id and pa.options_id=po.products_options_id and pa.options_values_id=pov.products_options_values_id and pa.products_id='".(int)$pid."' and po.products_options_id = '".$option_values[0]."' and pov.products_options_values_id='".$option_values[1]. "' ");
                  $att_value_result = tep_db_fetch_array($att_value);
				  $att_op_name.= $att_value_result['products_options_name'].'-'.$att_value_result['products_options_values_name'].'<br>';
				}
			}
			if($pid!=""){
			//$product_query = tep_db_query("Select p.products_id,p.products_price,pd.products_name,pr.email_address,pr.attributes_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION ." pd," .TABLE_PRODUCTS_REMINDERS. " pr where pd.products_id=p.products_id and pr.products_id=p.products_id  and pr.products_id='".$pid."' and pr.attributes_id='" . $attribute_value . "' group by pr.attributes_id,pr.email_address" );
				if(isset($HTTP_GET_VARS['attribute_value']))$product_query = tep_db_query("Select p.products_id,p.products_model,p.products_date_available,p.products_quantity,p.products_weight,p.products_price,pd.products_name,pd.products_url,pa.products_attributes_units_price,pr.email_address,pr.attributes_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd," . TABLE_PRODUCTS_ATTRIBUTES . " pa," . TABLE_PRODUCTS_REMINDERS . " pr where pd.products_id=p.products_id and pr.products_id=p.products_id  and pr.products_id=" . (int)$pid . "  and p.products_id=pa.products_id and pr.attributes_id='" . $attribute_value . "' group by pr.attributes_id,pr.email_address");
				else $product_query = tep_db_query("Select p.products_id,p.products_model,p.products_date_available,p.products_quantity,p.products_weight,p.products_price,pd.products_name,pd.products_url,pr.email_address,pr.attributes_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd," . TABLE_PRODUCTS_REMINDERS . " pr where pd.products_id=p.products_id and pr.products_id=p.products_id  and pr.products_id=" . (int)$pid . "  group by pr.attributes_id,pr.email_address");
			}
			else
				$product_query = tep_db_query("Select p.products_id,p.products_price,pd.products_name,pr.email_address,pr.attributes_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION ." pd," .TABLE_PRODUCTS_REMINDERS. " pr where pd.products_id=p.products_id and pr.products_id=p.products_id  group by pr.attributes_id,pr.email_address" );
			$num_rows=tep_db_num_rows($product_query);
			if ($num_rows>0) { 
				while($product_result=tep_db_fetch_array($product_query)){
				$merge_details[TEXT_PN]=$product_result['email_address'].$product_result['products_name'];
				$merge_details[TEXT_PP]=$product_result['products_price'];
				$merge_details[TEXT_PA]=$att_op_name;
				$merge_details[TEXT_P_M]=$product_result['products_model'];
				$merge_details[TEXT_P_DA]=$product_result['products_date_available'];
				$merge_details[TEXT_P_W]=$product_result['products_weight'];
				$merge_details[TEXT_P_A_UP]=$product_result['products_attributes_units_price'];
				$merge_details[TEXT_P_U]=$product_result['products_url'];
				$merge_details[TEXT_P_Q]=$product_result['products_quantity'];
				$merge_details[TEXT_SN]=STORE_OWNER;
				$merge_details[TEXT_SM]=STORE_NAME;
				$merge_details[TEXT_SE]=STORE_OWNER_EMAIL_ADDRESS;
				$send_details[0]['to_email']=$product_result['email_address'];
				$send_details[0]['from_name']=STORE_OWNER;
				$send_details[0]['from_email']=STORE_OWNER_EMAIL_ADDRESS;
				tep_send_default_email($HTTP_GET_VARS['msg_type'],$merge_details,$send_details);
				tep_db_query("delete from  " . TABLE_PRODUCTS_REMINDERS . " where email_address='" .$product_result['email_address'] . "' and products_id='" . $product_result['products_id'] . "' and attributes_id='" . $product_result['attributes_id'] . "'");
			 }
		}
		if($HTTP_GET_VARS['return']=='PE'){
 			echo 'email_sent_success';
 			exit;
 			//tep_redirect(tep_href_link(FILENAME_PRODUCTS_EXPECTED,'flag=Y&mcount=' .$num_rows));
 		}
		$command='fetch_message_details';
		$msg_id=$HTTP_GET_VARS['msg_type'];
		$alert_message=NOTICE_EMAIL_SENT_TO;
	 }
//==========initial parameters to perform ajax actions====
$submit_pagename=FILENAME_PRODUCTS_MESSAGE;
$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,$message_type_array,$fields_type,'P',0);
		exit;
	}
	elseif($command=='fetch_delete_message')
	{
		echo "del_message{}";
		exit;
	}
	elseif($command=='fetch_message_details')
	{  print_r($message);
		for($icnt=0;$icnt<sizeof($message);$icnt++)
		{ 
			if($message[$icnt]['id']==$msg_id)
			{  
				tep_get_message_details($message[$icnt],$message_type_array[$msg_id],$fields_type[$msg_id],'P',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;
	   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); ?>";
	/* else if (document.insert_message.message_text.value=="")
	   error_result="<?php //echo tep_db_input(ERR_EMPTY_EMAIL_TEXT); ?>";*/
	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;
	if (!editorHasFocus) {
		//alert("Please Select Editor");
		//return;
	}
	editor._doc.body.focus;
 	select_id=document.insert_message.fields.options[document.insert_message.fields.selectedIndex].value;
	if (select_id.length>2) 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>
<?php
// WebMakers.com Added: Java Scripts
include(DIR_WS_INCLUDES . 'javascript/' . 'webmakers_added_js.php')
?>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onLoad="SetFocus();">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'marketing_functions_js.php'); ?>
<?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%" 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><?php echo tep_draw_separator('pixel_trans.gif','1','10'); ?></td></tr>
				<tr>
					<td id="message_list">
						<?php
						tep_get_message_list($message,$message_type_array,$fields_type,'P',0)?>
					</td>
					</tr>
				<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td></tr>
				<?php } ?>
				</table>
				</td>
<!-- body_text_eof //-->
			</tr>
		</table>
		</td>
	</tr>
</table>
<!-- body_eof //-->
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>