<?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();  
	$command=(isset($HTTP_GET_VARS['command'])?$HTTP_GET_VARS['command']:'');
	if($command!="") {
		if($command=='search_details') {
			echo 'search_details^';
			$search_found=false;
			if (!isset($HTTP_POST_VARS["form_values"])){
				$from_date=$end_date=getServerDate();
				$show_email=$show_sms=false;
				$customers_id=0;
			} else {
				$from_date=(isset($HTTP_POST_VARS['from_date'])?tep_convert_date_raw($HTTP_POST_VARS['from_date']):'');
				$end_date=(isset($HTTP_POST_VARS['end_date'])?tep_convert_date_raw($HTTP_POST_VARS['end_date']):'');
				$customers_id=(isset($HTTP_POST_VARS['customers_id'])?(int)$HTTP_POST_VARS['customers_id']:0);
				$show_email=(isset($HTTP_POST_VARS['show_email'])?true:false);
				$show_sms=(isset($HTTP_POST_VARS['show_sms'])?true:false);
			}
			$date_option1=$date_option2=$customer_option=$type_option="";
			if ($from_date!="") $date_option1=" and date_format(h.send_date,'%Y-%m-%d')>='" . $from_date . "'";
			if ($end_date!="") $date_option2=" and date_format(h.send_date,'%Y-%m-%d')<='" . $end_date . "'";
			if ($customers_id>0) $customer_option=" and h.customers_id='" . (int)$customers_id . "'";

			if ($show_email && !$show_sms) $type_option=" and message_mode='E'";
			else if ($show_sms && !$show_email) $type_option=" and message_mode='S'";
			
			$search_query=tep_db_query("SELECT h.orders_id,h.subscription_id,h.customers_id,h.message_type,h.message_mode,h.email_address,h.send_date,date_format('%Y-%m-%d',h.send_date) as entry_date,o.customers_name,o.customers_email_address from " . TABLE_SUBSCRIPTION_MESSAGES_HISTORY . " h, " .
										TABLE_ORDERS . " o where 1=1 " . $date_option1 . $date_option2 . $customer_option . $type_option ." and h.orders_id=o.orders_id order by entry_date,o.customers_name");

			if (tep_db_num_rows($search_query)>0) $search_found=true;
			$search_content="";
			 if ($search_found) {
				$search_content='<table border="0" cellspacing="0" cellpadding="2" width="100%">' .
									'<tr class="dataTableHeadingRow">'.
										'<td class="dataTableHeadingContent" width="20"></td>'.
										'<td class="dataTableHeadingContent">' . TEXT_CUSTOMER_NAME . '</td>'.
										'<td class="dataTableHeadingContent">' . TEXT_CUSTOMER_EMAIL . '</td>'.
										'<td class="dataTableHeadingContent" width="200">' . TEXT_MESSAGE_ACTION . '</td>'.
										'<td class="dataTableHeadingContent" width="200">' . TEXT_SENT_DATE . '</td>'.
									'</tr>';
					while($search_result=tep_db_fetch_array($search_query)){ 
								$type='TEXT_' . $search_result["message_type"];
				
					$search_content.='<tr class="dataTableRow">'.
										'<td class="dataTableContent">' . (($search_result["message_mode"]=="E")? tep_image(DIR_WS_ICONS . "mail_simple.gif",IMAGE_MESSAGE_MAIL) : tep_image(DIR_WS_ICONS . "sms.gif",IMAGE_MESSAGE_MAIL)) . '</td>' .
										'<td class="dataTableContent">' . $search_result["customers_name"] . '</td>'.
										'<td class="dataTableContent">' . $search_result["customers_email_address"] . '</td>'.
										'<td class="dataTableContent">' . (defined($type)?constant($type):'') . '</td>' .
										'<td class="dataTableContent">' . format_date($search_result["send_date"]) . ' ' . date('h:i:s',strtotime($search_result["send_date"])) . '</td>' .
									'</tr>';
				 } 
					$search_content.= '</table>';
			 } else { 
					$search_content.= '<table cellpadding="2" cellspacing="0"><tr><td class="main">' . TEXT_NO_RESULTS . '</td></tr></table>';
			 }
			 echo $search_content;
			 exit;
		}
	}

	$query = tep_db_query("select customers_id, customers_firstname, customers_lastname,customers_email_address from " . TABLE_CUSTOMERS . " ORDER BY customers_lastname desc");
	$result = $query;

	$selected_name="";	
	if (tep_db_num_rows($result) > 0)
	{
		// Query Successful
		$SelectCustomerBox = "<select name='customers_id' size=10 onClick=\"javascript:setListboxText(0);toggleList(0);\" onFocus=\"javascript:toggleList(1);\" onBlur=\"javascript:toggleList(0);\" class='customerSelect'>\n";
		while($db_Row = tep_db_fetch_array($result)){ 
			$SelectCustomerBox .= "<option value='" . $db_Row["customers_id"] . "'";
			if($db_Row["customers_id"]==$customers_id){
				$SelectCustomerBox .= " SELECTED ";
				$selected_name=$db_Row["customers_lastname"] . ' ' . $db_Row["customers_firstname"] . ", " . $db_Row["customers_email_address"];
			}
				//$SelectCustomerBox .= ">" . $db_Row["customers_lastname"] . " , " . $db_Row["customers_firstname"] . " - " . $db_Row["customers_id"] . "</option>\n"; 
			$SelectCustomerBox .= ">" . $db_Row["customers_lastname"] . ' ' .  $db_Row["customers_firstname"] . ", " . $db_Row["customers_email_address"] . " (" . $db_Row["customers_id"]. ") </option>\n";
		}
		$SelectCustomerBox .= "</select>\n";
	}
