//Global variables
var timeID;
var refreshRate = 2000; // two seconds
var rnd = Math.random();

var isFirefox;
var isIE;

//var XmlHttp;
var AjaxServerPageName;
AjaxServerPageName = "online_ajax.aspx";

//Creating and setting the instance of appropriate XMLHTTP Request object to a xmlHttp?variable  
function getAjax()
{
	var XmlHttp;
	
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
	return XmlHttp;
}

// Get browser type
function sniffBrowserType() {
	isFirefox = ( navigator.appName == "Netscape" );
	isIE = (navigator.appName == "Microsoft Internet Explorer" ); 
}
function exitformPage()
{
　if(event.clientX < 0 && event.clientY<0)
　{ 
　    exitAjaxForm();
　    return false;
　}
　
}

function exitAjaxForm()
{
    rnd++;
　  url = 'online_ajax.aspx?action=ExitUser&session=' + rnd;
	req = getAjax();
	req.onreadystatechange = function(){	
		if( req.readyState == 4 && req.status == 200 ) {
			//getBufferText();
		}
	req.open( 'GET', url, true );
	req.send( null );
　}
}
// 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)) 
		{
		if(trimStr(getElement( "mytext" ).value) !="")
            {
			            postText();
			            getElement( "mytext" ).value ="";
			            return false;
			 }
            else {
            alert('请输入信息！');
	            return true;
            }
			
		}
		else {
			return true;
		}
	}	

	
}
function captureReturnBut( )
{
if(trimStr(getElement( "mytext" ).value) !="")
{
	postText();
	getElement( "mytext" ).value ="";
	return false;
}
else {
alert('请输入信息！');
	return true;
}
}

// Start the update timer
function setTimers()
{
	timeID = window.setTimeout( "updateAll()", refreshRate );
}

function setTimersAll()
{
	timeID = window.setTimeout( "updateAllList()", refreshRate );
}

// Start to update and reset the update timer
function updateAll()
{
	window.clearTimeout( timeID );
	getUserList();
	setTimers();
}

function updateAllList()
{
	window.clearTimeout( timeID );
	getUserListContent();
	setTimersAll();
}


function getUserList()
{
	rnd++;
	url = 'online_ajax.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 getUserListContent()
{
	rnd++;
	url = 'online_ajax.aspx?action=UserList&session=' + rnd;
	req = getAjax();

	req.onreadystatechange = function(){
	
		if( req.readyState == 4 && req.status == 200 ) {
		
//			obj = getElement( "userlist" );
//			obj.innerHTML = req.responseText;
			getBufferTextContent();
		}
	
	}
	
	req.open( 'GET', url, true );
	req.send( null );
}


function getBufferText()
{
    rnd++;
	url = 'online_ajax.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();
			window.parent.SessionGH.location.reload();  
			//document.frames.SessionGH.document.execCommand(’refresh’);
			//FocusWindow();
		}
	}
	
	req.open( 'GET', url , true );
	req.send( null );
}

function getBufferTextContent()
{
    rnd++;
	url = 'online_ajax.aspx?action=GetMsg&session=' + rnd;
	req = getAjax();
	
	req.onreadystatechange = function(){
	
		if( req.readyState == 4 && req.status == 200 )
		 {
		
			window.parent.SessionGH.location.reload();
			
			scrollChatPaneBottom();
		}
	}
	
	req.open( 'GET', url , true );
	req.send( null );
}

function postText()
{
	rnd++;
	chatbox = getElement( "mytext" );
	chat = chatbox.value;
	chatbox.value = "";
	var ghName = getElement( "useNameId") 
	var SessDbId = getElement( "SessLosid") 
	userid = location.search.substring( 1, location.search.length );
	url = 'online_ajax.aspx?action=PostMsg&u=' + ghName.value + '&t=' + chat + '&session=' + rnd+ '&sessionDB='+SessDbId.value ;
	
	req = getAjax();
	
	req.onreadystatechange = function(){
	
		if( req.readyState == 4 && req.status == 200 ) {
			scrollChatPaneBottom();
			window.parent.SessionGH.location.reload();
		}
	
	}
	
	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;\"><h5>Loading...</h5></div>";

//	chat = getElement( "chatbuffer" );
//	chat.innerHTML = loading;
//	
//	user = getElement( "userlist" );
//	user.innerHTML = loading;
}

function scrollChatPane()
{
	var obj = document.getElementById("SessionGH");
	obj.scrollTop = obj.scrollHeight;
	
}


function scrollChatPaneBottom()
{
	var obj = document.getElementById("SessionGH");
	obj.scrollTop = obj.scrollHeight;
}


function setFocus(ControlName)
{
	var control = document.getElementById(ControlName);
	if( control != null )
	{
		control.focus();
	}
}
function FocusWindow()
{

	//TODO: FireFox doesn't work?
	window.focus();

}

function trimStr(str) {
return str.replace(/(\s+)$/g, '');  
}