<?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('includes/classes/split_page_results_event.php');
  require('includes/functions/events_tree.php');
  require(DIR_WS_CLASSES . 'currencies.php');
  //added new

//Added for Categories Description 1.5  
  //require('includes/functions/sessions_description.php');

  $currencies = new currencies();
  
  $command=isset($HTTP_GET_VARS['command'])?$HTTP_GET_VARS['command']:'';
  if($command!=''){
  	switch($command){
		case 'search_events_waitlist':
			global $events_page;
			echo 'search_events_waitlist^^';
			echo events_search_waitlist();
			break;
		case 'events_waitlist_page_navigation':
			global $events_page;
			$events_page=isset($HTTP_GET_VARS['events_page'])?$HTTP_GET_VARS['events_page']:'';
			$events_id=isset($HTTP_GET_VARS['events_id'])?$HTTP_GET_VARS['events_id']:'';
			echo 'search_events_waitlist^^';
			echo events_search_waitlist($events_id);
			break;
		case 'set_flag':
			$events_id=isset($HTTP_GET_VARS['eID'])?$HTTP_GET_VARS['eID']:'';
			$error=set_flag();
			echo 'set_flag^^';
			echo events_search_waitlist($events_id);
			echo '^^'.$error;
			break;
		case 'event_move':
			echo event_move();
			$events_id=isset($HTTP_POST_VARS['events_id'])?$HTTP_POST_VARS['events_id']:'';
			echo 'search_events_waitlist^^';
			echo events_search_waitlist($events_id);
			break;
	}
  exit;
  }
  
  function event_move(){
  global $HTTP_POST_VARS;
  	 $events_id=$HTTP_POST_VARS['events_id'];
     $reservation_id=$HTTP_POST_VARS['reservation_id_value'];
     	/*if (is_array($reservation_id)==false)
        	tep_redirect(tep_href_link(FILENAME_WAITLISTS,'events_id=' . $events_id));*/
     $new_waitlist=$HTTP_POST_VARS['new_waitlist'];
	 // select event waitlist details
     $event_query=tep_db_query("SELECT waitsize_vip as V,waitsize_firstcome as F,waitsize_custom as C from " . TABLE_EVENTS . " where events_id='" . $events_id . "'");
     $event_result=tep_db_fetch_array($event_query);
     $size=(int)$event_result[$new_waitlist];
	 // iterate with the sessions
     for($icnt=0;$icnt<sizeof($reservation_id);$icnt++)
     {
       $split_array=explode("_",$reservation_id[$icnt]);
       $orders_products_id=(int)$split_array[0];
       $products_id=(int)$split_array[1];
	   // find the total count of users of particular session
       $total_count=tep_get_count(TABLE_ORDERS_PRODUCTS,"products_id='" . $products_id . "' and waitlist_type='" . $new_waitlist . "'");
       if ($total_count<$size) 
          tep_db_query("UPDATE " . TABLE_ORDERS_PRODUCTS . " set waitlist_type='" . $new_waitlist . "' where orders_products_id='" . $orders_products_id . "'");
     }  

  }
  
  function set_flag(){
  global $HTTP_GET_VARS,$events_id;
     $flag=$HTTP_GET_VARS['flag'];
     $products_id=(int)$HTTP_GET_VARS['pID'];
     $orders_products_id=(int)$HTTP_GET_VARS['opID'];
     $error='';
	 // find the waitlist group
	// echo $flag;
	// echo $events_id;
     if ($flag=='W'){
        $group=tep_find_waitlist_group($events_id,$products_id);
	if ($group!='E')
           tep_db_query("update " . TABLE_ORDERS_PRODUCTS . " set events_type='W', waitlist_type='" . $group . "' where orders_products_id='" . $orders_products_id . "'");
        else $error=ERR_EMPTY_WAIT_SIZE;
     } else {
	 	// check for maximum reservations
        if (tep_check_max_reservations($events_id,$products_id,$orders_products_id)==true){
           tep_db_query("update " . TABLE_ORDERS_PRODUCTS . " set events_type='R', waitlist_type='P' where orders_products_id='" . $orders_products_id . "'");
	   tep_send_event_email($orders_products_id,'WPR');
        }
	else $error=ERR_EMPTY_RESERVE;
     }
    if ($error==''){
    tep_process_waiting_list($events_id,$products_id);
	tep_promote_waiting_list($events_id,$products_id);
     }
 return $error; 
 }
  
  function events_search_waitlist($events_id=0,$call='ajax_call'){
  global $HTTP_POST_VARS,$events_page,$HTTP_GET_VARS,$languages_id;
  //$events_id=(isset($HTTP_GET_VARS['events_id'])?(int)$HTTP_GET_VARS['events_id']:0);
  

  $select_id=(isset($HTTP_POST_VARS['select_id'])?(int)$HTTP_POST_VARS['select_id']:0);
  
  $events_array=tep_get_events_array_single();

  $event_found=true;

	if (sizeof($events_array)<=0){
		$event_found=false;
	} else {
		if ($select_id!=0 && $select_id!=$events_id)
			$events_id=$select_id;
		else if ($select_id==0 && $events_id==0)
			$events_id=$events_array[0]['id'];
	}

	
  	$rows=0;
     $search_query="select o.orders_id,op.products_id,op.orders_products_id,op.events_type,op.waitlist_type,o.question_value_text,o.question_value_date,o.question_value_number,c.customers_firstname,c.customers_lastname,o.customers_postcode,ed.events_name,e.events_location,e.events_instructor,e.priority_question,s.start_date from " . 
                     TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " . TABLE_CUSTOMERS . " c, " .  TABLE_EVENTS_SESSIONS . " s, " . TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed " . 
		     "where op.orders_id=o.orders_id and c.customers_id=o.customers_id and op.products_id=s.sessions_id and s.events_id=e.events_id and ed.events_id=e.events_id " .
		     "and op.products_type='E' and op.events_type='W' and e.events_id='" .$events_id . "' and ed.language_id='" . $languages_id . "' order by op.waitlist_type";
//echos($search_query);
     //$reservations_split = new splitPageResults($HTTP_GET_VARS['page'], MAX_DISPLAY_SEARCH_RESULTS, $search_query, $search_query_numrows);
	 $reservations_split = new splitPageResultsEvent($events_page, MAX_DISPLAY_SEARCH_RESULTS, $search_query, $search_query_numrows);	    
     $result_query=tep_db_query($search_query);
     $cnt=tep_db_num_rows($result_query);
     $previous_group='';
     $waitlist_array[]=array('id'=>'V','text'=>TEXT_VIP);
     $waitlist_array[]=array('id'=>'F','text'=>TEXT_FIRSTCOME);
     $waitlist_array[]=array('id'=>'C','text'=>TEXT_CUSTOM);
	 echo tep_draw_form('waitlists',FILENAME_WAITLISTS,'action=move&events_id=' . $events_id,'post','onSubmit="return validateForm()"');
	 echo tep_draw_hidden_field('events_id',$events_id);
	 echo '<table width="100%" cellpadding="0" cellspacing="0" border="0">';
	 echo '<tr><td>'.tep_draw_separator('pixel_trans.gif','1','20').'</td></tr>'.
      	  '<tr><td><table border="0" width="100%" cellspacing="0" cellpadding="0">'.
           '<tr><td align="left" class="pageHeading" colspan="8">'.sprintf(HEADING_WAITLIST_TITLE,tep_get_events_name($events_id)).'</td></tr>'.
	   		'</table></td></tr>';
	 if($cnt>0){
	?> 
		 
        <tr><td valign="top"><table border="0" width='100%'cellspacing="0" cellpadding="0">
	   <tr class='dataTableHeadingRow'>
               <td class='dataTableHeadingContent'><?php echo TEXT_RESULT_SELECT; ?></td>
	       <td class='dataTableHeadingContent'><?php echo TEXT_RESULT_FIRSTNAME; ?></td>
	       <td class='dataTableHeadingContent'><?php echo TEXT_RESULT_LASTNAME; ?></td>
	       <td class='dataTableHeadingContent'><?php echo TEXT_RESULT_POQ; ?></td>
	       <td class='dataTableHeadingContent'><?php echo TEXT_RESULT_POSTCODE; ?></td>
	       <td class='dataTableHeadingContent'><?php echo TEXT_RESULT_DATE; ?></td>
	       <td class='dataTableHeadingContent' align='center'><?php echo TEXT_RESULT_ACTION; ?></td>
	   </tr>
   
           <?php 

	      $rows=0;$currow=0;
              while($result=tep_db_fetch_array($result_query)) { 
		    $rows++;
		    $currow++;
		    if (strlen($rows) < 2) {
		      $rows = '0' . $rows;
	      $question_type=tep_get_popquestion_type($result['priority_question']);
	      if ($question_type=='T')
	         $question_answer=tep_not_null($result['question_value_text'])?$result['question_value_text']:'';
	      else if ($question_type=='D')
	         $question_answer=($result['question_value_date']!='0000-00-00')?$result['question_value_text']:'';
	      else if ($question_type=='N')
	      	 $question_answer=((int)$result['question_value_number']>0?$result['question_value_number']:'');
		  } 
	       if ($previous_group!=$result['waitlist_type']){
	       ?>
    	       <tr height="20"><td class='main' style="color:#0000FF;" colspan="7">
	       <?php 
                  if ($result['waitlist_type']=='V')
	               echo TEXT_VIP_GROUP;
		     else if ($result['waitlist_type']=='F')
		       echo TEXT_FIRSTCOME_GROUP;
		     else if ($result['waitlist_type']=='C')
		       echo TEXT_CUSTOM_GROUP; 
                  $previous_group=$result['waitlist_type'];
                 }
               ?></td><tr>
    	       <tr class='dataTableRow' height=20>
	       <td class='dataTableContent'><?php echo tep_draw_checkbox_field('reservation_id_value[]',$result['orders_products_id'] . '_' . $result['products_id'],false,'','id=reservation_id_value[]'); ?></td>
	       <td class='dataTableContent'><?php echo $result['customers_firstname']; ?></td>
	       <td class='dataTableContent'><?php echo $result['customers_lastname']; ?></td>
	       <td class='dataTableContent'><?php echo $question_answer; ?></td>
	       <td class='dataTableContent'><?php echo $result['customers_postcode']; ?></td>
	       <td class='dataTableContent'><?php echo format_date($result['start_date']); ?></td>
	       <td class='dataTableContent' align='center'>
	       <?php 
		   if ($result['events_type']=='W'){
              echo tep_image(DIR_WS_IMAGES . 'icon_status_red.gif', IMAGE_ICON_STATUS_WAIT_LIST, 10, 10) . '&nbsp;&nbsp;<a href="javascript: setFlag(\'R\','.$events_id.','.$result['orders_products_id'].','.$result['products_id'].');">' . tep_image(DIR_WS_IMAGES . 'icon_status_green_light.gif', IMAGE_ICON_MOVE_RESERVATION, 10, 10) . '</a>';
           } else {
              echo '<a href="javascript: setFlag(\'W\','.$events_id.','.$result['orders_products_id'].','.$result['products_id'].');">' . tep_image(DIR_WS_IMAGES . 'icon_status_red_light.gif', IMAGE_ICON_MOVE_WAIT_LIST, 10, 10) . '</a>&nbsp;&nbsp;' . tep_image(DIR_WS_IMAGES . 'icon_status_green.gif', IMAGE_ICON_STATUS_RESERVED, 10, 10);
	       }
			?>
	       </td></tr>
       	       <?php } ?>
	       </table></td></tr>
          <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>	  

          <tr><td class="main">
	      <?php echo TEXT_SELECT_WAITLIST . '&nbsp;' . tep_draw_pull_down_menu('new_waitlist',$waitlist_array). '&nbsp;&nbsp;'.
		  '<a href="javascript: validateForm();">'.tep_image_button('button_move.gif',IMAGE_MOVE,'align=absmiddle ').'</a>&nbsp;<span id="move_ajax_load"></span>'; 
		  ?>
          </td></tr>

          <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
		  <tr><td><table border="0" width="100%" cellspacing="0" cellpadding="2">
	      <tr>
		<td class="smallText" valign="top">
		<?php echo $reservations_split->display_count($search_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $events_page, TEXT_DISPLAY_NUMBER_OF_WAITLISTS); ?>
		</td>
		<td class="smallText" align="right">
		<?php echo $reservations_split->display_script_links($search_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $events_page,1,'events_page_navigation(\'\'','1');
		?>&nbsp;</td>
	      </tr>
	    </table></td>
	  </tr>
	  <tr>
	  <td align="right">&nbsp;</td>
	  </tr>
      <input type="hidden" name="row_check" value="<?php echo $rows ?>">

	   <?php } else { ?>
	   <tr><td align="left" style="padding:10px;"  class='main' colspan="8"><?php echo TEXT_NO_RESULTS; ?></td></tr>
       <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
	   <?php } ?>
	   </table>
	  </form>
	  <?php
	  if($call=='ajax_call')
	 echo '^^'.$events_id;
  	} // function ends
  
  tep_get_last_access_file();
  
  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : ''); 

  $events_id=(isset($HTTP_GET_VARS['events_id'])?(int)$HTTP_GET_VARS['events_id']:0);
  $select_id=(isset($HTTP_POST_VARS['select_id'])?(int)$HTTP_POST_VARS['select_id']:0);

  $events_array=tep_get_events_array_single();

  $event_found=true;
	if (sizeof($events_array)<=0){
		$event_found=false;
	} else {
		if ($select_id!=0 && $select_id!=$events_id)
			$events_id=$select_id;
		else if ($select_id==0 && $events_id==0)
			$events_id=$events_array[0]['id'];
	}
  // move to waitlist user


  if (isset($HTTP_GET_VARS['page']) && ($HTTP_GET_VARS['page'] > 1)) $rows = $HTTP_GET_VARS['page'] * MAX_DISPLAY_SEARCH_RESULTS - MAX_DISPLAY_SEARCH_RESULTS;
     $rows=0;
     $search_query="select o.orders_id,op.products_id,op.orders_products_id,op.events_type,op.waitlist_type,o.question_value_text,o.question_value_date,o.question_value_number,c.customers_firstname,c.customers_lastname,o.customers_postcode,ed.events_name,e.events_location,e.events_instructor,e.priority_question,s.start_date from " . 
                     TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o, " . TABLE_CUSTOMERS . " c, " .  TABLE_EVENTS_SESSIONS . " s, " . TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed " . 
		     "where op.orders_id=o.orders_id and c.customers_id=o.customers_id and op.products_id=s.sessions_id and s.events_id=e.events_id and ed.events_id=e.events_id " .
		     "and op.products_type='E' and op.events_type='W' and e.events_id='" .$events_id . "' and ed.language_id='" . $languages_id . "' order by op.waitlist_type";

     $reservations_split = new splitPageResults($HTTP_GET_VARS['page'], MAX_DISPLAY_SEARCH_RESULTS, $search_query, $search_query_numrows);
     $result_query=tep_db_query($search_query);
     $cnt=tep_db_num_rows($result_query);
     $previous_group='';
     
     $waitlist_array[]=array('id'=>'V','text'=>TEXT_VIP);
     $waitlist_array[]=array('id'=>'F','text'=>TEXT_FIRSTCOME);
     $waitlist_array[]=array('id'=>'C','text'=>TEXT_CUSTOM);
