<?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');
	require('includes/classes/currencies.php');
	require(DIR_WS_INCLUDES . 'functions/events_sessions.php');
	
	$currencies=new currencies();
	$current_events_category_id=isset($HTTP_GET_VARS['ecID'])?(int)$HTTP_GET_VARS['ecID']:0;
	
	$category=array();
	$events_categories_array=tep_get_event_category_tree();


	if($current_events_category_id<=0) $current_events_category_id=(int)$events_categories_array[0]['id'];
	

	if ($current_events_category_id>0) $events_category_option=" and ecd2.events_categories_id='" . $current_events_category_id . "' ";


	$category_query=tep_db_query("SELECT ecd.events_categories_name,ecd2.events_categories_id,ecd2.events_id from " . TABLE_EVENTS_CATEGORIES_DESCRIPTION . " ecd, " . TABLE_EVENTS_TO_EVENTS_CATEGORIES . " ecd2, " . TABLE_EVENTS_DESCRIPTION . " ed, " . TABLE_EVENTS . " e " .
									" where e.events_id=ed.events_id and ecd.events_categories_id=ecd2.events_categories_id and ecd2.events_id=ed.events_id and ecd.language_id='" . $languages_id ."' and ed.language_id='" . $languages_id . "' " . EVENTS_CONDITION_STATUS . $events_category_option . " group by ecd2.events_id order by ecd.events_categories_name,ed.events_name");

	$category=array();
	while($category_result=tep_db_fetch_array($category_query)){
		$cat_id=$category_result["events_categories_id"];
		if (!isset($category[$cat_id])){
			$category[$cat_id]=array('name'=>$category_result["events_categories_name"],'events'=>array());
		}
		$category[$cat_id]['events'][]=$category_result["events_id"];
	}
	$events_option="";
	if ($login_groups_type==TEXT_INSTRUCTOR_ENTRY) $events_option=" and e.events_instructor='" . $login_id . "'";

	$events_query=tep_db_query("SELECT " . tep_event_fields_list() . ",ed.events_name,e.events_instructor,e.events_location,e.events_start_time," . 
								"events_start_time as start_time,time_format(e.events_start_time,'%h:%i %p') as events_start_time,time_format(e.events_end_time,'%h:%i %p') as events_end_time,e.events_date_available,ed.events_description,ecd2.events_categories_id,ecd2.events_id from " . 
								TABLE_EVENTS . " e, " . TABLE_EVENTS_DESCRIPTION . " ed,". TABLE_EVENTS_TO_EVENTS_CATEGORIES ." ecd2 where  e.events_id=ecd2.events_id and ed.events_id=ecd2.events_id " . $events_category_option  . EVENTS_CONDITION_STATUS . $events_option ." and ed.language_id='" . (int)$languages_id . "' order by events_name");
								

	$event_found=false;
	if (tep_db_num_rows($events_query)>0) $event_found=true;
	$events=array();
	if ($event_found)	{
		while($events_result=tep_db_fetch_array($events_query)){
			$events_id=$events_result["events_id"];
			
			$add_option="";

			$session_type=tep_get_session_type($events_id);

			$location_content=fetch_location_content($events_id,$events_result['events_location']);
			$instructor_content=fetch_instructor_content($events_id,$events_result['events_instructor']);
			$event_content=fetch_events_content($events_id,$events_result["events_description"]);
			
			if ($events_result['events_forward_period']=='Y'){
				$check_date=date('Y-m-d',tep_dateadd(time(),'day',(int)$events_result['events_forward_length']));
				$add_option=" and start_date<='" . $check_date . "' ";
			}
			$start_option=" and start_date>curdate() ";
			

			$events[$events_id]=array(	'details'=>$events_result,
										'start'=>'',
										'location'=>$location_content[1],
										'instructor'=>$instructor_content[1],
										'dates'=>array());
			$date_query=tep_db_query("SELECT start_date,sessions_id,recursive_sessions_id from " . TABLE_EVENTS_SESSIONS  .  " where events_id='" . (int)$events_id . "'" . $start_option . $add_option . " and sessions_type!='' order by start_date");
			$temp=array('reserve'=>'','waitlist'=>'');
			$prev_recursive=0;
			$recursive_array=array();
			$icnt=0;
			while($date_result = tep_db_fetch_array($date_query))
			{
				$details=tep_get_session_details($date_result,$events_result);
				$color="#FF0000";
				if ($details['status']=='R') {
					$color="#000000";
					if ($temp["reserve"]==''){
						$temp["reserve"]=$date_result["start_date"];
					}
				}
				if ($details['status']=='W') {
					$color="FFA715";
					if ($temp["waitlist"]==''){
						$temp["waitlist"]=$date_result["start_date"];
					}
				}
				$events[$events_id]['dates'][] = array(	'id' =>$date_result["sessions_id"],
														'date'=> $date_result['start_date'],
														'type'=> $details['status'],
														'recursive_id'=>$date_result["recursive_sessions_id"],
														'group'=>'',
														'reserved'=>$details["reserved"],
														'waitlist'=>$details["waitlist"],
														'renewal'=>$details["renewal"],
														'bgcolor'=>$color
										);
				if ($session_type=='R' && $prev_recursive!=$date_result["recursive_sessions_id"]){
					$prev_recursive=$date_result["recursive_sessions_id"];
					$recursive_array[]=$prev_recursive;
				}
			}
			if ($temp["reserve"]!=''){
				$events[$events_id]['start']=$temp["reserve"];
			} else if ($temp["waitlist"]!=''){
				$events[$events_id]['start']=$temp["waitlist"];
			}
			if ($session_type=='R' && sizeof($recursive_array)>0){
					$result_array=get_gap_dates_array($recursive_array);
					if (sizeof($result_array)>0){
						for ($icnt=0;$icnt<sizeof($result_array);$icnt++){
							$events[$events_id]['dates'][]=array('date'=>$result_array[$icnt],'type'=>'H','group'=>'','bgcolor'=>'','reserved'=>0,'waitlist'=>0,'renewal'=>0,'recursive_id'=>0);
						}
						usort($events[$events_id]['dates'],"user_compare");
					}
			}
			$dates_content=fetch_dates_content($events_id,$events[$events_id]['dates']);
			$final_content.=$event_content . $location_content[0] . $instructor_content[0] . $dates_content;
		}
	}
