<?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/functions/events_tree.php');
  require(DIR_WS_CLASSES . 'currencies.php');
  
  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
  $events_id = (isset($HTTP_GET_VARS['events_id']) ?(int)$HTTP_GET_VARS['events_id'] : 0);
  $select_id=(isset($HTTP_POST_VARS['select_id']) ?(int)$HTTP_POST_VARS['select_id'] : 0);
  $events_array=tep_get_events_array_single();
  $dsID=(isset($HTTP_GET_VARS['dsID'])?$HTTP_GET_VARS['dsID']:0);

	$event_found=true;
	if (sizeof($events_array)<=0){
		$event_found=false;
	} else {
		if ($select_id!=0 && $select_id!=$events_id)
			$events_id=$select_id;
		else if ($select_id==0 && $events_id==0)
			$events_id=$events_array[0]['id'];
	}  
	$event_query=tep_db_query("SELECT events_fees from " . TABLE_EVENTS ." where events_id='" . $events_id . "'");
	$event_found=false;
	if (tep_db_num_rows($event_query)>0){
		$event_result=tep_db_fetch_array($event_query);
	}
	
	switch ($action) {
		case 'update_discount':
			$sql_array=array(	'discount_start_date'=>tep_db_prepare_input($HTTP_POST_VARS['discount_start_date']),
								'discount_end_date'=>tep_db_prepare_input($HTTP_POST_VARS['discount_end_date']),
								'events_id'=>$events_id,
								'discount_value'=>tep_db_prepare_input($HTTP_POST_VARS['discount_value']),
								'discount_type'=>tep_db_prepare_input($HTTP_POST_VARS['discount_type'])
							);
			if (isset($HTTP_POST_VARS['discount_id'])){
				$update_id=$HTTP_POST_VARS['discount_id'];
				tep_db_perform(TABLE_EVENTS_DISCOUNTS,$sql_array,'update',"discount_id='" . $update_id . "'");
			} else {
				tep_db_perform(TABLE_EVENTS_DISCOUNTS,$sql_array);
				$update_id=tep_db_insert_id();
			}
        if (USE_CACHE == 'true') {
            tep_reset_cache_block('events_discounts');
        }
        tep_redirect(tep_href_link(FILENAME_EVENTS_DISCOUNTS, 'events_id=' . $events_id . '&dsID=' . $update_id));
        break;
   case 'delete_discount':
		tep_db_query("DELETE from " . TABLE_EVENTS_DISCOUNTS . " where discount_id='" . $dsID . "'");
		
		if (USE_CACHE == 'true') {
			tep_reset_cache_block('events_discounts');
		}
		
		tep_redirect(tep_href_link(FILENAME_EVENTS_DISCOUNTS,'events_id=' . $events_id));
		break;
 }
 $currencies=new currencies();
