<?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_LANGUAGES.$language.'/'.FILENAME_SUBSCRIPTION_COPY_MESSAGES);
  	$copy_command=isset($HTTP_GET_VARS['copy_command'])?$HTTP_GET_VARS['copy_command']:'';
	if($copy_command!=''){
		switch($copy_command){
		case 'copy_message':
			echo 'message^^^^copy_message^^^^';
			echo tep_subscription_copy_message();
			break;
		case 'save_message':
			echo 'message^^^^save_message^^^^';
			echo update();
			break;
		}
	exit;
	}
  tep_get_last_access_file();
  require('includes/functions/subscription_tree.php');
  tep_get_last_access_file();                                                                                                   //require('includes/functions/subscription_tree.php');
  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'] : '');
  $subscription_id = (isset($HTTP_GET_VARS['subscription_id']) ?(int)$HTTP_GET_VARS['subscription_id'] : 0);
  $select_id=(isset($HTTP_POST_VARS['select_id']) ?(int)$HTTP_POST_VARS['select_id'] : 0);
  $command=(isset($HTTP_GET_VARS['command']) ? $HTTP_GET_VARS['command'] : '');	
  $msg_id = (isset($HTTP_GET_VARS['msg_id']) ?$HTTP_GET_VARS['msg_id'] : 'RCF');
	$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)
							);

					    // event registeration
		$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['O']=array(array('id'=>'TITLE_P','text'=>TEXT_TITLE_O),
								array('id'=>'ON','text'=>'&nbsp;&nbsp;' . TEXT_ON),
								array('id'=>'OC','text'=>'&nbsp;&nbsp;' . TEXT_OC),
								array('id'=>'OD','text'=>'&nbsp;&nbsp;' . TEXT_OD),
								array('id'=>'BT','text'=>'&nbsp;&nbsp;' . TEXT_BT),
								array('id'=>'BA','text'=>'&nbsp;&nbsp;' . TEXT_BA),
								array('id'=>'PY','text'=>'&nbsp;&nbsp;' . TEXT_PY),
								array('id'=>'DD','text'=>'&nbsp;&nbsp;' . TEXT_DD),
								array('id'=>'SB','text'=>'&nbsp;&nbsp;' . TEXT_SB),
								array('id'=>'TX','text'=>'&nbsp;&nbsp;' . TEXT_TX),
								array('id'=>'TL','text'=>'&nbsp;&nbsp;' . TEXT_TL),
								array('id'=>'IL','text'=>'&nbsp;&nbsp;' . TEXT_IL));

		$fields_details['S']=array(array('id'=>'TITLE_S','text'=>TEXT_TITLE_S),
								array('id'=>'NA','text'=>'&nbsp;&nbsp;' . TEXT_SUB_NA),
								array('id'=>'CO','text'=>'&nbsp;&nbsp;' . TEXT_SUB_CO),
								array('id'=>'PE','text'=>'&nbsp;&nbsp;' . TEXT_SUB_PE),
								array('id'=>'ST','text'=>'&nbsp;&nbsp;' . TEXT_SUB_TY),
								array('id'=>'SD','text'=>'&nbsp;&nbsp;' . TEXT_SUB_SD),
								array('id'=>'FD','text'=>'&nbsp;&nbsp;' . TEXT_SUB_FD)
								);
								
		$fields_details['V']=array(array('id'=>'TITLE_V','text'=>TEXT_TITLE_V),
								array('id'=>'CF','text'=>'&nbsp;&nbsp;' . TEXT_CF),
								array('id'=>'CL','text'=>'&nbsp;&nbsp;' . TEXT_CL),
								array('id'=>'FE','text'=>'&nbsp;&nbsp;' . TEXT_FE),
								array('id'=>'IC','text'=>'&nbsp;&nbsp;' . TEXT_IC),
								array('id'=>'IF','text'=>'&nbsp;&nbsp;' . TEXT_IF));
						
	$message_subscription=array();
	$message_subscription=array(
							array('id'=>'RCF','text'=>TEXT_TYPE_SUB_RCF,'mode'=>1),
							array('id'=>'INV','text'=>TEXT_TYPE_SUB_INV,'mode'=>4),
							array('id'=>'RN1','text'=>TEXT_TYPE_SUB_RN1,'mode'=>4),
							array('id'=>'RN2','text'=>TEXT_TYPE_SUB_RN2,'mode'=>4),
							array('id'=>'RN3','text'=>TEXT_TYPE_SUB_RN3,'mode'=>4),
							array('id'=>'RN4','text'=>TEXT_TYPE_SUB_RN4,'mode'=>4),
							array('id'=>'RN5','text'=>TEXT_TYPE_SUB_RN5,'mode'=>4),
							array('id'=>'MEV','text'=>TEXT_TYPE_SUB_MEV,'mode'=>2)
						);
	
	$message_type_array=array();
	$message_type_array['RCF']=array(array('id'=>'AFR','text'=>TEXT_SUB_AFR),
									array('id'=>'DFR','text'=>TEXT_SUB_DFR));

	
	$fields_type['RCF']='P_O_S';
	$fields_type['RN1']='P_S';
	$fields_type['RN2']='P_S';
	$fields_type['RN3']='P_S';
	$fields_type['RN4']='P_S';
	$fields_type['RN5']='P_S';
	$fields_type['MEV']='P_E';
	$fields_type['INV']='S_V';

  	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']),
							'subscription_id'=>$subscription_id,
							'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_SUBSCRIPTION_MESSAGES,$sql_array);
			} else {
				$update_id=(int)$HTTP_GET_VARS['id'];
	
			tep_db_perform(TABLE_SUBSCRIPTION_MESSAGES,$sql_array,'update',"subscription_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_SUBSCRIPTION_MESSAGES . " where subscription_message_id='" . (int)$delete_id . "'");
		$command='fetch_delete_message';
		$msg_id=$HTTP_GET_VARS['msg_type'];
	} else if ($action=='test_send'){
		tep_send_subscription_test_email($HTTP_GET_VARS['msg_type'],(int)$subscription_id);
		$command='fetch_message_details';
		$msg_id=$HTTP_GET_VARS['msg_type'];
		$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_SUBSCRIPTION_MESSAGES . " set message_send='" . $new_type . "' where subscription_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_SUBSCRIPTION_MESSAGES;
$variable_name="subscription_id";
$form_name="select_subscription";
$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,'S',$subscription_id);
		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],'S',$subscription_id);
				exit;
			}
		}
	}
  $subscription_array=tep_get_subscription_array_single();
  $subscription_found=true;
  if (sizeof($subscription_array)<=0){
		$subscription_found=false;
  } else {
		if ($select_id!=0 && $select_id!=$subscription_id)
			$subscription_id=$select_id;
		else if ($select_id==0 && $subscription_id==0)
			$subscription_id=$subscription_array[0]['id'];
  }