?>
<!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">
<style type="text/css">
	#pup {position:absolute; visibility:hidden; z-index:200; width:130; }
</style>
<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">
<script language="javascript" src="includes/popup.js"></script>
<script language="JavaScript">
	var baby=new Array();
	var pevents_id=0;
	baby[1]="";
	function showpopup(type,events_id)
	{
		var element=document.getElementById(type+events_id);
		var content;
		if (!element) return;
		if (element.innerHTML=="") return;
		content='<table border="0" cellspacing="0" cellpadding="4" class="formArea" width="100%"><tr><td>';
		content+=element.innerHTML;
		content+='</td></tr></table>';
		popwidth=250;
		if (type=='sessiondesc_') popwidth=230;
		baby[1]=content;
		if (baby[1]!="")
		{
			popup("1","#ffffff");
		}	
	}
	function chkclick(frm)
	{
			if (document.goto.create_customer.checked==true)
			{	
				frm.action="<?php echo FILENAME_CREATE_ACCOUNT;?>";
			}
			else 
			{
				frm.action="<?php echo FILENAME_CREATE_ORDER;?>";
			}
	}
//  End -->

</script>
<?php echo $final_content;?>
	<!-- 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="0">
			<tr>
				<td class="pageHeading">
					<?php echo TEXT_HEADING_TITLE;?>
				</td>
			</tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif',10,20);?></td></tr>
			<tr>
				<td width="100%">
				<table border="0" width="100%" cellspacing=0 cellpadding="4">
					<tr>
						<td>
							<table border="0" width="100%" cellspacing=0 cellpadding="0">
							<tr class="attributes-odd">
								<td><?php echo tep_draw_separator('pixel_trans.gif',10,10);?></td>
							</tr>
			<tr>
				<td class="main">
				<?php echo tep_draw_form('goto', FILENAME_EVENTS_BROWSE, '', 'get');
				echo HEADING_TITLE_EVENT . ' ' . tep_draw_pull_down_menu('ecID',$events_categories_array, $current_events_category_id, 'onChange="this.form.submit();"'); 
				if (tep_db_num_rows($events_query)){
					echo '&nbsp;&nbsp;&nbsp;' . tep_draw_checkbox_field('create_customer', '',false,'') . '&nbsp;' . TEXT_CREATE_CUSTOMER;
				}
				echo '</form>';?>
			</tr>
		<?php 
			if ($event_found){		
				while(list($category_id,)=each($category)){
		?>
					<tr>
						<td>
						<table border="0" cellpadding="0" cellspacing="2">
							<tr>
								<td class="tableHeading">
									<?php echo '<font color="#0000FF">' . $category[$category_id]['name'] . '</font>';?>
								</td>
							</tr>
						</table>
						</td>
					</tr>
					<tr class="attributes-odd"><td><?php echo tep_draw_separator('pixel_trans.gif',10,10);?></td></tr>
		<?php 
					for ($kcnt=0;$kcnt<sizeof($category[$category_id]['events']);$kcnt++){
						$events_id=$category[$category_id]['events'][$kcnt];
						
						if (isset($events[$events_id])){
							$event=&$events[$events_id]['details'];
							$events_name=$event["events_name"];?>
						<tr>
							<td>
							<table border="0" cellpadding="2" cellspacing="0" width="100%" class="tableArea">
								<tr class="eventInfo" id="<?php echo "row_0" . $events_id;?>">
									<!--<td class="smalltext" width="300" onmouseover="javascript:showpopup(</?php echo "'eventdesc_','" . $events_id . "'";?>)" onMouseOut="javascript:kill()"><b></?php echo $events_name;?></b></td>-->
									
									<td class="smalltext" width="300" onMouseOver="javascript:showpopup(<?php echo "'eventdesc_','" . $events_id . "'";?>)" onMouseOut="javascript:kill()"><b><?php echo $events_name;?></b></td>
									<td class="smalltext" width="100" valign="top"><?php echo tep_get_event_daytext($events_id);?></td>
									<td class="smalltext" width="120" valign="top"><?php echo $event["events_start_time"] . "-" . $event["events_end_time"];?></td>
									<td class="smalltext" width="100" valign="top" onMouseOver="javascript:showpopup(<?php echo "'location_','" . $events_id . "'";?>)" onMouseOut="javascript:kill()"><?php echo $events[$events_id]['location'];?></td>
									<td class="smalltext" width="100" valign="top" align="right">
										<?php 	$interval=($event["sessions_select_interval"]>0?$event["sessions_select_interval"]:1);
												$fees=$interval*tep_add_tax($event["events_fees"],tep_get_tax_rate($event["events_tax_class_id"]));
												$info=TEXT_FEES_INFO_SINGLE;
												if ($interval>1) $info=TEXT_FEES_INFO;
												echo $currencies->format($fees)
										?>
									</td>
									<td class="smalltext" valign="top" align="left"><?php echo sprintf($info,$interval); ?>
									</td>
								</tr>
								<tr class="eventInfo" id="<?php echo "row_1" . $events_id;?>">
									<td></td>
									<td class="smalltext" valign="top" onMouseOver="javascript:showpopup(<?php echo "'instructor_','" . $events_id . "'";?>)" onMouseOut="javascript:kill()"><?php echo $events[$events_id]["instructor"];?></td>
									<td class="smalltext" valign="top" onMouseOver="javascript:showpopup(<?php echo "'sessiondesc_','" . $events_id . "'";?>)" onMouseOut="javascript:kill()">
									<?php $color="#000000";
										$start_element=array("spaces"=>0,'date'=>"",'type'=>'E');
										if ($events[$events_id]['start']!=''){
											$dates=&$events[$events_id]['dates'];
											for ($icnt=0;$icnt<sizeof($dates);$icnt++){
												if ($dates[$icnt]['date']==$events[$events_id]['start']) {
													$start_element=&$dates[$icnt];
													break;
												}
											}
										}
										if ($start_element['type']=='W') $color="#FFA715";
										if ($start_element['type']=='R' || $start_element['type']=='W')
											echo '<b>' . TEXT_START_DATE . '</b><font color="' . $color . '" >' . format_date($start_element['date']) . '</font>';
										else 
											echo '<span class=errorText>' . TEXT_NO_SESSIONS . '<span>';
									?>
									</td>
									<td class="smalltext" valign="top">
										<?php 	if ($start_element['type']=='R') 
													echo '<b>' . TEXT_SPACES . '</b>' . $start_element['reserved'];
												else if ($start_element['type']=='W')
													echo '<b>' . TEXT_SPACES . '</b>' . $start_element['waitlist'];
										?>
									</td>
									<td></td>
									<td class="smalltext" align="right" valign="middle">
									<?php
											if ($start_element['type']=='R' || $start_element['type']=='W'){
											echo tep_draw_form('create_order',FILENAME_CREATE_ORDER,'' ,'post','onsubmit="chkclick(this);"');
											echo '<input type="hidden" name="param_event" value="' . $events_id .'">';
											echo '<input type="hidden" name="param_session" value="' . $start_element['id'] .'">';
											echo tep_image_submit('button_add.gif',IMAGE_RESERVE,'align=absmiddle');
											echo '</form>';
										}
									?>
									</td>
								</tr>
								
							</table>
							</td>
						<tr>
						<tr class="attributes-odd"><td><?php echo tep_draw_separator('pixel_trans.gif',10,10);?></td></tr>
		<?php			}
					}
				} // while	
			} else { // if event found
		?>
			<tr>
				<td class="main">
					<?php echo '<div align=center><Br><Br><br><b>'.TEXT_NO_EVENTS.'</b>'.'</div>';?>
				</td>
			</tr>
		<?php }  // if event found in category
			
		?>
				</table>
				</td>
			</tr>
			</table>
			</td>
			</tr>
		</table>