?>
<!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>
<script language="javascript" src="includes/event_select.js"></script>
<link rel="stylesheet" type="text/css" href="includes/javascript/spiffyCal/spiffyCal_v2_1.css">
<script language="JavaScript" src="includes/javascript/spiffyCal/spiffyCal_v2_1.js"></script>
<script language="JavaScript1.3" type="text/javascript">
<!--
function validateFormSettings( theForm ) {
    var error_result="";
	if (theForm.discount_start_date.value=="" || (isValidDate(theForm.discount_start_date.value)==false)) {
		error_result="<?php echo tep_output_string(ERR_INVALID_SDATE); ?>";
	} else if (isValidDate(theForm.discount_end_date.value)==false) {
		error_result="<?php echo tep_output_string(ERR_INVALID_EDATE); ?>";
    } else if (isNaN(parseInt(theForm.discount_value.value))==true){
        error_result="<?php echo tep_output_string(ERR_INVALID_VALUE); ?>";
	} else if (theForm.discount_type[0].checked==true && !(theForm.discount_value.value>=1 && theForm.discount_value.value<=<?php echo (int)$event_result['events_fees']; ?>)){
        error_result="<?php echo tep_output_string(sprintf(ERR_INVALID_AMOUNT,$event_result['events_fees'])); ?>";
	} else if (theForm.discount_type[1].checked==true && !(theForm.discount_value.value>=1 && theForm.discount_value.value<=100)){
        error_result="<?php echo tep_output_string(ERR_INVALID_RANGE); ?>";
	}
    
	if (error_result!=""){
		alert(error_result);
		return false;
	}
    return true;
}
//--></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<div id="spiffycalendar" class="text"></div>
<!-- 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">
		<?php if ($event_found==true) { ?>
			<tr>
				<td class="pageHeading"><?php echo TEXT_HEADING_EVENT ?></td>
			</tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
			<tr>
				<td><?php echo tep_draw_form('select_event',FILENAME_EVENTS_DISCOUNTS); 
							$selected_name="";
				?>
				<table border="0" cellspacing="0" cellpadding="2">
					<div id="event_list" style="position:absolute;left:242px;top:130px;display:none;width:400px">
						<?php echo tep_draw_event_select_menu('select_id',$events_array,$events_id,'size=10 onClick="javascript:setListboxText(0);" class="customerSelect"');?>
					</div>
					<tr height=20>
						<td class="main" width="100"><?php echo TEXT_SELECT_EVENT;?></td>
						<td class="main"><?php echo tep_draw_input_field('select_text',$selected_name,'onFocus="javascript:toggleList(1);" onBlur="javascript:toggleList(0);" style="width:400px"');?></td>
						<td class="main" valign=center><?php echo tep_image_submit('button_select.gif',IMAGE_SELECT,'align=absmiddle size=');?></td>
					</tr>
				</table>
				</form>
				<script language="JavaScript">
					if(na6) document.select_event.select_text.addEventListener("keyup",selectList,true);
					if(iex) document.select_event.select_text.onkeyup=selectList;
					if(na6) document.select_event.select_text.addEventListener("keydown",scrollList,true);
					if(iex) document.select_event.select_text.onkeydown=scrollList;
				</script>
				</td>
			</tr>

			<tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','20'); ?></td></tr>

			<tr>
				<td class="pageHeading"><?php echo sprintf(TEXT_HEADING_TITLE,tep_get_events_name($events_id)); ?></td>  
			</tr>

			<tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','10'); ?></td></tr>

			<tr>
				<td valign="top">
				<table border="0" width="100%" cellspacing="0" cellpadding="0" valign="top">
					<tr>
						<td valign="top">
						<table border="0" align="left" valign="top" cellspacing="0" cellpadding="2">
							<tr class="dataTableHeadingRow">
								<td class="dataTableHeadingContent" width=100><?php echo TEXT_DISCOUNT_SDATE ;?></td>
								<td class="dataTableHeadingContent" width=100><?php echo TEXT_DISCOUNT_EDATE ;?></td>
								<td class="dataTableHeadingContent"><?php echo TEXT_DISCOUNT_VALUE ;?></td>
								<td class="dataTableHeadingContent" width=100><?php echo TEXT_DISCOUNT_TYPE; ?></td>
							</tr>
							<?php
							$result_query=tep_db_query("SELECT * from " . TABLE_EVENTS_DISCOUNTS . " where events_id='" . $events_id ."' order by discount_start_date");
							$cnt=0;
							while($result=tep_db_fetch_array($result_query)){
								$cnt++;
								if ($result['discount_id'] == $dsID || $dsID==0) {
									$dsID=$result['discount_id'];
									$selected=true;
									echo '              <tr id="defaultSelected" class="dataTableRowSelected" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="">';
								} else {
									echo '              <tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onClick="document.location.href=\'' . tep_href_link(FILENAME_EVENTS_DISCOUNTS,'events_id=' . $events_id . '&dsID=' . $result['discount_id']) . '\'">';
								}
							?>
							<td class="dataTableContent"><?php echo format_date($result['discount_start_date']); ?></td>
							<td class="dataTableContent"><?php echo format_date($result['discount_end_date']); ?></td>
							<td class="dataTableContent"><?php echo $result['discount_value']; ?></td>
							<td class="dataTableContent"><?php echo $result['discount_type']=='A'?'$':'%';?></td>
						</tr>
						<?php }
						 if ($cnt<=0) echo '<tr><td class="smallText">' . EMPTY_DISCOUNT . '</td></tr>'; 
						?>
						<tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','10'); ?></td></tr>
						<tr><td class="main" align=right colspan="4">
						<?php 
							if ($dsID>0 && $cnt>0 && $selected==true) {
								echo '<a href="' . tep_href_link(FILENAME_EVENTS_DISCOUNTS,'action=edit&events_id=' . $events_id . '&dsID=' . $dsID) . '">' . tep_image_button('button_edit.gif',IMAGE_EDIT_DISCOUNT) . '</a>&nbsp;';
								echo '<a href="' . tep_href_link(FILENAME_EVENTS_DISCOUNTS,'action=delete_discount&events_id=' . $events_id . '&dsID=' . $dsID) . '">' . tep_image_button('button_delete.gif',IMAGE_DELETE_DISCOUNT) . '</a>';
							} 
					    ?>
						</td>
					</tr>
				</table>
				</td>
			</tr>
			<?php
			
			$discount_details=array('discount_start_date'=>'',
									'discount_end_date'=>'',
									'discount_value'=>0,
									'discount_type'=>'A');
			$discount=new objectInfo($discount_details);			  
			if ($dsID>0 && $action=='edit'){
				$discount_query=tep_db_query("SELECT * from " . TABLE_EVENTS_DISCOUNTS . " where discount_id='" . $dsID . "'");
				if ($discount_result=tep_db_fetch_array($discount_query)){
					$discount=new objectInfo($discount_result);
				}
			}
			
			?>  
			<script language="javascript">
				var dateStart = new ctlSpiffyCalendarBox("dateStart", "new_discount", "discount_start_date","btnDate2","<?php echo $discount->discount_start_date; ?>",scBTNMODE_CUSTOMBLUE);
				<?php if ($session_type=='' || $session_type=='R') {?>
					var dateEnd = new ctlSpiffyCalendarBox("dateEnd", "new_discount", "discount_end_date","btnDate3","<?php echo $discount->discount_end_date; ?>",scBTNMODE_CUSTOMBLUE);
				<?php }   
				?>  
			</script>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','25'); ?></td></tr>    
			<tr>
				<td class="pageHeading"><?php echo TEXT_HEADING_NEW_TITLE; ?></td>  
			</tr>
			<tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','15'); ?></td></tr>
			<tr>
				<td>
					<table border="0" cellspacing="2" cellpadding="2" class="formArea">
						<?php echo tep_draw_form('new_discount',FILENAME_EVENTS_DISCOUNTS,'events_id=' . $events_id . '&action=update_discount','post','onSubmit="return validateFormSettings(this)"'); ?>
						<tr>
						<td class="main" width=100><?php echo TEXT_DISCOUNT_SDATE; ?><br><small>(YYYY-MM-DD)</small></td>
						<td class="main"><script language="javascript">dateStart.writeControl(); dateStart.dateFormat="yyyy-MM-dd";</script></td>
						</tr>
						<tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','10'); ?></td></tr>
						<tr>
							<td class="main" width=100><?php echo TEXT_DISCOUNT_EDATE; ?><br><small>(YYYY-MM-DD)</small></td>
							<td class="main"><script language="javascript">dateEnd.writeControl(); dateEnd.dateFormat="yyyy-MM-dd";</script></td>
						</tr>
						<tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','10'); ?></td></tr>
						<tr>
							<td class="main" width=100><?php echo TEXT_DISCOUNT_VALUE; ?></td>
							<td class="main"><?php echo tep_draw_input_field('discount_value',$discount->discount_value,'size=10 maxlength=10'); ?></td>
						</tr>
		
						<tr><td><?php echo tep_draw_separator('pixel_trans.gif','1','10'); ?></td></tr>
						
						<tr valign="top">
							<td class="main" valign=center><?php echo TEXT_DISCOUNT_TYPE; ?></td>
							<td class="main" valign=center>
							<?php 
								echo tep_draw_radio_field('discount_type','A',($discount->discount_type=='A'?true:false)) . '&nbsp;' . TEXT_TYPE_AMOUNT . '&nbsp;';
								echo tep_draw_radio_field('discount_type','P',($discount->discount_type=='P'?true:false)) . '&nbsp;' . TEXT_TYPE_PERCENTAGE . '&nbsp;';
							?>
							</td>
						</tr>
						<tr>
							<td  colspan="2" class="main" align=right>
							<?php 
							if ($action=='edit'){
								echo tep_draw_hidden_field('discount_id',$dsID);
								echo tep_image_submit('button_update.gif',IMAGE_UPDATE_DISCOUNT) . '&nbsp;';
								echo '<a href="' . tep_href_link(FILENAME_EVENTS_DISCOUNTS,'events_id=' . $events_id . '&dsID=' . $dsID) . '">' . tep_image_button('button_cancel.gif',IMAGE_CANCEL) . '</a>';
							} else {
								echo tep_image_submit('button_insert.gif',IMAGE_ADD_DISCOUNT);
							}
							?>
						   </td>
						<tr>
					</form>
				</table>
				</td>
			</tr>	
	</table>
	</td>
</tr>
<?php } else { ?>
   <tr><td class="pageHeading"><?php echo TEXT_HEADING_EMPTY_EVENTS ;?></td></tr>
<?php } ?>
   </table>
<!-- body_text_eof //-->
</tr></table>
<!-- body_eof //-->
</td></table></tr>
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>