// JavaScript Document
function disableEnterKey(e)
{
    var key;

    if(window.event)
        key = window.event.keyCode;     //IE
    else
        key = e.which;     //firefox
    //alert(key);
 
    if(key == 13)
        return false;
    else
        return true;
}

$(document).ready(function() {

    $('.identificati_button').click(function() {
        $('.div_login').show();
        return false;
    });

    $('.chiudi_div').click(function() {
        $('.div_login').hide();
        return false;
    });

    $("#sendAuth").click(function () {
        sendAjax("/ajaxRequest/login.php","","json","5000","","formAuth","showLoginInfo(result)","","0");
    });
 
    $("#logout").click(function () {
        sendAjax("/ajaxRequest/logout.php","","json","5000","","formAuth","showLogoutInfo(result)","","0");
    });

    
    $("#sendAuth3").click(function () {
        sendAjax("/ajaxRequest/login.php","","json","5000","","formAuth3","showLoginInfo(result)","","0");
        return false;
    });
 
});

function showLoginInfo(result)
{
    if(result.esito==true)
    {
        alert('Login effettuato!');
        //document.location.href='/'+result.from;
        window.location.reload();
        
        //document.location.replace('/'+result.from)
    //$('#boxAuth').html('Benvenuto <b> '+result.USERNAME+' </b> | <span id="logout" style="cursor:pointer;" onclick="logout()" >Logout</span>');
    }
    else
    {
        var errori = 'Login Fallito!\n';
        if(result.errors.length > 0)
        {
            for(i=0;i<result.errors.length;i++)
                errori = errori+result.errors[i]+'\n';
        }
        alert(errori);
    }
}

function login()
{
    sendAjax("ajaxRequest/login.php","","json","5000","","formAuth","showLoginInfo(result)","","0");
}

function logout()
{
    sendAjax("ajaxRequest/logout.php","","json","5000","","formAuth","showLogoutInfo(result)","","0");
}

function showLogoutInfo(result)
{
    if(result.esito==true)
    {
        alert('Logout effettuato!');
        /*var txt = '<form name="formAuth" id="formAuth" method="post" style="margin:0px;padding:0px;">'+
               '<table cellpadding="0" cellspacing="0" border="0">'+
               '<tr>'+
               '<td valign="top"><input type="text" name="user" id="user" accesskey="n" class="form" value="login"  style="margin-right:5px;">'+
               '<input type="password" name="passw" id="passw" accesskey="e" class="form" value="password"></td>'+
               '<td> &nbsp;'+
               '<input id="sendAuth" name="sendAuth" type="button" style="background:url(css/entity/entra.png); width:45px; height:20px; margin:0px; border:0px;" value="" alt="entra" onclick="login()"></td>'+
               '</td></tr><tr><td colspan="2"><span class="white_txt">Non sei registrato?</span> <a href="registrati.php">Clicca qui!</a></td></tr>'+
               '</table>'+
               '</form>';
        $('#boxAuth').html(txt);*/
        window.location.reload();
    }
}