<!-- body_eof //-->
		</td>
		</table>
	</tr>
</table>
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); 
	
	
	function tep_get_event_daytext($events_id){
		//$result=array('type'=>'','day'=>'');
		$result_text="";
		
		$recursive_query=tep_db_query("SELECT * from " . TABLE_RECURSIVE_SESSIONS . " where events_id='" . $events_id . "'");
		
		if (tep_db_num_rows($recursive_query)<=0) return $result_text;

		$session_result=tep_db_fetch_array($recursive_query);
		
		$type=$session_result['sessions_type_option'];
	

	     //$days_array=array('1'=>'Sunday','2'=>'Monday','3'=>'Tuesday','4'=>'Wednesday','5'=>'Thursday','6'=>'Friday','7'=>'Saturday');
		$days_array=array('1'=>'Monday','2'=>'Tuesday','3'=>'Wednesday','4'=>'Thursday','5'=>'Friday','6'=>'Saturday','7'=>'Sunday');
		$count_array=array('1'=>'first','2'=>'second','3'=>'third','4'=>'fourth');
		$months_array=array('1'=>'January','2'=>'February','3'=>'March','4'=>'April','5'=>'May','6'=>'June','7'=>'July','8'=>'August','9'=>'September','10'=>'October','11'=>'November','12'=>'December');

		switch($type){
			case 'DD':
				if ($session_result['sessions_type_daily_days']<=1){
					$result_text=TEXT_DAILY_DAY;}
				else
					$result_text=sprintf(TEXT_DAILY_DAYS,$session_result['sessions_type_daily_days']);
				break;
			case 'WW':
				$temp_string=$session_result['sessions_type_weekly_days'];
				$day_string="";
				for ($icnt=0;$icnt<strlen($temp_string);$icnt++){
					if (substr($temp_string,$icnt,1)=='1'){
						if ($day_string!='') $day_string=$day_string . ", ";
							$day_string=$day_string . $days_array[$icnt+1];
						}
					}
				if ($session_result['sessions_type_weekly_weeks']<=1)
					$result_text=sprintf(TEXT_WEEKLY_WEEK,$day_string);
				else
					$result_text=sprintf(TEXT_WEEKLY_WEEKS,$session_result['sessions_type_weekly_weeks'],$day_string);
				break;
			case 'MD':
				if ($session_result['sessions_type_monthly_months']>1)
					$result_text=sprintf(TEXT_MONTHLY_DAYS,$session_result['sessions_type_monthly_months'],$session_result['sessions_type_monthly_days']);
				else
					$result_text=sprintf(TEXT_MONTHLY_DAY,$session_result['sessions_type_monthly_days']);
				break;
			case 'MM':
				if ($session_result['sessions_type_monthly_weekmonths']>1)
					$result_text=sprintf(TEXT_MONTHLY_WEEKS,$session_result['sessions_type_monthly_weekmonths'],$count_array[$session_result['sessions_type_monthly_weeks']],$days_array[$session_result['sessions_type_monthly_weekdays']]);
				else
					$result_text=sprintf(TEXT_MONTHLY_WEEK,$count_array[$session_result['sessions_type_monthly_weeks']],$days_array[$session_result['sessions_type_monthly_weekdays']]);	  
				break;
			case 'YM':
				$result_text=sprintf(TEXT_YEARLY_MONTHS,$months_array[$session_result['sessions_type_yearly_months']],$session_result['sessions_type_yearly_monthdays']);
				break;
	        case 'YY':
				$result_text=sprintf(TEXT_YEARLY_WEEKS,$count_array[$session_result['sessions_type_yearly_weeks']],$days_array[$session_result['sessions_type_yearly_weekdays']],$months_array[$session_result['sessions_type_yearly_weekmonths']]);
				break;
	}
	return $result_text;   
}
function fetch_location_content($events_id,$location_id){
	$result=array();
	$result[0]='<div id="location_' . $events_id .'" style="display:none">';
	$result[1]="";

	$location_query=tep_db_query("SELECT location_name,location_details,location_travel_directions from " . TABLE_EVENTS_LOCATIONS . " where location_id='" . $location_id . "'");
	if (tep_db_num_rows($location_query)>0)	{
		$location_result=tep_db_fetch_array($location_query);
		$result[0].='<table border="0" cellspacing="0" cellpadding="3" width="100%">';
		$result[0].='<tr><td class="smalltext"><b>' . TEXT_LOCATION_NAME . '</b>' . $location_result["location_name"] . '</td></tr>';
		$result[0].='<tr><td class="smalltext"><b>' . TEXT_DETAILS . '</b>' . $location_result["location_details"] . '</td></tr>';
		$result[0].='<tr><td class="smalltext"><b>' . TEXT_DIRECTIONS . '</b>' . $location_result["location_travel_directions"] . '</td></tr>';

		$result[0].='</table>';
		$result[1]=$location_result["location_name"];
	}
	$result[0].="</div>\n";

	return $result;
	

}

