<?php 
if (!isset($_SESSION))
{
	session_start();
}
$sessie_id = rand(1, 100000000);
require_once('../config.php');
mysql_query("INSERT INTO sessies SET random=".$sessie_id.", datum=".strtotime(date("d F Y G:i:s")).";");
echo "<script>sessie_id = '".$sessie_id."';</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();
}
ajax_vrij = "ja";
verbinding = "ja";
ajax_set_verbroken_go = "nee";
timer = 4000;
function update()
{
	if (verbinding == "ja")
	{
		ajax_vrij = "nee";
		ajax_set_verbroken_go = "ja";
		ajax_set_verbroken = setTimeout("verbinding = 'nee';", 5000);
		document.getElementById('aantal').value++;
		xmlhttp.open("GET", "chatgo.php?sessie="+sessie_id+"&random="+Math.round(9000*Math.random()),true);
	    xmlhttp.onreadystatechange=function()
	    {
		    if (xmlhttp.readyState==4)
	       	{
		       	if(xmlhttp.status == 200)
	 			{
	 	 			document.getElementById('chatwindow').innerHTML = xmlhttp.responseText;
	 	 			//document.getElementById('statuswindow').innerHTML += "Klaar.<br>";
	 			}
	 			else if(xmlhttp.status == 404)
	 			{
	  				//document.getElementById('statuswindow').innerHTML += "Verbinding verbroken.<br>";
	 			}
	 			else
	 			{
	  				//document.getElementById('statuswindow').innerHTML += "Er was een probleem.<br>";
	 			}
	 			if (ajax_set_verbroken_go == "ja")
	 			{
		 			clearTimeout(ajax_set_verbroken);
		 			ajax_set_verbroken_go = "nee";
	 			}
	 			ajax_vrij = "ja";
	    		timer = 4000;
	    		counter();
	       	}
	    }
	    xmlhttp.send(null);
    }
}
function counter()
{
    if (timer == 0)
    {
	    if (ajax_vrij == "ja")
		{
        	update();
    	}
    	else
    	{
	    	timer = 4000;
    		counter();
		}
    }
    else
    {
        timer = timer - 1000;
        setTimeout("counter()", 1000);
    }
}
function check_connection()
{
	if (verbinding != "ja")
	{
		alert("Verbinding verbroken!");
	}
	else
	{
		setTimeout("check_connection()", 500);
	}
}
</script>
<body>
<pre><div id="chatwindow" style="width: 525px; height: 200px; border: 1px solid black;">Bezig met inladen...</div>
<div id="statuswindow" style="width: 525px; height: 100px; border: 1px solid black; overflow: scroll;"></div></pre>
<input type="text" id="randompje" style="width: 480px;"><br>
<input type="text" id="aantal" style="width: 480px;">
<script>
check_connection();
update();
document.getElementById('randompje').value = sessie_id;
</script> 