<?php 
/*
	Freeway eCommerce
	http://www.openfreeway.org
	Copyright (c) 2007 ZacWare
	Released under the GNU General Public License 
*/	
// Check to ensure this file is included in Freeway!
defined('_FEXEC') or die();
		//October 2012 - highlight the oversubscribed shows (Could be useful to someone?
		// if ($FSESSION->is_registered('customer_id')){
		// tep_check_ticket_limit((int)$FSESSION->customer_id,0);
				// $oversubscribed=array();
				// if($_SESSION['oversubscribed']){//session created in inc/functions/general.php tep_check_ticket_limit()
				// $oversubscribed=array_keys($_SESSION['oversubscribed']);//has all the Date-ids in an array
				// $FSESSION->remove('oversubscribed');//kill the session
				// }
				// }
		//July 2013 cumulative totals
		include_once('includes/functions/ga_tickets.php');
		ga_cart_check();
		ga_kill_sessions();
    	//July 2013 end
	
	//echo tep_draw_form('cart_quantity', tep_href_link(FILENAME_SHOPPING_CART, 'event_action=update_cart'));
	echo tep_draw_form('cart_quantity', tep_href_link(FILENAME_SHOPPING_CART, 'action=update_product'));
	
	?>
	
	<div class="section-header">
    <h2><?php echo HEADING_TITLE; ?></h2>
	</div>
<?php
	if ($messageStack->size('shopping_cart') > 0) 
	{
?>
      <div><?php echo $messageStack->output('shopping_cart'); ?></div>
<?php
	}
	if ($cart->count_contents() > 0) 
	{
		if($quantity_error !==TEXT_GA_CART_HEADING)
		{
					echo "<div class=\"alert alert-warning\">";		
					echo $quantity_error;
					echo "</div>";
		}
		
		?>
    <table class="table table-striped table-condensed">
      <tbody>
	  <tr>
	  <td class="productListing-heading"><?php echo TABLE_HEADING_REMOVE; ?></td>
	  <td class="productListing-heading"><?php echo TABLE_HEADING_PRODUCTS; ?></td>
	  <td class="productListing-heading heading-products"></td>
	  <td class="productListing-heading"><?php echo TABLE_HEADING_QUANTITY; ?></td>
	  <td class="productListing-heading" align="right"><?php echo TABLE_HEADING_TOTAL; ?></td>
	  </tr>

	<?php
		$any_out_of_stock = 0;
		$products = $cart->get_products();
		//print_r($products);
		$switch_continue_shopping = true;
		
		$show_shipping=false; 	
		
		###########################################################
		//$id=tep_db_input($products[$i]['id']);
		require_once(DIR_WS_INCLUDES . 'functions/categories_lookup.php');
	
    $products_name = NULL;
    for ($i=0, $n=sizeof($products); $i<$n; $i++) 
	{  
			
		$id=tep_db_input($products[$i]['id']);
        list($heading_name, $heading_venue,  $heading_date, $heading_time) = categories_lookup();
		$products_name .= '<tr id="p'.$products[$i]['id'].'">';
		
		if(basename($PHP_SELF) == 'shopping_cart.php')
		{ 
			// use delete checkbox?
			//$products_name .= '<td>' . tep_draw_checkbox_field('cart_delete[]', $products[$i]['sh_id']) . tep_draw_hidden_field('products_type[]', $products[$i]['element_type']) . '</td>';
			
			//refresh
			$products_name .= '<td>' . tep_draw_button('', 'fa fa-refresh', NULL, NULL, NULL, 'btn-info btn-xs') . ' ' . tep_draw_button('', 'fa fa-times', tep_href_link('shopping_cart.php', 'products_id=' . $products[$i]['sh_id'] . '&action=remove_product'), NULL, NULL, 'btn-danger btn-xs');
			//image + name
			$products_name.='<td class="heading-products-image" style="white-space: nowrap">
			
			<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $products[$i]['id']) . '">' . tep_product_small_image($products[$i]['image'], htmlspecialchars($products[$i]['title'])) . '</a>
			
			</td>' .
			'    <td class="cart-text"><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $products[$i]['id']) . '">
			<strong>' . $products[$i]['name'] . '</strong></a><br>' . 
			$heading_name .'<br>' . 
			$heading_venue . ' ' . $heading_date .' ' . $heading_time .'';
			
			if ($products[$i]['product_type']!='G')
			{
				if (STOCK_CHECK == 'true') 
				{
					$stock_check = tep_check_stock($products[$i]['id'], $products[$i]['quantity']);
					if (tep_not_null($stock_check)) 
					{
						$any_out_of_stock = 1;
						$products_name .= $stock_check;
					}		
				}

				$products_name.=(isset($products[$i]['discount_whole_text'])?'<br>' .$products[$i]['discount_whole_text']:'');
			}
				##################### //added Aug 2012 sakwoya@sakwoya.co.uk (Graeme Tyson)
			elseif ($products[$i]['product_type']=='G')
			{
				if (STOCK_CHECK == 'true') 
				{
					$stock_check_ga = tep_check_stock_ga($products[$i]['id'], $products[$i]['quantity']);
					if (tep_not_null($stock_check_ga)) 
					{
						$any_out_of_stock_ga = 1;
						$products_name .= $stock_check_ga;
					}		
				}
			if($FSESSION->is_registered('ga_quantity')&& $FSESSION->is_registered('ga_prod_id'))
			{//this is only true if sent here from checkout_process or anywhere else that the master quantity is checked
				if($_SESSION['ga_prod_id'] == $products[$i]['id'])
				{
					$products_name .='<span class="markProductOutOfStock">'. STOCK_CHANGE_LEFT . $_SESSION['ga_quantity'] .STOCK_CHANGE_RIGHT.'</span>' ;
					
				}	 
			}


				$products_name.=(isset($products[$i]['discount_whole_text'])?'<br>' .$products[$i]['discount_whole_text']:'');
			}

		}else
		{
		$products_name.=  '    <td class="cart-text" style="vertical-align: text-top;"><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $products[$i]['id']) . '"><b>' . $products[$i]['name'] . '</b></a><br>' . 
		$heading_name .'<br>' . 
		$heading_venue . ' ' . $heading_date .' ' . $heading_time .'';
        }
		
		//stock check 
		if (STOCK_CHECK == 'true') 
		{
		$stock_check = tep_check_stock($products[$i]['id'], $products[$i]['quantity']);
			if (tep_not_null($stock_check)) 
			{
			  $any_out_of_stock = 1;
			  $products_name .= $stock_check;
			}
		}
		
		//shipping
		if($products[$i]['weight']>0)
		$show_shipping=true;
		//pricebreaks
		if ($is_price_breaks_result["products_price_break"]=='Y')
		$input_text=tep_draw_pull_down_menu('cart_quantity[]', $products[$i]["price_breaks"],$products[$i]['quantity']);
		
		//quantity
		//P or SKU 6
		if ($products[$i]['product_type']=='P' || $products[$i]['sku'] == 6)
		{
		$input_text=tep_draw_hidden_field('cart_quantity[]', '1').'1';				
		}else
		//GA
		{
		$input_text=tep_draw_input_field('cart_quantity[]', $products[$i]['quantity'], ' size="4" style="text-align:center;"');
		}	 
	
		$products_name .= '<td class="cart-text">' . $input_text . tep_draw_hidden_field('products_id[]', $products[$i]['sh_id']);
		$products_name .= '</td>';
		//price
		$products_name .= '  <td class="cart-text" align="right" valign="top"><strong>' . $currencies->display_price($products[$i]['final_price'], tep_get_tax_rate($products[$i]['tax_class_id']), $products[$i]['quantity']) . '</strong></td>' . '</tr>';
	
    }//end for
    echo $products_name;
	