function fetch_instructor_content($events_id,$instructor_id){
	$result=array();
	$result[0]='<div id="instructor_' . $events_id .'" style="display:none">';
	$result[1]="";

	$instructor_query=tep_db_query("SELECT concat(a.admin_firstname,' ',a.admin_lastname) as instructors_name,i.instructors_street_address,i.instructors_city,i.instructors_state,i.instructors_postcode,i.instructors_country,i.instructors_description from " . TABLE_INSTRUCTORS . " i, " . TABLE_ADMIN . " a where i.admin_id='" . $instructor_id . "' and i.admin_id=a.admin_id");
	if (tep_db_num_rows($instructor_query)>0)	{
		$instructor_result=tep_db_fetch_array($instructor_query);
		$result[0].='<table border="0" cellspacing="0" cellpadding="2" width="100%">';
		//$result[0].='<tr><td class="main">' . $location_result["instructor_name"] . '</td></tr>';
		//$result[0].='<tr><td class="tableHeading">' . TEXT_DESCRIPTION . '</td></tr>';
		$result[0].='<tr><td class="smalltext"><b>' . TEXT_INSTRUCTOR_NAME . '</b>' . $instructor_result["instructors_name"] . '</td></tr>';
		$result[0].='<tr><td class="smalltext"><b>' . TEXT_DETAILS . '</b>' . $instructor_result["instructors_description"] . '</td></tr>';
		$result[0].='</table>';
		$result[1]=$instructor_result["instructors_name"];
	}
	$result[0].="</div>\n";

	return $result;
}

