<?php 
if (!isset($_SESSION))
{
	session_start();
}
require_once('header.php');
echo "<h1>Chatbox</h1>";
if ($aangemeld == "ja")
{
	action("gaat in de chatbox");
	echo "<script>chat_functie = '".halen("SELECT functie FROM members WHERE naam='".$_SESSION['naam']."';")."';</script>";
?>
<script>
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try
{
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
	try
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (E)
	{
		xmlhttp = false;
	}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined')
{
	xmlhttp = new XMLHttpRequest();
}
var xmlhttp2=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try
{
	xmlhttp2 = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
	try
	{
		xmlhttp2 = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (E)
	{
		xmlhttp2 = false;
	}
}
@end @*/
if (!xmlhttp2 && typeof XMLHttpRequest!='undefined')
{
	xmlhttp2 = new XMLHttpRequest();
}
timer = 6000;
doemaar = true;
doemaar2 = true;
function say(value)
{
	if (doemaar2)
	{
		doemaar2 = false;
		document.getElementById('chattext').value = '';
     	document.getElementById('chattext').focus();
		name = document.getElementById('name').value;
    	xmlhttp2.open("GET", "chatgo.php?text="+value+"&name="+name+"&functie="+chat_functie+"&random="+Math.round(9000*Math.random()),true);
    	xmlhttp2.onreadystatechange=function()
    	{
			if (xmlhttp2.readyState==4)
        	{
	        	if ((xmlhttp2.responseText.split('<ajax>'))[1] != "" && xmlhttp2.responseText != "")
	        	{
		        	document.getElementById('chatwindow').innerHTML = (xmlhttp2.responseText.split('<ajax>'))[1];
		        	document.getElementById('chatwindow').scrollTop = document.getElementById('chatwindow').scrollHeight;
	        	}
				doemaar2 = true;
        	}
    	}
    	xmlhttp2.send(null);
	}
	else
	{
		setTimeout("say(document.getElementById('chattext').value)", 500);
	}
}
function update()
{
	if (doemaar)
	{
		doemaar = false;
		xmlhttp.open("GET", "chatgo.php?random="+Math.round(9000*Math.random()),true);
    	xmlhttp.onreadystatechange=function()
    	{
		    if (xmlhttp.readyState==4)
        	{
	        	if ((xmlhttp.responseText.split('<ajax>'))[1] != "" && xmlhttp.responseText != "")
	        	{
		        	document.getElementById('chatwindow').innerHTML = (xmlhttp.responseText.split('<ajax>'))[1];
		        	document.getElementById('chatwindow').scrollTop = document.getElementById('chatwindow').scrollHeight;
	        	}
	            doemaar = true;
	            timer = 6000;
    			counter();
        	}
    	}
    	xmlhttp.send(null);
	}
	else
	{
		setTimeout("update()", 500);
	}
}
function counter()
{
    if (timer == 0)
    {
        update();
    }
    else
    {
        timer = timer - 1000;
        setTimeout("counter()", 1000);
    }
}
function checkEnter(e)
{
	var characterCode;
	if (e && e.which)
	{
		e = e;
		characterCode = e.which;
	}
	else
	{
		e = event;
		characterCode = e.keyCode;
	}
	if (characterCode == 13)
	{
		say(document.getElementById('chattext').value);
		return false;
	}
	return true;
}
</script>
<body onload="document.getElementById('chattext').focus();">
<input type="hidden" id="name" name="name" value="<?php echo $_SESSION['naam']; ?>">
<div id="chatwindow" style="width: 525px; height: 400px; overflow:scroll;">Bezig met inladen...</div>
Chat: <input type="text" id="chattext" name="chattext" onKeyPress="checkEnter(event);" onChange="hasvalue(this.value);" style="width: 480px; ">
<script>update();</script> 
<?php
}
else
{
	echo "Oeps! Hiervoor moet je aangemeld zijn!";
}
require_once ('footer.php');
?>