var c_xmlhttp;
var chat_msg_notify = '';
var IsMute = true;

var fco = new SWFObject("custom/chat/notify.swf", "notify", "1", "1", "8.0.15", "#ffffff", true);

function RunXML_C(mode,url,params,get) {
	// code for Mozilla, etc.
	if (window.XMLHttpRequest) {
	   c_xmlhttp=new XMLHttpRequest();
	   if (get) c_xmlhttp.onreadystatechange=LoadPage
	   c_xmlhttp.open(mode,url,true);
	   if (params) {
	      c_xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	      c_xmlhttp.setRequestHeader("Content-length", params.length);
	      c_xmlhttp.setRequestHeader("Connection", "close");
	   }
	   c_xmlhttp.send(params);
	} else if (window.ActiveXObject) {    // code for IE
	   c_xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	   if (c_xmlhttp) {
	      if (get) c_xmlhttp.onreadystatechange=LoadPage;
	      c_xmlhttp.open(mode,url,true);
	      if (params) {
	         c_xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	         c_xmlhttp.setRequestHeader("Content-length", params.length);
	         c_xmlhttp.setRequestHeader("Connection", "close");
	      }
	      c_xmlhttp.send(params);
	   }
	}
}

function LoadPage() {
	if (c_xmlhttp.readyState==4) {
	   if (c_xmlhttp.status==200) {
	      var myRes = c_xmlhttp.responseText;
	      if (myRes != '') {
                 var cobj = document.getElementById('msg_list');
                 if (chat_msg_notify != myRes) cobj.innerHTML = myRes;
                 if (chat_msg_notify != myRes && chat_msg_notify != '') {
                    if (!IsMute) ChatNotify();
                 }
                 chat_msg_notify = myRes;                 
                 cobj.scrollTop = cobj.scrollHeight;
		 c_xmlhttp = null;
	      }
	   }
	}
}

function SendTextEnter(e,user_id) {
	var keycode;
	if (window.event) {
	   keycode = window.event.keyCode;
	} else if (e) {
	   keycode = e.which;
	} else {
	   return true;
	}
	
	if (keycode == 13) {
	   SendText(user_id);
	   
	}
}

function SendText(user_id) {
	var msg = document.getElementById('chat_msg').value;
	if (msg.length > 0) {
	   var url = '/custom/chat/chat.inc.php?action=send&user_id=' + user_id + '&msg=' + escape(msg);
	   RunXML_C('GET',url,null,true);
	}
	document.getElementById('chat_msg').value = '';
}

function UpdateChat() {
	var url = '/custom/chat/chat.inc.php?action=update';
	RunXML_C('GET',url,null,true);
}

function UpdateInterval() {
	UpdateChat();
	setInterval("UpdateChat()",1000);
}

function ChatNotify() {
      fco.write("c_flashcontent");
}

window.onload = UpdateInterval;