?>
<!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/date-picker.js"></script>
<script language="javascript" src="includes/http.js"></script>
<script language="javascript">
	function do_submit() {
		document.getElementById('search_result').innerHTML=document.getElementById('loading').innerHTML;
		command='<?php echo tep_href_link(FILENAME_SUBSCRIPTION_MESSAGES_LOGS) ; ?>?command=search_details';
		do_post_command('search_message',command);
	}
	function do_result(result) {
		token=result.split('^');
		if(token[0]!="") {
			if(token[0]=='search_details') {
				document.getElementById('search_result').innerHTML=token[1];
			}
		}
	}
</script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onLoad="SetFocus();">
<div id="spiffycalendar" class="text"></div>
	<!-- header //-->
	<?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">
		<div id="loading" style="display:none"><div style="height:30px;margin-left:30px"><img src="images/24-1.gif"></div></div>
		<table border="0" width="100%" cellspacing="0" cellpadding="2">
			<tr>
				<td>
				<table border="0" cellspacing="0" cellpadding="2">
					<tr>
						<td class="pageHeading"><?php echo 'Subscription Messages Logs'; ?></td>
					</tr>
		        </table>
				</td>
			</tr>
			<?php echo tep_draw_form("search_message",FILENAME_SUBSCRIPTION_MESSAGES_LOGS,'');
					echo '<input type="hidden" name="form_values" value="1">';
			?>
			<tr>
				<td>
				<table border="0" cellspacing="0" cellpadding="4" >
					<tr>
						<td><table cellpadding="2" cellspacing="0" border="0" class="searchArea">
							<tr>
								<?php $_array=array('d','m','Y');  $replace_array=array('DD','MM','YYYY'); 	$date_format=str_replace($_array,$replace_array,EVENTS_DATE_FORMAT);?>
								<td  width="300px"><?php echo TEXT_FROM_DATE; ?><small>(YYYY-MM-DD)</small>
								<?php echo '&nbsp;&nbsp;' . tep_draw_input_field("from_date",format_date($from_date),"size=10",false,'text',false);?>
								<a href="javascript:show_calendar('search_message.from_date',null,null,'<?php echo $date_format;?>');"
									onmouseover="window.status='Date Picker';return true;"
									onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/>  
								</a>
								</td>
								<td><?php echo TEXT_END_DATE; ?><small>(YYYY-MM-DD)</small>
								<?php echo '&nbsp;&nbsp;' .tep_draw_input_field("end_date",format_date($end_date),"size=10",false,'text',false);?>
								<a href="javascript:show_calendar('search_message.end_date',null,null,'<?php echo $date_format;?>');"
									onmouseover="window.status='Date Picker';return true;"
									onmouseout="window.status='';return true;"><img border="none" src="images/icon_calendar.gif"/>  
								</a>
								</td>
							</tr>
						</table></td>		
					</tr>
					<div id="customer_list" style="position:absolute;display:none;width:400px;"><?php echo $SelectCustomerBox;?></div>
					<tr><td><table cellpadding="2" cellspacing="0" class="searchArea">
						<tr>
							<td><?php echo TEXT_SELECT_CUSTOMER; ?></td>
							<td ><?php echo tep_draw_input_field('customer_name',$selected_name,'onFocus="javascript:toggleText(1);" onBlur="javascript:toggleText(0);" style="width:400px;"');?></td>
						</tr>
					</table></td></tr>
		        </table>
				</td>
			</tr>
			<tr>
				<td><?php echo '<a href="javascript:do_submit();">' . tep_image_button('button_submit.gif',IMAGE_SEARCH) . '</a>' ;?></td>
			</tr>
			</form>
		<script language="JavaScript">
			var iex=(document.all);//ie
			var na6=(window.sidebar);//netscape 6.2
			
			if(na6) document.search_message.customer_name.addEventListener("keyup",selectList,true);
			if(iex) document.search_message.customer_name.onkeyup=selectList;
			if(na6) document.search_message.customer_name.addEventListener("keydown",scrollList,true);
			if(iex) document.search_message.customer_name.onkeydown=scrollList;
		var currentField="";
		var intervalId;
			function checkdropDown(){
				if (currentField==""){
					clearInterval(intervalId);
					hideList();
					intervalId="";
				}
			}
			function toggleText(mode){
				if (mode==1) {
					currentField="Text";
					showList();
					if (intervalId) clearInterval(intervalId);
					intervalId=setInterval("checkdropDown()",300);
				} else {
					currentField="";
				}
			}
			function toggleList(mode){
				if (mode==1){
					currentField="drop";
				} else {
					currentField="";
				}
			}
			function showList(){
				var listbox=document.getElementById("customer_list");
				var textbox=document.search_message.customer_name;
				listbox.style.display="";
				var left = getPageOffsetLeft(textbox);
				var top =getPageOffsetTop(textbox);
				
				if(window.scrollMaxX >0)listbox.style.left=left;
				else listbox.style.left=left;
				listbox.style.top=top+17;
			}
			function hideList(){
				var listbox=document.getElementById("customer_list");
				var textbox=document.search_message.customer_name;
				setListboxText(1);
				listbox.style.display="none";
			}
			function scrollList(e){
				var listbox=document.search_message.customers_id;

				if (iex) keyCode=window.event.keyCode;
				if (na6) keyCode=e.which;

				if (keyCode==40 || keyCode==38){
					if (keyCode==40 && listbox.selectedIndex>=0 && listbox.selectedIndex<listbox.options.length-1){
						listbox.selectedIndex+=1;
					} else if (keyCode==38 && listbox.selectedIndex>0 && listbox.selectedIndex<=listbox.options.length-1){
						listbox.selectedIndex-=1;
					}

					setListboxText(0);
					if (iex) window.event.returnValue=0;
					if (na6) e.returnValue=0;
					return;

				}
			}
			function selectList(e){
				var listbox=document.search_message.customers_id;
				var search_word=document.search_message.customer_name.value;
				var text_length;
				var found=-1;
				var listbox_word;
				var eventObject;
				var keyCode=0;
				var fromPos=0;
				var toPos=0;
	
				if (!listbox.options || listbox.options.length<=0) return;

				if (search_word!="") {
					search_word=search_word.toLowerCase();
					found=binaryFind(listbox,search_word,0,listbox.options.length-1);
					if (found>=0)
						listbox.selectedIndex=found;
				}

			}
			function setListboxText(mode){
				var listbox=document.search_message.customers_id;
				if (!listbox.options || listbox.options.length<=0 || listbox.selectedIndex<=-1) return;
				var textbox_name=document.search_message.customer_name;
				if (textbox_name.value=="" && mode==1) {
					document.search_message.customers_id.selectedIndex=-1;
					return "";
				}
				textbox_name.value=listbox.options[listbox.selectedIndex].innerHTML;
			}
			function binaryFind(listBox,fWord,sPos,ePos){
				var listElement;
				var tWord;
				var mPos;
				if (sPos>ePos) return -1;
				mPos=sPos+Math.ceil((ePos-sPos)/2);
				if (mPos<0 || mPos>=listBox.options.length) return -1;
				listElement=listBox.options[mPos].innerHTML.toLowerCase();

				tWord=listElement.substr(0,fWord.length);

				if (tWord==fWord) return mPos;
				if (mPos<=0 || mPos>=listBox.options.length) return -1;
				
				if (fWord<tWord) return binaryFind(listBox,fWord,mPos+1,ePos);
				else return binaryFind(listBox,fWord,sPos,mPos-1);
			}
			</script>
			<tr>
				<td><?php echo tep_draw_separator('pixel_trans.gif','10','5');?></td>
			</tr>
			<tr>
				<td>
				<table border="0" cellspacing="0" cellpadding="0">
					<tr>
						<td class="pageHeading"><?php echo HEADING_SEARCH_RESULTS; ?></td>
					</tr>
		        </table>
				</td>
			</tr>
			<tr>
				<td><?php echo tep_draw_separator('pixel_trans.gif','10','5');?></td>
			</tr>
			<tr><td><div id="search_result"></div></td></tr>
    	</table>
		</td>
<!-- body_text_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'); ?>
