var sendReqObj = createRequestObject();
var receiveReqObj = createRequestObject();
var lastMessage = 0;
var mTimer;


/* Function for initializating the page. */
function init() 
{
/* Setting the focus to the Message Box. */
	document.getElementById('txtMessageID2').focus();
/* Calling the function getText for recieving messages. */
	getText();
}

/* Making a request. */
function createRequestObject()
{
/* Initialising the variable xmlhttp */
	var xmlhttp = false;
/* Try and catch block for creating xmlhttp object according to the browser */
	try
	{
/* The xmlhttp object is created using the Microsoft's XML Parser. */
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try 
		{
		/* The xmlhttp object is created for Microsoft IE. */
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch (E) 
		{
			xmlhttp = false;
		}
	}
/* The xmlhttp object is created for browsers other than Microsoft IE. */
	if (!xmlhttp && typeof XMLHttpRequest!='undefined')
	{
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

/* Getting the current messages from the server */
function getText() 
{
	receiveReqObj.open("GET", 'doSql2.php?last=' + lastMessage, true);
	receiveReqObj.onreadystatechange = handleReceiveChat; 
	receiveReqObj.send(null);
}

/* Function for handling the return of chat text. */
function handleReceiveChat() 
{
	if (receiveReqObj.readyState == 4) 
	{
		if(receiveReqObj.status == 200) 
		{
			var chat_div = document.getElementById('divChat2');
		    chat_div.innerHTML = receiveReqObj.responseText;
			//var xmldoc = receiveReqObj.responseXML;
		}
	/* Refreshing the chat in every 2 seconds */
		mTimer = setTimeout('getText();',2000);
	}
}

/* Adding a message to the chat server. */
function sendText(ses,ip,message,ts,aD)
{
	//alert(ses+","+ip+","+message+","+ts+","+aD);
	if(document.getElementById('txtMessageID2').value == '')
	{
		alert("Enter a message");
		return;
	}
	if(document.getElementById('txtMessageID2').value != '')
	{
		//alert("OK");
		var obj = document.getElementById("bal");
		sendReqObj.open("POST", 'doSql2.php?ses=' + ses + '&ip=' + ip + '&message=' + message + '&ts=' + ts + '&aD=' + aD, true);
		sendReqObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		sendReqObj.onreadystatechange = handleSendChat; 
		sendReqObj.send(null);
		document.getElementById('txtMessageID2').value = "";
	}

}

/* After sending the message the page is updated. */
function handleSendChat() 
{
/* Clearing the existing timer so that there are no multiple timer instances running. */
	clearInterval(mTimer);
	getText();
}

function setStatus()
{
	var v = document.getElementById("sess").value;
	var xmlhttp2 = false;
/* Try and catch block for creating xmlhttp object according to the browser */
	try
	{
/* The xmlhttp object is created using the Microsoft's XML Parser. */
		xmlhttp2 = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try 
		{
		/* The xmlhttp object is created for Microsoft IE. */
			xmlhttp2 = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch (E) 
		{
			xmlhttp2 = false;
		}
	}
/* The xmlhttp object is created for browsers other than Microsoft IE. */
	if (!xmlhttp2 && typeof XMLHttpRequest!='undefined')
	{
		xmlhttp2 = new XMLHttpRequest();
	}
	
		serverPage = "SetStatus.php?s=" + v;
		xmlhttp2.open("POST", serverPage,true);
		xmlhttp2.onreadystatechange = function() 
		{
			if (xmlhttp2.readyState == 4 && xmlhttp2.status == 200) 
			{
				window.alert("NIST Help Desk!");
			}
		}
		xmlhttp2.send(null);	

	
}
