//istanza di XMLHttp
var xmlHttp = createXMLHttpRequestObject();



function createXMLHttpRequestObject(){
    
    var req;
        if(window.XMLHttpRequest){
        // Firefox, Safari, Opera...
            req = new XMLHttpRequest();
            
        } else if(window.ActiveXObject) {
            
            // Internet Explorer 5+        
                req = new ActiveXObject("Microsoft.XMLHTTP");
                
        } else {
            
                // se arriviamo in questo punto,
                // probabilmente abbiamo a che fare con un vecchio browser
                // e informiamo l'utente.
                alert('Errore: non riesco a creare l oggetto XMLHttpRequest');
        }  
return req;    
    
}

function controllomail(mail){
	var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
	if (!espressione.test(mail))
	{
	    return false;
	}else{
            
            return true;
            }
}

function send(){
    
    
    var nome = $("#name").val();
    var cognome = $("#surname").val();
    var email = $("#email").val();
    
    if((nome!="") && (cognome!="") && (email!="")){
        
        var verifica = controllomail(email);
    
        if(verifica){
        var params = "nm=" + nome + "&cg=" + cognome + "&em=" + email + "";

        if(xmlHttp){
        
        try
        {
            
           xmlHttp.open("POST","conf.php",true);
           xmlHttp.setRequestHeader("content-type", "application/x-www-form-urlencoded");
           xmlHttp.setRequestHeader("Content-length", params.length);
           xmlHttp.setRequestHeader("Connection", "close");
           xmlHttp.onreadystatechange = function(){
                
                if (xmlHttp.readyState == 4){
            
                        if(xmlHttp.status == 200){
                            
                                var ris = xmlHttp.responseText;
                                if(ris=="Indirizzo Email gia presente nel nostro database."){
                                    
                                    document.getElementById("help").innerHTML ="* Indirizzo Email gia presente nel nostro database.";
                                    }else {
                                        
                                        $("#centro").html("<center>" + ris + "</center>");
                                        }
                                
                        }
                }
               
                }
            xmlHttp.send(params);
            
            
            
       }
        catch(e)
        {
            
        }
        

    } 
    } else {
        
        document.getElementById("help").innerHTML ="* Indirizzo email non valido.";
        }
    } else {
        
        document.getElementById("help").innerHTML ="* Prego inserire tutti i campi.";
        }
}
