<?php

	if (! $sessionid)
	{
		Header("Location: ../login.php");
	}

	$phpgw_info["flags"] = array(
		"currentapp"              => "chat",
		"navbar_target"           => "_top",
		"enable_nextmatchs_class" => True
	);

	include("../header.inc.php");
	echo parse_navbar();

	$loginid = $phpgw_info["user"]["userid"];
	if ($action=="part")
	{
		if ($location == "public")
		{
			$phpgw->db->query("DELETE FROM chat_currentin WHERE loginid='$loginid' AND channel='$channel'");
		}
		if ($location == "private")
		{
			$user2 = $channel;
			$phpgw->db->query("DELETE FROM chat_privatechat WHERE ((user1='$loginid' AND user2='$user2') or (user1='$user2' AND user2='$loginid'))");
			$phpgw->db->query("INSERT INTO chat_privatechat (user1,user2,sentby,message,messagetype,timesent,closed) values ('$loginid','$user2','System','This chat has been closed by $loginid.','0','" . time() . " ','1')");
		}
	}
?>
  <script>
  self.name="first_Window";
  function chatwindow()
  {
    var value = "";
    for(var i = 0; i < document.load.channel.length; i++) {
      if (document.load.channel[i].selected) {
        value += document.load.channel[i].value;
      }
    }
    if (value != "")
    {
      chatwin=window.open("<?php echo $phpgw->link('/chat/load.php'); ?>"+"&channel="+value,"Chat","width=640,height=480,toolbar=no,scrollbars=yes,resizable=yes");
    }
  }
  </script>
  <table align=center>
    <tr>
      <td><?php echo lang("rooms"); ?></td>
      <td><?php echo lang("whowhere"); ?></td>
    </tr>
    <tr>
      <td>
        <form name="load" method="post" action="<?php echo $phpgw->link("/chat/load.php") ?>">
        <select name="channel" size="4" onChange="chatwindow();">
<?php
	$phpgw->db->query("SELECT account_lid as group_name FROM phpgw_accounts where account_type = 'g'");
	$phpgw->db->next_record();

	for ($i = 0; $i < $phpgw->db->nf(); $i++)
	{
		echo '          <option value="' . $phpgw->db->f('group_name') . '">' . $phpgw->db->f('group_name')."\n";
		$phpgw->db->next_record();
	}

	$phpgw->db->query("SELECT * from chat_privatechat WHERE (user1='$loginid' OR user2='$loginid') AND (closed!='1' AND messagetype='0')");
	while ($phpgw->db->next_record())
	{
		if ($phpgw->db->f("user1") == $loginid)
		{
			echo '          <option value="~' . $phpgw->db->f('user2') . '">&lt ' . $phpgw->db->f('user2') . ' &gt\n';
		}
		else
		{
			echo '          <option value="~' . $phpgw->db->f('user1') . '">&lt ' . $phpgw->db->f('user1') . ' &gt\n';
		}
		$phpgw->db->next_record();
	}
	// Note the following ja-vascript has a channel named 'Default' passed to it.  This needs to be fixed...
?>
        </select><br>
        <noscript><input type="submit" value="<?php echo lang('Enter Chat'); ?>"></noscript>
        </form>
      </td>
      <td valign=top>
        <table border=1>
<?php
	$limit=$phpgw->db->limit($start);
	$phpgw->db->query("select * from chat_currentin $ordermethod $limit");
	$ordermethod = "order by channel, loginid asc";
	$phpgw->db->query("select * from chat_currentin $ordermethod $limit");

	while ($phpgw->db->next_record())
	{
		$tr_color = $phpgw->nextmatchs->alternate_row_color($tr_color);
		echo "          <tr bgcolor=". $tr_color . ">\n";
		echo "            <td>" . $phpgw->db->f("loginid") . " - " . $phpgw->db->f("channel") . "</td>\n";
		echo "          </tr>\n";
	}
?>
        </table>
      </td>
    </tr>
  </table>
<?php
	$phpgw->common->phpgw_footer();
?>
