// JScript 文件//Global variables
var timeID;
var refreshRate = 2000; // two seconds
var rnd = Math.random();var isFirefox;
var isIE;var url;//var XmlHttp;
var AjaxServerPageName;
AjaxServerPageName = "Server.aspx";//Creating and setting the instance of appropriate XMLHTTP Request object to a xmlHttp variable  
function getAjax()
{
var XmlHttp;
//在IE下创建XMLHTTPRequest对象
try
{
XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

catch(oc)
{
XmlHttp = null;
}
}
//在Mozilla和Safari下创建XMLHTTPRequest对象
if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
{
XmlHttp = new XMLHttpRequest();
}
return XmlHttp;
}// 获取浏览器类型
function sniffBrowserType() {
isFirefox = ( navigator.appName == "Netscape" );
isIE = (navigator.appName == "Microsoft Internet Explorer" ); 
}// 捕捉事件,发送消息Capture the enter key on the input box and post message
function captureReturn( event )
{
if(event.which || event.keyCode)
{
if ((event.which == 13) || (event.keyCode == 13)) 
{
postText();
return false;
}
else {
return true;
}
}
}// 开始更新计时
function setTimers()
{
timeID = window.setTimeout( "updateAll()", refreshRate );
}// 更新开始并且重置计时器
function updateAll()
{
window.clearTimeout( timeID );
getUserList();
setTimers();
}
//获取用户列表
function getUserList()
{
rnd++;
url = 'Server.aspx?action=UserList&session=' + rnd;
req = getAjax(); req.onreadystatechange = function(){

if( req.readyState == 4 && req.status == 200 ) {

obj = getElement( "userlist" );
obj.innerHTML = req.responseText;
getBufferText();
}

}
req.open( 'GET', url, true );
req.send( null );
}
//获取聊天记录
function getBufferText()
{
    rnd++;
url = 'Server.aspx?action=GetMsg&session=' + rnd;
req = getAjax();

req.onreadystatechange = function(){

if( req.readyState == 4 && req.status == 200 ) {

obj = getElement( "chatbuffer" );
obj.innerHTML = req.responseText;
scrollChatPane();
//FocusWindow();
}
}
req.open( 'GET', url , true );
req.send( null );
}
function validate()
{
   var name=getElement("name");
       url = 'Server.aspx?action=checkLogin&u='+escape(name.value);
       req = getAjax();
       
       req.onreadystatechange = function(){
       
   if(req.readyState == 4 && req.status == 200 ) {
 obj=getElement("msg");
 var res=req.responseText;
 if(escape(res)=="OK"){
      window.location.href='Server.aspx?action=Login&u='+escape(name.value);
    return;
 }
obj.innerHTML=res;
}

}
req.open( 'GET', url , true );
req.send( null );
}
//发表消息
function postText()
{
rnd++;
//获取消息并清空文本框
chatbox = getElement( "mytext" );
chat = chatbox.value;
chatbox.value = "";
//从URL中获取游客的GUID
//得到href属性中,'?'后面的部分。   
 //  <a   href="http://localhost/web.asp?a=1&b=2"></a>会得到'a=1&b=2'
userid = location.search.substring( 1, location.search.length );//文件名后面URL部份,,包括"?",, //组合Ajax服务器URL
url = 'Server.aspx?action=PostMsg&u=' + userid + '&t=' + encodeURIComponent(chat) + '&session=' + rnd;
//创建并实例化XMLHTTPRequest对象
req = getAjax();
//更新页面显示新消息
req.onreadystatechange = function(){ if( req.readyState == 4 && req.status == 200 ) 
{
updateAll();
}

}

req.open( 'GET', url, true );
req.send( null );
}
//获取节点的值
function getElement( id ) 
{
if( isIE ) {
return document.all[ id ];
}
else {
return document.getElementById( id );
}
}//显示载入提示
function showLoadScreen()
{
var loading = "<div style=\"text-align:center;color:red;\"><img src=\"img/loader.gif\" /><h4>Loading...</h4></div>"; chat = getElement( "chatbuffer" );
chat.innerHTML = loading;

user = getElement( "userlist" );
user.innerHTML = loading;
}function scrollChatPane()
{
var obj = document.getElementById("chatpane");
obj.scrollTop = obj.scrollHeight;
}function setFocus(ControlName)
{
var control = document.getElementById(ControlName);
if( control != null )
{
control.focus();
}
}function FocusWindow()
{
window.focus();
}