<?php
ob_start();
session_start();
/******************INCLUDES**********************/
include('connection/connection.php');
include_once('index.fns.php');
include_once('lib/class.user.php');
include_once('lib/class.report_handler.php');
include_once('lib/class.validate.php');
include_once 'admin_end/config.php';
include_once 'admin_end/includes/mysql.inc.php';

$frontEnd= new frontEnd;
$user= new user;
/************************************************/

$action = isset($_POST['action'])?$_POST['action']:NULL;
$id = $_REQUEST['id'];
$level= $_REQUEST['level'];
$parent= $_REQUEST['parent'];


// Number of entries per page
$numentries=50;

// Get form data
if(!isset($_GET['page'])) {
	$page=0;
} else {
	$page=$_GET['page'];
}
if(!isset($_GET['sort'])) {
	$sort='name';
} else {
	$sort=$_GET['sort'];
}
if(!isset($_GET['order'])) {
	$order='ASC';
} else {
	$order=$_GET['order'];
}

// Set up vars, all default order ASC
$orderAddress='ASC';
$orderName='ASC';
$orderPhone='ASC';
$orderFax='ASC';
$orderLocation='ASC';
$orderLsent='ASC';
$orderList='ASC';
$orderWebsiteAddress='ASC';
$orderEmailAddress1='ASC';

// Change current sorted group order
switch($sort) {
	case 'Location':
		if($order=='ASC') {
			$orderLocation='DESC';
		} else {
			$orderLocation='ASC';
		}
		break;
	case 'name':
		if($order=='ASC') {
			$orderName='DESC';
		} else {
			$orderName='ASC';
		}
		break;
	case 'PhoneNo1':
		if($order=='ASC') {
			$orderPhone='DESC';
		} else {
			$orderPhone='ASC';
		}
		break;
	case 'WebsiteAddress':
		if($order=='ASC') {
			$orderWebsiteAddress='DESC';
		} else {
			$orderWebsiteAddress='ASC';
		}
		break;
	case 'EmailAddress1':
		if($order=='ASC') {
			$orderEmailAddress1='DESC';
		} else {
			$orderEmailAddress1='ASC';
		}
		break;
	case 'listid':
		if($order=='ASC') {
			$orderList='DESC';
		} else {
			$orderList='ASC';
		}
		break;
}


	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link rel="stylesheet" href="css/style.css" />
</head>

<body>
<? 
$query='SELECT * FROM companies WHERE type="A" and category_id='.$id.' ORDER BY name';
$result=mysqli_query($db, $query) or die('Unable to retreive subscribers');

// Navigation menu
$d=0;
$f=0;
$g=1+$d/$numentries;
$num_rows=mysqli_num_rows($result);
$currentpage=($page/$numentries)+1;
$prev=$page-$numentries;
$next=$page+$numentries;
$nav='';
if($page>=$numentries) {
	$nav.='<a href="' . $self . '?page=' . $prev . '&sort=' . $sort . '&order=' . $order . '&id='.$id.'&level='.$level.'"><<</a>&nbsp;';
} else {
	$nav.='<<&nbsp;';
}
while($row=mysqli_fetch_array($result)) {
	if($f>=$page-3*$numentries && $f<=$page+7*$numentries) {
		if($f%$numentries==0 && $currentpage != $g) {
			$nav.='<a href="' . $self . '?page=' . $d . '&sort=' . $sort . '&order=' . $order . '&id='.$id.'&level='.$level.'">' . $g . '</a> ';
		}
		else if($currentpage==$g) {
			$nav.=$currentpage . ' ';
		}
	}
	$d=$d+1;
	$g=1+$d/$numentries;
	$f++;
}  
if($page<=$num_rows-$numentries) {
	$nav.='<a href="' . $self . '?page=' . $next . '&sort=' . $sort . '&order=' . $order . '&id='.$id.'&level='.$level.'">>></a>&nbsp;';
} else {
	$nav.='>>&nbsp;';
}

$query='SELECT * FROM companies WHERE type="A" and category_id='.$id.' ORDER BY ' . $sort . ' ' . $order . ' limit ' . $page . ', ' . $numentries;

