/* *****************************************************
INSTRUCTIONS FOR PROPER INSTALLATION OF CAPTCHA INTO FORMS

1) This script must be included to invoke these functions. 

2) Any custom onbody or onload code will need to be included with the getMd5 call that the captcha ajax needs to make

   window.onload = function(){

      custom_page_fuction(); //example

     //needed for captcha esi ajax
     getMd5FromResponse( document.getElementById('captchaDiv').innerHTML ); 
   }

3) A call to validateCaptcha() needs to be placed at the top of the validateForm() function 

4) Include the captcha box by using the line: 
   
   When including the line below, replace the '[' ']' brackets with '<' '>' brackets so that esi will render properly
  
   [esi:include no-store="on" src="/esi/captcha_block.html" onerror="continue" /]
   

***************************************************** */

var outputFromCgi = "Error: No Captcha Found";
var cgiMd5;

function GetXmlHttpObject()
{

  var xmlHttp = null;
  try { //Firefox, Opera 8.0+, Safari
    xmlHttp =  new XMLHttpRequest();
  } catch(e) {
    try { //Internet Explorer
      xmlHttp =  new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlHttp =  new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        if ( !GetXmlHttpObject.failedDetection ) {
          alert("Votre navigateur ne prend pas en charge AJAX.");
          GetXmlHttpObject.failedDetection = true;
        }
      }
    }
  }
  return xmlHttp;
}
/******************************************************/
function stateChanged(funcs, xmlHttpObject){

   if (xmlHttpObject.readyState == 4 && xmlHttpObject.status == 200) {

      outputFromCgi = xmlHttpObject.responseText; //output varies on invoking function

                          //any custom code that needs to be run gets done here
                          // funcs contains custom code to execute after the desired readyState is complete

      if (funcs != "") { eval( funcs ); }
   } 

   return false;
}
/******************************************************/
function callCgi(url,codeToRun){

   xmlHttp = GetXmlHttpObject();
   //xmlHttp.failedDetection = false;

   if (xmlHttp == null) { alert("Votre navigateur ne prend pas en charge Ajax."); return; }

   xmlHttp.onreadystatechange = function(){
                                     if (xmlHttp.readyState == 4)
                                     if (xmlHttp.status == 200)
                                     stateChanged(codeToRun, xmlHttp);
                                }

   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
}
/******************************************************/
function insertCaptcha(){

   var url       = "http://www.akamai.fr/enfr/cgi/captcha.cgi"; 
   var timeStamp = "ms="+new Date().getTime();  //timestamp to avoid IE caching bug
   var fullUrl   = url + "?" + timeStamp;

   //outputFromCgi here contains the html that creates the captcha image
   var runWhenAjaxComplete = "document.getElementById('captchaDiv').innerHTML = outputFromCgi;getMd5FromResponse(outputFromCgi)";

   callCgi(fullUrl,runWhenAjaxComplete);
}
/******************************************************/
function authenticateFromCgi(){

   document.getElementById('submit').disabled = true; //To prevent quick typing, slow authen bug
   
   var url            = "http://www.akamai.fr/enfr/cgi/captcha.cgi";
   var userCodeParam  = "capCode=" + document.getElementById('capCode').value;
   var md5Param       = "md5=" + cgiMd5;
   var timeStampParam = "ms="+new Date().getTime();  //timestamp to avoid IE caching bug
   var originParam = "origin=" + document.getElementById('origin').value;
   var callerParam         = "caller=web"; 
   var fullUrl        = url + "?" + md5Param + "&" + userCodeParam + "&" + timeStampParam + "&" + originParam + "&" + callerParam;

   var runWhenAjaxComplete = "if(outputFromCgi == '0'){haveerrors = 1;} else {haveerrors = 0;}  document.getElementById('submit').disabled = false;"; 

   callCgi(fullUrl,runWhenAjaxComplete);
}
/******************************************************/
//this function takes as input the captcha html from the cgi output
function getMd5FromResponse(text){

   var line = /.*value=.*(\w*).*/m;

   var temp = text.match(line)[0];

   var val = /value=\W*(\w*)/;

   cgiMd5 = temp.match(val)[1];
}
/******************************************************/
function validateCaptcha(){
  
   //if output != 1, then the code did not match the md5sum: user entered wrong code or some other error

   if ( outputFromCgi == "1" ) { }
   else {  
   
      insertCaptcha();
      haveerrors = true;
      document.getElementById('capCode').value = "";
   }
}
/******************************************************/







