var onCheck=0;
var onDoubleCheck=0;
var labels = new Array();	

if (linguaMaster==1) {
    labels["err_manca"]="You have to fill all the fields";
    labels["err_username"]="The Username you chose is not valid.\n\nYou can only use letters, numbers and underscores.";
    labels["err_mail"]="The e-mail address you chose is not valid.";
    labels["err_studio"]="The studio Name you chose is not valid.\n\nYou can only use letters, numbers, spaces, periods and underscores.";
	
    labels["controllo"]="I'm checking your data.";
    labels["user_occupato"]="This Username has already been taken. Please choose another.";
    labels["studio_occupato"]="This Studio Name has already been taken. Please choose another.";
} else {
    labels["err_manca"]="Non hai compilato tutti i campi";
    labels["err_username"]="Lo Username inserito non � valido.\n\nPuoi utilizzare solamente lettere, numeri e underscore.";
    labels["err_mail"]="L\'indirizzo mail inserito non � valido.";
    labels["err_studio"]="Il nome inserito per lo Studio non � valido.\n\nPuoi utilizzare solamente lettere, numeri, spazi, punti e underscore.";
	
    labels["controllo"]="Sto controllando i dati inseriti.";
    labels["user_occupato"]="Lo Username inserito &egrave; gi&agrave; occupato. Prova a digitarne un altro.";
    labels["studio_occupato"]="Esiste gi&agrave; uno studio con questo nome. Prova a digitarne un altro.";
}


function checkDatiReg() {
    var regUser=document.getElementById("regUser");
    var regMail=document.getElementById("regMail");
    //	var regStudio=document.getElementById("regStudio");
    if (regUser.value==null || regUser.value=="" || regMail.value==null || regMail.value=="" /* || regStudio.value==null || regStudio.value=="" */) {
        alert(labels["err_manca"]);
        return false;
    }
    var filtroUser = /^[a-zA-Z0-9\_]+$/;
    if (!filtroUser.test(regUser.value)) {
        alert(labels["err_username"]);
        regUser.focus();
        return false;
    }
    var filtroMail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filtroMail.test(regMail.value)) {
        alert(labels["err_mail"]);
        regMail.focus();
        return false;
    }
    //	var filtroStudio = /^[a-zA-Z0-9\_\. ]+$/;
    //	if (!filtroStudio.test(regStudio.value)) {
    //		alert(labels["err_studio"]);
    //		regStudio.focus();
    //		return false;
    //	}
    onCheck=0;
    onDoubleCheck=0;
    var controllo = new chiamataAJAXLogin();
    controllo.checkLibero(0,regUser.value);
//	var controllo1 = new chiamataAJAXLogin();
//	controllo1.checkLibero(1,regStudio.value);
// document.formregistrazione.submit();
	
	
	
}

function chiamataAJAXLogin() {
	
    var that= this;
	
    this.checkLibero = function(campo, valore) {
        if (!document.getElementById || !document.createTextNode) {
            return;
        }
				
        try {
            this.richiesta = new XMLHttpRequest();
        } catch(error) {
            try {
                this.richiesta = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(error) {
                return true;
            }
        }
        that.campo = campo;
        that.valore = valore;
        that.contErrore = document.getElementById("regFondo");
        that.form = document.formregistrazione;
        if (!that.contErrore) {
            return;
        }
		
        this.richiesta.open('get','./checkLibero.php?cam='+that.campo+'&val='+that.valore);
	
        this.richiesta.onreadystatechange = function() {
            if (that.richiesta.readyState==1) {
                that.contErrore.innerHTML = '<p>'+labels["controllo"]+'</p>';
            }
            if (that.richiesta.readyState==4) {
                if (that.richiesta.status && /200|304/.test(that.richiesta.status)) {
					
                    if (that.richiesta.responseText==0) {
                        that.form.submit();
                        return false;
                    } else if (that.richiesta.responseText==1) {
                        if (that.campo==0) {
                            that.contErrore.innerHTML+= "<p>"+labels["user_occupato"]+"</p>";
                        } else if (that.campo==1){
                            that.contErrore.innerHTML+= "<p>"+labels["studio_occupato"]+"</p>";
                        }
                        return false;
                    } 
                } else {
                    alert('Il controllo non ha funzionato, la pagina non riesce a caricare tutti i dati. \n\nStatus: '+this.status+'\n\nProva a ricaricare la pagina.');
                    return true;
                }
            }
        }
	
        this.richiesta.setRequestHeader('If-Modified-Since','Wed, 05 Apr 2006 00:00:00 GMT');
        this.richiesta.send(null);
        return false;
    }
	
		
	
	
/*
	this.fallita = function(requester) {
		alert('XMLHttp non ha funzionato. Status: '+requester.status);
		return true;
	}
	
	this.riuscita = function(requester) {
		this.data = requester.responseText;
		this.data = this.data.replace(/\n/g, '<br />');
		this.outputContainer.innerHTML = data;
		return false;
	}
	*/
}