$result=mysqli_query($db, $query) or die('Could not get subscribers');
$table = "level".$level."_categories";
$qz1 = mysql_query("select * from  level1_categories WHERE id=".$id);
$row1=mysql_fetch_array($qz1);
echo '<br><h2>'.$row1['category_name'].'</h2>';

echo '<table border="0" cellpadding="0" cellspacing="0" style="font-size:11px;"><tr style=" background-color:#aaaaaa; color:#ffffff; height:22px;"><td width="120"><a href="#" style="color:#ffffff;font-size:11px;">Logo</a></td><td width="160"><a href="' . $self . '?page=' . $page . '&sort=name&order=' . $orderName . '&id='.$id.'&level='.$level.'" style="color:#ffffff;font-size:11px;">Name</a></td><td width="130"><a href="' . $self . '?page=' . $page . '&sort=PhoneNo1&order=' . $orderPhone . '&id='.$id.'&level='.$level.'" style="color:#ffffff;font-size:11px;">Phone</a></td><td width="165"><a href="' . $self . '?page=' . $page . '&sort=EmailAddress1&order=' . $orderEmailAddress1 . '&id='.$id.'&level='.$level.'" style="color:#ffffff;font-size:11px;">Email</a></td><td width="160">Website</td></tr>';
$j=0;
$curdate=date("Y-m-d");
while($row=mysqli_fetch_array($result)) 
{

if($row['ExpiryDate']=='0000-00-00')
	{
	$exp_date='';
	}
	else
	{
	$exp_date=$row['ExpiryDate'];
	}	
		
if($exp_date>=$curdate or $exp_date=='')
{
	if($j%2==0) $style='class="grey"'; else $style='class="white"';
	echo '<tr class="mainrow" style="height:40px;">
	<td '.$style.'>&nbsp;';
	if($row['type'] == 'C') 
	$url = 'detailC.php?company_id='.$row['company_id'].'&cat_id='.$id.'&level='.$level.'&parent='.$parent;
	else if($row['type'] == 'B') 
	$url = 'detailB.php?company_id='.$row['company_id'].'&cat_id='.$id.'&level='.$level.'&parent='.$parent;
	else if($row['type'] == 'D' or $row['type'] == 'E') 
	$url = 'detailD.php?company_id='.$row['company_id'].'&cat_id='.$id.'&level='.$level.'&parent='.$parent;
	else if($row['type'] == 'A') 
	$url = 'detailA.php?company_id='.$row['company_id'].'&cat_id='.$id.'&level='.$level.'&parent='.$parent;
	else if($row['type'] == '1') 
	$url = 'detail.php?company_id='.$row['company_id'].'&cat_id='.$id.'&level='.$level.'&parent='.$parent;

	if($row['CompanyLogo'] != '')
	echo '<a href="'.$url.'" target="_parent"><img src="images/companyImages/logo/'.$row['CompanyLogo'].'" width="100" class="img" border="0"></a>';
	
	echo '</td><td '.$style.'>&nbsp;<a href="'.$url.'" style="font-weight:normal;font-size:11px;" target="_parent">' . $row['name'] . '</a></td><td '.$style.' style="font-size:11px;">&nbsp;' . $row['PhoneNo1'] . '</td><td '.$style.'>&nbsp;';
	
	if($row['EmailAddress1'] != '') echo '<a href="mailto:'.$row['EmailAddress1'].'" target="_blank" style="font-size:11px;">'.$row['EmailAddress1'].'</a>';
	echo '</td><td '.$style.'>&nbsp;';
	if($row['WebsiteAddress'] != '') echo '<a href="http://'.$row['WebsiteAddress'].'" target="_blank" style="font-size:11px;">'.$row['WebsiteAddress'].'</a>';
	echo '</td></tr>';
}
}
echo '</table>';
/*echo '<br /><div>NOTE: Refresh page after confirming subscribers - <a href="javascript:window.location.reload(true);">Refresh</a></div>';*/
echo '<br /><div>' . $nav . '</div><div>&nbsp;</div>';
	
	
		  
		  ?>
</body>
</html>
