<?php

include "includes/config.php";
include "includes/session.php";  
include "includes/functions.php";

if (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}

$currency_attributes = displayCurrencyType();
// setting pagination limit From Admin settings

$limit=getPaginationLimit();

if($limit==0)
{
	$limit=10;
}	
//$limit=3;

$page=$_GET['page'];
if(empty($page))
	{
		$page = 1;
	}
//End pagination settings
if (isset($_GET["catid"]) and $_GET["catid"] !="") {
    $catid = $_GET["catid"];
} else if (isset($_POST["catid"]) and $_POST["catid"] !="") {
    $catid = $_POST["catid"];
}

if(isset($_GET["txtSearch"]) and $_GET["txtSearch"]!=""){
	$txtSearch = $_GET["txtSearch"];
	$cmbSearchType = $_GET["cmbSearchType"];
}else if(isset($_POST["txtSearch"]) and $_POST["txtSearch"]!=""){
	$txtSearch = $_POST["txtSearch"];
	$cmbSearchType = $_POST["cmbSearchType"];
}

$giftCategoryFlag=CheckGiftCertificateCategory($catid);
if(!isset($catid) && $catid =="" && $txtSearch=="")
{
        $showproductlisting="false";
        
}
if((isset($catid) && $catid!="")  ||  $txtSearch!="" || $giftCategoryFlag=='Y')
{
        $showproductlisting="true";
        
} 
/*display the active template*/
$active_template = displayTemplate();
include "includes/htmltop.php";
/* Top File Name */
include_once ("includes/".$active_template[0]); 
// Check Category is a Gift Certificate Category 
?> 
<script language="javascript" type="text/JavaScript"><!--
function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
}
function clickSearch()
{
	document.frmCatalog.submit();
}
function confirmDelete(){
	if(confirm('<?php echo CONFIRM_DELETE_RECORDS_TEXT;?>') ){
		return true;
	}
	return false;
}
//-->
</script>
				<div align="left" class="main_body">
							<!--mainbody area start -->
						<div align="left" id="body_left_area">
							<!--body left area start -->
							<!--categories area start -->
			<?php 
            // $limit=3;   
              $limit=20;
			$sql_category = " SELECT * FROM ".$tableprefix."categories WHERE parent_id LIKE ".GetSQLValueString($catid,"text")." ORDER BY category_name ";
			$sql_qr=dopaging($sql_category,'',$limit);	
			$totalrows=mysql_num_rows(mysql_query($sql_category));
			//echo $sql_category;
			$result_category = mysql_query($sql_qr) or die(mysql_error());
			?>			
			<!--categories area end -->	
			<!--body left area end -->
						</div>
		   <!--center content area start-->		
		 <form name="frmCatalog" action="<?php echo $_SERVER["PHP_SELF"]?>" method="POST" enctype="multipart/form-data">
		 <input type="hidden" name="catid" value="<?php echo htmlentities($catid);?>" >
		 <div align="left" id="item_details">
               <!--items display area start -->
			<?php if($catid!='')
			{
			?>
	              <div align="left" id="items_top_area">
                     <div align="left" id="items_top_area_ttl">
                     <div class="brdcrumb_container">
                            <?php  echo stripslashes(getCategoryLinkNew("categorydetail.php",GetSQLValueString($catid,"text")));?>
                     </div>
                     </div>
                    </div>
		   <?php } ?>
	       <div class="text_information" align="center"><?php echo $message?></div>		
		 <?php	
			if(!isset($catid) || $catid =="")
			{
				$catid = 0;
			}
			
			//die(mysql_num_rows($result_category));
	        	$showproducts = false;
			if(mysql_num_rows($result_category) > 0 )
			{
				
				
				$rowcnt1=0;
				
	?>
	<div class="category_listings_new">
	<!--new design-->
	
		<?php 	
			while($row_category = mysql_fetch_array($result_category))
		    {
			
					
							 $sql_route_catalog="SELECT route,category_id from ".$tableprefix."categories cat  where cat.route REGEXP '^".$row_category['category_id'].",|,^".$row_category['category_id']."|,".$row_category['category_id'].",|,".$row_category['category_id']."$|^".$row_category['category_id']."$'  ";
							 $rs_route_catalog=mysql_query($sql_route_catalog) or die(mysql_error());
							
							$current_catlist_catalog=array(); 
							 if(mysql_num_rows($rs_route_catalog)>0)
							 {
								while($List_catalog=mysql_fetch_array($rs_route_catalog))
								{
									$current_catlist_catalog[]=$List_catalog['category_id'];
								}	
							 }
							 
							 if(count($current_catlist_catalog)>0)
							 {
									 $categoryString_catalog=@implode("','",$current_catlist_catalog);
							 }	
							 else
							 {
								$categoryString_catalog=$current_catlist_catalog;
							 }
							 
							$count_product_catalog = 0;
							$productListingFee = getSettingsValue('product_listing_fee');
							$sql_catalog="SELECT count(p.product_id) AS pcount
							FROM ".$tableprefix."products p INNER JOIN ".$tableprefix."product_options po ON po.product_id = p.product_id AND po.default_option = 'YES'
							INNER JOIN ".$tableprefix."categories cat ON  p.product_category=cat.category_id
							INNER JOIN ".$tableprefix."artists ar ON ar.artist_id =p.product_artist_id 
							WHERE ar.vapproved ='Y' AND ar.deleted='N' AND ar.plan_expired='N'
							AND p.product_category IN  ('".$categoryString_catalog."')
							AND p.vapproved = 'Y' ";
                                                        if($productListingFee > 0){ 
                                                        $sql_catalog .= " AND p.payment_status='COMPLETED' ";
                                                        }
                                                        $sql_catalog .= " AND  p.deleted !='Y'  ";
							$result33 = mysql_query($sql_catalog);
						//echo $sql_catalog;
							 if (mysql_num_rows($result33) != 0) 
							 {
								$rw_catalog = mysql_fetch_array($result33);
								$count_product_catalog  =$rw_catalog['pcount'];
							//	print_r($rw_catalog);//exit();
							 }	
								
									   
                                                       if($row_category["gift_certificate_flag"]=="Y")
                                                       {
                                                                     $product_count_sql_catalog = "SELECT COUNT(p.product_id) AS gcount
                                                                                                                      FROM ".$tableprefix."products p
                                                                                                                      INNER JOIN ".$tableprefix."product_options po ON po.product_id = p.product_id AND po.default_option = 'YES'
                                                                                                                      INNER JOIN ".$tableprefix."artists s on p.product_artist_id=s.artist_id
                                                                                                                      INNER JOIN ".$tableprefix."giftcoupon_master gm oN gm.ngift_product_id=p.product_id
                                                                                                                      AND p.product_category IN (".$row_category['category_id'].")
                                                                                                                      AND p.vapproved = 'Y' AND s.vapproved='Y' AND gm.vgift_delete_status = 'N' AND  p.deleted !='Y'";

                                                                                                                     if($productListingFee > 0){
                                                                                                                        $product_count_sql_catalog .= " AND p.payment_status='COMPLETED' ";
                                                                                                                     }

                                                            //echo $product_count_sql;
                                                       $product_count_reslt_catalog = mysql_query($product_count_sql_catalog)or die(mysql_error());
                                                       if(mysql_num_rows($product_count_reslt_catalog) > 0)
                                                       {
                                                               $count_product_catalog = 0;
                                                               while($product_counts_catalog = mysql_fetch_object($product_count_reslt))
                                                               {
                                                              // 	print_r($product_count_reslt); exit();
                                                                       $count_product_catalog = $product_counts_catalog->gcount;
                                                               }
                                                       }

                                                       }
				 if($rowcnt1%2=='0')
			   {
			   		$rowcnt1='0';
					
			   }//end if
			   
			   if(strlen($row_category["category_name"])>30)
									 	 $category_name = substr($row_category["category_name"],0,30)."...";
									else
									  	 $category_name = $row_category["category_name"];
										 
										 
									   
$avatarImage = 'categories/avatar/noimage.jpg';
        if(is_file('categories/avatar/' . $row_category["avatar"]) && file_exists('categories/avatar/' . $row_category["avatar"]))
        {
            $avatarImage = 'categories/avatar/' . $row_category["avatar"];
        }
									
			?>																										
		<div class="cat_box">			
            <a href="categorydetail.php?catid=<?php echo htmlentities($row_category["category_id"])?>"><img src="<?php echo $avatarImage; ?>" /></a>
        <h5><a href="categorydetail.php?catid=<?php echo htmlentities($row_category["category_id"])?>"><?php echo stripslashes($category_name);?>&nbsp;(<?php echo $count_product_catalog;?>)</a></h5>
</div>
        <?php
				$rowcnt1++;
			}//end while
			
	?>
	
	
	
	
	
	<!--new design-->
	
				
				<div class="clear"></div>
				</div>
<?php 						
	 }//end if
		else
		{
			
			$showproducts = true;

		}
		
		
