/** richiamata da subscription php per la verifica dei dati della registrazione */

// funzione per prendere un elemento con id univoco
function ElementoXId(id_elemento) {
    var elemento;
    if(document.getElementById){
        elemento = document.getElementById(id_elemento);
    } else if (document.layers){ //NN4
        elemento = document.layers[id_elemento];
    }else{
        elemento = document.all[id_elemento];
    }
    return elemento;
}

var ajax = false; // inizializza l'oggetto per la richiesta XMLHttpRequest
	
// funzione per assegnare un oggetto XMLHttpRequest
function assegnaXMLHttpRequest() {
    var XHR = null,	browserUtente = navigator.userAgent.toUpperCase();
		if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object"){
				XHR = new XMLHttpRequest();
		}else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) {
				if(browserUtente.indexOf("MSIE 5") < 0){
					  XHR = new ActiveXObject("Msxml2.XMLHTTP");
				}else{
					  XHR = new ActiveXObject("Microsoft.XMLHTTP");
        }  
		}
		return XHR;
};

setInterval('blinkIt()',500);
function blinkIt() {
    if (!document.all){
        return;
    } else {
        for(i=0;i<document.all.tags('blink').length;i++){
            s=document.all.tags('blink')[i];
            s.style.visibility=(s.style.visibility=='visible')?'hidden':'visible';
        }
    }
}

function CreaMsg(msg,tipo){
    var strmsg = "";
    if(tipo == "ko"){
        strmsg = "<img src=\"elementi_vari/accessdenied.gif\" border=\"0\" height=\"16\" width=\"9\">";
        strmsg += "<font class=\"testo_rosso10\">"+msg+"</font>";
    }else if(tipo == "ok"){
        strmsg = "<img src=\"elementi_vari/checkbox.gif\" border=\"0\" height=\"18\" width=\"18\">";
        strmsg += "<font class=\"testo_verde10\">"+msg+"</font>";
    }
    
    return strmsg;
}

// Fase 1 del controllo: verifica il nickname scelto per lunghezza ed eventuale esistenza nel database
function Rec_0(){
    var ajax = assegnaXMLHttpRequest(), // crea loggetto XMLHttpRequest
        NickValue = ElementoXId("uname").value,
        Problem = ElementoXId("problem_0"),
        Scheda = ElementoXId("msg_uname"); // div dove viene scritto il contenuto variabile della pagina
    if(ajax) {
        // invia la richiesta
        ajax.open("get", "subscr_class.php?act=0&nick="+escape(NickValue), true);
        ajax.setRequestHeader("connection", "close"); // per chiudere la connessione
        ajax.onreadystatechange = function() {
            if(ajax.readyState === readyState.COMPLETATO) {
                if(statusText[ajax.status] === "OK") {
                    var response = ajax.responseText;  // gestisce la risposta con un output nel div 'msgrec'
                    if(response == ''){ 
                        Scheda.innerHTML = CreaMsg(" verifica non riuscita","ko");
                    }else if(response == 'si'){
                        Scheda.innerHTML = CreaMsg(" disponibile","ok");
                        Problem.value = "";                                                                                                     
                    }else{
                        Problem.value = response;
                        Scheda.innerHTML = CreaMsg(response,"ko");                
                    }                                                                                        
                }else{                
                   Scheda.innerHTML = CreaMsg(" Verifica non riuscita","ko");                                     
                }
            }
        }
        ajax.send(null);
    }else{
        Scheda.innerHTML = CreaMsg(" verifica non riuscita","ko");
    }
    
    return false; 
}    

// controlla che la password abbia i requisti di sicurezza
function Rec_1(){
    var ValPwd = ElementoXId("pwd").value,
        Uid = ElementoXId("uname").value,
        Problem = ElementoXId("problem_1"),
        MSGpwd = ElementoXId("msg_pwd");
        
    if(ValPwd!=""){
        if(ValPwd.length<6){
            MSGpwd.innerHTML = CreaMsg("troppo corta (min.6 car.)","ko");
            Problem.value = "password troppo corta (minimo 6 caratteri)";
        }else if(ValPwd==Uid || ValPwd=="123456" || ValPwd=="qwerty" || ValPwd=="000000"){
            MSGpwd.innerHTML = CreaMsg("poco sicura","ko");
        }else{
            MSGpwd.innerHTML = CreaMsg("approvata","ok");
            Problem.value = ""; 
        }
    }
    
    return false;
}

