
	var init=true;
	var notLoadingChat=true;

	function formatText(textstr)
	{
		return textstr.replace(/</g,"&lt;");
	}
		
	function getChat()
	{
		//Get and display last msg using HTTP request obj
		if (notLoadingChat)
		{
			notLoadingChat=false;
			objAsp= new ActiveXObject("MICROSOFT.XMLHTTP");
			postXML= new ActiveXObject("MICROSOFT.XMLDOM");
			postXML.async=false;
		
			objAsp.onreadystatechange=checkIfLoaded;
			//Post some XML to show whether this is the first time that the chatXML is being requested
			var postXMLStr="<chat><firsttime>" +init +"</firsttime></chat>";
			postXML.loadXML(postXMLStr);
			init=false;
			objAsp.open("POST", "getChat.asp", true);
			objAsp.send(postXMLStr);
		}
	} // end function getChat

	function updateChat()
	{
		//Use the HTTPrequest header to add a line of chat to the one held on the server.
		colourSelected="000000";

		//if text message is empty, donot post the msg
		if (textmsg.value!="")
		{
			//Check which radio button has been selected
			var textmessage=formatText(textmsg.value);	
			for (var j=0;j<5;j++)
			{
				if (colourSelect[j].checked)
				{
					if (colourSelect[j].value=="other")
					{
						colourSelected=userColourCode.value;
					}else{
						colourSelected=colourSelect[j].value;
					}
				}
			}
			objAsp2= new ActiveXObject("MICROSOFT.XMLHTTP");
			postXML= new ActiveXObject("MICROSOFT.XMLDOM");
			postXML.async=false;

			// here is the tricky bit, we have to parse msg as CDATA - in case any illegal character. eg. < ...
			postXMLStr="<line><text><![CDATA[" +textmessage +"]]></text><from>" +username.value +"</from><style>" +style.value +"</style><colour>" +colourSelected +"</colour></line>";
			postXML.loadXML(postXMLStr);
			Address="updateChat.asp";	// call to update
			objAsp2.open("POST", Address, true);
			objAsp2.send(postXML);
			textmsg.value="";
		}
	} // end function updateChat
	
	function checkIfLoaded()
	{
		//The HTTPrequest object has several states during loading.
		//Check whether is state=4 which means that it is fully loaded.
		//If it is then display add the contents to the text area
		if (objAsp.readystate==4)
		{
			notLoadingChat=true;
			if (objAsp.responseXML.xml!="")
			{
				var newChatXML=objAsp.responseXML;
				var lineXML=newChatXML.documentElement.selectNodes("line");
				for (var i=0;i<lineXML.length;i++)
				{
					lineNodes=lineXML(i);
					chatText=lineNodes.selectSingleNode("text");
					chatFrom=lineNodes.selectSingleNode("from");
					chatStyle=lineNodes.selectSingleNode("style");
					chatColour=lineNodes.selectSingleNode("colour");
					ChatArea.innerHTML=ChatArea.innerHTML +"<FONT face=tahoma color=" +chatColour.text +"><B>" +chatFrom.text +"</B>" +chatStyle.text +" " +chatText.text +"</FONT><BR/>";
					ChatArea.scrollIntoView(false);
				}
			}
		}
	}