<?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);
}
if(isset( $_SESSION["sess_userid"]) and $_SESSION["sess_userid"]!= "") {
	$userid = $_SESSION["sess_userid"];
}

$currency_attributes = displayCurrencyType();
/*if(isset( $_GET["begin"]) and $_GET["begin"]!= ""){
 $begin = $_GET["begin"];
}else{
$begin = 0;
}
if(isset( $_GET["num"]) and $_GET["num"]!= ""){
$num = $_GET["num"];
}else{
$num = 0;
}
if(isset( $_GET["numBegin"]) and $_GET["numBegin"]!= ""){
$numBegin = $_GET["numBegin"];
}else{
$numBegin = 0;
}*/



if(isset( $_POST["ddlCategory"]) and $_POST["ddlCategory"]!= "") {

	$ddlCategory = $_POST['ddlCategory'];

	/*-------Multicart 2.0 Upgradation ends------*/

}
else if(isset( $_GET["ddlCategory"]) and $_GET["ddlCategory"]!= "") {

	$ddlCategory = $_GET['ddlCategory'];

	/*-------Multicart 2.0 Upgradation ends------*/
}

else {
	$ddlCategory = $_GET['cat'];

}

//echo $ddlCategory;

$ddlCategory=stripslashes($ddlCategory);


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

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

$txtSearchtext=trim($txtSearch);

$txtSearch = trim($txtSearch);

if(isset( $_POST["chkProductName"]) and $_POST["chkProductName"]!= "") {
	$chkProductName = $_POST["chkProductName"];
	if($chkProductName == "on") {
		$chkProductName = "1";
	}
}else if(isset( $_GET["chkProductName"]) and $_GET["chkProductName"]!= "") {
	$chkProductName = $_GET["chkProductName"];
	if($chkProductName == "on") {
		$chkProductName = "1";
	}
}else {
	$chkProductName = $_GET["pn"];
}

if(isset( $_POST["chkProductDescription"]) and $_POST["chkProductDescription"]!= "") {
	$chkProductDescription = $_POST["chkProductDescription"];
	if($chkProductDescription == "on") {
		$chkProductDescription = "1";
	}
}else {
	$chkProductDescription = $_GET["pd"];
}
if(isset( $_POST["chkProductArtist"]) and $_POST["chkProductArtist"]!= "") {
	$chkProductArtist = $_POST["chkProductArtist"];
	if($chkProductArtist == "on") {
		$chkProductArtist = "1";
	}
}else {
	$chkProductArtist = $_GET["ps"];
}
if(isset( $_POST["chkIncludeSubcategories"]) and $_POST["chkIncludeSubcategories"]!= "") {
	$chkIncludeSubcategories = $_POST["chkIncludeSubcategories"];
	if($chkIncludeSubcategories == "on") {
		$chkIncludeSubcategories = "1";
	}
}else if(isset( $_GET["chkIncludeSubcategories"]) and $_GET["chkIncludeSubcategories"]!= "") {
	$chkIncludeSubcategories = $_GET["chkIncludeSubcategories"];
	if($chkIncludeSubcategories == "on") {
		$chkIncludeSubcategories = "1";
	}
}else {
	$chkIncludeSubcategories = $_GET["isc"];
}

if($ddlCategory!='') {
	if($chkIncludeSubcategories == "1") {
		$categorylist  = implode(",",getCategoryDescendants($ddlCategory));
	}else {
		$categorylist  = $ddlCategory;
	}
}

if(isset($_POST["btnSearch"])) {
	$sd=1;
}else if($_GET["sd"] =="1") {
	$sd=1;
}
$where = "";

