<?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 );
	Header("Cache-control: private, no-cache");  
	Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); # Past date
	Header("Pragma: no-cache");
	require('includes/application_top.php');
	tep_get_last_access_file();
	require('includes/functions/events_tree.php');
  $events_id=(isset($HTTP_POST_VARS['select_id'])?(int)$HTTP_POST_VARS['select_id']:0);
  if ($events_id==0){
	  $events_id=(isset($HTTP_GET_VARS['events_id'])?(int)$HTTP_GET_VARS['events_id']:0);
  }
  $hide_pending=(isset($HTTP_POST_VARS['hide_pending'])?'Y':'');
?>
<!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/event_select.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">

<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2" id="holetable">
  <tr> 
    <!-- body_text //-->
    <td width="100%" valign="top" id="formtable">
      <table border="0" width="100%" cellspacing="0" cellpadding="0">
	<tr height="5">
	  <!-- <td class="pageHeading"><?php //echo HEADING_TITLE; ?></td>!-->
	</tr>
	<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
	<tr >
		<td><?php echo tep_draw_form('select_event',FILENAME_EVENTS_REPORTS_WAITLIST); 
					$selected_name="";
		?>
		<table border="0" cellspacing="0" cellpadding="2">
			<div id="event_list" style="position:absolute;display:none;width:400px">
				<?php echo tep_draw_event_select_menu('select_id',$events_array,$events_id,'size=10 style="width:400px" onClick="javascript:setListboxText(0);toggleList(0);" onFocus="javascript:toggleList(1);" onBlur="javascript:toggleList(0);" class="customerSelect"','','',false);?>
			</div>
			<tr height=20>
				<td class="main" width="100"><label><?php echo TEXT_SELECT_EVENT;?></label></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>
				<td class="main"><?php echo tep_draw_checkbox_field('hide_pending','Y',($hide_pending=='Y'?true:false)) . "<label>" . HIDE_PENDING_PAYMENTS . "</label>&nbsp;&nbsp;";?></td>
				<td class="main" valign=center><?php echo tep_image_submit('button_select.gif',IMAGE_SELECT,'align=absmiddle size=');?></td>
			</tr>
			
		</table>
		</form>
		<script language="JavaScript">
			if(na6) document.select_event.select_text.addEventListener("keyup",selectList,true);
			if(iex) document.select_event.select_text.onkeyup=selectList;
			if(na6) document.select_event.select_text.addEventListener("keydown",scrollList,true);
			if(iex) document.select_event.select_text.onkeydown=scrollList;
		</script>
		</td>
	</tr>
	<tr ><td class="cell_bg_report_header">&nbsp;</td></tr>
	    <?
			$display_chart=false;
			if ($events_id>0){
				$display_count=array();
				$events_details_query=tep_db_query("SELECT events_pending_period,events_max_reserve,waitsize_vip,waitsize_firstcome,waitsize_custom from " . TABLE_EVENTS . " where events_id='" . $events_id . "'");
				if (tep_db_num_rows($events_details_query)<=0) $display_chart=false;
				$result=tep_db_fetch_array($events_details_query);
				$max_reserve=(int)$result['events_max_reserve'];
				$pending_period=(int)$result['events_pending_period'];
				
				//$cDate="curdate()";
		    fetch_reserved_total();    // get reserved users count
		    fetch_waitlist_total('V'); // get vip count users count
		    fetch_waitlist_total('F'); // get firstcome count users count
		    fetch_waitlist_total('C'); // get custom users count
		    ksort($display_count);
		    if (sizeof($display_count>0)) $display_chart=true;
		    $icnt=1;
                    
		    $dmr_0='[sp0]'; // delimiter 0
		    $dmr_1='[sp1]'; // delimiter 1
		    $dmr_2='[sp2]'; // delimiter 2
		    $input_text="";
			$text="";
		    while(list($key,$value)=each($display_count)){
		        if ($text!="") $text.=$dmr_2;
			//$text.=date("M.d",strtotime($key)) . $dmr_1;
			$text.=$display_count[$key]['display'] . $dmr_1;
			unset($display_count[$key]['display']);
		        $text.=implode($dmr_1,$display_count[$key]);
			$icnt++;
			//if ($icnt>12) break;
		    }
		    if ($text!=""){
		        $cmd_array[]=1;  //display background
			$cmd_array[]=1;  //display yscale units

			//total scale units
			$cmd_array[]=$max_reserve+(int)$result['waitsize_vip']+(int)$result['waitsize_firstcome']+(int)$result['waitsize_custom'];
			//total_reservations
			$cmd_array[]=$max_reserve;
			$cmd_array[]=$events_id;
			$cmd_array[]=HTTP_SERVER . DIR_WS_ADMIN;
			$cmd_array[]=$hide_pending;
		    }
		    if ($text!='' && $display_chart==true){
		        $cmd_array[]=$text;
			$flash_input_text="show_graph[sp]" . implode($dmr_0,$cmd_array);
		    }
		}
	   ?>

	<tr><td>
	   <table border="0" cellspacing="0" cellpadding="0" width=100%>
	     <tr>
	        <td align=center class="main">
	        <b><?php echo tep_get_events_name($events_id); ?></b>
		</td>
	     </tr>
	   <?php if ($flash_input_text!=""){  ?>
	     
	     <tr>
		<td align="left" valign="top">
			<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"  codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" id="flashLayer" WIDTH="800" HEIGHT="700" VALIGN="TOP" style="z-index:10;">
				<PARAM NAME=movie VALUE="graph/wait_graph.swf"> 
				<PARAM NAME=quality VALUE=high> 
				<PARAM NAME=bgcolor VALUE=#C6C5CA> 
				<PARAM NAME=wmode VALUE="transparent">
				<script Language="JavaScript">
					document.write('<EMBED src="graph/wait_graph.swf" quality=high bgcolor=#C6C5CA  wmode="transparent" flashvars="__yamzbrowser='+window.navigator.appName+'" WIDTH="800" HEIGHT="700" VALIGN="TOP" id="flashLayer" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>');
				</script>
			</OBJECT>
		</td>
	    </tr>
	    <script language=javascript>document.flashLayer.scrollIntoView();</script>
		<script Language="JavaScript">
			var Mmovie;
			var Minterval;
			Mmovie = document.getElementById("flashLayer");
			function check_flash(){
				if (Mmovie.PercentLoaded()==100){
					clearInterval(Minterval);
					Mmovie.SetVariable( "VBCalling", '<?php echo $flash_input_text ?>');
				}
			}			
			Minterval=setInterval("check_flash()",500);
		</script>
	  <?php } else if ($events_id>0) { ?>
	  	<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '15');?></td></tr>
	  	<tr>
		   <td class=main><?php echo TEXT_NO_RECORDS_FOUND; ?></td>
		</tr>
	  <?php } ?>
	</table>
	</td>
	</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'); ?>
