<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="com.csc.library.util.*,com.csc.library.utilities.*"%>
<jsp:useBean id="task" class="com.csc.library.system.Task" scope="page" />
<%

	task.setChannel(request, response);
	task.checkProfile();
	UProfile profile = task.getUProfile();
	ThaiUtilities th = new ThaiUtilities();
	String topic_name = th.ASCII2Unicode(request.getParameter("topic_name"));
	String topic_id = request.getParameter("topic_id");
	
  	CheckUser ch=new CheckUser();
	ch.setRequest(request);	
	ch.process();	
	
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<%@page contentType="text/html; charset=UTF-8"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>myHR Forums</title>
<script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
<link href="style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="CSS/DDMENU.css">
<script>
function sendMessage(topic_id,user_id,user_name)
{
    var text = DWRUtil.getValue("text");
    DWRUtil.setValue("text", "");
    Chat.addMessage(gotMessages, text,topic_id,user_id,user_name);
}
function clearHash(topic_id)
{
    Chat.clearHash(gotClear, topic_id);
}
function gotClear(messages)
{
	if(messages != ''){
		alert("ล้างข้อมูลเรียบร้อยแล้ว");
	}	
}
function addUser(topic_id,user_id,user_name)
{    
    Chat.addUser(gotUsers, topic_id,user_id,user_name);
}
function gotUsers(users)
{
    var userlog = "";
    for (var data in users)
    {
    	//alert(messages[data].role);
    	if(users[data].role == '2'||users[data].role == '1'){
    		var begin = "<img src='images/admin-chat.gif' width='20' height='20' /><span class='style7'>&nbsp;";
    		var end = "&nbsp;</span><br />";  
    		userlog = begin+ users[data].userName+end+ userlog;
    	}else{
    		var begin = "<img src='images/user_chat.gif' width='20' height='20' /><span class='syle7'>&nbsp;";
    		var end = "&nbsp;</span><br />";    		
    		userlog = begin+ users[data].userName+end+userlog;    		
    	}   
    	    
    }
    //alert(chatlog);
    DWRUtil.setValue("userlog", "<div>"+userlog+"</div>");
}
function gotMessages(messages)
{
    var chatlog = "";
    for (var data in messages)
    {
    	//alert(messages[data].role);
    	if(messages[data].role == '2'){
    		var begin = "<img src='images/admin-chat.gif' width='20' height='20' /><span class='style7'>&nbsp;";
    		var end = "&nbsp;</span><br />";  
    		chatlog = begin+ messages[data].userName+" : "+messages[data].timePost+" :: "+messages[data].text +end+ chatlog;
    	}else if(messages[data].role == '1'){
    		var begin = "<img src='images/admin-chat.gif' width='20' height='20' /><span class='styleC'>&nbsp;";
    		var end = "&nbsp;</span><br />";  
    		chatlog = begin+ messages[data].userName+" : "+messages[data].timePost+" :: "+messages[data].text +end+ chatlog;
    	}else{
    		var begin = "<img src='images/user_chat.gif' width='20' height='20' /><span class='syle7'>&nbsp;";
    		var end = "&nbsp;</span><br />";    		
    		chatlog = begin+ messages[data].userName+" : "+messages[data].timePost+" :: "+messages[data].text +end+chatlog;    		
    	}   
    	    
    }
    //alert(chatlog);
    DWRUtil.setValue("chatlog", "<div>"+chatlog+"</div>");       
}
function gotDatum(result)
{
    var messages = result[0];
    gotMessages(messages);
    
    var users = result[1];
    gotUsers(users);
}
function showMsg(){
	with(document.cscform){
		message.focus();
		Chat.getData(gotDatum,topic_id.value);
		setTimeout("showMsg()",1000);
	}
}
function goEnter(ev){	
	if (window.event && window.event.keyCode == 13){
  		sendMessage('<%=request.getParameter("topic_id")%>','<%=profile.get("employeeid") %>','<%=ch.getUserName() %>');
  	}
}
function initMessages(topic){
	with(document.cscform){
		Chat.initMessages(topic);
	}
}
</script>
</head>
<body onload="initMessages('<%=topic_id%>');addUser('<%=topic_id%>','<%=profile.get("employeeid") %>','<%=ch.getUserName() %>');showMsg();">
<form name="cscform" method="post" action="topic_chat.jsp">
<input type="hidden" name="topic_id" value="<%=topic_id%>"> 
<input type="hidden" name="topic_name" value="<%=topic_name%>"> 
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'></script> 
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/Chat.js'></script> 
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'></script> 

<table width="1005" border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td bgcolor="#FFFFFF"><br />
		
		<br />
		<table width="98%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF">
			<tr>
				<td bordercolor="#000000">
				<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#4F5D6F">
					<tr>
						<td height="39" class="maintitle"><%=topic_name%></td>
					</tr>
					<tr>
						<td class="subtitle"></td>
					</tr>
				</table>
				</td>
			</tr>
			<tr>
				<td bordercolor="#CCCCCC">
				<table width="100%" border="0" cellspacing="1" cellpadding="0">
					<tr valign="top">
						<td width="78%" height="300" class="td02" valign="top">
							<div id="chatlog"></div>
						</td>
						<td width="22%" valign="top" class="td01top">
						<table width="100%" border="0" cellpadding="0" cellspacing="1" class="td01top">
							<tr>
								<td valign="top"><div id="userlog"></div></td>
							</tr>
						</table>
						</td>
					</tr>
					<tr>
						<td colspan="2" class="td02">
						<div align="left">ข้อความ 
							<input id="text" name="message" type="text" size="100" onkeypress="goEnter(event);" tabindex="0"/> 
							<a href="javascript:sendMessage('<%=topic_id%>','<%=profile.get("employeeid") %>','<%=ch.getUserName() %>');"><img src="images/go.jpg" width="60" height="22" align="absmiddle" border="0" /></a>&nbsp;
							
							<%if(ch.canLockTopic()){ %>
							<input type="button" value="ลบข้อความ" onclick="clearHash('<%=topic_id%>');">
							<%} %>
						</div>
						</td>
					</tr>
					<!-- 
					<tr>
						<td colspan="2" class="td01">
						<div align="right"><img src="images/kl_db.gif" width="20" height="20" align="absmiddle" /> knowledge <img src="images/eldb.gif" width="20" height="20" align="absmiddle" /> e-learning</div>
						</td>
					</tr>
					-->
				</table>
				</td>
			</tr>
		</table>
		<br />
		</td>
	</tr>
</table>
</form>
<jsp:include page="footer.jsp" flush="false" />