function createObject() {
var tipo_richiesta;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
tipo_richiesta = new ActiveXObject("Microsoft.XMLHTTP");
}else{
tipo_richiesta = new XMLHttpRequest();
}
return tipo_richiesta;
}
var http = createObject();
var http2 = createObject();
var risposta = "";








function controllavaliditaemail (e) {
  if(e != ""){
	var emailStr = e;
        /* The following pattern is used to check if the entered e-mail address
           fits the user@domain format.  It also is used to separate the username
           from the domain. */
        var emailPat=/^(.+)@(.+)$/
        /* The following string represents the pattern for matching all special
           characters.  We don't want to allow special characters in the address.
           These characters include ( ) < \> @ , ; : \ " . [ ]    */
        var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
        /* The following string represents the range of characters allowed in a
           username or domainname.  It really states which chars aren't allowed. */
        var validChars="\[^\\s" + specialChars + "\]"
        /* The following pattern applies if the "user" is a quoted string (in
           which case, there are no rules about which characters are allowed
           and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
           is a legal e-mail address. */
        var quotedUser="(\"[^\"]*\")"
        /* The following pattern applies for domains that are IP addresses,
           rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
           e-mail address. NOTE: The square brackets are required. */
        var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
        /* The following string represents an atom (basically a series of
           non-special characters.) */
        var atom=validChars + '+'
        /* The following string represents one word in the typical username.
           For example, in john.doe@somewhere.com, john and doe are words.
           Basically, a word is either an atom or quoted string. */
        var word="(" + atom + "|" + quotedUser + ")"
        // The following pattern describes the structure of the user
        var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
        /* The following pattern describes the structure of a normal symbolic
           domain, as opposed to ipDomainPat, shown above. */
        var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


        /* Finally, let's start trying to figure out if the supplied address is
           valid. */

        /* Begin with the coarse pattern to simply break up user@domain into
           different pieces that are easy to analyze. */
        var matchArray=emailStr.match(emailPat)
        if (matchArray==null) {
          /* Too many/few @'s or something; basically, this address doesn't
             even fit the general mould of a valid e-mail address. */
            document.getElementById('cEmail').innerHTML = "<img src=\"/immagini/divieto.gif\" alt=\"KO\" /> <font color='red'>Il campo Email sembra essere errato (controllare @ e .)</font>"
            return false
        }
        var user=matchArray[1]
        var domain=matchArray[2]

        // See if "user" is valid
        if (user.match(userPat)==null) {
            // user is not valid
            document.getElementById('cEmail').innerHTML = "<img src=\"/immagini/divieto.gif\" alt=\"KO\" /> <font color='red'>Il campo Email sembra essere errato (controllare la parte prima della @).</font>"
            return false
        }

        /* if the e-mail address is at an IP address (as opposed to a symbolic
           host name) make sure the IP address is valid. */
        var IPArray=domain.match(ipDomainPat)
        if (IPArray!=null) {
            // this is an IP address
              for (var i=1;i<=4;i++) {
                if (IPArray[i]>255) {
                    document.getElementById('cEmail').innerHTML = "<img src=\"/immagini/divieto.gif\" alt=\"KO\" /> <font color='red'>Il campo Email sembra essere errato (controllare la parte dopo la @).</font>"
                return false
                }
            }
            return true
        }

        // Domain is symbolic name
        var domainArray=domain.match(domainPat)
        if (domainArray==null) {
            document.getElementById('cEmail').innerHTML = "<img src=\"/immagini/divieto.gif\" alt=\"KO\" /> <font color='red'>Il campo Email sembra essere errato (controllare bene la parte dopo la @).</font>"
            return false
        }

        /* domain name seems valid, but now make sure that it ends in a
           three-letter word (like com, edu, gov) or a two-letter word,
           representing country (uk, nl), and that there's a hostname preceding
           the domain or country. */

        /* Now we need to break up the domain to get a count of how many atoms
           it consists of. */
        var atomPat=new RegExp(atom,"g")
        var domArr=domain.match(atomPat)
        var len=domArr.length
        if (domArr[domArr.length-1].length<2 ||
            domArr[domArr.length-1].length>3) {
           // the address must end in a two letter or three letter word.
           document.getElementById('cEmail').innerHTML = "<font color='red'><img src=\"/immagini/divieto.gif\" alt=\"KO\" /> Il campo Email sembra essere errato (controllare bene la parte dopo la @, sembra troppo corto).</font>"
           return false
        }

        // Make sure there's a host name preceding the domain.
        if (len<2) {
           var errStr="<img src=\"/immagini/divieto.gif\" alt=\"KO\" /> Il campo Email sembra essere errato, contrallarlo!"
           document.getElementById('cEmail').innerHTML = errStr
           return false
        }
           
		 document.getElementById('cEmail').innerHTML = "<img src=\"/check.gif\" alt=\"OK\" />";
		 return true;
  }
  else return false;
}




var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