<?php 

  function fetch_reserved_total(){
    global $display_count,$events_id,$pending_period,$hide_pending;
	$pending_option="";

	if ($pending_period>0 && $hide_pending=='Y'){
		//$pending_option=" and (o.orders_status>1 or date_add(o.date_purchased,interval '" . $pending_period . "' day)>='" . getServerDate() . "')";
		$pending_option=" and ((op.orders_products_status>1  and op.orders_products_status<5) or (op.orders_products_status=1 and (date_add(o.date_purchased,interval '" . $pending_period . "' day)>='" . getServerDate() . "')))";
	}

    $sql="SELECT count(s.start_date) as total_count,s.start_date from ". TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " . TABLE_EVENTS_SESSIONS . " s where o.orders_id=op.orders_id and op.products_type='E' and op.events_type='R' and op.products_id=s.sessions_id and s.events_id='". $events_id . "'" . $pending_option . " and s.start_date>='".getServerDate()."' and s.sessions_type!='' group by s.start_date";
	
    $query=tep_db_query($sql);
    $icnt=1;
    while($result=tep_db_fetch_array($query)){
	//$result['start_date']=format_date($result['start_date']);
      if (!isset($display_count[$result['start_date']]))
       $display_count[$result['start_date']]=array('R'=>0,'V'=>0,'F'=>0,'C'=>0,'display'=>format_date($result['start_date']));
       
       $display_count[$result['start_date']]['R']=(int)$result['total_count'];
       $icnt++;
       //if ($icnt>12) break;
    }
  }
  function fetch_waitlist_total($type){
    global $display_count,$events_id,$pending_period,$hide_pending;
	$pending_option="";
	if ($pending_period>0 && $hide_pending=='Y'){
		//$pending_option=" and (o.orders_status>1 or date_add(o.date_purchased,interval '" . $pending_period . "' day)>='" . getServerDate() . "')";
		$pending_option=" and ((op.orders_products_status>1 and op.orders_products_status<5) or (op.orders_products_status=1 and (date_add(o.date_purchased,interval '" . $pending_period . "' day)>='" . getServerDate() . "')))";
	}
    $sql="SELECT count(s.start_date) as total_count,s.start_date from ". TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " . TABLE_EVENTS_SESSIONS . " s where o.orders_id=op.orders_id and op.products_type='E' and op.events_type='W' and op.waitlist_type='" . $type . "' and op.products_id=s.sessions_id and s.events_id='". $events_id . "'" . $pending_option . " and s.start_date>='".getServerDate() ."' and s.sessions_type!='' group by s.start_date";
	
    $query=tep_db_query($sql);
    $icnt=1;
    while($result=tep_db_fetch_array($query)){
	//$result['start_date']=format_date($result['start_date']);
      if (!isset($display_count[$result['start_date']]))
       $display_count[$result['start_date']]=array('R'=>0,'V'=>0,'F'=>0,'C'=>0,'display'=>format_date($result['start_date']));
       
       $display_count[$result['start_date']][$type]=(int)$result['total_count'];
       $icnt++;
       //if ($icnt>12) break;
    }
  }
?>