?>
<!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 language="javascript" src="includes/subscription_select.js"></script>
<script type="text/javascript" src="htmlarea/htmlarea.js"></script>
<script type="text/javascript" src="htmlarea/editor.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'); ?>
<!-- header_eof //-->

<!-- body //-->
<table id="holetable" border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr> 
	<!-- body_text //-->
	<td width="100%" valign="top" id="formtable">
	<table border="0" width="100%" cellspacing="0" cellpadding="0">
    	<tr>
			<td>
		    <table border="0" width="100%" cellspacing="0" cellpadding="0">
			<?php if ($subscription_found==true){
				      if ($action==''){ 
			?>
				<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
				<tr>
					<td><?php echo tep_draw_form('select_subscription',FILENAME_SUBSCRIPTION_MESSAGES); 
								$selected_name="";
					?>
					<table border="0" cellspacing="0" cellpadding="2">
						<div id="subscription_list" style="position:absolute;display:none;width:400px">
							<?php echo tep_draw_subscription_select_menu('select_id',$subscription_array,$subscription_id,'size=10 style="width:400px" onClick="javascript:setListboxText(0);toggleList(0);" onFocus="javascript:toggleList(1);" onBlur="javascript:toggleList(0);" onchange="javascript:fetch_message();" class="customerSelect"');?>
						</div>
						<tr>
							<td class="main" width="150"><?php echo TEXT_SELECT_SUBSCRIPTION;?></td>
							<td class="main"><?php echo tep_draw_input_field('select_text',$selected_name,'onFocus="javascript:toggleText(1);" onBlur="javascript:toggleText(0);" style="width:400px"');?></td>
						</tr>
					</table>
					</form>
					<script language="JavaScript">
						if(na6) document.select_subscription.select_text.addEventListener("keyup",selectList,true);
						if(iex) document.select_subscription.select_text.onkeyup=selectList;
						if(na6) document.select_subscription.select_text.addEventListener("keydown",scrollList,true);
						if(iex) document.select_subscription.select_text.onkeydown=scrollList;
					</script>
					
					</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_subscription,$message_type_array,$fields_type,'S',$subscription_id)?>
					</td>
					</tr>
			<!--	<tr>
						<td class="main"><?php //echo '<a href="' . tep_href_link(FILENAME_SUBSCRIPTION_COPY_MESSAGES,'suID=' . $subscription_id . '&type=su') . '">' . tep_image_button('button_copy_messages.gif',IMAGE_COPY_MESSAGES) . "</a>"; ?></td>
					</tr>
			!-->		
					<tr><td><br></td></tr>
					<tr>
					<td class="openContent" id="copy_message_load"></td>
					</tr>
				</form>
				<?php } 
					} else {?>
						<tr><td class="pageHeading"><?php echo TEXT_HEADING_EMPTY_SUBSCRIPTIONS ;?></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'); ?>
<script language=javascript>
var ajax_image_load='<?php echo tep_image(DIR_WS_IMAGES.'24-1.gif'); ?>';
	function message_validateForm()	{		
			var icnt;
			var listbox,error_message='';
			var error_result=Array();
			var listbox_name=Array("select_message[]","select_subscription[]");
			error_result[0]="<?php echo stripslashes(addslashes(ERR_SELECT_MESSAGE));?>";
			error_result[1]="<?php echo stripslashes(addslashes(ERR_SELECT_SUBSCRIPTION));?>";
			for(ecnt=0;ecnt<listbox_name.length;ecnt++){
				listbox=document.copy_message.elements[listbox_name[ecnt]];
				if (listbox==null) break;
				if (listbox.length){
					for(icnt=0;icnt<listbox.length;icnt++){
						if (listbox[icnt].checked){
							 error_result[ecnt]="";
							 break;
						}
					}
				} else {
					if (listbox.checked) error_result[ecnt]="";
				}
			}
			if (error_result[0]!="")
				error_message=error_result[0];

			if (error_result[1]!="")
				error_message+='\n'+error_result[1];
		if(error_message!='')
			alert(error_message);
		else {
		do_post_command('copy_message','<?php echo tep_href_link(FILENAME_SUBSCRIPTION_MESSAGES,'copy_command=save_message'); ?>');
		document.getElementById('copy_message_load').innerHTML=ajax_image_load;
		}
	}
	document.getElementById('copy_message_load').style.display='none';
	function copy_message(suID,type){
	hide_message_details();
	document.getElementById('copy_message_load').style.display='';
	document.getElementById('copy_message_load').innerHTML=ajax_image_load;
	do_get_command('<?php echo tep_href_link(FILENAME_SUBSCRIPTION_MESSAGES,'copy_command=copy_message&suID='); ?>'+suID+'&type='+type);
	}
	function close_copy_message(){
	document.getElementById('copy_message_load').style.display='none';
	document.getElementById('copy_message_load').innerHTML='';
	}
</script>
<?php
function tep_subscription_copy_message(){
	global $HTTP_GET_VARS;
	require(DIR_WS_CLASSES . '/currencies.php');
  	$currencies=new currencies;
	
	$action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
	$subscription_id = (isset($HTTP_GET_VARS['suID']) ?(int)$HTTP_GET_VARS['suID'] : 0);
	$template_type=(isset($HTTP_GET_VARS['type'])?strtolower($HTTP_GET_VARS['type']) : "su");
	$templates=getTemplates($subscription_id,$template_type);
	//table name;
	switch($template_type){
		case "su":
		default:
			$table_name=TABLE_SUBSCRIPTION_MESSAGES;
			$back_url=FILENAME_SUBSCRIPTION_MESSAGES;
			$fields_list="message_send,message_send1,message_subject,message_reply_to,message_text,message_format,subscription_id,message_type";
	
	$type_params="";
	reset($templates);
	while(list($key,)=each($templates)){
		if ($type_params=="") $type_params.=" and message_type in(";
		$type_params.="'" . $key . "',";
	}
	$type_params=substr($type_params,0,-1) . ")";
	$message_query=tep_db_query("SELECT count(*) as total,message_type from " . $table_name . " where subscription_id='" . $subscription_id . "' " . $type_params . " group by message_type");
	while($message=tep_db_fetch_array($message_query)){
		if ((int)$message["total"]>0 && isset($templates[$message['message_type']])){
			$templates[$message['message_type']]['status']=1;
			$templates[$message['message_type']]['color']='#0000FF';
		}
	}
	$subscription_array=array();
	$subscription_query=tep_db_query("SELECT su.subscription_id,sud.subscription_name from " . TABLE_SUBSCRIPTIONS . " su, " . TABLE_SUBSCRIPTIONS_DESCRIPTION . " sud where su.subscription_id=sud.subscription_id " . SUBSCRIPTION_CONDITION_STATUS . " order by sud.subscription_name");
	while($subscription_result=tep_db_fetch_array($subscription_query)){
		if ($subscription_id!=$subscription_result["subscription_id"])
			$subscription_array[$subscription_result['subscription_id']]=array('text'=>$subscription_result['subscription_name'],'color'=>"#000000");
	}
	echo tep_draw_form("copy_message",FILENAME_SUBSCRIPTION_COPY_MESSAGES,'action=update&suID=' . $subscription_id . '&type=' . $template_type,"post",'onSubmit="javascript:return validateForm();"');
	echo tep_draw_hidden_field('suID',$subscription_id).tep_draw_hidden_field('type',$template_type);
	?>
	
	<table border="0" width="100%" cellspacing="0" cellpadding="2">
	<tr>
		<td class="openContent_top"><table width="100%" cellpadding="0" cellspacing="0" border="0">
		<tr>
			<td class="pageHeading"><?php echo tep_image(DIR_WS_IMAGES.'template/icon_active.gif'); ?>&nbsp;Copy Messages - <?php echo tep_get_subscription_name($subscription_id); ?></td>
			<td align="right"><?php echo '<a href="javascript: message_validateForm();">'.tep_image(DIR_WS_IMAGES.'template/img_savel.gif','Save','','','').'</a>&nbsp;&nbsp;<a href="javascript: close_copy_message();">'.tep_image(DIR_WS_IMAGES.'template/img_closel.gif','Close','','','').'</a>'; ?>&nbsp;</td>
		</tr>
		</table></td>
	</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif','1','5');?></td>
		</tr>
			<tr>
				<td>
				<table border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td>
							<table border="0" cellpadding="2" cellspacing="5" >
								<tr class="attributes-even">
									<td class="tableHeading"><?php echo SELECT_MESSAGES;?></td>
									<td class="tableHeading"><?php echo SELECT_SUBSCRIPTIONS;?></td>
								</tr>
								<tr>
									<td class="tableHeading"><?php echo customListBox("select_message",$templates,0,300,200);?></td>
									<td class="tableHeading"><?php echo customListBox("select_subscription",$subscription_array,0,500,200);?></td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td><?php echo tep_draw_separator('pixel_trans.gif','1','5');?></td>
					</tr>
				</table>
				</td>
			</tr>
		</table>	
	 </form>
   <?php }?>
<?php }?>	
	
<?php  // function used in this page

	function update(){
		global $HTTP_POST_VARS;
		$message_keys=&$HTTP_POST_VARS["select_message"];
		$subscription_keys=&$HTTP_POST_VARS["select_subscription"];
	
		$subscription_id = (isset($HTTP_POST_VARS['suID']) ?(int)$HTTP_POST_VARS['suID'] : 0);
		$template_type=(isset($HTTP_POST_VARS['type'])?strtolower($HTTP_POST_VARS['type']) : "su");
		$templates=getTemplates($subscription_id,$template_type);
		
		/*if (sizeof($message_keys)<=0 || sizeof($subscription_keys)<=0 || $subscription_id<0)
			tep_redirect(tep_href_link($back_url,'subscription_id=' . $subscription_id));
		*/
		switch($template_type){
			case "su":
			default:
				$table_name=TABLE_SUBSCRIPTION_MESSAGES;
				$back_url=FILENAME_SUBSCRIPTION_MESSAGES;
				$fields_list="message_send,message_send1,message_subject,message_reply_to,message_text,message_format,subscription_id,message_type";
		}
		// fetch subscription into total array to find and subscription has already message
		$status=array();
		reset($subscription_keys);
		for ($icnt=0;$icnt<sizeof($subscription_keys);$icnt++){
			$status[$subscription_keys[$icnt]]=array();
		}
		//get the input message_ids,subscription_ids
		$message_string=join("','",$message_keys);
		$subscription_string=join(",",$subscription_keys);

		$message_query=tep_db_query("SELECT  " . $fields_list . " from " . $table_name . " where subscription_id='" . $subscription_id . "' and message_type in('" . $message_string . "')");

		// get the message status for subscriptions we have to copy
		$subscription_status_query=tep_db_query("SELECT count(*) as total,message_type,subscription_id from " . $table_name . " where subscription_id in(" . $subscription_string . ") and message_type in('" . $message_string . "') group by subscription_id,message_type");
		
		while($subscription_status=tep_db_fetch_array($subscription_status_query)){
			if (isset($status[$subscription_status["subscription_id"]])){
				$status[$subscription_status["subscription_id"]][$subscription_status["message_type"]]=$subscription_status["total"];
			}
		}
		while($message=tep_db_fetch_array($message_query)){
			reset($status);
			//iterate with the subscription we have to find insert or update
			while(list($sukey,)=each($status)){
				$message["subscription_id"]=$sukey;
				$count=isset($status[$sukey][$message["message_type"]])?$status[$sukey][$message["message_type"]]:0;
				if ($count>0){ // update operation
					tep_db_perform($table_name,$message,"update","subscription_id='" . $sukey . "' and message_type='" . $message["message_type"] . "'");
				} else { //insert operation
					tep_db_perform($table_name,$message);
				}
			}
		}
	}	

  // function used in this page

	function &getTemplates($subscription_id,$type){
		$templates=array();
		switch($type){
			case "su": //subscription messages
			default:
				$templates=array(	'RCF'=>array('text'=>TEXT_TYPE_SUB_RCF,'status'=>0,'color'=>'#FF0000'),
									'INV'=>array('text'=>TEXT_TYPE_SUB_INV,'status'=>0,'color'=>'#FF0000'),
									'RN1'=>array('text'=>TEXT_TYPE_SUB_RN1,'status'=>0,'color'=>'#FF0000'),
									'RN2'=>array('text'=>TEXT_TYPE_SUB_RN2,'status'=>0,'color'=>'#FF0000'),
									'RN3'=>array('text'=>TEXT_TYPE_SUB_RN3,'status'=>0,'color'=>'#FF0000'),
									'RN4'=>array('text'=>TEXT_TYPE_SUB_RN4,'status'=>0,'color'=>'#FF0000'),
									'RN5'=>array('text'=>TEXT_TYPE_SUB_RN5,'status'=>0,'color'=>'#FF0000'));

		}
		return $templates;
	}
	function customListBox($name,&$list,$selected="",$width=300,$height=100){
	$result="";
		$result.='<div id="' . $name . '_box" style="background-color: #FFFFFF;border-color: #7b9ebd; border-style: solid; border-width: 1px;overflow:auto;width:' . $width . ';height:' . $height . '">' . "\n";
		$result.='<table border="0" cellspacing="0" cellpadding="2" width="100%" valign="middle" align="center" class="BoxTable">' . "\n";
		reset($list);

		$selected=array();
		if (isset($HTTP_POST_VARS[$name]) && is_array($HTTP_POST_VARS[$name])){
			$selected=&$HTTP_POST_VARS[$name];
		}
		if (count($list)>0){
			while(list($key,)=each($list)){
				$result.='<tr class="attributes-even"><td class="main" width="20">' . "\n";
				$result.='<input type="checkbox" name="' . $name . '[]" value="' . $key . '" ' . (in_array($key,$selected)?"checked":"") . ' >';
				$result.='<td class="main"><font color="' . $list[$key]['color'] . '">' . $list[$key]['text'] .'</font></td></tr>' ."\n";
			}
		} else {
			$result.='<tr><td class="main">' . TEXT_NO_SUBSCRIPTIONS . '</td></tr>';
		}
		$result.='</table></div>' . "\n";
		echo $result;
	}
?>