﻿// JScript File
<!--
var sendReq = getXmlHttpRequestObject();
			var sendReq = getXmlHttpRequestObject();
			var receiveReq = getXmlHttpRequestObject();
			var userReq = getXmlHttpRequestObject();
			var lastMessage = 0;
			var mTimer;
			var mTimerUser;
			//Function for initializating the page.
			function startChat() {
				//Set the focus to the Message Box.
				document.getElementById('txt_message').focus();
				//Start Recieving Messages.
				insertUser();		
				getChatText();
				
			}
			
			  var isChat;
       isChat = false;
             			
			//Gets the current messages from the server
			function insertUser() {
				if (userReq.readyState == 4 || userReq.readyState == 0) {
				    var schiamata;
				    schiamata = 'Chat/RiceviUser.aspx?chat=1&ins=1&rand=' + new Date().getTime();				 
					userReq.open("GET", schiamata, true);
					userReq.onreadystatechange = handleListaUserChat; 
					userReq.send(null);
				}			
			}
			
			//Gets the current messages from the server
			function getUser() {
				if (userReq.readyState == 4 || userReq.readyState == 0) {
				    var schiamata;
				    schiamata = 'Chat/RiceviUser.aspx?chat=1&rand=' + new Date().getTime();				 
					userReq.open("GET", schiamata, true);
					userReq.onreadystatechange = handleListaUserChat; 
					userReq.send(null);
				}			
			}
			
			//Gets the current messages from the server
			function outUser() {
			try
			{
			  if(isChat)
			  {
				  if (userReq.readyState == 4 || userReq.readyState == 0) {
				      var schiamata;
				      schiamata = 'Chat/RiceviUser.aspx?chat=1&ins=2&rand=' + new Date().getTime();				 
					  userReq.open("GET", schiamata, true);
					  userReq.onreadystatechange = handleUserOutChat; 
					  userReq.send(null);
				  }			
				}
			}
			catch(ex)
			{
			}
			}
			
			//Function for handling the return of chat text
			function handleListaUserChat() {
				if (userReq.readyState == 4) {
					var chat_div = document.getElementById('div_userlist');
					var xmldoc = userReq.responseXML;
					var message_nodes = xmldoc.getElementsByTagName("user"); 
					var n_messages = message_nodes.length;
					chat_div.innerHTML = "";
					for (i = 0; i < n_messages; i++) {
						var user_node = message_nodes[i].getElementsByTagName("name");
						chat_div.innerHTML += user_node[0].firstChild.nodeValue + '&nbsp;';
						chat_div.innerHTML += '<br />';
					}
					mTimerUser = setTimeout('getUser();',10000); //Refresh our list in 10 seconds
				}
			}
			
			//Function for handling the return of chat text
			function handleUserOutChat() {
				if (userReq.readyState == 4) {			
				}
			}
			
			//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 getChatText() {
				if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
				    var schiamata;
				    schiamata = 'Chat/RiceviChat.aspx?chat=1&last=' + lastMessage + '&rand=' + new Date().getTime();				 
					receiveReq.open("GET", schiamata, true);
					receiveReq.onreadystatechange = handleReceiveChat; 
					receiveReq.send(null);
				}			
			}
			
			//Add a message to the chat server.
			function sendChatText() {
				if(document.getElementById('txt_message').value == '') {
					alert("You have not entered a message");
					return;
				}
				if (sendReq.readyState == 4 || sendReq.readyState == 0) {
					sendReq.open("POST", 'Chat/RiceviChat.aspx?chat=1&last=' + lastMessage + '&rand=' + new Date().getTime() , true);
					sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					sendReq.onreadystatechange = handleSendChat; 
					var param = 'message=' + document.getElementById('txt_message').value;
					param += '&name=Ryan Smith';
					param += '&chat=1';
					sendReq.send(param);
					document.getElementById('txt_message').value = '';
				}							
			}
			//When our message has been sent, update our page.
			function handleSendChat() {
				//Clear out the existing timer so we don't have 
				//multiple timer instances running.
				clearInterval(mTimer);
				getChatText();
			}
			//Function for handling the return of chat text
			function handleReceiveChat() {
				if (receiveReq.readyState == 4) {
					var chat_div = document.getElementById('div_chat');
					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");
						var text_node = message_nodes[i].getElementsByTagName("text");
						var time_node = message_nodes[i].getElementsByTagName("time");
						chat_div.innerHTML += '<b>' + user_node[0].firstChild.nodeValue + '&nbsp;' + '</b>';
						chat_div.innerHTML += '<font class="chat_time">' + time_node[0].firstChild.nodeValue + '</font><br />';
						chat_div.innerHTML += text_node[0].firstChild.nodeValue + '<br />';
						chat_div.scrollTop = chat_div.scrollHeight;
						lastMessage = (message_nodes[i].getAttribute('id'));
					}
					mTimer = setTimeout('getChatText();',4000); //Refresh our chat in 2 seconds
				}
			}
			//This functions handles when the user presses enter.  Instead of submitting the form, we
			//send a new message to the server and return false.
			function blockSubmit() {
				sendChatText();
				return false;
			}
			
			function MandaChat(event)
      {
          if (event.keyCode == 13)   
          {   
              sendChatText();
          }   
      }
			
			//This cleans out the database so we can start a new chat session.
			function resetChat() {
				if (sendReq.readyState == 4 || sendReq.readyState == 0) {
					sendReq.open("POST", 'Chat/RiceviChat.aspx?chat=1&last=' + lastMessage + '&rand=' + new Date().getTime(), true);
					sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					sendReq.onreadystatechange = handleResetChat; 
					var param = 'action=reset';
					sendReq.send(param);
					document.getElementById('txt_message').value = '';
				}							
			}
			//This function handles the response after the page has been refreshed.
			function handleResetChat() {
				document.getElementById('div_chat').innerHTML = '';
				getChatText();
			}	








//-->