?>

<?php if($totalrows>$limit)
{?>
<div  class="pagination"> <?php echo rightpaging();?></div>
<?php  } ?>

<?php 		
		if($showproducts)
		{
			
?>					
<div class="category_searchdiv">
<table cellpadding="0" cellspacing="0" width="100%">
					<tr>
					<td align="right">
					
					<div class="prdct_search_container">
					<?php if(($_REQUEST['catid']!="" || $txtSearch!='')){?>
					 &nbsp;<span align="right" id="items_top_area_ttl_4"  class="f_right_marg"><?php echo SORT_BY_TEXT;?></span>
					 &nbsp;<select onchange="clickSearch()" class="topsearch_fld f_right_marg" name="sel_order_by" id="sel_order_by">
					<option value=""><?php echo SELECT_ORDER_TEXT;?></option>
					<option value="price asc" <?php if($_REQUEST['sel_order_by']=="price asc"){ echo "selected";}?>><?php echo PRICE_ORDER_LOW_HIGH_TEXT;?></option>
					<option value="price desc" <?php if($_REQUEST['sel_order_by']=="price desc"){ echo "selected";}?>><?php echo PRICE_ORDER_HIGH_LOW_TEXT;?></option>
					<option value="vendor asc" <?php if($_REQUEST['sel_order_by']=="vendor asc"){ echo "selected";}?>><?php echo VENDOR_ORDER_ASC_TEXT;?></option>
					<option value="vendor desc" <?php if($_REQUEST['sel_order_by']=="vendor desc"){ echo "selected";}?>><?php echo VENDOR_ORDER_DESC_TEXT;?></option>
					<option value="rating desc" <?php if($_REQUEST['sel_order_by']=="rating desc"){ echo "selected";}?>><?php echo RATING_ORDER_HIGH_LOW_TEXT;?></option>
					</select>
						
					<?php }?>
						 &nbsp;<input type="text" name="txtSearch"  class="textarea" maxlength="50" size="40" value="<?php echo $txtSearch;?>"/>

					  <input name="Submit" type="submit" class="button" value="<?php echo SEARCH_BUTTON;?>"/>
					  </div>
					  </td>
		  </tr>
					 
					  <?php
					  
					   if($_REQUEST['catid']=='' && $txtSearch=='')
					        {
					  ?>
<tr><td align="center"><?php include_once("includes/categorylist_catalog.php");?>
					  </td>
					  </tr>
					  <?php 
					  
					  }//end if
					?>
			            </td>
				</tr>
					    		
						</table>
						</div>
							
				
		
		
<?php
 
 if($showproductlisting=="true")
 
 { // display products
 		
	include_once("includes/productlist.php");
	
 }  else {

}

} // end if show products
?>

	
		<div align="left" class="float_clear1"></div>
			

		</div>
								
	<!--items display area end -->
	</form>
	<!--center content area end-->		
		<!--mainbody area end -->
        </div>
<?php
include "includes/".$active_template[2];	/* Bottom File Name */
?>																		