<?php
function soobic_get_category_id($cid) {
	$ocid=$cid;
	$pid=1;
	while ($pid!=0) {
	$psql='select c.categories_id,c.parent_id from categories c where c.categories_id ='.$ocid.' limit 1';
    $p_query = tep_db_query($psql);
    $p_result = tep_db_fetch_array($p_query);
	$pid=$p_result['parent_id'];
	$ocid=$pid;
	if ($pid==0) 
	    return $p_result['categories_id'];
	}
}

	function tep_get_categories_id_from_products_id2($product_id)
	{
		$cat_id = "";
		$categories_raw = tep_db_query("SELECT categories_id FROM " .TABLE_PRODUCTS_TO_CATEGORIES. " WHERE products_id = '" .$product_id. "'");
			
		while ($cat = tep_db_fetch_array($categories_raw)) 
		{
			$cat_id .= $cat['categories_id'].",";
		}
		return substr($cat_id, 0, -1);	
	}
	
	$categories_id_list = tep_get_categories_id_from_products_id2($product_info['products_id']);

    $info_box_contents = array();
    $info_box_contents[] = array('text' => TABLE_HEADING_RELATIVE_PRODUCT);
    new contentBoxHeading($info_box_contents);

    $row = 0;
    $col = 0;
    $info_box_contents = array();

	$relative_products_raw = tep_db_query("SELECT ptc.products_id, p.products_image, p.products_price, pd.products_name, p.products_tax_class_id FROM ".TABLE_PRODUCTS_TO_CATEGORIES." ptc LEFT JOIN " .TABLE_PRODUCTS." p ON ptc.products_id = p.products_id LEFT JOIN " .TABLE_PRODUCTS_DESCRIPTION. " pd ON p.products_id = pd.products_id WHERE p.products_status = 1 AND pd.language_id = ".(int)$languages_id." AND p.products_id <> ".(int)$product_info['products_id']." AND ptc.categories_id in ($categories_id_list) order by rand() LIMIT 6");

	while ($relative_products = tep_db_fetch_array($relative_products_raw)) 
	{
			$picture_url = 'products_images/'. $relative_products['products_image'];

		if ($new_price = tep_get_products_special_price($relative_products['products_id'])) 
		{
		  $products_price = '<s>' . $currencies->display_price($relative_products['products_price'], tep_get_tax_rate($relative_products['products_tax_class_id'])) . '</s> <span class="productSpecialPrice">' . $currencies->display_price($new_price, tep_get_tax_rate($relative_products['products_tax_class_id'])) . '</span>';
		} 
		else 
		{
		  $products_price = $currencies->display_price($relative_products['products_price'], tep_get_tax_rate($relative_products['products_tax_class_id']));
		}

	    $info_box_contents[$row][$col] = array('align' => 'center',
                                           'params' => 'class="smalltext" width="33%" valign="top"',
                                           'text' => '<a href="'. tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $relative_products['products_id']) . '" >' . tep_image(DIR_WS_IMAGES . $picture_url, $relative_products['products_name'], 90, 90) . '</a><br><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $relative_products['products_id']) . '" >' . $relative_products['products_name'] . '</a><br>' . '<span class="jus_pri_spe">'.$products_price.'</span>');
		$col ++;
		if ($col > 2)
		{
		  $col = 0;
		  $row ++;
		}
		
	}
	  new contentBox($info_box_contents);
?>