function controllavaliditaemail2(e){
var returnval=emailfilter.test(e.value);
if (returnval==false){
	alert("Inserisci un indirizzo email valido.");
	e.select();
	return false;
}
else{
	return true;
}
};

function controllanick(e){

if (e.value.length<5){
alert("Username deve contenere almeno 5 caratteri");
e.select();
return false;
}
else{

	return true;
}
};

function controllaempy(e1,e2,e3,e4,e5,e6){
var corretto= true;
if(e1.value=="")
	corretto =false;
if(e2.value=="")
	corretto =false;
if(e3.value=="")
	corretto =false;
if(e4.value=="")
	corretto =false;
if(e5.value=="")
	corretto =false;
if(e6.value=="")
	corretto =false;

if(!corretto)
	alert("tutti i campi contrassegnati con (*) devono essere riempiti!");
return corretto;
}

function controllapassword(e1,e2){
if(e1 != e2 | e1==""){
	if(e2!="")
		document.getElementById('cPass').innerHTML = '<img src="/immagini/divieto.gif" alt="KO" /> <font color="red">le due password non coincidono</font>';
	return false;
}
else{
	document.getElementById('cPass').innerHTML = "<img src=\"/check.gif\" alt=\"OK\" />";
	return true;
}
}


function controllapasswordMask(e1,e2){
if(e1.value != e2.value){
return false;
}
else return true;
}


function controllaUsername(username){
var espressione =/\W/;
var stringa = username.value;
//alert(stringa);
if (espressione.test(stringa))
{
    alert("il nickname non puņ contenere caratteri speciali");
	return false;
}
return true;
}


function controllaemail(e1, e2){
if(e1.value != e2.value){
	alert("i due indirizzi email inseriti non coincidono");
	e1.select();
return false;
}
else return true;
}

function controllatutto(email1, email2, password1, password2, nickname, nome, provenienza){
var corretto= true;
if(!controllavaliditaemail(email1)){
	corretto= false;
}
if(!controllaemail(email1, email2))
	corretto=false;
if(!controllaUsername(nickname))
	corretto=false;
if(!controllapassword(password1, password2))
	corretto=false;
if(!controllanick(nickname))
	corretto = false;
if(!controllaempy(email1, email2, password1, password2, nickname, nome))
	corretto = false;

if(corretto==true){
	document.modulo.method = "get";
    document.modulo.action = "save.php";
	document.modulo.submit();
}

}

function controllaprivacy(corretto){
	if(document.getElementById('privacy').checked == true)
		return true;
	else{
		return false;
	}
}

function checkAll(email, password1, password2, nickname){

var corretto= true;
if(!controllavaliditaemail(email))
	corretto= false;
if(!controllapassword(password1, password2))
	corretto=false;
if(!controllaprivacy(corretto))
	corretto=false;
return corretto;
}


function checkpass(){

var corretto= true;
if(!controllapassword(document.getElementById('passw').value, document.getElementById('passw2').value))
	corretto=false;
return corretto;
}



function inviadati(valore1) {
	var testo = "";
	if(valore1==""){
	}
	else{
			http.open('get', '/js/php/verificanick.php?username=' + valore1);
		//alert(id_muro);
		http.onreadystatechange = handleResponse;
		http.send(null);
	}
	
}



function check(username,pass1,pass2,email) {
	var testo = "";
	if(username=="" ){
	}else{
		http.open('get', '/js/php/verificanick.php?username=' + username);
		http.onreadystatechange = handleResponse2;
		http.send(null);
	}
	if(email != ""){
  		http2.open('get', '/js/php/verificaemail.php?email=' + email);
		http2.onreadystatechange = handleResponse3;
		http2.send(null);
	}
	
}


function enableBt(){
	var email = document.getElementById('mail').value;
	var password1 = document.modulo.passw.value;
	var password2 = document.getElementById('passw2').value;
	var nickname = document.getElementById('username').value;
	
	if(document.getElementById('btcontinua')){
		if(document.getElementById('cUsername').innerHTML.indexOf("OK")>=0 && document.getElementById('cEmail').innerHTML.indexOf("OK")>=0 && checkAll(email, password1, password2, nickname))
			document.getElementById('btcontinua').disabled=false; 
		else document.getElementById('btcontinua').disabled='disable';
	}
}

function handleResponse2() {
	if(http.readyState == 4){
	var response = http.responseText;
	document.getElementById('cUsername').innerHTML = response;
	enableBt();	
	}
} 

function handleResponse3() {
	if(http2.readyState == 4){
	var response = http2.responseText;
	document.getElementById('cEmail').innerHTML = response;
	enableBt();	
	}
} 


function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
document.getElementById('cUsername').innerHTML = response;
//alert(document.getElementById('dati').innerHTML.indexOf("OK"));
if(document.getElementById('btcontinua')){
	if(document.getElementById('cUsername').innerHTML.indexOf("OK")>=0 && checkpass())
		document.getElementById('btcontinua').disabled=false; 
	else document.getElementById('btcontinua').disabled='disable';
}
}
} 


