
			var sendReq = getXmlHttpRequestObject();
			var receiveReq = getXmlHttpRequestObject();
			var lastMessage = 0;
			var mTimer;
			//Function for initializating the page.
			function startChatOnline() {
				//Set the focus to the Message Box.
				//document.getElementById('txt_message').focus();
				//Start Recieving Messages.				
				getChatOnline();			}		
			//Gets the browser specific XmlHttpRequest Object
			function getXmlHttpRequestObject() {
				if (window.XMLHttpRequest) {
					return new XMLHttpRequest();
				} else if(window.ActiveXObject) {
					return new ActiveXObject("Microsoft.XMLHTTP");
				} else {
					document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object.  Consider upgrading your browser.';
				}
			}



			//Gets the current messages from the server
			function getChatOnline() {
				if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
					receiveReq.open("GET", 'http://365muaban.vn/getChatOnline.php?cat=11&last=' + lastMessage, true);
					receiveReq.onreadystatechange = handleReceiveOnline; 
					receiveReq.send(null);
				}			
				document.getElementById('div_chatonline').innerHTML='';			
			}

			//Function for handling the return of chat text
			function handleReceiveOnline() {
				if (receiveReq.readyState == 4) {
					var chat_div = document.getElementById('div_chatonline');
					var xmldoc = receiveReq.responseXML;
					var message_nodes = xmldoc.getElementsByTagName("message"); 
					var n_messages = message_nodes.length
					for (i = 0; i < n_messages; i++) {
						var user_node = message_nodes[i].getElementsByTagName("user");

						chat_div.innerHTML += '<a href="javascript:window.alert(\'Vui lòng đăng nhập mới được sử dụng chức năng.\')" onMouseOver="Tip(\'Tài khoản: <b>'+ user_node[0].firstChild.nodeValue +'</b><br>(<a href=http://365muaban.vn/shop/'+ user_node[0].firstChild.nodeValue +' target=_blank>Vào shop</a>)\', WIDTH, 135, TITLE, \'Thông tin\', SHADOW, true, FADEIN, 300, FADEOUT, 300, STICKY, 1, CLOSEBTN, true, CLICKCLOSE, true)" onMouseOut="UnTip()">' + user_node[0].firstChild.nodeValue + '</a>, ';
						//lastMessage = (message_nodes[i].getAttribute('id'));
					}
					mTimer = setTimeout('getChatOnline();',10000); //Refresh our chat in 10 seconds
				}
			}
	
		function addtext(text) {document.frmmain.txt_message.value = document.frmmain.txt_message.value+= text;document.getElementById('txt_message').focus();}

		function limitText(limitField, limitCount, limitNum) {
			var limitCount;
			if (limitField.value.length > limitNum) {
				limitField.value = limitField.value.substring(0, limitNum);
			} else {
				limitCount.value = limitNum - limitField.value.length;
			}
		}




	(function($){
		var EADVIETNAM = window.EADVIETNAM = function() {
			var _registered = {
				init: []
			};
			return {
				init: function() {
					$.each(_registered.init, function(nr, fn){
						fn.call();
					});
				},
				extend: function(prop) {
					for (var i in prop) {
						if (prop[i] != undefined) {
							this[i] = prop[i];
						}
					}
				},
				register: function(fn, type) {
					if (!_registered[type]) {
						_registered[type] = [];
					}
					_registered[type].push(fn);
				}
			};
		}();
		$(EADVIETNAM.init);
	})(jQuery);
	

	//Khởi tạo ColorPicker
	(function($){
		var ViColor = function() {
			$('#colorSelector').ColorPicker({
				color: '#666666',
				onShow: function (colpkr) {
					$(colpkr).fadeIn(500);
					return false;
				},
				onHide: function (colpkr) {
					$(colpkr).fadeOut(500);
					return false;
				},
				onChange: function (hsb, hex, rgb) {
					EadChat.TextColor = hex;
				}
			});
		};	
		EADVIETNAM.register(ViColor, 'init');
	})(jQuery)



	var EadChat={	
		lastMessage : 0,
		mTimer: null,
		lastMessage_p : 0,
		mTimer_p: null,
		UserChat: '',
		TextColor: '666666',
		FontSize: '12',
		SmilerDir: 'http://365muaban.vn/templates/smiler/',
		Format: new Array(false,false,false,false,false,false),
		
		//Hàm bắt sự kiện nhấn Enter.
		checkkey: function(e){
			if( !e ) {
				if( window.event ) {
					e = window.event;
				} else {
					return;
				}
			}
	
			if( e.keyCode == 13  ) {
				this.blockSubmit();
			} else if( e.which == 13 ) {
				this.blockSubmit();
			} else if( e.charCode == 13  ) {
				this.blockSubmit();
			} else {
				return;
			}
		},
		
		startChat:function() {
//		<!-- IF session_user -->
//			document.getElementById('txt_message').focus();
//		<!-- ENDIF session_user -->
			this.getChatText();
		},		


		getChatText:function() {
			Vi = this;
			$.ajax({
				type: "GET",
			   	url: "http://365muaban.vn/getChat.php",
			   	data: "chat=1&cat=11&user=&mid=1280584808&last="+this.lastMessage,
			   	success: function(msg){
			    	Vi.handleReceiveChat(msg);
			  	}
			});	
		},


///////////////////////////////////////////////////////////////////////////////////////
		startChat_pop:function() {
//		<!-- IF session_user -->
//			document.getElementById('txt_message').focus();
//		<!-- ENDIF session_user -->
			this.getChatText_pop();
		},		


		getChatText_pop:function() {
			Vi = this;
			$.ajax({
				type: "GET",
			   	url: "http://365muaban.vn/getChat.php",
			   	data: "chat=1&cat=&user=&mid=1280584808&last="+this.lastMessage_p,
			   	success: function(msg){
			    	Vi.handleReceiveChat_pop(msg);
			  	}
			});	
		},
		
		handleReceiveChat_pop:function(xmldoc) {
			var chat_div_pop = document.getElementById('div_chat_pop');
			var message_nodes = xmldoc.getElementsByTagName("message"); 
			var n_messages = message_nodes.length
			for (i = 0; i < n_messages; i++) {
				var user_node = message_nodes[i].getElementsByTagName("user");
				var text_node = message_nodes[i].getElementsByTagName("text");
				var time_node = message_nodes[i].getElementsByTagName("time");
				var T_Color = message_nodes[i].getElementsByTagName("color");
				var code_node = message_nodes[i].getElementsByTagName("code");
				T_Color = T_Color[0].firstChild.nodeValue;
				var F_Size = message_nodes[i].getElementsByTagName("size");
				F_Size = F_Size[0].firstChild.nodeValue;
				this.lastMessage_p = (message_nodes[i].getAttribute('id'));
				
						chat_div_pop.innerHTML += '<span class="chat_username"><a href="javascript:window.alert(\'Vui lòng đăng nhập mới được sử dụng chức năng.\')">' + user_node[0].firstChild.nodeValue + '</a></span> '; 
										
				chat_div_pop.innerHTML += '<span class="chat_time">(' + time_node[0].firstChild.nodeValue + ')</span>: ';
				var strText = this.insertImage(text_node[0].firstChild.nodeValue);
				chat_div_pop.innerHTML += '<span style="font-size: '+F_Size+'px; color: #'+T_Color+'">'+ strText + '</span><br />';
				chat_div_pop.scrollTop = chat_div_pop.scrollHeight;
			}
			this.mTimer_p = setTimeout('EadChat.getChatText_pop();',1000); //Refresh our chat in 2 seconds
		},
		
////////////////////////////////////////////////////////////////////////////////////////

		sendChatText:function() {
			if($('#txt_message').val().length < 2) {
				$('#txt_message').val('');
				$('#txt_message').focus();
				return;
			}
			var ChatText = $('#txt_message').val();
			if(this.Format[3]){
				ChatText = ChatText.bold();
			}
			if(this.Format[4]){
				ChatText = ChatText.italics();
			}
			if(this.Format[5]){
				ChatText = '<u>'+ChatText+'</u>';
			}
			var message = 'message='+ChatText;
			Vi = this;
			$.ajax({
				type: "POST",
			   	url: "http://365muaban.vn/getChat.php?chat=1&cat=11&last=" + this.lastMessage,
			   	data: message+"&name="+this.UserChat+"&chat=1&Color="+this.TextColor+"&Size="+this.FontSize,
			   	success: function(msg){
			    	//Vi.handleSendChat();
					timedCount();
					$('#txt_message').val('');
			  	}
			});						
		},


		handleSendChat:function() {
			clearInterval(this.mTimer);
			this.getChatText();
			clearInterval(this.mTimer_p);
			this.getChatText_pop();
			timedCount();
		},


		handleReceiveChat:function(xmldoc) {
			var chat_div = document.getElementById('div_chat');
			var message_nodes = xmldoc.getElementsByTagName("message"); 
			var n_messages = message_nodes.length
			for (i = 0; i < n_messages; i++) {
				var user_node = message_nodes[i].getElementsByTagName("user");
				var text_node = message_nodes[i].getElementsByTagName("text");
				var time_node = message_nodes[i].getElementsByTagName("time");
				var T_Color = message_nodes[i].getElementsByTagName("color");
				var code_node = message_nodes[i].getElementsByTagName("code");
				T_Color = T_Color[0].firstChild.nodeValue;
				var F_Size = message_nodes[i].getElementsByTagName("size");
				F_Size = F_Size[0].firstChild.nodeValue;
				this.lastMessage = (message_nodes[i].getAttribute('id'));
				
						chat_div.innerHTML += '<span class="chat_username"><a href="javascript:window.alert(\'Vui lòng đăng nhập mới được sử dụng chức năng.\')">' + user_node[0].firstChild.nodeValue + '</a></span> '; 
										
				chat_div.innerHTML += '<span class="chat_time">(' + time_node[0].firstChild.nodeValue + ')</span>: ';
				var strText = this.insertImage(text_node[0].firstChild.nodeValue);
				chat_div.innerHTML += '<span style="font-size: '+F_Size+'px; color: #'+T_Color+'">'+ strText + '</span><br />';
				chat_div.scrollTop = chat_div.scrollHeight;
			}
			this.mTimer = setTimeout('EadChat.getChatText();',1000); //Refresh our chat in 2 seconds
		},

		blockSubmit:function() {
			this.sendChatText();
			return false;
		},
		
		handleResetChat:function() {
			$('#div_chat').html("");
			this.getChatText();
			
		},


		showsmiler: function()	{
			var EText = '<table style="margin: 0; padding: 0">';
			EText += '<td align="center"><img src="'+ this.SmilerDir + '01.png" width="24" alt="" title=";01;" onclick="EadChat.insertSymbol(\';01;\'); return hs.close(this);"/></td>';
			EText += '<td align="center"><img src="'+ this.SmilerDir + '02.png" width="24" alt="" title=";02;" onclick="EadChat.insertSymbol(\';02;\'); return hs.close(this);"/></td>';
			EText += '<td align="center"><img src="'+ this.SmilerDir + '03.png" width="24" alt="" title=";03;" onclick="EadChat.insertSymbol(\';03;\'); return hs.close(this);"/></td>';
			EText += '<td align="center"><img src="'+ this.SmilerDir + '04.png" width="24" alt="" title=";04;" onclick="EadChat.insertSymbol(\';04;\'); return hs.close(this);"/></td>';
			EText += '<td align="center"><img src="'+ this.SmilerDir + '05.png" width="24" alt="" title=";05;" onclick="EadChat.insertSymbol(\';05;\'); return hs.close(this);"/></td>';
			EText += '<td align="center"><img src="'+ this.SmilerDir + '06.png" width="24" alt="" title=";06;" onclick="EadChat.insertSymbol(\';06;\'); return hs.close(this);"/></td>';
			EText += '<td align="center"><img src="'+ this.SmilerDir + '07.png" width="24" alt="" title=";07;" onclick="EadChat.insertSymbol(\';07;\'); return hs.close(this);"/></td>';
			EText += '<td align="center"><img src="'+ this.SmilerDir + '08.png" width="24" alt="" title=";08;" onclick="EadChat.insertSymbol(\';08;\'); return hs.close(this);"/></td>';
			EText += '<td align="center"><img src="'+ this.SmilerDir + '09.png" width="24" alt="" title=";09;" onclick="EadChat.insertSymbol(\';09;\'); return hs.close(this);"/></td>';
			EText += '<td align="center"><img src="'+ this.SmilerDir + '10.png" width="24" alt="" title=";10;" onclick="EadChat.insertSymbol(\';10;\'); return hs.close(this);"/></td>';
			var k = 11;
			for(i=0; i<5; i++){
				EText += '<tr>'
				for (j = 0; j < 10; j++) {
					EText += '<td align="center"><img src="'+ this.SmilerDir + k + '.png" width="24" alt="" title=";' + k + ';" onclick="EadChat.insertSymbol(\';' + k + ';\'); return hs.close(this);"/></td>';
					k++;
				}
				EText += '<tr/>';
			}
			EText += '</table>';
			$('#smiler_box').html(EText);
		},

		insertSymbol: function(str){
			var Text = $('#txt_message').val();
			Text += str;
			$('#txt_message').val(Text);
			$('#txt_message').focus();
		},

		insertImage: function(str){
			var index = str.indexOf(";");
			if( (index!= -1) && (index < (str.length-3))){
				var arrstr = str.split(';');
					for (k = 1; k < arrstr.length; k += 2) {
						var alphaExp = /^[0-9]+$/;
						if ((arrstr[k].match(alphaExp)) && (arrstr[k] > 0) && (arrstr[k] < 61)) {
							arrstr[k] = '<img src="' + this.SmilerDir + arrstr[k] + '.png" width="24" alt="" title=";' + arrstr[k] + ';"/>'
						}
						else {
							arrstr[k] = ';' + arrstr[k] + ';';
						}
					}
					var text = '';
					for(l=0; l<arrstr.length; l++)
						text += arrstr[l];
					str = text;
			}
			return str;
		},


		formatText: function(str){
			switch(str){
				case 'bold':
					if (!this.Format[3]) {
						if (this.Format[0]) {
							document.getElementById('bold').src = 'http://365muaban.vn/templates/images/nobold.png';
							this.Format[0] = false;
						}
						else {
							document.getElementById('bold').src = 'http://365muaban.vn/templates/images/sebold.png';
							this.Format[0] = true;
						}
					}
					break;
				case 'italic':
					if (!this.Format[4]) {
						if (this.Format[1]) {
							document.getElementById('italic').src = 'http://365muaban.vn/templates/images/noitalic.png';
							this.Format[1] = false;
						}
						else {
							document.getElementById('italic').src = 'http://365muaban.vn/templates/images/seitalic.png';
							this.Format[1] = true;
						}
					}
					break;
				case 'underline':
					if (!this.Format[5]) {
						if (this.Format[2]) {
							document.getElementById('underline').src = 'http://365muaban.vn/templates/images/nounderline.png';
							this.Format[2] = false;
						}
						else {
							document.getElementById('underline').src = 'http://365muaban.vn/templates/images/seunderline.png';
							this.Format[2] = true;
						}
					}
					break;
			}
			$('#txt_message').focus();
		},


		formatText2: function(str){
			switch(str){
				case 'bold':
					if (this.Format[3]) {
						document.getElementById('bold').src = 'http://365muaban.vn/templates/images/nobold.png';
						this.Format[3] = false;
					}
					else {
						document.getElementById('bold').src = 'http://365muaban.vn/templates/images/sebold.png';
						this.Format[3] = true;
					}
					break;
				case 'italic':
					if (this.Format[4]) {
						document.getElementById('italic').src = 'http://365muaban.vn/templates/images/noitalic.png';
						this.Format[4] = false;
					}
					else {
						document.getElementById('italic').src = 'http://365muaban.vn/templates/images/seitalic.png';
						this.Format[4] = true;
					}
					break;
				case 'underline':
					if (this.Format[5]) {
						document.getElementById('underline').src = 'http://365muaban.vn/templates/images/nounderline.png';
						this.Format[5] = false;
					}
					else {
						document.getElementById('underline').src = 'http://365muaban.vn/templates/images/seunderline.png';
						this.Format[5] = true;
					}
					break;
			}
			$('#txt_message').focus();
		},


		addmethod: function(){
			this.showsmiler();
			$('#bold').mouseover(function(){EadChat.formatText('bold');});
			$('#bold').mouseout(function(){EadChat.formatText('bold');});
			$('#bold').click(function(){EadChat.formatText2('bold');});
			$('#italic').mouseover(function(){EadChat.formatText('italic');});
			$('#italic').mouseout(function(){EadChat.formatText('italic');});
			$('#italic').click(function(){EadChat.formatText2('italic');});
			$('#underline').mouseover(function(){EadChat.formatText('underline');});
			$('#underline').mouseout(function(){EadChat.formatText('underline');});
			$('#underline').click(function(){EadChat.formatText2('underline');});
		}
	};
	
		EadChat.UserChat = '';
		hs.graphicsDir = 'http://365muaban.vn/templates/highslide/graphics/';
		hs.outlineType = 'glossy-dark';
		hs.showCredits = false;
		hs.wrapperClassName = 'draggable-header';

