

//Visualizza il form di registrazione
function showRegisterForm_ctl00_Login1() {

    //Visualizza il form di registrazione
    $j('#SfondoFormRegistrazione_ctl00_Login1').css('opacity', '0.8');
    $j('#SfondoFormRegistrazione_ctl00_Login1').fadeIn(500);
    setTimeout(function() { $j('#FormRegistrazione_ctl00_Login1').fadeIn(500); $j('#FormRegistrazione_ctl00_Login1').children().each(function() { $j(this).fadeIn(500); }); }, 700);

} 

//Nasconde il form di registrazione
function hideRegisterForm_ctl00_Login1() {

    //Visualizza il form di registrazione
    $j('#FormRegistrazione_ctl00_Login1').children().each(function () { $j(this).fadeOut(500); });
    $j('#FormRegistrazione_ctl00_Login1').fadeOut(500);
    setTimeout(function () { $j('#SfondoFormRegistrazione_ctl00_Login1').fadeOut(500); }, 700);

}

//Oggetto responsabile dell'abilitazione del bottone
var enabler_ctl00_Login1 = new Object();
enabler_ctl00_Login1.btn = null;
enabler_ctl00_Login1.username = false;
enabler_ctl00_Login1.password = false;
enabler_ctl00_Login1.mail = false;
enabler_ctl00_Login1.licenza = false;
enabler_ctl00_Login1.setUsername = function(user) {
    enabler_ctl00_Login1.username = user;
    enabler_ctl00_Login1.btn.disabled = !(enabler_ctl00_Login1.username && enabler_ctl00_Login1.password && enabler_ctl00_Login1.mail && enabler_ctl00_Login1.licenza);
}
enabler_ctl00_Login1.setPassword = function(pss) {
    enabler_ctl00_Login1.password = pss;
    enabler_ctl00_Login1.btn.disabled = !(enabler_ctl00_Login1.username && enabler_ctl00_Login1.password && enabler_ctl00_Login1.mail && enabler_ctl00_Login1.licenza);
}
enabler_ctl00_Login1.setMail = function(mail) {
    enabler_ctl00_Login1.mail = mail;
    enabler_ctl00_Login1.btn.disabled = !(enabler_ctl00_Login1.username && enabler_ctl00_Login1.password && enabler_ctl00_Login1.mail && enabler_ctl00_Login1.licenza);
}
enabler_ctl00_Login1.setLicenza = function(lic) {
    enabler_ctl00_Login1.licenza = lic;
    enabler_ctl00_Login1.btn.disabled = !(enabler_ctl00_Login1.username && enabler_ctl00_Login1.password && enabler_ctl00_Login1.mail && enabler_ctl00_Login1.licenza);
}
    