// verifica che l'utente non inserisca l'indirizzo email nella casella del comune di residenza
function Rec_2(){
    var ValComune = ElementoXId("comune").value,
        Problem = ElementoXId("problem_2"),
        MSGcomune = ElementoXId("msg_comune");
   
    Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
    if (ValComune != ""){
        if (Filtro.test(ValComune) || ValComune.indexOf('@',0) != -1){
            MSGcomune.innerHTML = CreaMsg(" non consentito","ko");
            ElementoXId("msg_comune").focus();
            Problem.value = "il comune non deve essere un indirizzo email";    
        }else{
            MSGcomune.innerHTML = CreaMsg(" valido","ok");
            Problem.value = ""; 
        }
    }
    
    return false;
}

// verifica la conferma della password
function Rec_3(){
    var ValPwd = ElementoXId("pwd").value,
        ValVPwd = ElementoXId("vpwd").value,
        Problem = ElementoXId("problem_3"),
        MSGVpwd = ElementoXId("msg_vpwd");
        
    if(ValVPwd!=""){
        if(ValPwd != ValVPwd){
            MSGVpwd.innerHTML = CreaMsg(" verifica password errata","ko");
            Problem.value = " verifica password errata";
        }else{
            MSGVpwd.innerHTML = CreaMsg("password confermata","ok");
            Problem.value = "";        
        }
    }
    
    return false;
}

// verifica la data di nascita
function Rec_4(){
    var ajax = assegnaXMLHttpRequest(),
        ValDay = ElementoXId("day").value,
        ValMonth = ElementoXId("month").value,
        ValYear = ElementoXId("year").value,
        Problem = ElementoXId("problem_4"),       
        MSGeta = ElementoXId("msg_eta");
        
    if(ajax) {
        // invia la richiesta
        ajax.open("get", "subscr_class.php?act=1&day="+ValDay+"&month="+ValMonth+"&year="+ValYear, true);
        ajax.setRequestHeader("connection", "close"); // per chiudere la connessione
        ajax.onreadystatechange = function() {
            if(ajax.readyState === readyState.COMPLETATO) {
                if(statusText[ajax.status] === "OK") {
                    var response = ajax.responseText;  // gestisce la risposta con un output nel div 'msg_eta'
                    if(response == ''){ 
                        MSGeta.innerHTML = CreaMsg(" verifica non riuscita","ko");
                    }else if(response == 'si'){
                        MSGeta.innerHTML = CreaMsg(" valida","ok");
                        Problem.value = "";                                                                                                     
                    }else{
                        MSGeta.innerHTML = CreaMsg(response,"ko");
                        Problem.value = response;                        
                    }                                                                                        
                }else{                
                   MSGeta.innerHTML = CreaMsg(" verifica non riuscita","ko");                                     
                }
            }
        }
        ajax.send(null);
        
    }else{
        MSGeta.innerHTML = CreaMsg(" verifica non riuscita","ko");
    }
    
    return false;
}

// verifica l'indirizzo email
function Rec_5(){
    var ajax = assegnaXMLHttpRequest(),
        Valemail = ElementoXId("usremail").value,
        Problem = ElementoXId("problem_5"),       
        MSGeta = ElementoXId("msg_usremail");
        
    if(ajax) {
        // invia la richiesta
        ajax.open("get", "subscr_class.php?act=2&eml="+Valemail, true);
        ajax.setRequestHeader("connection", "close"); // per chiudere la connessione
        ajax.onreadystatechange = function() {
            if(ajax.readyState === readyState.COMPLETATO) {
                if(statusText[ajax.status] === "OK") {
                    var response = ajax.responseText;  // gestisce la risposta con un output nel div 'msg_usremail'
                    if(response == ''){ 
                        MSGeta.innerHTML = CreaMsg(" verifica non riuscita","ko");
                    }else if(response == 'si'){
                        MSGeta.innerHTML = CreaMsg(" valida","ok");
                        Problem.value = "";                                                                                                     
                    }else{
                        MSGeta.innerHTML = CreaMsg(response,"ko");
                        Problem.value = response;                        
                    }                                                                                        
                }else{                
                   MSGeta.innerHTML = CreaMsg("Verifica non riuscita","ko");                                     
                }
            }
        }
        ajax.send(null);
        
    }else{
        MSGeta.innerHTML = CreaMsg(" verifica non riuscita","ko");
    }
    
    return false;
}

