<?php	

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

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

	if ($channel == "")
	{
		Header("Location: " . $phpgw->link("/chat/"));
	}
	
	$loginid = $phpgw_info["user"]["userid"];

	if ($channel[0] == '~')
	{
		$location = "private";
		$newchannel = str_replace("~","",$channel);
	}
	else
	{
		$location = "public";
		$newchannel = $channel;
	}

	if ($action == "newprivate")
	{
		$user2 = $channel;
		$location =  "private";
		$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','New chat with $loginid and $user2','0','" . time() . "','0')");
	}

	if ($location != "private")
	{
		$phpgw->db->query("SELECT * FROM chat_currentin WHERE loginid='$loginid' AND channel='$newchannel'");
		if ($phpgw->db->nf() == 0)
		{
			$phpgw->db->query("INSERT INTO chat_currentin (loginid, channel, lastmessage) values ('$loginid','$newchannel','" . time() . "')");
		}
		else
		{
			$phpgw->db->query("UPDATE chat_currentin SET lastmessage='" . time() . "' WHERE loginid='$loginid' AND channel='$newchannel'");
		}
	}

	echo '<html>
<head>
<title>' . lang('Chat') . '</title>
</head>';
	echo "  <FRAMESET COLS=130,*,126' FRAMEBORDER='0'>\n";
		echo "    <FRAME SRC='" . $phpgw->link("/chat/channels.php","channel=$newchannel&location=$location") . "' NAME='static' SCROLLING=AUTO>\n";
		echo "    <FRAME SRC='" . $phpgw->link("/chat/body.php","channel=$newchannel&action=$action&location=$location&user2=$user2") . "' NAME='body'>\n";
		echo "    <FRAME SRC='" . $phpgw->link("/chat/users.php","channel=$newchannel&location=$location") . "' NAME='users' SCROLLING=AUTO>\n";
	echo "  </FRAMESET>\n";
	echo "</html>\n";
?>
