var msgInterval, usrInterval;
var updateMsgInterval = 5000;
var updateUsrInterval = 20000;
var smileyStatus = 0; //hide
var maxMsgID = 0; // initial value

function updateMsg()
{
var xmlHttp;
document.body.style.cursor='default';
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  
  
  
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
		var str=xmlHttp.responseText;
		//alert(str);
		//var  i=	  str.indexOf('<body>');
		//var  j =  str.indexOf('</body>');
		var x = str.indexOf("<msgID>")+7;
		var y = str.indexOf("</msgID>");
		var returnMaxMsgID = parseInt(str.substring(x, y));
		if (returnMaxMsgID > maxMsgID)
     	{
     		// update the client
     		document.getElementById("message_list").innerHTML=str;
			//document.getElementById("message_list").innerHTML=str.substring(i+ 6,j);
	        objDiv = document.getElementById("message_list");
	        objDiv.scrollTop = 0;
	        objDiv.scrollTop = objDiv.scrollHeight;
	        // Also, update client variable maxMsgID to latest message ID
	        maxMsgID = returnMaxMsgID;	        
        }
        
      }
    }
  
   
  //xmlHttp.open("GET","chat_messages.asp",true);
  xmlHttp.open("GET","chat_messages.asp?MaxID=" + maxMsgID.toString(),true);
  xmlHttp.setRequestHeader( "Content-Type", "text/HTML");
  xmlHttp.setRequestHeader( "CharSet", "UTF-8");
  xmlHttp.send(null);
  
  document.body.style.cursor='auto';
  }
  
function updateUsr()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  
  
  
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      document.getElementById("member_list").innerHTML=xmlHttp.responseText;
      objDiv = document.getElementById("member_list");
      objDiv.scrollTop = objDiv.scrollHeight;
      }
    }
  
  xmlHttp.open("GET","chat_members.asp",true);
  xmlHttp.setRequestHeader( "Content-Type", "text/HTML");
  xmlHttp.setRequestHeader( "CharSet", "UTF-8");
  xmlHttp.send(null);
  
  }
  
function postMessage()
{
document.body.style.cursor='default';
// get information from submit form
  name = document.frmShoutbox.name.value;
  msg = document.frmShoutbox.msg.value;  
  color = document.frmShoutbox.color.options[document.frmShoutbox.color.selectedIndex].value;
  fsize = document.frmShoutbox.fsize.options[document.frmShoutbox.fsize.selectedIndex].value;
  if (msg == "")
  {
  	alert('Vui long nhap noi dung!');
  }
else
{
var xmlHttp;
var numOfSmileys = 35;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }

  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      
      currentMessages = document.getElementById("message_list").innerHTML;
      for (var x = 1; x <= numOfSmileys; x++)
      {
		msg = msg.replace("-" + x + "-",  " <img src='images/smileys/" + x + ".gif' align=top> ");
	  }
	  updatedMessage = currentMessages + " <font class='Name'> <b>" + name + ":</b></font>" + " <font class='Message'><span style=color:" + color + ";font-size:" + fsize + ";>" + msg + "</span></font><br>";	  
	  
	  document.getElementById("message_list").innerHTML=updatedMessage;
      objDiv = document.getElementById("message_list");
      objDiv.scrollTop = 0;
      objDiv.scrollTop = objDiv.scrollHeight;
      
      // clear the input text field
      document.frmShoutbox.msg.value = "";
      }
    }
  
  
  
  var url = "chat_add.asp?name=" + encodeURIComponent(name) + "&msg=" + encodeURIComponent( msg)+"&color=" + color + "&fsize=" + fsize; 
	
  xmlHttp.open("GET",url,true);
  xmlHttp.setRequestHeader( "Content-Type", "text/HTML");
  xmlHttp.setRequestHeader( "CharSet", "UTF-8");
  xmlHttp.send(null);  
 
 

  // xmlHttp.open("POST", "chat_add.asp", false);
	//xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	//xmlHttp.send(params);
}
document.body.style.cursor='auto';  
}
  
function startUpdatingMsg()
{
	msgInterval = setInterval ("updateMsg()", updateMsgInterval);

}

function stopUpdatingMsg()
{
	clearInterval(msgInterval);
}

function startUpdatingUsr()
{
	usrInterval = setInterval ("updateUsr()", updateUsrInterval);

}

function stopUpdatingUsr()
{
	clearInterval(usrInterval);
}


function disableEnterKey(e)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     

     if (key == 13)
     {
     	 postMessage();
     } 
     return (key != 13);
}
function toggleSmileys(id)
{
	var myStatus;
	if (smileyStatus == 1)
	{
		smileyStatus = 0;
		myStatus = 'none';
	}
	else
	{
		smileyStatus = 1;
		myStatus = 'block';
	}
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = myStatus;
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = myStatus;
		}
		else { // IE 4
			document.all.id.style.display = myStatus;
		}
	}
}

function insertAtCursor_NEW(myField, myValue) {
	//IE support
	if (document.selection) {
	var temp;
	myField.focus();
	sel = document.selection.createRange();
	temp = sel.text.lenght;
	sel.text = myValue;
	if (myValue.length == 0) {
	sel.moveStart('character', myValue.length);
	sel.moveEnd('character', myValue.length);
	} else {
	sel.moveStart('character', -myValue.length + temp);
	}
	sel.select();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
	var startPos = myField.selectionStart;
	var endPos = myField.selectionEnd;
	myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
	myField.selectionStart = startPos + myValue.length;
	myField.selectionEnd = startPos + myValue.length;
	} else {
	myField.value += myValue;
	}
}