// alla fine di tutto, quando l'utente clicca sulla conferma, prima di andare nella pagina successiva (sub1.php)
// verifichiamo che tutto sia stato convalidato.
function Iscriviti(){
    var ValSessof = document.subscr.sesso[0].checked,
        ValSessom = document.subscr.sesso[1].checked,
        ValCercof = document.subscr.crc[0].checked,
        ValCercom = document.subscr.crc[1].checked,
        ValDay = ElementoXId("day").value,
        ValMonth = ElementoXId("month").value,
        ValYear = ElementoXId("year").value,
        ValComune = ElementoXId("comune").value,
        ValProvincia = ElementoXId("provincia").value,
        ValEstero = ElementoXId("estero").value,
        ValUname = ElementoXId("uname").value,
        ValPwd = ElementoXId("pwd").value,
        ValVpwd = ElementoXId("vpwd").value,
        ValUsremail = ElementoXId("usremail").value,
        ValInvauth = ElementoXId("invauth").checked,
        ValInvprivacy = ElementoXId("invprivacy").checked,
        ValProblem_0 = ElementoXId("problem_0").value,
        ValProblem_1 = ElementoXId("problem_1").value,
        ValProblem_2 = ElementoXId("problem_2").value,
        ValProblem_3 = ElementoXId("problem_3").value,
        ValProblem_4 = ElementoXId("problem_4").value,
        ValProblem_5 = ElementoXId("problem_5").value,
        err_num = 0,
        StrErr = "";
       
    if( (ValSessof == false && ValSessom == false) || (ValCercof == false && ValCercom == false) || ValDay == "" || ValMonth == "" || ValYear == "" || ValComune == "" ||
        ValProvincia == "" || ValEstero == "" || ValUname == "" || ValPwd == "" || ValVpwd == "" ||
        ValUsremail == "" || ValInvauth != true || ValInvprivacy != true ){
        StrErr += "mancano dei dati: compila attentamente tutte le caselle";
        err_num++;
    }else{
        if(ValProblem_0 != ""){
            if(err_num>0){ StrErr += "\n"; }
            StrErr += ValProblem_0+"\n";
            err_num++;
        }        
        if(ValProblem_1 != ""){
            if(err_num>0){ StrErr += "\n"; }
            StrErr += ValProblem_1+"\n";
            err_num++;        
        }
        if(ValProblem_2 != ""){
            if(err_num>0){ StrErr += "\n"; }
            StrErr += ValProblem_2+"\n";
            err_num++;        
        }
        if(ValProblem_3 != ""){
            if(err_num>0){ StrErr += "\n"; }
            StrErr += ValProblem_3+"\n";
            err_num++;        
        }
        if(ValProblem_4 != ""){
            if(err_num>0){ StrErr += "\n"; }
            StrErr += ValProblem_4+"\n";
            err_num++;        
        }
        if(ValProblem_5 != ""){
            if(err_num>0){ StrErr += "\n"; }
            StrErr += ValProblem_5+"\n";
            err_num++;        
        }
    }
    
    if(StrErr != ""){
        alert("ATTENZIONE\n "+StrErr);
    }else{
        if (document.all) { // IE
            document.all.subscr.submit();
        } else if (document.layers){ //NN4
            document.layers["subscr"].submit();
        } else if (document.getElementById) { // Opera e NN6
            document.getElementById("subscr").submit();
        }        
    }
    
    return false;
}

// oggetto di verifica stato
var readyState = {
		INATTIVO:	0,
		INIZIALIZZATO:	1,
		RICHIESTA:	2,
		RISPOSTA:	3,
		COMPLETATO:	4
};

// Mette in un array i codici restituiti dal server [la scelta dell' array è per evitare problemi con vecchi browsers]
var statusText = new Array();
statusText[100] = "Continue";
statusText[101] = "Switching Protocols";
statusText[200] = "OK";
statusText[201] = "Created";
statusText[202] = "Accepted";
statusText[203] = "Non-Authoritative Information";
statusText[204] = "No Content";
statusText[205] = "Reset Content";
statusText[206] = "Partial Content";
statusText[300] = "Multiple Choices";
statusText[301] = "Moved Permanently";
statusText[302] = "Found";
statusText[303] = "See Other";
statusText[304] = "Not Modified";
statusText[305] = "Use Proxy";
statusText[306] = "(unused, but reserved)";
statusText[307] = "Temporary Redirect";
statusText[400] = "Bad Request";
statusText[401] = "Unauthorized";
statusText[402] = "Payment Required";
statusText[403] = "Forbidden";
statusText[404] = "Not Found";
statusText[405] = "Method Not Allowed";
statusText[406] = "Not Acceptable";
statusText[407] = "Proxy Authentication Required";
statusText[408] = "Request Timeout";
statusText[409] = "Conflict";
statusText[410] = "Gone";
statusText[411] = "Length Required";
statusText[412] = "Precondition Failed";
statusText[413] = "Request Entity Too Large";
statusText[414] = "Request-URI Too Long";
statusText[415] = "Unsupported Media Type";
statusText[416] = "Requested Range Not Satisfiable";
statusText[417] = "Expectation Failed";
statusText[500] = "Internal Server Error";
statusText[501] = "Not Implemented";
statusText[502] = "Bad Gateway";
statusText[503] = "Service Unavailable";
statusText[504] = "Gateway Timeout";
statusText[505] = "HTTP Version Not Supported";
statusText[509] = "Bandwidth Limit Exceeded";
