// JavaScript Document ajax_captcha.js
/*
	This is the JavaScript file for the How to Create CAPTCHA Protection using PHP and AJAX Tutorial
	You may use this code in your own projects as long as this 
	copyright is left in place.  All code is provided AS-IS.
	This code is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	
	For the rest of the code visit http://www.WebCheatSheet.com
	
	Copyright 2006 WebCheatSheet.com	
*/

//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; 
   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) {
   //Set the content of the DIV element with the response text
   document.getElementById('result').innerHTML = receiveReq.responseText;
   //Get a reference to CAPTCHA image
   img = document.getElementById('imgCaptcha'); 
   //Change the image
   img.src = 'button3.php?' + Math.random();
 }
}
//Called every time when form is perfomed
function getParam(theForm) {
 //Set the URL
 var url = 'captcha.php';
 //Set up the parameters of our AJAX call
 var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value );
 //Call the function that initiate the AJAX request
 makeRequest(url, postStr);
}

// code 2 http://www.crackajax.net/captchaform.php
        var url = 'captcheck.php?code=';
        var captchaOK = 2;  // 2 - not yet checked, 1 - correct, 0 - failed
        
        function getHTTPObject()
        {
        try {
        req = new XMLHttpRequest();
          } catch (err1)
          {
          try {
          req = new ActiveXObject("Msxml12.XMLHTTP");
          } catch (err2)
          {
          try {
            req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (err3)
            {
	req = false;
            }
          }
	}
        return req;
	}
        
        var http = getHTTPObject(); // We create the HTTP Object        
        
        function handleHttpResponse() {
        if (http.readyState == 4) {
            captchaOK = http.responseText;
            if(captchaOK != 1) {
              alert('Le code n\'est pas correct. Essayez encore.');
              document.getElementById('code').value='';
              document.getElementById('code').focus();
              return false;
              }
              document.getElementById('request').submit();
           }
        }

        function checkcode(thecode) {
        http.open("GET", url + escape(thecode), true);
		// alert('Le code '+ escape(thecode));
        http.onreadystatechange = handleHttpResponse;
        http.send(null);
        }
        
        function checkform() {
        // First the normal form validation
		 if(document.getElementById('nom').value=='') {
          alert('Mettez le nom...');
          document.getElementById('nom').focus();
          return false;
          }
        if(document.getElementById('prenom').value=='') {
          alert('Le prénom?');
          document.getElementById('prenom').focus();
          return false;
          }
        if(document.getElementById('code').value=='') {
          alert('Recopiez exactement le code de l\'image');
          document.getElementById('code').value='';
          document.getElementById('code').focus();
          return false;
          }
          // Now the Ajax CAPTCHA validation
          checkcode(document.getElementById('code').value);
          return false;
        }      
