<?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');
  
  
  // get input entries to find type of entry

  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');

	switch ($action) {
		//delete a location
		case 'delete_location_confirm':
			$location_id=(isset($HTTP_POST_VARS['location_id'])?(int)$HTTP_GET_VARS['location_id']:0);
			if ($location_id>0) {	
				tep_db_query("DELETE from " . TABLE_EVENTS_LOCATIONS . " where location_id='" . $delete_id . "'");
				if (USE_CACHE == 'true') {
					tep_reset_cache_block('events_locations');
				}
			}
			tep_redirect(tep_href_link(FILENAME_EVENTS_LOCATIONS));
			break;
      //insert or edit a location
		case 'update_location':
			$sub_action=$HTTP_POST_VARS['sub_action'];
			if ($sub_action=='location'){
				$sql_data = array(	'location_name' => tep_db_prepare_input($HTTP_POST_VARS['location_name']),
									'location_details' =>tep_db_prepare_input($HTTP_POST_VARS['location_details']),
									'location_travel_directions'=>tep_db_prepare_input($HTTP_POST_VARS['location_travel_directions']),
									'location_equipment'=>tep_db_prepare_input($HTTP_POST_VARS['location_equipment']),
									'location_contact_person'=>tep_db_prepare_input($HTTP_POST_VARS['location_contact_person'])
								);
				if (isset($HTTP_POST_VARS['location_id']) && ((int)$HTTP_POST_VARS['location_id']>0)) {
					$update_id=(int)$HTTP_POST_VARS['location_id'];
					tep_db_perform(TABLE_EVENTS_LOCATIONS, $sql_data, 'update', "location_id = '" . $update_id . "'");
				} else {
					tep_db_perform(TABLE_EVENTS_LOCATIONS, $sql_data);
				}
				if (USE_CACHE == 'true') {
					tep_reset_cache_block('events_locations');
				}
				tep_redirect(tep_href_link(FILENAME_EVENTS_LOCATIONS));
			} else if ($sub_action=='option_add'){
				$sql_data=array('equipment_name'=>tep_db_prepare_input($HTTP_POST_VARS['edit_equipment']));
				tep_db_perform(TABLE_EVENTS_LOCATIONS_OPTIONS,$sql_data);
				$HTTP_POST_VARS['location_equipment']=$HTTP_POST_VARS['edit_equipment'];
			} else if ($sub_action=='option_update'){
				$sql_data=array('equipment_name'=>tep_db_prepare_input($HTTP_POST_VARS['edit_equipment']));
				tep_db_perform(TABLE_EVENTS_LOCATIONS_OPTIONS,$sql_data,'update','equipment_name="' . tep_db_input($HTTP_POST_VARS['location_equipment']) . '"');
			} else if ($sub_action=='option_delete'){
				$sql_data=tep_db_query("DELETE FROM " . TABLE_EVENTS_LOCATIONS_OPTIONS . " where equipment_name='" . tep_db_input($HTTP_POST_VARS['location_equipment']) . "'");
			}
			if (USE_CACHE == 'true') {
				tep_reset_cache_block('events_locations_options');
			}
			echo tep_draw_form('redirect',FILENAME_EVENTS_LOCATIONS,'action=new_location');
			reset($HTTP_POST_VARS);
			while(list($key,$value)=each($HTTP_POST_VARS)){
				echo tep_draw_hidden_field($key,$value);
			}
			echo '<form>';
			echo '<script language=javascript>document.redirect.submit();</script>';
			break;
    }
?>
<!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>
<?php  // WebMakers.com Added: Java Scripts   
      include(DIR_WS_INCLUDES . 'javascript/' . 'webmakers_added_js.php')
?>
</head>

