//Our XmlHttpRequest object
var receiveReq = getXmlHttpObject();
var autocaptcha = "<form id='frmCaptcha' name='frmCaptcha'>"
autocaptcha = autocaptcha + "<table align='center'>"
autocaptcha = autocaptcha + "<tbody><tr align='center'><td><img id='imgCaptcha' src='scripts/create_image.php' />"
autocaptcha = autocaptcha + "</td></tr><tr><td>"
autocaptcha = autocaptcha + "<div id='captchavalid' align='center'>Please enter the letters you see above<br />"
autocaptcha = autocaptcha + "This technique helps us stop spam</div>"
autocaptcha = autocaptcha + "</td></tr><tr align='center'><td>"
autocaptcha = autocaptcha + "<input id='txtCaptcha' type='text' name='txtCaptcha' value='' height='30' style='font-size:0.9em' maxlength='7' size='15' />"
autocaptcha = autocaptcha + "</td></tr><tr align='center'><td>"
autocaptcha = autocaptcha + "<input id='btnCaptcha' type='button' value='Validate' name='btnCaptcha' onclick='getParam(document.frmCaptcha)' />"
autocaptcha = autocaptcha + "</td></tr></tbody></table>"
autocaptcha = autocaptcha + "</form>"
					
//Gets the browser specific XmlHttpRequest Object 


function getXmlHttpObject() {
 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.");
 }
}


//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
   var mytest = receiveReq.responseText.substr(0,6);
   if (mytest=="<input")
   		{
		//document.getElementById('agresult').innerHTML = receiveReq.responseText;
		document.getElementById('agcaptcha').style.visibility = "hidden";
		document.getElementById('agcaptcha').style.display = "none";
		document.forms[0].submit()
		}
	else
		{
			document.getElementById('txtCaptcha').value = "" ;
			document.getElementById('captchavalid').style.color = '#F80F0F'
		}
   //Get a reference to CAPTCHA image
   img = document.getElementById('imgCaptcha'); 
   //Change the image
   img.src = 'scripts/create_image.php?' + Math.random();
 }
}

//Called every time when form is perfomed
function getParam(theForm) {
 
if (! cc() ) {return false;}
	
 //Set the URL
  var url = 'scripts/captcha.php';
 //Set up the parameters of our AJAX call
 var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value );
// postStr = postStr.toUpperCase();
 //Call the function that initiate the AJAX request
 makeRequest(url, postStr);
}

function cc()
{
 /* check for a cookie */
	document.cookie = 'killme' + escape('nothing')

  if (document.cookie == "") 
  {
    /* if a cookie is not found - alert user -
     change cookieexists field value to false */
    alert("Cookies need to be enabled to process the form!");
	return false ;

    /* If the user has Cookies disabled an alert will let him know 
        that cookies need to be enabled to log on.*/ 

  }
  else {return true;}
}

