<?php
/*
	osCommerce, Open Source E-Commerce Solutions 
	http://www.oscommerce.com 
	
	Copyright (c) 2003 osCommerce 
	
	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');

 
	if (!tep_session_is_registered('customer_id')) {
		$navigation->set_snapshot();
		tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
	}
 
	if (isset($HTTP_POST_VARS['events_id']) && is_numeric($HTTP_POST_VARS['events_id']))
		$events_id=tep_db_prepare_input($HTTP_POST_VARS['events_id']);
	else
		tep_redirect(tep_href_link(FILENAME_ACCOUNT,'','SSL'));

  
	if (isset($HTTP_POST_VARS['orders_products_id']) && is_array($HTTP_POST_VARS['orders_products_id']))
		$orders_products_id=tep_db_prepare_input($HTTP_POST_VARS['orders_products_id']);
	else
		tep_redirect(tep_href_link(FILENAME_ACCOUNT,'','SSL'));



	if (isset($HTTP_POST_VARS['session_ids']) && is_array($HTTP_POST_VARS['session_ids']))
		$session_ids=tep_db_prepare_input($HTTP_POST_VARS['session_ids']);
	else
		tep_redirect(tep_href_link(FILENAME_ACCOUNT,'','SSL'));



	if (isset($HTTP_POST_VARS['new_session_ids']) && is_array($HTTP_POST_VARS['new_session_ids']))
		$new_session_ids=tep_db_prepare_input($HTTP_POST_VARS['new_session_ids']);
	else
		tep_redirect(tep_href_link(FILENAME_ACCOUNT,'','SSL'));
     
     
	$reservation_array=array();
	
	$process_wait=(isset($HTTP_POST_VARS['process_wait'])?tep_db_prepare_input($HTTP_POST_VARS['process_wait']):'');
	
	$event_query=tep_db_query("SELECT e.events_id,e.events_tax_class_id,e.events_max_reserve,e.events_fees,ed.events_name,t.tax_rate,e.events_renewal_end FROM " . TABLE_EVENTS . " e," . TABLE_EVENTS_DESCRIPTION. " ed," . TABLE_TAX_RATES . " t where e.events_id='" . tep_db_input($events_id) . "' and e.events_id=ed.events_id and e.events_tax_class_id=t.tax_class_id and ed.language_id=" . tep_db_input($languages_id));
    if ($event_result=tep_db_fetch_array($event_query)){
        $events_max_reserve=((int)$event_result['events_max_reserve'])-tep_get_renewal_users_count($event_result['events_id'],$event_result['events_renewal_end']);
		$events_wait_list=$event_result['events_wait_list'];
		$events_name=$event_result['events_name'];
		$events_fees=$event_result['events_fees'];
		$events_tax=$event_result['tax_rate'];
	}

	$cnt=0;
	for($icnt=0;$icnt<sizeof($orders_products_id);$icnt++){
		$temp_count=0;
		$err_type='N';
		$check_session_id=(int)$session_ids[$icnt];
		$update_id=(int)$orders_products_id[$icnt];
		$new_session_id=(int)$new_session_ids[$icnt];
		if ($check_session_id!=0 && $update_id!=0 && $new_session_id!=0 && $check_session_id!=$new_session_id){
			if (tep_check_single_reservation($new_session_id,$customer_id)){
				$err_type='A';
			} else {
				$temp_count=tep_get_count(TABLE_ORDERS_PRODUCTS,"products_id='" . $new_session_id . "' and events_type='R' and products_type='E'");
				if ($temp_count>=$events_max_reserve){
					if ($events_wait_list!='Y'){
						$err_type='E'; 
					} else {
						$err_type='W';
						$waitlist_group=tep_find_waitlist_group($events_id,$new_session_id);
						if ($waitlist_group=='E'){
							$err_type='M'; // empty seats
						} else {
							$err_type='W';  //wait list
							if ($process_wait=='Y'){
								$err_type='N';
								tep_db_query("UPDATE " . TABLE_ORDERS_PRODUCTS . " set products_id='" . tep_db_input($new_session_id) . "', events_type='W' , waitlist_type='" . tep_db_input($waitlist_group) . "' where orders_products_id='" . tep_db_input($update_id) . "'");
							}
						}
					}
				} else {
					tep_db_query("UPDATE " . TABLE_ORDERS_PRODUCTS . " set products_id='" . tep_db_input($new_session_id) . "', events_type='R' where orders_products_id='" . tep_db_input($update_id) . "'");
				}
			}
		}
		if ($err_type!='N'){
			$reservation_string.=tep_draw_hidden_field('orders_products_id[]',$update_id);
			$reservation_string.=tep_draw_hidden_field('session_ids[]',$check_session_id);
			$reservation_string.=tep_draw_hidden_field('new_ids[]',$new_session_id);
			$reservation_string.=tep_draw_hidden_field('error[]',$err_type);}
		}
		tep_check_waiting_list($events_id);
		if ($reservation_string==''){
			tep_redirect(tep_href_link(FILENAME_ACCOUNT_RESERVATIONS,'','SSL'));
		}
		echo tep_draw_form('move_return',tep_href_link(FILENAME_MOVE_RESERVATIONS));
		echo tep_draw_hidden_field('events_id',$events_id);
		echo $reservation_string;
		echo '</form>';
?>
<html>
<script>
	document.move_return.submit();
</script>
</html>