?>
    </tbody>
    </table>
		<?php
			if(basename($PHP_SELF) == 'shopping_cart.php')
			{ 
		?>
			<div style="text-align:right"><strong>
		<?php  
			echo SUB_TITLE_SUB_TOTAL;
			}
			else
			{
		?>
			
		<?php
				echo SUB_TITLE_TOTAL;
			}
			
			
			// Discount Start
			global $FSESSION;
				$fp1=0;
			
			
			if (ALLOW_GUEST_TO_SEE_PRICES=='true' && !($FSESSION->is_registered('customer_id'))) 
			{
				 echo '<span id="cart_subtotal">&nbsp;'.$currencies->format($cart->show_total()).'</span>';
			}
			elseif ($FSESSION->is_registered('customer_id')) 
			{
				//$fp1=$currencies->format($cart->show_total()-$fp1);
				$fp1=$currencies->format($cart->show_total());
				echo '<span id="cart_subtotal">&nbsp;'.$fp1 . '&nbsp;&nbsp;</span>';
			}
			else {
				echo PRICES_LOGGED_IN_TEXT;
			}
			//Discount end
			
		?></strong></div>
		
<?php
		if ($any_out_of_stock == 1) 
		{
			if (STOCK_ALLOW_CHECKOUT == 'true') 
			{
	?>
	 
			<div class="stockWarning"><br><?php echo OUT_OF_STOCK_CAN_CHECKOUT; ?></div>
	<?php
			}else 
			{
	?>
			<div class="stockWarning"><br><?php echo OUT_OF_STOCK_CANT_CHECKOUT; ?></div>
	<?php
			}
		}
