
<script type="text/javascript">
	function submitbutton(){
		document.cart.submit();
	}
</script>
<?php

if(!isset($_SESSION))
{
session_start();
}

function writeShoppingCart() {
	$cart = $_SESSION['cart'];
	if (!$cart) {
		return '<p>'.JText::_("COM_JEGROUPBUY_NO_ITEMS_IN_CART").'</p>';
	} else {
		// Parse the cart session variable
		$items = explode(',',$cart);
		$s = (count($items) > 1) ? 's':'';
		return '<p>'.sprintf(JText::_("COM_JEGROUPBUY_ITEMS_IN_CART"),count($items),$s).'</p>';
	}
}

function showCart($pid) {

	$db	= JFactory::getDBO();
	$cart = $_SESSION['cart'];

	$total=0;

	if ($cart) {
		$items = explode(',',$cart);
		$contents = array();

		foreach ($items as $item) {
			$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
		}

		$link = JURI::root()."components/com_jegroupbuy/assets/images/refresh.jpg";
		$update_link = JRoute::_('index.php?option=com_jegroupbuy&view=productdetails&task=addtoCart&action=update&tmpl=component&pid='.$pid, false);
		$output[] = '<form style=\'margin-bottom:25px;\' action="'.$update_link.'" method="post" name="cart" id="cart">';
		$output[] = '<table id=\'popup-tab\' width=\'100%\' style=\'border-collapse:separate;padding:3px;\' cellspacing=\'0\'>';
		$output[] = '<div id="update" style="text-align:right;margin-right:25px;display:block;"><a style=\'text-decoration:none;\' onclick=\'submitbutton();\' href=\'javascript:void(0);\'><img border=\'0\' src="'.$link.'" width=\'32\' height=\'32\' ></a></div>';
		//$output[] = '<div id="update" style="text-align:right;margin-right:25px;display:block;"><button type="submit" style="padding:3px;">Update cart</button></div>';
		$output[] = '<tr>';
		$output[] = '<th width=\'30%\' style=\'padding:5px;\'>'.JText::_("COM_JEGROUPBUY_TITLE").'</th>';
		$output[] = '<th width=\'10%\'>'.JText::_("COM_JEGROUPBUY_PRICE").'</th>';
		$output[] = '<th width=\'10%\'>'.JText::_("COM_JEGROUPBUY_QUANTITY").'</th>';
		$output[] = '<th width=\'10%\'>'.JText::_("COM_JEGROUPBUY_TOTAL_PRICE").'</th>';
		$output[] = '<th width=\'10%\'>'.JText::_("COM_JEGROUPBUY_REMOVE").'</th>';
		$output[] = '</tr>';
		$output[] = '<tr><td colspan=\'6\'><hr/></td></tr>';

		foreach ($contents as $id=>$qty) {
			$query		= $db->getQuery(true);
			$query->select('*');
			$query->from('#__jegroupbuy_product');
			$query->where('id ='.$id);
			$db->setQuery($query);
			$row		= $db->loadObject();

			$query1		= $db->getQuery(true);

			$query1->select('sum(quantity)');
			$query1->from('#__jegroupbuy_payment AS p');
			$query1->where('p.p_id = '.$id);
			$db->setQuery($query1);

			$number_of_bought		= $db->loadResult();

			if(empty($number_of_bought))
			{
				$number_of_bought = 0;
			}

			$output[] = '<tr style=\'background-color:#D0E8FA;\'>';
			$output[] = '<td style=\'padding:9px;\'>'.$row->title.'</td>';
			$output[] = '<td >'.$row->currencysymbol.' '.$row->deal_price.'</td>';
			$output[] = '<td ><input type="text" name="qty'.$id.'" value="'.$qty.'" size="1" style="width:20px;" maxlength="3" onblur="checkAvail(this.value,'.$id.','.$number_of_bought.','.$row->total_bought.')"/></td>';
			$output[] = '<td>'.$row->currencysymbol.' '.($row->deal_price * $qty).'</td>';
			$link = JRoute::_('index.php?option=com_jegroupbuy&view=productdetails&task=addtoCart&action=delete&tmpl=component&pid='.$id, false);
			$output[] = '<td ><a href="'.$link.'" class="r">Remove</a></td>';
			$total += $row->deal_price * $qty;
			$_SESSION['total']= $total;
			$output[] = '</tr>';
			$output[] = '<tr><td colspan=\'6\'><hr/></td></tr>';
		}
		$output[] = '</table>';
		$output[] = '<div style=\'text-align:right;margin:10px 0px 0px 0px;width:80%;\'>'.JText::_("COM_JEGROUPBUY_TOTAL").'<span style=\'font-weight:bold;\'>'.$row->currencysymbol." ".$total.'</span></div>';
		$output[] = '</form>';
	} else {
		$output[] = '<p>You shopping cart is empty.</p>';
	}
	return join('',$output);
}

