var i=0;

var xmlhttp=false;

function ajax(){
try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }


if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
}

function captcha(code){
	ajax();
		var query="code="+code;
		var url= "include/captcha/check_captcha.php";
		xmlhttp.onreadystatechange=handlestatechange_captcha;
		xmlhttp.open("POST",url,false);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		xmlhttp.send(query);
		//setTimeout('alert()',5250);
		//return xmlhttp.responseText
		
	}
	
	function handlestatechange_captcha() {
		if (xmlhttp.readyState==4){
			if (xmlhttp.status==200){
				parseresults_captcha();
			}
		}
	}
	
	function parseresults_captcha (){
		response=xmlhttp.responseText;
		//if (response==0){
		//alert('Il codice inserito non corrisponde a quello nella figura');
		//var img=document.getElementById('img');
		//var new_img="<img src=\"include/captcha/securimage_show.php?version="+i;
		//img.innerHTML=new_img;
		//i++;
		//}
	}
	
		function change_image (){
		var img=document.getElementById('img');
		var new_img="include/captcha/securimage_show.php?version="+i;
		//img.innerHTML=new_img;
		var captcha=document.getElementById('captcha');
		//img.removeChild(captcha);
		
		nuova_immagine = document.createElement("IMG")
		nuova_immagine.setAttribute("src",new_img);
		nuova_immagine.setAttribute("id","captcha");

		// inseriamola quindi nel nostro paragrafo
		
		img.replaceChild(nuova_immagine,captcha);
		
		//img.appendChild(nuova_immagine);
		
		i++;
		}