<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onLoad="SetFocus();">
<!-- 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%" valign="top">
		<table border="0" width="100%" cellspacing="0" cellpadding="2">
		<?php 
		if ($action=='new_location') {
			$equipment_query=tep_db_query("SELECT equipment_name from " . TABLE_EVENTS_LOCATIONS_OPTIONS . ' order by equipment_name');
			$ecnt=tep_db_num_rows($equipment_query);
			$equipment_array=array();
			while ($result=tep_db_fetch_array($equipment_query)){
			  $equipment_array[]=array('id'=>$result['equipment_name'],'text'=>$result['equipment_name']);
			}
			$cnt=0;
			$location_info=array(	'location_id'=>0,
							 		'location_name'=>'',
									'location_details'=>'',
									'location_travel_directions'=>'',
									'location_equipment'=>'',
									'location_contact_person'=>''
								);
			$loInfo=new objectInfo($location_info); 
			if (isset($HTTP_GET_VARS['loID'])){
				$location_info_query=tep_db_query("SELECT * from " . TABLE_EVENTS_LOCATIONS . " where location_id='" . (int)$HTTP_GET_VARS['loID'] . "'");
				$location_result=tep_db_fetch_array($location_info_query);
				if (is_array($location_result))	$loInfo=new objectInfo($location_result);
				$cnt=tep_db_num_rows($location_info_query);
			} else if (tep_not_null($HTTP_POST_VARS)){
				$loInfo=new objectInfo($HTTP_POST_VARS);
			}
			?>
			<script language=javascript>
				   var action_types=new Array();
				   action_types[1]='location';
				   action_types[2]='option_add';
				   action_types[3]='option_update';
				   action_types[4]='option_delete';
					function validateForm(lfrm){
						var error_result="";
						if (lfrm.sub_action.value=='location'){
							if (lfrm.location_name.value=="")
								error_result="<?php echo tep_output_string(ERR_EMPTY_LOCATION_NAME); ?>";
						} else if (lfrm.sub_action.value=='option_add' || lfrm.sub_action.value=='option_update'){
							if (lfrm.edit_equipment.value=="")
								error_result="<?php echo tep_output_string(ERR_EMPTY_EQUIPMENT_NAME); ?>";
						}
						if (error_result!=""){
							alert(error_result);
							return false;
						}
						return true;
					}
					function setFormAction(lvalue){
						document.location_detail.sub_action.value=action_types[lvalue];
						if (validateForm(document.location_detail))
							document.location_detail.submit();
					}
					function show_equipment(){
						var index=document.location_detail.location_equipment.selectedIndex;
						if (index>=0) 
							document.location_detail.edit_equipment.value=document.location_detail.location_equipment.options[index].value;
						else
							document.location_detail.edit_equipment.value='';
					}
			</script>
			<tr>
				<td>
				<table border="0" width="100%" cellspacing="0" cellpadding="2">
					<tr>
						<td class="pageHeading"><?php echo HEADING_NEW_TITLE ; ?></td>
					</tr>
				</table>
				</td>
			</tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '2', '10'); ?></td></tr>
			<tr>
				<td valign="top">
				<table border="0" cellspacing="2" cellpadding="4" class="formArea">
				<?php echo tep_draw_form('location_detail',FILENAME_EVENTS_LOCATIONS,'action=update_location','post');?>
					<tr valign="top">
						<td class="main"><?php echo TEXT_LOCATION_NAME;?></td>
						<td class="main"><?php echo tep_draw_input_field('location_name',$loInfo->location_name,'size=65');?></td>
					</tr>
					<tr valign="top">
						<td class="main"><?php echo TEXT_LOCATION_DETAILS; ?></td>
						<td class="main"><?php echo tep_draw_textarea_field('location_details','true','64','4',$loInfo->location_details);?></td>
					</tr>
					<tr valign="top">
						<td class="main"><?php echo TEXT_LOCATION_DIRECTIONS; ?></td>
						<td class="main"><?php echo tep_draw_textarea_field('location_travel_directions',true,'64','4',$loInfo->location_travel_directions); ?></td>
					</tr>
					<tr valign="top">
						<td class="main"><?php echo TEXT_LOCATION_EQUIPMENT; ?></td>
						<td class="main">
						<?php 
							echo tep_draw_pull_down_menu('location_equipment',$equipment_array,$loInfo->location_equipment,'onChange="show_equipment()"') . '&nbsp;'; 
							if (sizeof($equipment_array)>0) echo tep_image_button('button_delete.gif',IMAGE_DELETE_EQUIPMENT,'class="cursorHand" align="absmiddle" onClick="setFormAction(4)"');
							echo '<br><br>';
							echo tep_draw_input_field('edit_equipment',$loInfo->location_equipment) . '&nbsp;' . tep_image_button('button_insert.gif',IMAGE_INSERT_EQUIPMENT,'class="cursorHand" align="absmiddle" onClick="setFormAction(2)"') . '&nbsp;';
							if (sizeof($equipment_array)>0) echo tep_image_button('button_update.gif',IMAGE_UPDATE_EQUIPMENT,'class="cursorHand" align="absmiddle" onClick="setFormAction(3)"');
						?>
						<script langauge=javascript>show_equipment();</script>
						</td>
					</tr>
					<tr valign="top">
						<td class="main"><?php echo TEXT_LOCATION_CONTACT; ?></td>
						<td class="main"><?php echo tep_draw_input_field('location_contact_person',$loInfo->location_contact_person,'size=64'); ?></td>
					</tr>
					<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '2', '10'); ?></td></tr>
					<tr valign="top">
						<td class="main" colspan="2" align=right>
						<?php 
							echo '<input type=hidden name="sub_action" value="">';
							if (isset($loInfo->location_id) && ((int)$loInfo->location_id>0)) echo '<input type=hidden name=location_id value="' . $loInfo->location_id . '">';
							echo tep_image_button('button_update.gif',IMAGE_UPDATE_LOCATION,'"align="absmiddle" class="cursorHand" onClick="setFormAction(1)"');
							echo '&nbsp;<a href="' . tep_href_link(FILENAME_EVENTS_LOCATIONS) . '">' . tep_image_button('button_cancel.gif',IMAGE_CANCEL_LOCATION) . '</a>';
						?>
	    			</tr>
			       <?php echo '</form>'; ?>
	    	   </table>
		     </td>
		</tr>
