//===========================================
//               GeneralFunctions!
//===========================================
function changeBgColor(id, color) {
   var item;
   if (document.getElementById)
   {
      item = document.getElementById(id);
   }
   else
      if (document.all)
      {
         item = document.all(id);
      }
   if (item && item.style)
   {
      item.style.backgroundColor = color;
   }
}
function SetVisible(id, visible) {
   var item;
   if (document.getElementById)
   {
      item = document.getElementById(id);
   }
   else
      if (document.all)
      {
         item = document.all(id);
      }
   if (item && item.style)
   {
      item.style.visibility = visible;
   }
}

//document.getElementById(queueChecks[checking].id+"Spam").style.Visibility="visible";

//===========================================
//               CheckSpam!
//===========================================
var queueChecks=new Array();
var checking=-1;
var xmlHttp=null;
var checkDelay=1500; //millis Hogere waarde: save meer roundtrips
var roundTrips=0;
var keysUpCount=0;
var retries=0;    //aantal fouten in de communicatie
var maxRetries=3; //aantal

function startCheck(extraDelay)
{
   if (!extraDelay) var extraDelay=0;
   if ((queueChecks.length>0) && (checking==-1))
   {
      checking=0;
      setTimeout("doCheck();",checkDelay+extraDelay);
   }
}

function doCheck()
{
   //var now=new Date();
   //if ((now-lastCheck)>checkDelay)
      xmlHttp=getXmlHttpObject();
      if (xmlHttp==null)
      {
         //Blijkbaar is de browser niet in staat tot dit soort dingen.
         //Dan doen we nix! (Kieper de array ook maar leeg.)
         queueChecks=null;
         checking=-1;
         return;
      }
      //var url="http://hoephoep.sytes.net/ofinba/!ofinba.check_date";
      //url=url+"?f_datum="+queueChecks[checking].value;
      //url=url+"&f_nocache="+Math.random();
      var url="http://www.zuma-tilburg.nl/isspam.php"
      var qry="f_message="+queueChecks[checking].value;
         qry+="&f_nocache="+Math.random();
      xmlHttp.onreadystatechange=stateChanged;
      xmlHttp.open("POST",url,true);
      xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
      xmlHttp.send(qry);
}



function addCheck(x)
{
   keysUpCount++;
   queueChecks[queueChecks.length]= x;
   startCheck();
}

function stateChanged()
{
   try
   {
      if (xmlHttp.readyState == 4)
      {
         if (xmlHttp.status == 200)
         {
            //communicatie is gelukt, retries weer op 0
            retries=0;
            if (xmlHttp.responseText.substr(1)>0.9)
            {
               //document.getElementById(queueChecks[checking].id).style.background=
               //         "url('spam.png') no-repeat fixed bottom right";

               SetVisible(queueChecks[checking].id+"Spam","visible");
               changeBgColor(queueChecks[checking].id,"#ffbbbb");
            }
            else
            {
            	 //document.getElementById(queueChecks[checking].id+"Spam").style.visibility="hidden";
               changeBgColor(queueChecks[checking].id,"");
               SetVisible(queueChecks[checking].id+"Spam","hidden");
            }
            // Deze check is nu voltooid, mag dus van de stack af.
            //Shift(): Pak onderste record van stack.
            //Dit in tegenstelling tot
            //pop(): pakt bovenste rec.
            var tmp=queueChecks.shift();
            // Als de Array langer dan 2 is gooien nog wat meer weg
            // zodat we wat roundtrips kunnen uitsparen.
            var zelfdeId=true
            var trash=new Object();
            while ((queueChecks.length>=2) && (zelfdeId==true))
            {
               if ((queueChecks[0].id == queueChecks[1].id) && (queueChecks[0].id == tmp.id) )
               {
                  trash=queueChecks.shift();
               }
               else
               {
                  zelfdeId=false;
               }
            }
            // Geef aan dat we niet meer aan t checken zijn.
            checking=-1;
            //Schiet n nieuwe check af.
            startCheck();
         }
         else
         {
            // Foutje, het zal wel. We vallen de gebruiker hier
            // niet mee lastig. We herstarten wel de check.
            if (retries<maxRetries)
            {
               retries++;
               checking=-1;
               startCheck(5000);
            }
         }
      }
   }
   catch( e )
   {
      // Foutje, het zal wel. We vallen de gebruiker hier
      // niet mee lastig. We herstarten wel de check.
      if (retries<maxRetries)
      {
         retries++;
         checking=-1;
         startCheck(5000);
      }
   }
}

function getXmlHttpObject()
{
   var xmlHttp=null;
   try
   {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
   }
   catch (e)
   {
      // Internet Explorer
      try
      {
         xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
         try
         {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch (e)
         {
            // Dan maar helemaal niet.
            xmlHttp=null;
         }
      }
   }
   return xmlHttp;
}


