/*
	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;
    
  	if( receiveReq.responseText.indexOf("Validation failed")<0){
   		
   		if( document.getElementById )
			{
				// Netscape 6+ , IE5+
				document.getElementById("frmCaptcha").style.visibility="hidden";
			}
			else if( document.all )
			{
				// IE4
				document.all["frmCaptcha"].style.visibility="hidden";
			}
			else if( document.layers )
			{
				// Netscape 4
				document.layers["frmCaptcha"].visibility="hidden";
			}

   		
  	}
  	else{
  		//Get a reference to CAPTCHA image
	   img = document.getElementById('imgCaptcha'); 
	   //Change the image
	   img.src = 'ci_image.php?' + Math.random();
  	}
 }
}

//Called every time when form is perfomed
function getParam(theForm) {

	if(theForm.txtEmail.value==''){
		window.alert('Please enter your email in the field provided.');
	}
	else if(theForm.txtEmail.value.indexOf('@')<=0 
				|| theForm.txtEmail.value.indexOf('.')<=0 
				|| theForm.txtEmail.value.indexOf(' ')!=-1){
		window.alert('Please ensure the e-mail address is specified correctly.');
	}
	else { 
		 //Set the URL
		 var url = 'ci_validation.php';
		 //Set up the parameters of our AJAX call
		 var postStr = theForm.txtVerify.name + "=" + encodeURIComponent( theForm.txtVerify.value );
		 postStr += "&"+ theForm.txtText.name + "=" + encodeURIComponent( theForm.txtText.value );
		 postStr += "&"+ theForm.txtEmail.name + "=" + encodeURIComponent( theForm.txtEmail.value );
		 //Call the function that initiate the AJAX request
		 makeRequest(url, postStr);
	}
}

function checkEnter(e){ //e is event object passed from function invocation
	var characterCode; //literal character code will be stored in this variable
	
	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}
	
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		getParam(document.frmCaptcha);
		return false; 
	}
	else{
		return true; 
	}

}

function disableEnterKey(e)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}