abstract class Coupontemp{

	public static function couponTemplate($details,$sitename,$uname,$adname) {
		$db			= JFactory::getDBO();
		$user		= JFactory::getUser();
		$total		= 0;

		$output		= "";

		$split_val 	= explode(":",$details);
		$id 		= explode(",",$split_val[0]);
		$quan 		= explode(",",$split_val[1]);

		$output[] =  sprintf(JText::_("COM_JEGROUPBUY_HI"),$uname);

		$output[] =  sprintf(JText::_("COM_JEGROUPBUY_INTRO_TEXT"),$sitename);

		$output[] = '<table width=\'100%\' style=\'border-collapse:separate;padding:3px;\' cellspacing=\'0\'>';
		$output[] = '<tr>';
		$output[] = '<th width=\'30%\' style=\'padding:5px;\' align=\'left\'>Title</th>';
		$output[] = '<th width=\'10%\' align=\'left\'>Price</th>';
		$output[] = '<th width=\'10%\' align=\'left\'>Quantity</th>';
		$output[] = '<th width=\'10%\' align=\'left\'>Total</th>';
		$output[] = '<th width=\'10%\' align=\'left\'>Shopname</th>';
		$output[] = '<th width=\'10%\' align=\'left\'>Shopemail</th>';
		$output[] = '</tr>';
		$output[] = '<tr><td colspan=\'6\'><hr/></td></tr>';

		for($i=0,$j=$quan; $i<count($id); $i++)
		{
			$query		= $db->getQuery(true);

			$query->select('p.*,s.shop_name,s.email As storeemail');
			$query->from('#__jegroupbuy_product As p');
			$query->leftJoin('#__jegroupbuy_stores AS s  ON s.id = p.store_id');
			$query->where('p.id ='.$id[$i]);
			$db->setQuery($query);
			$row[$i]		= $db->loadObject();

			$output[] = '<tr style=\'background-color:#D0E8FA;\'>';
			$output[] = '<td style=\'padding:9px;\'>'.$row[$i]->title.'</td>';
			$output[] = '<td>'.$row[$i]->currencysymbol.' '.$row[$i]->deal_price.'</td>';
			$output[] = '<td><input type="text" name="qty'.$j[$i].'" value="'.$j[$i].'" size="3" maxlength="3" /></td>';
			$output[] = '<td>'.$row[$i]->currencysymbol.' '.($row[$i]->deal_price * $j[$i]).'</td>';
			$output[] = '<td>'.$row[$i]->shop_name.'</td>';
			$output[] = '<td>'.$row[$i]->storeemail.'</td>';
			$total += $row[$i]->deal_price * $j[$i];
			$_SESSION['total']= $total;
			$output[] = '</tr>';
			$output[] = '<tr><td colspan=\'6\'><hr/></td></tr>';
		}
		$output[] = '<tr>';
		$output[] = '<td colspan=\'6\' style=\'text-align:right;\'> Grand total:'.$row->currencysymbol." ".$total.'</td>';
		$output[] = '</tr>';
		$output[] = '</table><br/>';
		$output[] = sprintf(JText::_("COM_JEGROUPBUY_THANKS"),$adname);

		return join('',$output);
	}
}
?>