if($chkProductName == "1") {
	$where .= '  AND p.product_name LIKE "%'.$txtSearch.'%" ';

}
if($chkProductDescription == "1") {

	$where .= '  AND  p.product_description LIKE "%'.mysql_real_escape_string($txtSearch).'%" ';


}
if($chkProductArtist == "1") {

	$where .= '  AND  (s.artist_name LIKE "%'.mysql_real_escape_string($txtSearch).'%" || s.first_name LIKE "%'.mysql_real_escape_string($txtSearch).'%" || s.last_name LIKE "%'.mysql_real_escape_string($txtSearch).'%" ||  s.company LIKE "%'.mysql_real_escape_string($txtSearch).'%" ) ';


}

/*-------Multicart 2.0 Upgradation starts------*/


if(isNotNull($categorylist)) {

	$where .= " AND p.product_category IN ($categorylist) ";

}

/*-------Multicart 2.0 Upgradation ends------*/


$sql = "SELECT DISTINCT p.product_id,po.product_option_id,(p.product_price+po.additional_price) AS product_price,po.image_small,po.image_big,po.product_stock,po.discount,
		p.product_name,s.artist_name,
		(SELECT pm.vimage_name FROM ".$tableprefix."product_view_images pm where pm.nimage_productid=p.product_id LIMIT 0,1)AS smallimage
				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
										WHERE p.deleted != 'Y' AND p.vapproved='Y' AND s.plan_expired='N' ";
if(getSettingsValue('product_listing_fee') > 0) {
	$sql .= " AND p.payment_status='COMPLETED' ";
}

$sql = $sql.$where;

$ddlSelectedCategory=str_replace("'", "", stripslashes($ddlCategory));

if(!(isset($pagenum))) {
	$pagenum = 1;
}
$limit=getPaginationLimit();

$result=mysql_query($sql) or die(mysql_error());

$totalrows = mysql_num_rows($result);

if(empty($page)) {
	$page = 1;
}

$limit=getPaginationLimit();

if($limit==0) {
	$limit=12;
}

$sql_qr=dopaging($sql,'',$limit);

$result=mysql_query($sql_qr) or die(mysql_error());

$catlist = make_selectlist(0, 0);

/*display the active template*/

