<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<?php
if(!isset($_SESSION))
{
session_start();
}
?>

<script type="text/javascript">
	function closepage(url){
		var selfUrl = unescape(url);
		self.close();
		parent.window.location.replace(selfUrl);

		setTimeout(function(){parent.window.location.href = selfUrl},1000);
	}
</script>

<link rel="stylesheet" type="text/css" href="<?php echo JURI::root().'components/com_jegroupbuy/assets/css/style.css'?>">

<?php
// Include functions
require_once( JPATH_COMPONENT . DS . 'helpers' . DS . 'functions.inc.php' );

// Process actions
$action = $_GET['action'];
if(isset($_SESSION['cart']))
{
	$cart = $_SESSION['cart'];
}else{
	$cart = "";
}
$id = $_GET['pid']; 

switch ($action) {
	case 'add':
		if ($cart) {
			$cart .= ','.$_GET['pid'];
		} else {
			$cart = $_GET['pid'];
		}
		break;
	case 'delete':
		if ($cart) {
			$items = explode(',',$cart);
			$newcart = '';
			foreach ($items as $item) {
				if ($_GET['pid'] != $item) {
					if ($newcart != '') {
						$newcart .= ','.$item;
					} else {
						$newcart = $item;
					}
				}
			}
			$cart = $newcart;
		}
		break;
	case 'update':
	if ($cart) {
		$newcart = '';

		foreach ($_POST as $key=>$value) {
				if (stristr($key,'qty')) {
				$id = str_replace('qty','',$key);
				$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
				$newcart = '';
				foreach ($items as $item) {
					if ($id != $item) {
						if ($newcart != '') {
							$newcart .= ','.$item;
						} else {
							$newcart = $item;
						}
					}
				}
				for ($i=1;$i<=$value;$i++) {
					if ($newcart != '') {
						$newcart .= ','.$id;
					} else {
						$newcart = $id;
					}
				}
			}
		}
	}
	$cart = $newcart;
	break;
}
$_SESSION['cart']	= $cart;
$payment_url 		= 'index.php?option=com_jegroupbuy&view=productdetails&task=paymentDetails&pid='.$id;
$continue_url		= "index.php?option=com_jegroupbuy&view=jegroupbuy";
$img_left_link = JURI::root()."components/com_jegroupbuy/assets/images/continue_shopping.gif";
$img_right_link = JURI::root()."components/com_jegroupbuy/assets/images/proceed.jpg";
?>

<div id="shoppingcart">
	<h1>Your Shopping Cart</h1>
	<?php echo writeShoppingCart(); ?>
</div>

<div id="contents">
	<h1>Please check quantities...</h1>
	<?php echo showCart($id);
	if(!empty($_SESSION['cart'])){
	 ?>
	<a id="continue-shop" style="display:'';" href="<?php echo $continue_url; ?>" onclick="javascript:closepage('<?php echo $continue_url; ?>');"><img src="<?php echo $img_left_link; ?>" border="0"></a>
	<a id="proceed-to-checkout" style="display:'';" href="<?php echo $payment_url; ?>" onclick="javascript:closepage('<?php echo $payment_url; ?>');"><img src="<?php echo $img_right_link; ?>" border="0"></a>
	<?php } ?>
</div>
