<?php

/*

  osCommRes, Services Oline 
http://www.oscommres.com 
Copyright (c) 2005 osCommRes 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare

Released under the GNU General Public License 
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );

	require('includes/application_top.php');
	tep_get_last_access_file();require('includes/classes/currencies.php');

	$currencies=new currencies();
	$action=isset($HTTP_GET_VARS["action"])?$HTTP_GET_VARS["action"]:'';
	$found_results=false;
	
	switch($action){
		case "send_message": // store message in table
			$sql_array=array(	'message_from'=>$login_email,
								'message_to'=>tep_db_prepare_input($HTTP_POST_VARS["message_to"]),
								'message_date'=>getServerDate(true),
								'message_subject'=>tep_db_prepare_input($HTTP_POST_VARS["message_subject"]),
								'message_text'=>tep_db_prepare_input($HTTP_POST_VARS["message_text"]),
								'message_unread'=>'Y'
							);
					
			tep_db_perform(TABLE_EVENTS_ADMIN_MESSAGES,$sql_array);
			tep_redirect(tep_href_link(FILENAME_EVENTS_ADMIN_MESSAGES));
			break;
		case "delete_message": // delete message in table
			$delete_ids=(isset($HTTP_POST_VARS["message_id"])?join(",",$HTTP_POST_VARS["message_id"]):'');
			if ($delete_ids!=""){
				tep_db_query("DELETE from " . TABLE_EVENTS_ADMIN_MESSAGES . " where message_id in(" . $delete_ids . ")");
			}
			tep_redirect(tep_href_link(FILENAME_EVENTS_ADMIN_MESSAGES));
			break;
		case "view_message": //view message and reply
			$fetch_id=(isset($HTTP_GET_VARS['message_id'])?(int)$HTTP_GET_VARS['message_id']:0);
			if ($fetch_id>0){
				$message_filter=" and message_id='" . $fetch_id ."'";
				tep_db_query("UPDATE " . TABLE_EVENTS_ADMIN_MESSAGES . " set message_unread='N' where message_id='" . (int)$fetch_id . "'");
			} else {
				$action="new_message";
			}
			break;
		default:
			$message_filter="";
	}
	if ($action=="view_message" || $action==""){ //select message content
		$query="SELECT am.message_id,am.message_from,am.message_to,am.message_date,am.message_subject,am.message_text,am.message_unread,concat(a.admin_firstname,' ',a.admin_lastname) as admin_name from " . TABLE_EVENTS_ADMIN_MESSAGES . " am, " . TABLE_ADMIN . " a where message_to='" . $login_email . "' " . $message_filter . " and am.message_from=a.admin_email_address order by am.message_date desc";
		if ($action==""){
			$messages_split = new splitPageResults($HTTP_GET_VARS['page'], MAX_DISPLAY_SEARCH_RESULTS, $query, $messages_query_numrows);
		}
		$message_query=tep_db_query($query);
		$cnt=tep_db_num_rows($message_query);
		if ($cnt>0)	$found_results=true;
	}
	$admin_query=tep_db_query("SELECT concat(admin_firstname,' ',admin_lastname) as admin_name from " . TABLE_ADMIN . " where admin_id=" . (int)$login_id);
	$admin_result=tep_db_fetch_array($admin_query);
?>
<!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">
<div id="spiffycalendar" class="text"></div>
	<!-- 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%" valign="top">
	<table border="0" width="100%" cellspacing="0" cellpadding="0">
	<?php if ($action==""){ ?>
		<tr><td class="pageHeading"><?php echo sprintf(HEADING_MESSAGES,$admin_result["admin_name"]);?></td></tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '15'); ?></td>
		</tr>
		<?php if ($found_results){?>
		<script language="javascript">
			function changeRowStyle(object,style_name){
				object.className=style_name;
			}
			function validateForm(frm){
				var element=frm.elements["message_id[]"];
				var error="";
				if (element){
					if (element.length){
						for(icnt=0;icnt<element.length;icnt++){
							if (element[icnt].checked) return true;
						}
						error="<?php echo addslashes(stripslashes(ERR_SELECT_MESSAGE));?>";
					} else {
						if (!element.checked) error="<?php echo addslashes(stripslashes(ERR_SELECT_MESSAGE));?>";
					}
				} else {
					error="<?php echo addslashes(stripslashes(ERR_SELECT_MESSAGE));?>";
				}
				if (error!=""){
					alert(error);
					return false;
				}
				return true;
			}
			function navigate(id){
				location.href="<?php echo tep_href_link(FILENAME_EVENTS_ADMIN_MESSAGES,'action=view_message' . '&message_id=');?>"+id;
			}
		</script>
		<tr>
			<td>
				<table border="0" width="100%" cellspacing="0" cellpadding="2">
					<tr>
						<td class="smallText" valign="top"><?php echo $messages_split->display_count($messages_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $HTTP_GET_VARS['page'], TEXT_DISPLAY_NUMBER_OF_MESSAGES); ?></td>
						<td class="smallText" align="right"><?php echo $messages_split->display_links($messages_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $HTTP_GET_VARS['page'],tep_get_all_get_params(array('page','x','y'))); ?>&nbsp;</td>
					</tr>
				</table>
			</td>
		</tr>
		<?php }
			echo tep_draw_form("delete_message",FILENAME_EVENTS_ADMIN_MESSAGES,'action=delete_message','post','onSubmit="javascript:return validateForm(this);"');?>
		<tr>
		<td>
		<table width="100%" border="0" cellspacing="0" cellpadding="2" >
			<tr>
				<td class="tableHeading" width="20"> </td>
				<td class="tableHeading" width="20"> </td>
				<td class="tableHeading"><?php echo TABLE_FROM;?></td>
				<td class="tableHeading"><?php echo TABLE_SUBJECT;?></td>
				<td class="tableHeading" width="150"><?php echo TABLE_DATE;?></td>
			</tr>
			<?php if ($found_results) {
					$rows=0;$currow=0;	
					while($message_result=tep_db_fetch_array($message_query)) {
						$rows++;
						$currow++;
						if (strlen($rows) < 2) {
						  $rows = '0' . $rows;
						}
						if ($currow%2==0) $class="attributes-even";
						else $class="attributes-odd";
			?>
					
					<tr class="<?php echo $class;?>" onMouseOver="javascript:changeRowStyle(this,'messageRowOver');" onMouseOut="javascript:changeRowStyle(this,'<?php echo $class;?>');">
							<td class="main"><?php echo tep_draw_checkbox_field('message_id[]',$message_result['message_id']);?></td>
							<td class="main">
								<?php 	if ($message_result["message_unread"]=="Y") {
											echo tep_image(DIR_WS_ICONS . "unread.gif",IMAGE_UNREAD);
										} else {
											echo tep_image(DIR_WS_ICONS . "read.gif",IMAGE_READ);
										}
								?>
							</td>
							<td class="main" nowrap onClick="javascript:navigate(<?php echo $message_result["message_id"]?>);"><?php echo $message_result["admin_name"];?></td>
							<td class="main" nowrap onClick="javascript:navigate(<?php echo $message_result["message_id"]?>);"><?php echo $message_result["message_subject"];?></td>
							<td class="main" onClick="javascript:navigate(<?php echo $message_result["message_id"]?>);"><?php echo format_date($message_result["message_date"]) . ' ' . date("H:m:s",strtotime($message_result["message_date"]));?></td>
					</tr>
		
			<?php 	$row_cnt++;
					} //while
		 
		} else { // found results ?>
		<tr><td class="smallText" colspan="5" align="center"><?php echo NO_RESULTS;?></td></tr>		
	<?php
		} // check action
	?>
		</table>
		</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '15'); ?></td>
		</tr>
		<tr>
			<td>
				<table border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td><?php	echo '<a href="' . tep_href_link(FILENAME_EVENTS_ADMIN_MESSAGES,'action=new_message') . '">' . tep_image_button('button_new.gif',IMAGE_NEW_MESSAGE) . '<a>';
									if ($found_results) echo '&nbsp;' . tep_image_submit('button_delete.gif',IMAGE_DELETE_MESSAGE);
							?>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		</form>
	<?php } else if ($action=='new_message' || $action=='view_message') { ?>
		<script language="JavaScript">
			function validateForm(frm){
				var error_result="";
				if (frm.message_subject.value==""){
					error_result="<?php echo addslashes(stripslashes(ERR_SUBJECT)); ?>";
				} else if (frm.message_text.value==""){
					error_result="<?php echo addslashes(stripslashes(ERR_TEXT)); ?>";
				}
				if (error_result!=""){
					alert(error_result);
					return false;
				}
				return true;
			}
		</script>
		<?php if ($action=='view_message') { ?>
		<tr>
			<td>
				<table width="100%" border="0" cellspacing="2" cellpadding="2" >
					<tr>
						<td class="tableHeading" width="200"><?php echo TABLE_FROM;?></td>
						<td class="tableHeading" width="200"><?php echo TABLE_SUBJECT;?></td>
						<td class="tableHeading" width="100"><?php echo TABLE_DATE;?></td>
						<td class="tableHeading"><?php echo TABLE_TEXT;?></td>
					</tr>
					<?php 
						$message_result=tep_db_fetch_array($message_query);
					?>
					<tr class="attributes-odd">
						<td class="main" nowrap valign="top"><?php echo $message_result["admin_name"];?></td>
						<td class="main" nowrap valign="top"><?php echo $message_result["message_subject"];?></td>
						<td class="main" valign="top"><?php echo format_date($message_result["message_date"]);?></td>
						<td class="main" valign="top"><?php echo tep_output_string($message_result["message_text"]);?></td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<?php
			}
		?>
		<tr>
		<td class="pageHeading"><?php echo ($action=='view_message'?TEXT_REPLY_MESSAGE:TEXT_NEW_MESSAGE); ?></td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<?php
			//fetch admin users into combo array
			
			$admin_users=array();
			$admin_query=tep_db_query("SELECT admin_id,concat(admin_firstname,' ',admin_lastname) as admin_name,admin_email_address from " . TABLE_ADMIN . " where admin_hide_backend='N' order by admin_name");
			while($admin_result=tep_db_fetch_array($admin_query)){
				$admin_users[]=array('id'=>$admin_result["admin_email_address"],'text'=>$admin_result["admin_name"]);
			}
			echo tep_draw_form("new_message",FILENAME_EVENTS_ADMIN_MESSAGES,'action=send_message','post','onSubmit="return validateForm(this);"');
		?>
		<tr>
			<td>
				<table border="0" cellspacing="5" cellpadding="2" >
					<?php 	if ($action!='view_message'){ ?>
						<tr>
							<td class="main"><?php echo TEXT_MESSAGE_TO; ?></td>
							<td class="main"><?php echo tep_draw_pull_down_menu('message_to',$admin_users); ?></td>
						</tr>
					<?php	} else { 
								echo '<input type=hidden name="message_to" value="' . $message_result['message_from'] . '">';
							} 
					?>
					<tr>
						<td class="main"><?php echo TEXT_MESSAGE_SUBJECT; ?></td>
						<td class="main"><?php echo tep_draw_input_field('message_subject','','size=40',true); ?></td>
					</tr>
					<tr>
						<td class="main" valign=top><?php echo TEXT_MESSAGE_TEXT; ?></td>
						<td class="main"><?php echo tep_draw_textarea_field('message_text','soft','80','24',$mesInfo->message_text,'id="message_text"');?></td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>
		<tr>
			<td>
				<table border="0" cellspacing="0" cellpadding="0">
					<tr>
						<td class="main" align="right">
						<?php
							echo '<a href="' . tep_href_link(FILENAME_EVENTS_ADMIN_MESSAGES) . '">' . tep_image_button("button_cancel.gif",IMAGE_CLOSE) . '</a>';
							echo '&nbsp' . tep_image_submit("button_send.gif",IMAGE_SEND_MESSAGE);
						?>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		</form>
	<?php 
	//MaxiDVD Added WYSIWYG HTML Area Box + Admin Function v1.7 - 2.2 MS2 events Description HTML - </form>
		if (HTML_AREA_WYSIWYG_DISABLE == 'Disable') {} else { ?>
				<script>initEditor('message_text');</script>
	<?php }
	} 
	?>
	</table>
	</td>
	<!-- body_eof //-->
	</tr>
</table>
<!-- body_eof //-->
	<!-- footer //-->
	<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
	<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