<?php } else if ($action=="new_location_preview"){ // action
			$location_query=tep_db_query("SELECT location_id,location_name,location_equipment,location_details,location_travel_directions,location_contact_person from " .
											TABLE_EVENTS_LOCATIONS . " where location_id='" . $HTTP_GET_VARS['loID'] . "'");
			$location_result=tep_db_fetch_array($location_query);
			$loInfo=new objectInfo($location_result);
?>
			<tr>
				<td>
				<table border="0" width="100%" cellspacing="0" cellpadding="2">
					<tr>
						<td class="pageHeading"><?php echo $loInfo->location_name ; ?></td>
					</tr>
				</table>
				</td>
			</tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '2', '10'); ?></td></tr>
			<tr>
				<td valign="top">
				<table border="0" cellspacing="2" cellpadding="0">
					<tr>
						<td colspan="2" class="main"><?php echo $loInfo->location_details;?></td>
					</tr>
					<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '2', '5'); ?></td></tr>
					<tr>
						<td width="150" class="main"><b><?php echo TEXT_LOCATION_DIRECTIONS;?></b></td>
						<td class="main"><?php echo $loInfo->location_travel_directions;?></td>
					</tr>
					<tr>
						<td class="main"><b><?php echo TEXT_LOCATION_EQUIPMENT;?></b></td>
						<td class="main"><?php echo $loInfo->location_equipment;?></td>
					</tr>
					<tr>
						<td class="main"><b><?php echo TEXT_LOCATION_CONTACT;?></b></td>
						<td class="main"><?php echo $loInfo->location_contact_person;?></td>
					</tr>
					<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '2', '5'); ?></td></tr>					
					<tr>
						<td class="main" colspan="2"><?php echo '<a href="' . tep_href_link(FILENAME_EVENTS_LOCATIONS,'loID=' . $loInfo->location_id) . '">' . tep_image_button('button_back.gif',IMAGE_BACK) . '</a>';?></td>
					</tr>
