<?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_CLASSES . 'currencies.php');

  //added new
  

//Added for Categories Description 1.5  
  function get_err_type($type){
     if ($type=='M')
        return TEXT_MOVE_ERR_EMPTY;
     else if($type=='W')
        return TEXT_MOVE_ERR_WAITLIST;
     else if($type=='A')
        return TEXT_MOVE_ERR_ALREADY;
  }
  function get_selected_date($index,$date){
  	global $new_session,$find_array;
	$select_id=0;
	if (isset($new_session[$index])){
		$select_id=$new_session[$index];
	} else if (isset($find_array[$date])) {
		$select_id=$find_array[$date];
	}
	return $select_id;
  }
  $currencies = new currencies();
     $err_array=array();
     $reserve_ids=(isset($HTTP_POST_VARS['reservation_id'])?$HTTP_POST_VARS['reservation_id']:'');
	 $success_ids=(isset($HTTP_POST_VARS['moved_ids'])?$HTTP_POST_VARS['moved_ids']:array());
	 $failure_ids=(isset($HTTP_POST_VARS['unmoved_ids'])?$HTTP_POST_VARS['unmoved_ids']:array());
	 $new_session=(isset($HTTP_POST_VARS['new_session'])?$HTTP_POST_VARS['new_session']:array());
	 $type=(isset($HTTP_POST_VARS['items_type'])?$HTTP_POST_VARS['items_type']:'');

	if (sizeof($reserve_ids)<=0 || sizeof($type)<=0) tep_redirect(tep_href_link(FILENAME_SEARCH_RESERVATIONS));
	
	$select_string_order="";
	$select_string_single="";
	for ($icnt=0;$icnt<sizeof($reserve_ids);$icnt++){
		$reserve_id=$reserve_ids[$icnt];
		if (isset($type[$reserve_id]) && $type[$reserve_id]=="G"){
			$split_array=split("_",$reserve_id);
			if (sizeof($split_array)>1) $select_string_order.=" (op.orders_id='" . $split_array[0] . "' and s.events_id='" . $split_array[1] . "') or ";
		} else {
			$select_string_single.="'" . $reserve_id . "',";
		}
	}
	$query=$query_part_1=$query_part_2="";
	
	if (strlen($select_string_order)>4){
		$select_string_order=substr($select_string_order,0,-4);
		$query_part_1="select o.orders_id,op.orders_products_id,op.products_id,o.customers_id,o.customers_name,o.customers_email_address,op.final_price,op.events_type,op.products_name,
		     		    s.sessions_id,s.start_date,op.products_tax,s.events_id from " . 
        	            TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s " .
		    			"where o.orders_id=op.orders_id and s.sessions_id=op.products_id and op.products_type='E' and op.events_type in ('R','W') and (" . $select_string_order . ")";

	}
	if (strlen($select_string_single)>2){
		$select_string_single=substr($select_string_single,0,-1);
		$query_part_2="select o.orders_id,op.orders_products_id,op.products_id,o.customers_id,o.customers_name,o.customers_email_address,op.final_price,op.events_type,op.products_name,
		     		    s.sessions_id,s.start_date,op.products_tax,s.events_id from " . 
        	            TABLE_ORDERS . " o, " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s " .
		    			"where o.orders_id=op.orders_id and s.sessions_id=op.products_id and op.products_type='E' and op.events_type in ('R','W') and op.orders_products_id in (" . $select_string_single . ")";
	}

     if ($query_part_1!='' && $query_part_2!='')
        $query=$query_part_1 . ' union ' . $query_part_2;
     else if($query_part_1!='')
        $query=$query_part_1;
     else if($query_part_2!='')
        $query=$query_part_2;

     $query=$query . " order by events_id,start_date,customers_name";
	 
	 
     
	$result_query=tep_db_query($query);
	
	$new_events_id=isset($HTTP_POST_VARS['new_events_id'])?(int)$HTTP_POST_VARS['new_events_id']:0;
	
	$new_event_query=tep_db_query("select e.session_style,e.session_gaps,e.session_reduction,e.events_fees,e.waitfee_vip,e.events_forward_period,e.events_forward_length,ed.events_name from " . TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed where e.events_id=ed.events_id and e.events_id='" . $new_events_id . "' and ed.language_id='" . $languages_id . "'");

	$new_event_result=tep_db_fetch_array($new_event_query);



	if ($new_event_result['events_forward_period']=='Y'){
		$check_date=date('Y-m-d',tep_dateadd(time(),'day',(int)$new_event_result['events_forward_length']));
		$check_data=" and start_date<='" . $check_date . "' ";
	}	 

	$params=array("tx_fname","tx_lname","page","tx_words","tx_code",
							"tx_from","tx_until","tx_event","tx_reserve","tx_waitlist","tx_rtype","tx_session","tx_current","orders_id","events_id","tx_pending");


	$search_post_params=tep_prepare_hidden_params($params,"post","post",array());
	$search_get_params=tep_prepare_hidden_params($params,"post","get",array());
		
	// get new session dates
	$session_query=tep_db_query("select sessions_id,start_date from " . TABLE_EVENTS_SESSIONS . " where events_id='" . $new_events_id . "' and start_date>current_date and sessions_type!='' " . $check_data . " order by start_date");
	
	$sessions_array=array();
	$find_array=array();
	while ($session_details = tep_db_fetch_array($session_query)) {
		$sessions_array[] = array(	'id' => $session_details['sessions_id'],
									'text' => format_date($session_details['start_date']));
		$find_array[$session_details['start_date']]=$session_details['sessions_id'];
		$rows++;
	}
	$move_elements=array();
	$icnt=0;
	while($result=tep_db_fetch_array($result_query)){
		$current_event=$result["events_id"];
		if (!isset($move_elements[$current_event])) {
			$move_elements[$current_event]=array();
			$move_elements[$current_event]['status']=1;
			$move_elements[$current_event]['dates']=array();
			$old_event_query=tep_db_query("select e.session_style,e.session_gaps,e.session_reduction,e.events_fees,e.waitfee_vip from " . TABLE_EVENTS . " e where e.events_id='" . $current_event . "'");
			$old_event_result=tep_db_fetch_array($old_event_query);
			$move_elements[$current_event]['name']=$result["products_name"];
			if ($old_event_result["session_style"]!=$new_event_result["session_style"] || $old_event_result["session_gaps"]!=$new_event_result["session_gaps"] || $old_event_result["events_fees"]!=$new_event_result["events_fees"] || $old_event_result["waitfee_vip"]!=$new_event_result["waitfee_vip"])
				$move_elements[$current_event]['status']=0;
		}
		$current_date=$result["start_date"];
		if (!isset($move_elements[$current_event]['dates'][$current_date])) {
			$move_elements[$current_event]['dates'][$current_date]=array();
		}
		if (!isset($success_ids[$result["orders_products_id"]])){
			
			$move_elements[$current_event]['dates'][$current_date][]=array('id'=>$result["orders_products_id"],'name'=>$result["customers_name"]);
			//echo sizeof($move_elements[$current_event]['dates'][$current_date]);
		}
	}
?>
<!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>
</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">
<tr> 
	<!-- body_text //-->	  
    <td width=100% align=left valign="top">
	<table border="0" width="100%" cellspacing="0" cellpadding="2">	
		<tr>
        	<td>
			<table border="0" width="100%" cellspacing="0" cellpadding="2">
				<tr>
					<td align=left class='pageHeading' colspan='2'><?php echo sprintf(HEADING_EVENT_TITLE,$new_event_result["events_name"]); ?></td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
		</tr>	  
		<?php 
			if (tep_db_num_rows($session_query)>0) {
		  		if (sizeof($failure_ids)>0) {
		?>
					<tr>
						<td>
						<table border="0" width="100%" class="dataTableContent">
							<tr>
								<td><?php echo '<font color=red>' . TEXT_MOVE_ERROR . '</font>'; ?></td>
							</tr>
						</table>
						</td>
					</tr>
		<?php   }
					echo tep_draw_form('move_reservation_confirm',FILENAME_MOVE_RESERVATIONS_PROCESS);
					echo '<input type="hidden" name="new_events_id" value="' . $new_events_id . '">';
					echo $search_post_params;
					for ($icnt=0;$icnt<sizeof($reserve_ids);$icnt++){
						echo '<input type="hidden" name="reservation_id[]" value="' . $reserve_ids[$icnt] . '">';
					}
					if (sizeof($type)>0){
						reset($type);
						while(list($id,$value)=each($type)){
							echo '<input type="hidden" name="items_type[' . $id . ']" value="' . $value . '">';
						}
					}
					if (sizeof($success_ids)>0){
						reset($success_ids);
						while(list($id,)=each($success_ids)){
							echo '<input type="hidden" name="moved_ids[' . $id . ']" value="1">';
						}
					}
		?>
					<tr>
					<td>
						<table border="0" width=100% cellspacing="" cellpadding="2">
							<tr align="left" class="attributes-odd">
								<td>
									<table border="0" cellpadding="2" cellspacing="0" width="100%"> 
										<tr>
											<td class="tableHeading" align="left" width=100><?php echo TEXT_DATE ?></td>
											<td class="tableHeading" align="left"><?php echo TEXT_MEMBERS; ?></td>
											<td class="tableHeading" align="left" width=300><?php echo TEXT_SELECT_DATE; ?>
										</tr>
									</table>
								</td>
							</tr>
							<tr>
								<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5');?></td>
							</tr>
							<tr>
								<td>
								<table border="0" width="100%" cellpadding="2">
								<?php
								$row_count=0;
								$event_cnt=0;
								//iterate using each of the events
								reset($move_elements);
								while(list($events_id,$events)=each($move_elements)){
									if (sizeof($events['dates'])>0){
										echo '<tr class="attributes-even"><td class="tableHeading" valign="top">' . sprintf(TEXT_EVENT_FROM,$events['name']);
										if ($events['status']==0) echo '<br><span class="errorText">' . TEXT_EVENT_MISMATCH . '</span>';
										echo '</td></tr>' . "\n";
										echo '<tr class="attributes-odd"><td><table border="0" width="100%" cellpadding="2">'  . "\n";
										while(list($start_date,$session)=each($events['dates'])){
											if (sizeof($session)>0){
												echo 	'<tr><td class="tableHeading" width=100 valign="top">' . format_date($start_date) . '</td>'  . "\n" . 
														'<td valign="top"><table border="0" width="100%" cellpadding="0">'  . "\n";
												
												for ($icnt=0;$icnt<sizeof($session);$icnt++){
													echo 	'<tr><td class="main" valign="top">' . $session[$icnt]['name']  . "\n";
													if ($events['status']==1){
														echo	'<input type="hidden" name="move_elements[]" value="' . $session[$icnt]['id'] . '">'  . "\n" . 
																'<input type="hidden" name="new_session_index[]" value="' . $event_cnt . '">' . "\n" . 
																'</td></tr>' . "\n";
														if (isset($failure_ids[$session[$icnt]['id']])){
															echo '<tr><td class="smallText">' . get_err_type($failure_ids[$session[$icnt]['id']]) . "</td></tr>";
														}
													}
												}
												echo '</table></td>' . "\n";
												echo '<td valign="middle" width=300>';
												echo '<table border="0" width="100%" cellpadding="2">' . "\n";
												if ($events['status']==1){
													echo '<tr><td class="main" align="left">' . tep_draw_pull_down_menu('new_session_id[' . $event_cnt . ']',$sessions_array,get_selected_date($event_cnt,$start_date));
													echo '</td></tr>' . "\n";
													$event_cnt++;
												}
												echo '</table>';
												echo '</td></tr>' . "\n";
												
											}
										}
										echo '</table></td></tr>' . "\n";
									}
								}
								echo '<tr><td>' . tep_draw_separator('pixel_trans.gif', '1', '5') . '</td></tr>' . "\n";
								echo '<tr><td class="main">' . tep_draw_checkbox_field('process_wait','Y') . TEXT_CHECK_WAITLIST . '</td></tr>' . "\n";
								echo '</table></td></tr></table></td></tr>' . "\n";
								echo '<tr><td>' . tep_draw_separator('pixel_trans.gif', '1', '5') . '</td></tr>' . "\n";
								echo '<tr><td align=left class="main">';
								echo '<a href="' . tep_href_link(FILENAME_SEARCH_RESERVATIONS,$search_get_params . 'action=search') . '">' . tep_image_button('button_back.gif',IMAGE_BACK,'align=absmiddle') . '</a>&nbsp;&nbsp;' . "\n";
								if ($event_cnt>0) echo tep_image_submit('button_move.gif',IMAGE_BUTTON_MOVE,'align=absmiddle') . "\n";
								?>
          </table></td>
	  </form>
  	 </tr>
	 <?php } else { ?>
		<tr>
			<td class=main><?php echo '<font color=red>' . TEXT_NO_SESSIONS . '</font><p>'; ?></td>
		</tr>
		<tr>
			<td class="main"><?php echo '<a href="' . tep_href_link(FILENAME_SEARCH_RESERVATIONS,$search_get_params . 'action=search') . '">' . tep_image_button('button_back.gif',IMAGE_BACK) . '</a>'; ?>
			</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'); ?>