$active_template = displayTemplate();
include "includes/htmltop.php";
/* Top File Name */
include_once ("includes/".$active_template[0]);
?>
<div
	align="left" class="main_body">
	<!--mainbody area start -->

	<!--center content area start-->

	<form name="frmSearch" action="<?php echo $_SERVER["PHP_SELF"]?>"
		method="POST">
		<input type="hidden" name="sd" value="<?php echo $sd;?>">
		<div align="left" id="item_details">
			<!--items display area start -->
			<div align="left" id="items_top_area">
				<div align="left" id="items_top_area_ttl">
					<?php echo SEARCH_BUTTON;?>
				</div>
			</div>
			<div class="text_information" align="center">
				<?php echo $message?>
			</div>
			<div align="center" id="items_display_area">
				<table cellpadding="2" cellspacing="2" width="100%">
					<tr>
						<td>
							<div class="searc_tbltop">
								<table border=0 width="80%" align=center>
									<tr>
										<td align="left"><?php echo SEARCH_FOR_TEXT;?> &nbsp;</td>
										<td>&nbsp;</td>
										<td align="left"><input type="text" class="textarea"
											value="<?php echo (stripslashes($txtSearchtext));?>"
											name="txtSearch" size="30" maxlength="100"></td>							
										</tr>
									<tr><td align="left"><?php echo CATERGORY_IN_TEXT;?>&nbsp;</td>
										<td>&nbsp;</td>
										<td align="left" class="linksCopy2"><?php echo makeDropDownList("ddlCategory", $catlist, $ddlSelectedCategory, true, "textbox", $properties, $behaviors)?>
											&nbsp; <input type="checkbox" class="checkbox"
											name="chkIncludeSubcategories"
											<?php if($chkIncludeSubcategories == "1") {
												echo " CHECKED ";
											}?>> &nbsp;&nbsp; <?php echo SUB_CATEGORY_INCLUDE_TEXT;?>
										</td>
										
										
									</tr>
									<tr><td align="left" valign="top"><?php echo MATCH_WITH_TEXT;?>&nbsp;</td>
										<td>&nbsp;</td>
										<td align="left">
											<div class="linksCopy2">
												
												&nbsp; <input type="checkbox" name="chkProductName"
													class="checkbox"
													<?php if($chkProductName == "1") {
														echo " CHECKED ";
													}?>><?php echo PRODUCT_NAME_TEXT1;?> <br>
												
												&nbsp; <input type="checkbox" name="chkProductDescription"
													class="checkbox"
													<?php if($chkProductDescription == "1") {
														echo " CHECKED ";
													}?>> <?php echo PRODUCT_DESCRIPTION_TEXT;?><br>
												
												&nbsp; <input type="checkbox" name="chkProductArtist"
													class="checkbox"
													<?php if($chkProductArtist == "1") {
														echo " CHECKED ";
													}?>> <?php echo PRODUCT_SELLER_TEXT;?><br>
											</div>
										</td>
										
									</tr>
									<tr>
										<td colspan="3">
									
									</tr>
									<tr>
										<td colspan="3" align="center"><input type="submit"
											value="<?php echo SEARCH_BUTTON;?>" name="btnSearch"
											class="button"></td>
									</tr>
								</table>
								<div class="clear"></div>
							</div>
						</td>
					</tr>
				</table>

				<?php
				if($sd==1) {
                    ?>
				<table cellpadding="2" cellspacing="2" width="100%">
					<tr>
						<td>
							<div class="search_details">
								<div class="clear"></div>
								<?php
								$prodimagedir = "products/";
								if(mysql_num_rows($result) > 0 ) {
                                        $i=1;
                                        $cells =4;
                                        $count_row = 1;
                                        while($row = mysql_fetch_array($result)) {
                                            $i++;
                                            $link = "<a  href='productdetails.php?productid=".$row["product_id"]."'>";
                                            $check = "<input type='checkbox' name='chkproducts[]' value='" . $row["product_id"] . "' >";
                                            $price = $row['product_price'];

                                            // Image Selection Modification // Done By Asha
                                            $imageurl='';
                                            if($row["image_small"] !="") {
                                           $product_image = $row["image_small"];

                                       }  // end if product image null check
                                       elseif($row["image_big"] != "") {
                                           $product_image = $row["image_big"];
                                       }
                                       else
                                       {
                                       	$product_image=$row["smallimage"];
                                       }

                                       if($product_image=="") {
                                           $imageurl = $prodimagedir."noimage.jpg";
                                       }
                                       else {
                                           if(strstr($product_image, "http://")) {
                                               $imageurl = $product_image;
                                           }
                                           /*elseif(is_file($prodimagedir.$product_image) && file_exists($prodimagedir.$product_image)) {
                                            $imageurl = $prodimagedir.$product_image;
                                           }*/
                                           else {
                                               $imageurl = $prodimagedir.$product_image;
                                           }
                                       }

                                       $imageurl = "<img class='product_img' src='".$site_url."/".$imageurl."' border='0' >";
                                       // Image Listing Modification End

                                       $discountstr="";

                                       if(checkProductStock($row["product_id"]) > 0) {
                                                $productinstock = true;
                                            }
                                            else {
                                                $productinstock = false;
                                            }

                                            $currency_attributes = displayCurrencyType();
                                            $conversionRate	= ConvertedCurrencyRate($currency_attributes[2]);
                                            $converted_price = $price * $conversionRate;
                                            $discountprice=0;

                                            if($row["discount"] !="0") {
                                                $discountprice=$converted_price- ($converted_price*$row["discount"])/100;
                                                $discountprice =number_format($discountprice,2,".","");
                                                $discountstr="<br>discount(".$row["discount"]."%)";

                                            }

                                            $display_product_price = number_format($converted_price,2,".","");
                                            ?>

								<div
									style="background-color: #FFFFFF; float: left; height: 290px; margin: 0 5px 12px 5px; width: 230px; padding-top: 10px;"
                                            <?php if($count_row==4) {?> style="margin-right:0px;"
                                            <?php } ?>>
									<div align="center">
										<a
											href="<?php echo SITE_URL?>/productdetails.php?productid=<?php echo $row["product_id"]?>"><?php echo $imageurl?>
										</a>
									</div>


									<h5 align="right" style="padding-right: 20px;">
										<a class="prname_searchpg"
											href="<?php echo SITE_URL?>/productdetails.php?productid=<?php echo $row["product_id"]?>"
											title="<?php echo stripslashes($row["product_name"]);?>"><?php echo stripslashes(ucwords(strtolower(substr($row["product_name"],0,25)))); ?>
										</a>
									</h5>


									<div class="product_price" align="left">
										<p>
										
										
										<p>
											<?php if($discountprice!=0) { ?>
											<?php echo  $currency_attributes[0] ?>
											<?php echo $discountprice;?>
											(<strike><?php echo  $currency_attributes[0] ?> <?php echo $display_product_price;?>
											</strike>)
											<?php }  else { ?>
											<?php echo  $currency_attributes[0] ?>
											<?php echo $display_product_price;?>
											<?php } ?>
										</p>
									</div>
									<?php
									if($productinstock) {
                                                    ?>
									<div class="adtocart_tbn" align="left">
                                                                            <a class="adtocart" href="<?php echo SITE_URL?>/homepage_addtocart.php?productid=<?php echo $row["product_id"]?>&product_option_id=<?php echo $row["product_option_id"]?>" class="add_cart_small"><?php echo ADD_TO_CART_TEXT; ?></a>
                                                                            <a class="more_btn" href="<?php echo SITE_URL?>/productdetails.php?productid=<?php echo $row["product_id"]?>" class="more_btn"><?php echo MORE_TEXT; ?></a>
         
										<!--<a class="adtocart"
											href="<?php echo SITE_URL?>/homepage_addtocart.php?productid=<?php echo $row["product_id"]?>&product_option_id=<?php echo $row["product_option_id"]?>"><img
											src="<?php echo SITE_URL?>/themes/<?php echo $active_template[5]?>/view_cart_btn.gif"
											width="73" height="20" alt="<?php echo ADD_TO_CART_TEXT;?>"
											border="0" /> </a> <a class="more_btn"
											href="<?php echo SITE_URL?>/productdetails.php?productid=<?php echo $row["product_id"]?>"><img
											src="<?php echo SITE_URL?>/themes/<?php echo $active_template[5]?>/btn_more.gif"
											width="73" height="20" alt="<?php echo MORE_TEXT;?>"
											border="0" /> </a>-->
									</div>
									<?php
                                                }
                                                else {
                                                    ?>
									<div class="adtocart_tbn" align="left">
										<img src="<?php echo SITE_URL?>/images/home_outfstock.gif"
											alt="<?php echo OUT_STOCK_TEXT;?>" border="0" />
									</div>
									<?php

                                                }
                                                ?>
									<div class="clear"></div>
								</div>

								<?php
								if($count_row ==$cells) {
                                                $count_row = 1;
                                            }
                                            else {
                                                $count_row++;
                                            }
                                        }
                                    }
                                    else {
                                        ?>
								<?php echo NO_RESULT_TEXT;?>
								<?php
                                    }
                                    ?>
								<div class="clear"></div>
							</div>
						</td>
					</tr>
				</table>
				<?php if($totalrows>$limit) {?>
				<div class="pagination">
					<?php echo rightpaging();?>
				</div>
				<?php  } ?>
				<?php
                }
                ?>
			</div>
			<!--items display area end -->
		</div>
	</form>
	<!--center content area end-->
	<!--mainbody area end -->
</div>
<?php
include "includes/".$active_template[2];	/* Bottom File Name */
?>