
//Gets the browser specific XmlHttpRequest Object 
function getXmlHttpRequestObject() {
 if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Mozilla, Safari ...
 } else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE
 } else {
    //Display our error message
    alert("Your browser doesn't support the XmlHttpRequest object.");
 }
}

//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();

//Initiate the AJAX request
function makeRequest(url, param) {
//If our readystate is either not started or finished, initiate a new request
 if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
   //Set up the connection to captcha_test.html. True sets the request to asyncronous(default) 
   receiveReq.open("POST", url, true);
   //Set the function that will be called when the XmlHttpRequest objects state changes
   receiveReq.onreadystatechange = updatePage; 

   //Add HTTP headers to the request
   receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   receiveReq.setRequestHeader("Content-length", param.length);
   receiveReq.setRequestHeader("Connection", "close");

   //Make the request
   receiveReq.send(param);
 }   
}

//Called every time our XmlHttpRequest objects state changes
function updatePage() {
 //Check if our response is ready
 if (receiveReq.readyState == 4) {
 	
 	 window.clearTimeout(timeoutID);
 	 
   //Set the content of the DIV element with the response text
   
   if (receiveReq.responseText == 'CAPTCHA') {
   	 // error in captcha, re-enter
   	 alert('De ingevoerde anti-spam code komt niet overeen, probeer het opnieuw');
   	 document.getElementById('cfcaptcha').focus();
   } else if (receiveReq.responseText == 'ERROR') {
   	 // error ?
   	 alert('Er is een onbekende fout');
   } else if (receiveReq.responseText == 'INVALID') {
   	 document.getElementById('commenterror').style.display = 'block';
   } else {
   	 // alles paletti, comment laten zien
     document.getElementById('newcomment').innerHTML = receiveReq.responseText;
     // form legen
     document.frmComment.reset();
   }
   
   curvyCorners.redraw();
 }
}



function procesCForm() {
  var url = "/newscomment.php";
  //Set up the parameters of our AJAX call
  
  document.getElementById('commenterror').style.display = 'none';

  var cfname    = encodeURIComponent(document.getElementById('cfname').value);
  var cfmail    = encodeURIComponent(document.getElementById('cfmail').value);
  var cfcomment = encodeURIComponent(document.getElementById('cfcomment').value);
  var news_id   = encodeURIComponent(document.getElementById('news_id').value);
  
  if (document.getElementById('cfcaptcha')  != null) var cfcaptcha = encodeURIComponent(document.getElementById('cfcaptcha').value);
  else var cfcaptcha = 'nvt';

  var pData = "isAjax=1&cfname="+cfname+"&cfmail="+cfmail+"&cfcomment="+cfcomment+"&cfcaptcha="+cfcaptcha+"&news_id="+news_id;

  makeRequest(url, pData);
  
  timeoutID = window.setTimeout('document.frmComment.submit()', 1500);
  
  return false;
}