function fetch_events_content($events_id,&$description){
	$result='<div id="eventdesc_' . $events_id .'" style="display:none">';
	$result.='<table border="0" cellspacing="0" cellpadding="3" width="100%">';
	//$result.='<tr><td class="tableHeading">' . TEXT_DESCRIPTION . '</td></tr>';
	$result.='<tr><td class="smalltext">' . $description . '</td></tr>';
	$result.='</table>';
	$result.="</div>\n";
	
	return $result;
	
}
function fetch_dates_content($events_id,&$dates){
	$result.='<div id="sessiondesc_' . $events_id .'" style="display:none">';
	$result.='<table border="0" cellspacing="0" cellpadding="2" width="100%">';
	$result.='<tr><td><table cellspacing="1" cellpadding="1" width="100%">';
	if (sizeof($dates)>0){
		$result.='<tr><td class="smalltext"><b>' . TEXT_DATE  . '</b></td><td class="smalltext"><b>' . TEXT_FREE_SEATS . '</b></td><td class="smallText"><b>' . TEXT_RENEWAL_SEATS . '</b></td></tr>';
		reset($dates);
		$dates=array_slice($dates,0,10);
		for ($icnt=0;$icnt<sizeof($dates);$icnt++){
			if ($dates[$icnt]['type']=='R')
				$result.='<tr><td class="smalltext">' . sprintf("<font color=\"%s\">%s</font>",$dates[$icnt]['bgcolor'],format_date($dates[$icnt]['date'])) . '</td><td class="smalltext">' . $dates[$icnt]["reserved"] . '</td>';
			else if ($dates[$icnt]['type']=='W')
				$result.='<tr><td class="smalltext">' . sprintf("<font color=\"%s\">%s</font>",$dates[$icnt]['bgcolor'],format_date($dates[$icnt]['date'])) . '</td><td class="smalltext">' . $dates[$icnt]["waitlist"] . '</td>';
			else if ($dates[$icnt]['type']=='E')
				$result.='<tr><td class="smalltext">' . sprintf("<font color=\"%s\">%s</font>",$dates[$icnt]['bgcolor'],format_date($dates[$icnt]['date'])) . '</td><td class="smalltext">0</td>';
			else
				$result.='<tr><td class="smalltext">' . format_date($dates[$icnt]['date']) . '</td><td class="smalltext" colspan="2">' . TEXT_HOLIDAY . '</td></tr>';
			if ($dates[$icnt]['type']=='R' || $dates[$icnt]['type']=='W' || $dates[$icnt]['type']=='E')
				$result.='<td class="smallText">' . $dates[$icnt]["renewal"] . '</td></tr>';
		}
	} else {
		$result.='<tr><td class="smalltext">' . TEXT_NO_SESSIONS . '</td></tr>'; 
	}
	$result.='</table></td></tr></table>';
	$result.='</div>';
	return $result;
}
function user_compare($a, $b) 
{
    if ($a['date'] == $b['date']) {
        return 0;
    }
    return ($a['date'] < $b['date']) ? -1 : 1;
}

?>