?>
<!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>
<script language="javascript" src="includes/http.js"></script>
	<script language=javascript>
    function validateForm()
    {
      var found=false;
      pos=0;
    //  if (document.all.row_check.value>1){alert('dfdsfsd');
/*	if(document.getElementById('reservation_id_value[]').length>0) {
		for(i=0,len=document.getElementById('reservation_id_value[]').length;i<len;i++)
			if(document.getElementById('reservation_id_value[]')[i].checked)
				found=true;
     // }
	 }  else {
	 			if(document.getElementById('reservation_id_value[]').checked)
				found=true;
	}
*/      
	if(document.waitlists.elements['reservation_id_value[]'].length>0) {
		for(i=0,len=document.waitlists.elements['reservation_id_value[]'].length;i<len;i++)
			if(document.waitlists.elements['reservation_id_value[]'][i].checked)
				found=true;
     // }
	 }  else {
	 			if(document.waitlists.elements['reservation_id_value[]'].checked)
				found=true;
	}
      

	  if (found==false)
         alert("<?php echo tep_db_input(ERR_SELECT_RESERVATION); ?>");
	  else{
	  document.getElementById('move_ajax_load').innerHTML=ajax_image_load;
	  do_post_command('waitlists','<?php echo tep_href_link(FILENAME_WAITLISTS,'command=event_move'); ?>');
	  }

    }
	/*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>

	<script language="javascript">
	var ajax_image_load='<?php echo tep_image(DIR_WS_IMAGES.'24-1.gif'); ?>',events_id='';
	function search_events_waitlist(){
	do_post_command('select_event','<?php echo tep_href_link(FILENAME_WAITLISTS,'command=search_events_waitlist'); ?>');
	document.getElementById('select_event_ajax_load').innerHTML=ajax_image_load;
	}
	function do_result(result){
	var token=result.split('^^');
		switch(token[0]){
			case 'search_events_waitlist':
				document.getElementById('ajax_load').innerHTML=token[1];
				events_id=token[2];
				document.getElementById('select_event_ajax_load').innerHTML='';
				break;
			case 'set_flag':
				document.getElementById('ajax_load').innerHTML=token[1];
				error=token[3];
					if(error && error!=''){
					document.getElementById('error_tag').style.display='';
					document.getElementById('error_message').innerHTML=error;
					setInterval('error_function()',3000);
					clearInterval();
					}
				break;
		}
	}
	function error_function(){
	document.getElementById('error_tag').style.display='none';
	}
	function events_page_navigation(arg1,agr2,agr3,agr4){
		var page_no=1;
		if(agr2==1) var obj=document.getElementById("page_1");
		else if(agr2==2) var obj=document.getElementById("page_2");
		if(agr2 && agr4){
			if(obj){
 	 			page_no=parseInt(obj.options[obj.selectedIndex].value);
 	 			if(agr4=='next') {
 	 				if(obj.length==obj.selectedIndex+1) return;
 	 				page_no+=1;
 	 			}else if(agr4=='prev') {
	 	 			if(obj.selectedIndex==0) return;
 		 			page_no-=1;
 	 			}
 	 			if(page_no<=1) page_no=1;
 	 		} 	 		
		}		
		if(agr2==1) command="<?php echo tep_href_link(FILENAME_WAITLISTS,'command=events_waitlist_page_navigation&events_page='); ?>"+page_no+'&events_id='+events_id;
		else if(agr2==2) command="<?php echo tep_href_link(FILENAME_WAITLISTS,'command=events_waitlist_page_navigation&events_page='); ?>"+page_no+'&events_id='+events_id;

		if(command!="") do_get_command(command);
		document.getElementById('ajax_load').innerHTML=ajax_image_load;

	}
	function setFlag(flag,eID,opID,pID){
	document.getElementById('ajax_load').innerHTML=ajax_image_load;
	do_get_command('<?php echo tep_href_link(FILENAME_WAITLISTS,'command=set_flag&flag='); ?>'+flag+'&eID='+eID+'&opID='+opID+'&pID='+pID);
	}
	</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% align=left valign="top" id="formtable"><table border="0" width="100%" cellspacing="0" cellpadding="0">
	  <?php if ($event_found==true) { ?>
	  
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '5', '10'); ?></td></tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '5', '10'); ?></td></tr>
			
			<tr>
				<td><?php echo tep_draw_form('select_event',FILENAME_WAITLISTS); 
							$events_array=tep_get_events_display_tree();
							$selected_name="";
				?>
				<div id="event_list" style="position:absolute;left:242px;top:130px;display:none;width:400px;">
				<?php echo tep_draw_event_select_menu('select_id',$events_array,$events_id,'size=10 onClick="javascript:setListboxText(0);toggleList(0);search_events_waitlist();" onFocus="javascript:toggleList(1);" onBlur="javascript:toggleList(0);" class="customerSelect"');?>
				</div>
				<table border="0" cellspacing="0" cellpadding="2">
					<tr>
						<td class="main" width="100"><?php echo TEXT_SELECT_EVENT;?></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;"');?>&nbsp;<span id="select_event_ajax_load"></span></td>
						<td class="main" valign=center><?php //echo tep_image_submit('button_select.gif',IMAGE_SELECT,'align=absmiddle ');
						//echo '<a href="javascript: search_events_waitlist();">'.tep_image_button('button_select.gif',IMAGE_SELECT,'align=absmiddle ').'</a>';
						?></td>
					</tr>
				</table>
				</form>
				
				</td>
			</tr>

      <tr style="display:none;padding:10px;" id="error_tag"><td><table width="100%" border="0" cellpacing="0" cellpadding="2" class="formArea">
      <tr><td class="main" id="error_message" style="color:#FF0000;"></td></tr>
	  </table></td>
	  </tr>
      <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
	  <tr><td id="ajax_load"><?php echo events_search_waitlist($events_id,''); ?></td></tr>
<?php } else { ?>
   <tr><td class="pageHeading"><?php echo TEXT_HEADING_EMPTY_EVENTS ;?></td></tr>
<?php } ?>
      </table>
      </td>
</tr>
</table>
<!-- body_text_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>

