
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(myform) {
	alert("OK 1 - "+http.readyState);
	if (http.readyState == 4) { 
		captchaOK = http.responseText;
		alert("OK 2 -"+captchaOK);
		if(captchaOK != 1) {
			alert('Codice errato');
			myform.code.focus();
			myform.code.value='';
			return false;
		}
		myform.submit();
	}
}



function checkcode(thecode,myform) {
	alert("OK A - "+thecode);
	http.open("GET", url + escape(thecode), true);
	http.onreadystatechange = handleHttpResponse(myform);
	http.send(null);
}



function checkform(myform) {

	sAlert1 = "Valore mancante per il campo ";
	sAlert2 = "Indirizzo e-mail non valido";
	// First the normal form validation
	if (myform.nome.value == '') {
		myform.nome.focus();
		alert(sAlert1 + "'NOME'");
		return false;
	}
	if (myform.cognome.value == '') {
		myform.cognome.focus();
		alert(sAlert1 + "'COGNOME'");
		return false;
	}
	if (myform.email.value == '' || myform.email.value.indexOf ('@', 0) < 1 || myform.email.value.indexOf ('.', 0) < 1) {
		alert(sAlert2);
		myform.email.focus();
		return false;
	}
/*
	if (myform.dal.value == '') {
		 myform.dal.focus();
		 alert(sAlert1 + "'DAL'");
		 return false;
	}
	if (myform.al.value == '') {
		myform.al.focus();
		alert(sAlert1 + "'AL'");
		return false;
	}
*/
	var str1=myform.boxH_dal_0.value;
	var str2=myform.boxH_al_0.value;
	var date1 = new Date(parseInt(str1.substring(6,10),10), parseInt(str1.substring(3,5),10), parseInt(str1.substring(0,2),10)); 
	var date2 = new Date(parseInt(str2.substring(6,10),10), parseInt(str2.substring(3,5),10), parseInt(str2.substring(0,2),10)); 
	if(date2 < date1) {
		alert("Attenzione, il valore del campo AL e\' antecedente quello del campo DAL.");
		myform.boxH_al_0.focus();
		return false;
	}

	if (myform.privacy.checked == '') {
		myform.privacy.focus();
		alert('Per proseguire occorre acconsentire al trattamento dei dati personali');
		return false;
	}
/*
	if(myform.code.value=='') {
		myform.code.value='';
		myform.code.focus();
		alert('Inserire il codice di 6 numeri che appare nell\'immagine');
		return false;
	}
	// Now the Ajax CAPTCHA validation
	checkcode(myform.code.value,myform);
	return false;
*/
}