var url = '/verify.php?code=';
var captchaOK = 2;  // 2 - not yet checked, 1 - correct, 0 - failed
        
function getHTTPObject() {
  var req = false;   
	if (typeof XMLHttpRequest != "undefined")   
		req = new XMLHttpRequest();   
	if (!req && typeof ActiveXObject != "undefined") {   
		try {   
			req=new ActiveXObject("Msxml2.XMLHTTP");   
		} 
		catch (e1) {   
			try {   
				req=new ActiveXObject("Microsoft.XMLHTTP");   
			} 
			catch (e2) {   
				try {   
					req=new ActiveXObject("Msxml2.XMLHTTP.4.0");   
				} 
				catch (e3) {   
					req=null;   
				}   
			}   
		}   
	}   
  
    if(!req && window.createRequest)   
        req = window.createRequest();
    
    if (!req) alert("Il browser non supporta AJAX");   
  
    return req;   
}

var http = getHTTPObject(); // We create the HTTP Object        

function handleHttpResponse() {
	if (http.readyState == 4) {
		captchaOK = http.responseText;
		if(captchaOK != 1) {
		  alert('Codice errato');
		  document.myform.code.value='';
		  document.myform.code.focus();
		  return false;
		}
		document.myform.submit();
	}
}

function checkcode(thecode) {
		http.open("GET", url + escape(thecode), true);
		http.onreadystatechange = handleHttpResponse;
		http.send(null);
}
        
function checkform() {
         sAlert1 = "Valore mancante per il campo ";
         sAlert2 = "Indirizzo e-mail non valido";
       
	    // Nome
		if (document.myform.nome.value == '') {
		 alert(sAlert1 + "'NOME'");
		 document.myform.nome.focus();
		 return false;
		}
	    // Cognome
		if (document.myform.cognome.value == '') {
		 alert(sAlert1 + "'COGNOME'");
		 document.myform.cognome.focus();
		 return false;
		}
	    // Email
		if (document.myform.email.value == '' || document.myform.email.value.indexOf ('@', 0) < 1 || document.myform.email.value.indexOf ('.', 0) < 1) {
		alert(sAlert2);
		document.myform.email.focus();
		return false;
		}
	    // Telefono
		if (document.myform.tel.value == '') {
		 document.myform.tel.focus();
		 alert(sAlert1 + "'TELEFONO'");
		 return false;
		}
	    // Privacy
		if (document.myform.privacy.checked == false) {
		 alert('Per proseguire bisogna accetare il trattamento dei dati');
		 document.myform.privacy.focus();
		 return false;
		}
	    // Codice CAPTCHA
		if(document.myform.code.value=='') {
		 alert('Inserire il numero che è visualizzato nel box');
		 document.myform.code.value='';
		 document.myform.code.focus();
		 return false;
		}
	  // Now the Ajax CAPTCHA validation
	  checkcode(document.myform.code.value);
	  return false;
}