//Document ready
$j(document).ready(function() {

    //Svuotano i campi del form di registrazione
    document.getElementById('ctl00_Login1_txtUsername').value = '';
    document.getElementById('ctl00_Login1_txtPassword').value = '';
    document.getElementById('ctl00_Login1_txtConfermaPassword').value = '';
    document.getElementById('ctl00_Login1_txtMail').value = '';
    document.getElementById('ctl00_Login1_txtConfermaMail').value = '';

    //Memorizza il bottone da abilitara
    enabler_ctl00_Login1.btn = document.getElementById('ctl00_Login1_btnRegistrati');

    //Funzione per il controllo dell'uguaglianza della password e della sua conferma
    var passCheckFunc = function() {
        var pss = document.getElementById('ctl00_Login1_txtPassword').value;
        var conf = document.getElementById('ctl00_Login1_txtConfermaPassword').value;
        if (pss != '' && conf != '') {
            if (pss == conf) {
                document.getElementById('passwordCheck_ctl00_Login1').innerHTML = '<img src="/images/ok16.gif" align="middle" alt="Valori uguali" title="Valori uguali" />&nbsp;&nbsp;&nbsp;Sono uguali';
                document.getElementById('passwordCheck_ctl00_Login1').style.color = 'green';
                enabler_ctl00_Login1.setPassword(true);
            } else {
                document.getElementById('passwordCheck_ctl00_Login1').innerHTML = '<img src="/images/no16.png" align="middle" alt="Valori diversi" title="Valori diversi" />&nbsp;&nbsp;&nbsp;Sono diversi';
                document.getElementById('passwordCheck_ctl00_Login1').style.color = 'red';
                enabler_ctl00_Login1.setPassword(false);
            }
        } else {
            document.getElementById('passwordCheck_ctl00_Login1').innerHTML = '';
            enabler_ctl00_Login1.setPassword(false);
        }
    }

    //Assegna gli handler
    document.getElementById('ctl00_Login1_txtPassword').onblur = passCheckFunc;
    document.getElementById('ctl00_Login1_txtConfermaPassword').onblur = passCheckFunc;

    //Funzione per il controllo dell'uguaglianza della mail e della sua conferma
    var mailCheckFunc = function() {
        var pss = document.getElementById('ctl00_Login1_txtMail').value;
        var conf = document.getElementById('ctl00_Login1_txtConfermaMail').value;
        if (pss != '' && conf != '') {
            if (pss == conf) {
                document.getElementById('mailCheck_ctl00_Login1').innerHTML = '<img src="/images/ok16.gif" align="middle" alt="Valori uguali" title="Valori uguali" />&nbsp;&nbsp;&nbsp;Sono uguali';
                document.getElementById('mailCheck_ctl00_Login1').style.color = 'green';
                enabler_ctl00_Login1.setMail(true);
            } else {
                document.getElementById('mailCheck_ctl00_Login1').innerHTML = '<img src="/images/no16.png" align="middle" alt="Valori diversi" title="Valori diversi" />&nbsp;&nbsp;&nbsp;Sono diversi';
                document.getElementById('mailCheck_ctl00_Login1').style.color = 'red';
                enabler_ctl00_Login1.setMail(false);
            }
        } else {
            document.getElementById('mailCheck_ctl00_Login1').innerHTML = '';
            enabler_ctl00_Login1.setMail(false);
        }
    }

    //Assegna gli handler
    document.getElementById('ctl00_Login1_txtMail').onblur = mailCheckFunc;
    document.getElementById('ctl00_Login1_txtConfermaMail').onblur = mailCheckFunc;

    //Funzione per il controllo dell'esistenza del nome utente
    document.getElementById('ctl00_Login1_txtUsername').onblur = function() {

        //Verifica che non sia vuoto
        if (document.getElementById('ctl00_Login1_txtUsername').value != '') {

            //Ottiene ajax
            var ajax = GetXMLHttpRequest();

            //Handler per il termine della richiesta
            ajax.onreadystatechange = function() {
                if (ajax.readyState == 4) {

                    //Verifica se l'utente esiste
                    if (ajax.responseText == 'true') {
                        setTimeout(function() {
                            document.getElementById('usernameCheck_ctl00_Login1').innerHTML = '<img src="/images/no16.png" align="middle" alt="Username già esistente" title="Username già esistente" />&nbsp;&nbsp;&nbsp;Username già esistente';
                            document.getElementById('usernameCheck_ctl00_Login1').style.color = 'red';
                            enabler_ctl00_Login1.setUsername(false);
                        }, 100);
                    } else {
                        setTimeout(function() {
                            document.getElementById('usernameCheck_ctl00_Login1').innerHTML = '<img src="/images/ok16.gif" align="middle" alt="Username disponibile" title="Username disponibile" />&nbsp;&nbsp;&nbsp;Username disponibile';
                            document.getElementById('usernameCheck_ctl00_Login1').style.color = 'green';
                            enabler_ctl00_Login1.setUsername(true);
                        }, 100);
                    }

                    //Sblocca il nome utente
                    setTimeout(function() { document.getElementById('ctl00_Login1_txtUsername').disabled = false; }, 100);

                }
            }

            //Richiede la pagina
            ajax.open('get', 'http://www.marcosroom.it/Command/CheckUsernamePresence.aspx?user=' + document.getElementById('ctl00_Login1_txtUsername').value, true);
            ajax.send();

            //Mette l'attesa
            document.getElementById('usernameCheck_ctl00_Login1').innerHTML = '<img src="/images/attesa.gif" alt="Attendi..." title="Attendi..." />';

            //Blocca il controllo
            document.getElementById('ctl00_Login1_txtUsername').disabled = true;
            document.getElementById('ctl00_Login1_btnRegistrati').disabled = true;

        } else {
            document.getElementById('usernameCheck_ctl00_Login1').innerHTML = '';
            enabler_ctl00_Login1.setUsername(false);
        }

    }

});











//Visualizza il form di registrazione
function showLoginForm_ctl00_Login1() {

    //Visualizza il form di registrazione
    $j('#SfondoFormLogin_ctl00_Login1').css('opacity', '0.8');
    $j('#SfondoFormLogin_ctl00_Login1').fadeIn(500);
    setTimeout(function() { $j('#FormLogin_ctl00_Login1').fadeIn(500); $j('#FormLogin_ctl00_Login1').children().each(function() { $j(this).fadeIn(500); }); }, 700);

} 

//Nasconde il form di registrazione
function hideLoginForm_ctl00_Login1() {

    //Visualizza il form di registrazione
    $j('#FormLogin_ctl00_Login1').children().each(function () { $j(this).fadeOut(500); });
    $j('#FormLogin_ctl00_Login1').fadeOut(500);
    setTimeout(function () { $j('#SfondoFormLogin_ctl00_Login1').fadeOut(500); }, 700);

}