?>
<?php //added Aug 2012 sakwoya@sakwoya.co.uk (Graeme Tyson) ?>
<?php //check for the individual GA tickets stock
		if ($any_out_of_stock_ga == 1) 
		{
			if (STOCK_ALLOW_CHECKOUT == 'true') 
			{
	?>
		
			<div class="stockWarning"><br><?php echo OUT_OF_STOCK_CAN_CHECKOUT_GA; ?></div>
		  
	<?php
			} else 
			{
	?>
			<div class="stockWarning"><br><?php echo OUT_OF_STOCK_CANT_CHECKOUT_GA; ?></div>
	<?php
			}
		}
// end individual GA checks
// Now check the master quantity 
		if($FSESSION->is_registered('ga_quantity'))
		{//this is only true if sent here from checkout_process or anywhere else that the master quantity is checked
		
		?>
		<div class="stockWarning"><br><?php echo OUT_OF_STOCK_CANT_CHECKOUT_GA_MASTER; ?></div>
		<?php	   
		$FSESSION->remove('ga_quantity');
		$FSESSION->remove('ga_prod_id');
		}//end of $FSESSION('ga_quantity')
?>
		
			<table class="table">
              <tr>
                <td>
				<?php //if(basename($PHP_SELF) == 'shopping_cart.php') echo tep_template_image_submit('button_update_cart.gif', IMAGE_BUTTON_UPDATE_CART); ?>
				</td>
				<?php 
					$back = sizeof($navigation->path)-2;
				?>
                <td>
				<?php  if(basename($PHP_SELF) == 'shopping_cart.php') 
				{
					//echo '<a class="pull-right" href="' . tep_href_link(FILENAME_DEFAULT,$FSESSION->prev_category_viewed) . '">' . tep_template_image_button_basic('button_continue_shopping.gif', IMAGE_BUTTON_CONTINUE_SHOPPING) . '</a>';
					echo '<a class="pull-right" href="' . tep_href_link(FILENAME_FEATURED_CATEGORIES,'') . '">' . tep_template_image_button_basic('button_continue_shopping.gif', IMAGE_BUTTON_CONTINUE_SHOPPING) . '</a>';
				}
				?>
				</td>
                <td align="right" class="main">
			<?php //box office refund
			if($_SESSION['customer_country_id']==999 && isset($_SESSION['box_office_refund'] ))
			{
			?>
                <script type="text/javascript">
				<!--
				function confirmation() {
					var answer = confirm("<?php echo BO_REFUND; ?>")
					if (answer){
						return true
					}
					else{
						return false
					}
				}
				//-->
				</script>
            	<div id="btnCheckOut" style="float:right;padding:2px 1px 10px 1px">
				<?php echo '<a onclick="return confirmation()" title="' . IMAGE_BUTTON_REFUND . '" href="' . tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL') . '">' . tep_template_image_button('button_checkout.gif', IMAGE_BUTTON_REFUND) . '</a>'; ?>
			</div>
            <?php 
			} //bor
			elseif($_SESSION['customer_country_id']==999 && isset($_SESSION['box_office_reservation'] ))
			{?>
                <script type="text/javascript">
				<!--
				function confirmation() {
					var answer = confirm("<?php echo BOR_CONFIRM_RESERVATIONS; ?>")
					if (answer){
						return true
					}
					else{
						return false
					}
				}
				//-->
				</script>
            	<div id="btnCheckOut" style="float:right;padding:2px 1px 10px 1px">
				<?php echo '<a onclick="return confirmation()" title="' . IMAGE_BUTTON_REFUND . '" href="' . tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL') . '">' . tep_template_image_button('button_checkout.gif', IMAGE_BUTTON_REFUND) . '</a>'; ?>
			</div>
            <?php 
			}else
			{
			?>		
				
				
				<?php echo '<a href="' . tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL') . '">' . tep_template_image_button_basic('button_checkout.gif', IMAGE_BUTTON_CHECKOUT) . '</a>'; 
				}
				?>
                </td>
                </tr>
            </table></form>
<?php
	} else 
	{
?>
<?php echo TEXT_CART_EMPTY; ?>
		<table class="table">
          <tr>
			<td align="right" class="">
			<?php echo '<a href="' . tep_href_link(FILENAME_DEFAULT) . '">' . tep_template_image_button_basic('button_continue.gif', IMAGE_BUTTON_CONTINUE) . '</a>'; ?></td>
          </tr>
        </table></form>
<?php
  }
?>