<?php
/*
 * Copyright (c) 2004-2009 Luiz Otavio O Souza <loos.br@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * $Id: group.inc.php 112 2009-03-15 17:30:28Z loos-br $
 */

    function check_chats($email, $contact_email) {
     global $mysql;

        $sql  = "SELECT DISTINCT l1.sb_id FROM log as l1, log as l2 WHERE ";
        $sql .= "(l1.email = '$email' AND l1.type = 'start' AND ";
        $sql .= " l2.email = '$contact_email' AND l2.type = 'join' AND ";
        $sql .= " l2.sb_id = l1.sb_id) OR ";
        $sql .= "(l1.email = '$email' AND l1.type = 'join' AND ";
        $sql .= " l2.email = '$contact_email' AND l2.type = 'start' AND ";
        $sql .= " l2.sb_id = l1.sb_id) ";

        $rsCountChat = mysql_query($sql, $mysql);
        return mysql_num_rows($rsCountChat);
    }

?>
 <table border=1 cellpadding=0 cellspacing=0
	style="border-color: gray; border-style: solid; border-width: 1;">
  <tr><td>
   <table border=1 width=300>
    <tr align="center">
     <td bgcolor="#EBEBEB" width="100%">Contatos</td>
    </tr>
<?php
    $sql  = "SELECT * FROM contact_groups WHERE email = '$_REQUEST[email]' ";
    $sql .= "ORDER BY name";

    $grs = mysql_query($sql, $mysql) or die('fail to run query');

    $i = 1;
    while ($grow = mysql_fetch_array($grs)) {
	$sql  = "SELECT *, contact_flags & 0x10 AS contact_cam ";
	$sql .= "FROM contacts WHERE email = '$_REQUEST[email]' ";
	$sql .= "AND contact_group = '". $grow['id'] ."' ";
	$sql .= "ORDER BY contact_status, contact_dn";

	$rs = mysql_query($sql, $mysql) or die('fail to run query');

?>
    <tr>
     <td bgcolor="#EBEBEB"><a href="javascript: expand('<?php echo $i ?>')">+</a>
	<?php echo $grow['name'] ?> (<?php echo mysql_num_rows($rs) ?>)</td>
    </tr>
    <tr id="<?php echo $i++ ?>" style="DISPLAY: ">
     <td>
      <table border=1 width="100%">
<?php
	while ($row = mysql_fetch_array($rs)) {

	    $status = $arrayStatus[$row['contact_status']];

	    $Chats = check_chats($row['email'], $row['contact_email']);
?>
    <tr>
     <td bgcolor="#EBEBEB">
	<table border=0 cellspacing=0 cellpadding=0 width="100%">
	<tr>
	<td width=<?php echo (intval($row['contact_lists']) & 8) ? "18" : "36" ?>><img
                src="imagens/<?php echo $status['img'] ?>" alt="<?php echo $status['msg'] ?>" border=0><?php echo !(intval($row['contact_lists']) & 8) ?
                "<img src=\"imagens/notinlist.png\" alt=\"N&atilde;o tem voc&ecirc;\" height=16 width=16 border=0>" : "" ?></td>

           <td><a href="#" title="<?php echo $row['contact_email'] ?>"><?php echo $row['contact_dn'] ?></td>
           <td width=<?php $i = 0; if (intval($row['contact_cam']) > 0) $i += 18; if ($Chats > 0) $i += 18; echo $i; ?>><?php
		if ($Chats > 0) {
			echo "<a href=\"javascript: popup('chat-to.php?email=". $row['email'] ."&to=". $row['contact_email'] ."')\" " .
                	     "title=\"$row[contact_email]\"><img src=\"imagens/send_im.gif\" border=0></a>";
		}
		if (intval($row['contact_cam']) > 0) {
        		echo "<img src=\"imagens/webcam.png\" alt=\"N&atilde;o tem voc&ecirc;\" height=16 width=16 border=0>";
		} ?></td>
          </tr>
        </table>

     </td>
    </tr>
<?php
	}
?>
      </table>
     </td>
    </tr>
<?php
    }
?>
   </table>
   </td>
  </tr>
 </table>