//Esegue il login
function doLogin_ctl00_Login1() {
    
    //Ottiene i valori
    var user = document.getElementById('ctl00_Login1_txtUsernameLogin').value;
    var pss = document.getElementById('ctl00_Login1_txtPasswordLogin').value;
    
    //Ottiene il campo di risposta
    var response = document.getElementById('loginResponse_ctl00_Login1');
    
    //Controlla che non siano vuoti
    if (user == '' || pss == ''){ 
        response.innerHTML = '<span style="color:red">Entrambi i campi sono necessari</span>';
        return false;
    }
    
    //Mette l'attesa
    response.innerHTML = '<img src="/images/attesa.gif" alt="Attendi..." title="Attendi..." />';
    document.getElementById('ctl00_Login1_btnLogin').disabled = true;
    
    //AJAX
    var ajax = GetXMLHttpRequest();
    
    //Handler per il termina della richiesta
    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4) {
            
            //Verifica la risposta del server
            if (ajax.responseText == 'usernameinesistente') {
                response.innerHTML = '<span style="color:red">Username inesistente</span>';
                document.getElementById('ctl00_Login1_btnLogin').disabled = false;
            } else if (ajax.responseText == 'autenticato') {
                response.innerHTML = '<span style="color:green">Login riuscito</span>';
                setTimeout(function() { window.location.reload(); }, 3000);
            } else if (ajax.responseText == 'nonautenticato') {
                response.innerHTML = '<span style="color:red">Password errata</span>';
                document.getElementById('ctl00_Login1_btnLogin').disabled = false;
            } else if (ajax.responseText == 'disabilitato') {
                response.innerHTML = '<span style="color:red">Utente non abilitato.<br />Per abilitarlo controllare la mail<br />e seguire le istruzioni specificate</span>';
                document.getElementById('ctl00_Login1_btnLogin').disabled = false;
            }
            
        }
    }
    
    //Richiede l'autenticazione
    ajax.open('post', 'http://www.marcosroom.it/Command/UserData.aspx', true);
    ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
    ajax.setRequestHeader("connection", "close");
    ajax.send('action=login&username=' + user + '&password=' + pss + '&remember=' + document.getElementById('ctl00_Login1_checkRicordami').checked.toString());
    
}

//Registra l'handler per il login
$j(document).ready(function() {
    document.getElementById('ctl00_Login1_btnLogin').onclick = function() { doLogin_ctl00_Login1(); return false; };
});














//Funzione per visualizzare la password dimenticata
function showPasswordDimenticata_ctl00_Login1() {

    //Nasconde i campi del login
    $j('#tableLogin_ctl00_Login1').children().each(function () { $j(this).fadeOut(500); });
    $j('#tableLogin_ctl00_Login1').fadeOut(500);

    //Visualizza il recupero password
    setTimeout(function () {
        $j('#tableReimpostazionePassword_ctl00_Login1').children().each(function () { $j(this).fadeIn(500); });
        $j('#tableReimpostazionePassword_ctl00_Login1').fadeIn(500);
    }, 500);

}

//Handler per il click del bottone Reimposta
function btnReimposta_ctl00_Login1_Click() {

    //Ottiene il nome utente
    var user = document.getElementById('txtUsernameReimpostazionePassword_ctl00_Login1').value;

    //Ottiene la cella di risposta
    var response = document.getElementById('responseReimpostaPassword_ctl00_Login1');

    //Verifica che non sia vuoto
    if (user == '') {
        response.innerHTML = '<span style="color:red">Inserire Username</span>';
        return false;
    }

    //Ottiene ajax
    var ajax = GetXMLHttpRequest();
    
    //Handler per il termine della richiesta
    ajax.onreadystatechange = function () {
        if (ajax.readyState == 4) {
        
            //Verifica la risposta
            if (ajax.responseText == 'nouser') {
                response.innerHTML = '<span style="color:red">Impossibile trovare l\'utente specificato</span>';
            } else if (ajax.responseText == 'disabled') {
                response.innerHTML = '<span style="color:red">Utente disabilitato</span>';
            } else if (ajax.responseText == 'codepresent') {
                response.innerHTML = '<span style="color:red">Procedura di reimpostazione password già avviata</span>';
            } else if (ajax.responseText == 'ok') {
                response.innerHTML = '<span style="color:green">Procedura di reimpostazione password avviata correttamente.<br />Ti è stata mandata una mail all\'indirizzo da te specificato durante l\'iscrizione.<br />Segui attentamente le istruzioni per reimpostare la tua password.<br /><br />NB: per poter effettuare nuovamente il login è necessario aggiornare la pagina.</span>';
            }
    
            //Sblocca il pulsante
            document.getElementById('btnReimpostaPassword_ctl00_Login1').disabled = false;
        
        }
    }

    //Richiede la pagina per l'invio della mail di reimpostazione
    ajax.open('get', 'http://www.marcosroom.it/Command/SendMailReimpostazionePasswordUtente.aspx?username=' + user, true);
    ajax.send();
    
    //Blocca il bottone e mette l'attesa
    document.getElementById('btnReimpostaPassword_ctl00_Login1').disabled = true;
    response.innerHTML = '<img src="/images/attesa.gif" alt="Attendi..." title="Attendi.." />';

}