<?php
/*
	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');
	$mode=(isset($HTTP_POST_VARS['mode'])?tep_db_prepare_input($HTTP_POST_VARS['mode']):'');
	$sessions_id=(isset($HTTP_POST_VARS['products_id'])?(int)$HTTP_POST_VARS['products_id']:0);
	$sessions_count=(isset($HTTP_POST_VARS['products_qty'])?(int)$HTTP_POST_VARS['products_qty']:0);
	$question_type=(isset($HTTP_POST_VARS['question_type'])?tep_db_prepare_input($HTTP_POST_VARS['question_type']):'');
	$question_value_data=(isset($HTTP_POST_VARS['question_value_data'])?tep_db_prepare_input($HTTP_POST_VARS['question_value_data']):'');
	$old_orders_id=(isset($HTTP_POST_VARS['orders_id'])?(int)$HTTP_POST_VARS['orders_id']:'');
	$selected_attendees=(isset($HTTP_POST_VARS['select_attendees'])?(int)$HTTP_POST_VARS['select_attendees']:0);
	//group booking
	$no_attendees=(isset($HTTP_POST_VARS['no_attendees'])?(int)$HTTP_POST_VARS['no_attendees']:1);
	//group booking
	// check for presence old orders id if the type of request is from complete payment
	if ($mode!=''){
		if ($old_orders_id>0){
			tep_redirect(tep_href_link(FILENAME_CHECKOUT_EVENT,tep_get_all_get_params(array('mode','oID')) . "&products_id=" . $sessions_id . "&products_qty=" . $sessions_count . "&question_type=" . $question_type . "&question_value_data=" . $question_value_data . "&event_action=buy_now&element_type=E&events_type=U&old_orders_id=" . $old_orders_id));
		}
	}
  
  	// agree to the terms and conditions
	$agree=(isset($HTTP_POST_VARS['agreed'])?tep_db_prepare_input($HTTP_POST_VARS['agreed']):'');
	if ($agree!='1') {
		tep_redirect(tep_href_link(FILENAME_SESSION_DETAILS,tep_get_all_get_params(array('event_action'))));
	}

  
  	// check for the sessions_id and no of sessions
	$result_array=array();
	if($sessions_id==0 || $sessions_count==0)
		tep_redirect(tep_href_link(FILENAME_SESSION_DETAILS,tep_get_all_get_params(array('event_action'))));
	
	// get free session dates to display to user.
	$session_array=array();
	$result_sql = "SELECT 	e.events_id,e.events_fees,e.waitfee_vip,e.waitfee_firstcome,e.waitfee_custom,
										s.start_date,e.events_max_reserve,e.events_renewal_end,e.events_wait_list,e.events_pending_period,
										e.priority_vip,e.priority_firstcome,e.priority_custom,e.priority_question,
										e.waitsize_vip,e.waitsize_firstcome,e.waitsize_custom,e.events_forward_period,e.events_renewal_start,
										e.events_forward_length,e.session_gaps,e.session_reduction,e.session_style,e.events_tax_class_id,e.events_group_bookings from " . 
										TABLE_EVENTS_SESSIONS . " s," . TABLE_EVENTS . " e where s.events_id=e.events_id and s.sessions_id='". tep_db_input($sessions_id) . "'";
	$result_query=tep_db_query($result_sql);
	//if got event info
	if (tep_db_num_rows($result_query)){
		$events_result=tep_db_fetch_array($result_query);
		$events_id=$events_result['events_id'];
		// get free reservations	
		$total_reservations = tep_get_free_sessions($sessions_id,$sessions_count,'R',$no_attendees);
		if (sizeof($session_array)<=0) {
			if ($events_result['events_wait_list']=='Y') {
				// get waitlisted reservations
				$total_reservations = tep_get_free_sessions($sessions_id,$sessions_count,'W',$no_attendees);
			}
		} else if (sizeof($session_array)<$sessions_count){ 
			//  check for waiting list style ,single session or event
			if ($events_result["session_style"]=="S" && $events_result['events_wait_list']=='Y'){
				end($session_array);
				$key=key($session_array);
				$total_reservations = tep_get_free_sessions($key,$sessions_count-sizeof($session_array),'W',$no_attendees);
			}
		}
	}
	//end of get free sessions
	if (sizeof($session_array)>0) {
		//check for session reduction if "N" the no of quoted sessions must equal the selected sessions
		if ($events_result["session_reduction"]!='Y' && sizeof($session_array)<$sessions_count){
			$session_array=array();
		} else {
			uasort($session_array,"cmp");
			reset($session_array);
		}
	}

	$sessions_count=sizeof($session_array);
	// check for presence of one reservation seat
	$found_results='empty';
	$id_string="";
	if (sizeof($session_array)>0){
		$found_results='waitlist';
		reset($session_array);
		while(list($sid,)=each($session_array)){
			if ($session_array[$sid]['type']=='R'){
				$found_results='reserve';
			}
			$id_string.="'" . $session_array[$sid]['date'] . "',";
		}
		$id_string=substr($id_string,0,-1);
	}

	$content = CONTENT_CHECKOUT_EVENT;
	//for single session
			if(defined('SHOW_SEAT_AVAILABILITY') && (strtolower(SHOW_SEAT_AVAILABILITY)=='false' || strtolower(SHOW_SEAT_AVAILABILITY)=='no') && $found_results=='reserve'){
			    echo "<html><head></head><body>";
			    echo tep_draw_form("checkout_event",tep_href_link(FILENAME_EVENTS_BOOKING_DETAILS,tep_get_all_get_params()));
				echo tep_draw_hidden_field("old_orders_id",$old_orders_id);
				echo tep_draw_hidden_field("events_id",$events_id);
				echo tep_draw_hidden_field("event_question_type",$question_type);
				echo tep_draw_hidden_field("event_question_value_data",$question_value_data);
				reset($session_array);
				while(list($id,)=each($session_array)) {					
					if ($session_array[$id]['type']=='R') echo '<input type="hidden" name="session_select_' .  $id . '" value="1">';
					if ($events_result["session_gaps"]!='Y') echo '<input type="hidden" name="session_select_' .  $id . '" value="1">'; ?>
						<input type=hidden name="session_id[]" value="<?php echo $id;?>">
						<input type=hidden name="session_type[]" value="<?php echo $session_array[$id]["type"];?>">
						<input type=hidden name="session_group[]" value="<?php echo $session_array[$id]["group"];?>">
						<input type=hidden name="session_date[]" value="<?php echo $session_array[$id]["date"];?>">
				<?php } ?>
					<input type="hidden" name="no_attendees" value="<?php echo tep_output_string($no_attendees) ;?>"> 
				  </form>
				  <script language="javascript">if(document.checkout_event) document.checkout_event.submit();</script>
				  </body>
				</html>				
			<?php } 
	//for single session
	require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_CHECKOUT_EVENT);
	
	require(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/' . TEMPLATENAME_MAIN_PAGE);
	
	require(DIR_WS_INCLUDES . 'application_bottom.php');
		 
	unset($session_array);

function cmp($a, $b) 
{
    if ($a['date'] == $b['date']) {
        return 0;
    }
    return ($a['date'] < $b['date']) ? -1 : 1;
}
?>
