function RefineChannelFilter($qWhere, $c, $crono, $dbh, $children = "")
{
    $ret = "";
    if ($crono == 1 && $qWhere != "IN (-1)") {
        $query = "SELECT tag_id,tag_group_id FROM tag_x_channel WHERE channel_id={$c}";
        $result = mysql_query($query, $dbh);
        if (mysql_num_rows($result) > 0) {
            $i = 0;
            $tags = array();
            while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
                if ($row[0] >= 0) {
                    if (!in_array($row[0], $tags)) {
                        $tags[$i] = $row[0];
                        $i++;
                    }
                } else {
                    if ($row[1] >= 0) {
                        $x = GetTagsInGroup($row[1], $dbh);
                        for ($j = 0; $j < sizeof($x); $j++) {
                            if (!in_array($x[$j], $tags)) {
                                $tags[$i] = $x[$j];
                                $i++;
                            }
                        }
                    }
                }
            }
            $tWhere = implode(",", $tags);
            $tWhere = "IN(" . $tWhere . ")";
            if ($children == "") {
                $query = "SELECT DISTINCT message.message_id FROM message,tag_x_message WHERE message.message_id = tag_x_message.message_id AND tag_id " . $tWhere;
            } else {
                $query = "SELECT DISTINCT message.message_id FROM message,tag_x_message WHERE message.channel_id IN (" . $children . ") AND message.message_id = tag_x_message.message_id AND tag_id " . $tWhere;
            }
            $result = mysql_query($query, $dbh);
            if (mysql_num_rows($result) > 0) {
                $i = 0;
                while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
                    $messages[$i] = $row[0];
                    $i++;
                }
                if ($qWhere == "") {
                    $ret = "IN(" . implode(",", $messages) . ")";
                } else {
                    str_replace("IN", "", $qWhere);
                    str_replace("(", "", $qWhere);
                    str_replace(")", "", $qWhere);
                    $qWhere = trim($qWhere);
                    $searched_messages = explode(",", $qWhere);
                    $result_messages = array();
                    $j = 0;
                    for ($i = 0; $i < sizeof($searched_messages); $i++) {
                        if (in_array($searched_messages[$i], $messages)) {
                            $result_messages[$j] = $searched_messages[$i];
                            $j++;
                        }
                    }
                    if (sizeof($result_messages) > 0) {
                        $ret = "IN(" . implode(",", $result_messages) . ")";
                    }
                }
            } else {
                if ($qWhere == "") {
                    $ret = "IN (-1)";
                }
            }
        }
    }
    return $ret;
}
function GetTagsInChannel($c, $dbh)
{
    $ret = array();
    $query = "SELECT tag_id,tag_group_id FROM tag_x_channel WHERE channel_id={$c}";
    $result = mysql_query($query, $dbh);
    $i = 0;
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        if ($row[0] >= 0) {
            if (!in_array($row[0], $ret)) {
                $ret[$i] = $row[0];
                $i++;
            }
        } else {
            if ($row[1] >= 0) {
                $x = GetTagsInGroup($row[1], $dbh);
                for ($j = 0; $j < sizeof($x); $j++) {
                    if (!in_array($x[$j], $ret)) {
                        $ret[$i] = $x[$j];
                        $i++;
                    }
                }
            }
        }
    }
    return $ret;
}