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

	$phpgw_info["flags"] = array(
		"currentapp" => "chat",
		"noheader" => True,
		"nonavbar" => True
	);
	include("../header.inc.php");

	$loginid = $phpgw_info["user"]["userid"];
	echo '<head>
<META HTTP-EQUIV="Refresh" Content="30" HREF="#bottom">
</head>';

	$limit =$phpgw->db->limit($start);
	$phpgw->db->query("SELECT * FROM chat_privatechat WHERE (user1='$loginid' OR user2='$loginid') AND (closed!='1' AND messagetype='0') $limit");	
	$size = $phpgw->db->nf();

	$phpgw->db->query("SELECT account_lid AS group_name FROM phpgw_accounts where account_type = 'g'");
	$phpgw->db->next_record();

	$size = $size + $phpgw->db->nf();
	if (($size == 0) || ($size == 1))
	{
		$size = 2;
	}

	echo "<center><b>\n";
	echo lang("Channels") . ":\n";

	$phpgw->db->query("SELECT account_lid AS group_name FROM phpgw_accounts where account_type = 'g'");
	$phpgw->db->next_record();

	echo "</b><form method=\"post\" action=\"".$phpgw->link("/chat/load.php")."\" target=\"_top\">\n";
	echo "<input type=hidden name=channel value='$channel'>\n";
	echo "<select name=channel size=$size>\n";

	for ($i = 0; $i < $phpgw->db->nf(); $i++)
	{
		echo "<option value= '" . $phpgw->db->f("group_name") . "'>" . $phpgw->db->f("group_name");
		$phpgw->db->next_record();
	}	
	$phpgw->db->query("SELECT * from chat_privatechat WHERE (user1='$loginid' OR user2='$loginid') AND (closed!='1' AND messagetype='0')");
	$phpgw->db->next_record();

	for ($i = 0; $i < $phpgw->db->nf(); $i++)
	{
		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();
	}

	$ordermethod = "order by channel";

	$phpgw->db->query("SELECT * from chat_privatechat WHERE (user1='$loginid' OR user2='$loginid') AND (closed!='1' AND messagetype='0')");
	$phpgw->db->next_record();
	$size = $phpgw->db->nf();

	$phpgw->db->query("select * from chat_currentin WHERE loginid='$loginid' $ordermethod $limit");
	$phpgw->db->next_record();

	$size = $size + $phpgw->db->nf();
	if (($size == 0) || ($size == 1))
	{
		$size = 2;
	}

	echo "</select><br><br>";
	echo "<input type=submit value='" . lang("Switch Room") . "'></form>\n";
	echo "<form method=\"post\" action=\"".$phpgw->link("/chat/index.php")."\" target=\"_parent\">\n";
	echo "<input type=hidden name=action value='part'>\n";
	echo "<input type=hidden name=location value='$location'>\n";
	echo "<input type=hidden name=channel value='$channel'>\n";
	echo "<select name=channel size=$size>\n";

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

	$phpgw->db->query("SELECT * from chat_privatechat WHERE (user1='$loginid' OR user2='$loginid') AND (closed!='1' AND messagetype='0')");
	$phpgw->db->next_record();

	for ($i = 0; $i < $phpgw->db->nf(); $i++)
	{
		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();
	}

	echo "</select>\n";
	echo "<input type=submit value='" . lang("Part Channel") . "'></form></center><p>\n";
?>