<?php } else { // action?>
		<tr>
			<td>
			<table border="0" width="100%" cellspacing="0" cellpadding="2">
				<tr>
					<td class="pageHeading"><?php echo HEADING_TITLE ; ?></td>
				</tr>
			</table>
			</td>
		</tr>
		<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '2', '10'); ?></td></tr>
		<tr>
			<td valign="top">
			<table border="0" cellspacing="0" cellpadding="0" width="100%">
				<tr>
					<td valign="top">
					<table border="0" cellspacing="0" cellpadding="0" width="100%">
						<tr class="dataTableHeadingRow" valign="top">
							<td class="dataTableHeadingContent"><b><?php echo TABLE_HEADING_NAME; ?></b></td>
							<td class="dataTableHeadingContent"><b><?php echo TABLE_HEADING_EQUIPMENT; ?></b></td>
							<td class="dataTableHeadingContent"><b><?php echo TABLE_HEADING_CONTACT; ?></b></td>
						</tr>
						<?php
							$location_query=tep_db_query("select location_id,location_name,location_equipment,location_contact_person from " . TABLE_EVENTS_LOCATIONS . ' order by location_name');
							
							while ($locations = tep_db_fetch_array($location_query)) {
								$rows++;
		
								if ((!isset($HTTP_GET_VARS['loID']) || $HTTP_GET_VARS['loID'] == $locations['location_id']) && !isset($loInfo)){
									$loInfo=new objectInfo($locations);
								}
								if (isset($loInfo) && is_object($loInfo) && $loInfo->location_id==$locations["location_id"]){
									echo '              <tr height=15 id="defaultSelected" class="dataTableRowSelected" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\'' . tep_href_link(FILENAME_EVENTS_LOCATIONS, 'loID=' . $locations['location_id'] . '&action=new_location_preview&read=only') . '\'">' . "\n";
								} else {
									echo '              <tr height=15 class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\'' . tep_href_link(FILENAME_EVENTS_LOCATIONS,'loID=' . $locations['location_id']) . '\'">' . "\n";						
								}
						?>  
								  <td class='dataTableContent'><?php echo $locations['location_name']; ?></td>
								  <td class='dataTableContent'><?php echo $locations['location_equipment']; ?></td>
								  <td class='dataTableContent'><?php echo $locations['location_contact_person']; ?></td>
								</tr>
						<?php 
							}	 
							if ($rows<=0) {
						?>
								<tr><td class="smallText"><?php echo TEXT_EMPTY_LOCATION;?></td></tr>
						<?php 
							} 
						?>
								<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '2', '10'); ?></td></tr>
								<tr>
									<td colspan="3" class="main" valign=top align="right">
										<?php echo '<a href="' . tep_href_link(FILENAME_EVENTS_LOCATIONS,'action=new_location') . '">' . tep_image_button('button_new.gif',IMAGE_NEW_LOCATION) . '</a>'; ?>
									</td>
								</tr>
							</table>
							</td>
				<?php
					$heading=array();
					$contents=array();
					switch($action){
						case 'delete_location':
							if (isset($loInfo) && is_object($loInfo)){
								$child_query=tep_db_query("SELECT count(*) as total_count from " . TABLE_EVENTS . " where events_location='" . $loInfo->location_id . "'");
								$child_result=tep_db_fetch_array($child_query);
								$total_count=(int)$child_result['total_count'];

								$heading[] = array('text' => '<b>' . TEXT_INFO_HEADING_DELETE_LOCATION . '</b>');
								$contents = array('form' => tep_draw_form('delete_location', FILENAME_EVENTS_LOCATIONS, 'action=delete_location_confirm') . tep_draw_hidden_field('location_id', $loInfo->location_id));
								$contents[] = array('text' => TEXT_DELETE_LOCATION_INTRO);
								$contents[] = array('text' => '<br><b>' . $loInfo->location_name . '</b>');
								if ($total_count>0) $contents[]=array('text'=>'<br>' . ERROR_LOCATIONS_LINK_TO_EVENT);
								$contents[] = array('align' => 'center', 'text' => '<br>' . ($total_count<=0?tep_image_submit('button_delete.gif', IMAGE_DELETE_LOCATION):'') . ' <a href="' . tep_href_link(FILENAME_EVENTS_LOCATIONS,'loID=' . $loInfo->location_id) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL_LOCATION) . '</a>');
								break;
							}
							break;
						default:
							if ($rows>0){
								if (isset($loInfo) && is_object($loInfo)){
									$heading[] = array('text' => '<b>' . $loInfo->location_name . '</b>');
									$contents[] = array('align' => 'center', 'text' => '<a href="' . tep_href_link(FILENAME_EVENTS_LOCATIONS, 'loID=' . $loInfo->location_id . '&action=new_location') . '">' . tep_image_button('button_edit.gif', IMAGE_EDIT_LOCATION) . '</a> <a href="' . tep_href_link(FILENAME_EVENTS_LOCATIONS, 'loID=' . $loInfo->location_id . '&action=delete_location') . '">' . tep_image_button('button_delete.gif', IMAGE_DELETE_LOCATION) . '</a>');
								}
							} else {
							  $heading[] = array('text' => '<b>' . EMPTY_LOCATION . '</b>');
							  $contents[] = array('text' => TEXT_NO_LOCATIONS);
							}
							break;
					}
					if ( (tep_not_null($heading)) && (tep_not_null($contents)) ) {
					  echo '            <td width="25%" valign="top">' . "\n";
					  $box = new box;
					  echo $box->infoBox($heading, $contents);
					  echo '            </td>' . "\n";
					}
				?>
					</tr>
				</table>
				</td>
	      </tr>
<?php } ?>
    </table>
    </td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->
</tr></table>
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>