function doLogin(usecookie, successUrlRedirect){
  var user = $j('form[name="frmlogin"] input[name="user"]').val();
  var pass = $j('form[name="frmlogin"] input[name="pass"]').val();
  var hash = crypt(user, pass);
  
  $j('#cookiechoice').hide();
  $j('#loginloadingtext').html("Accesso in corso...");  
  $j('#loginloading').show();

  
  $j.ajax({
		  type: "POST",
		  url: "/pages/login/ajaxLogin.php",
		  data: {"hash" : hash,
             "usecookie" : usecookie },
		  timeout: 10000,
		  success: function(data) {
          if (data.success){
            if (successUrlRedirect == "") location.reload(true);
            else location.href=successUrlRedirect;            
          }else if (data.banned_message){
            $j('#loginloading').hide();
            $j('#logincontainer').show();
            $j('form[name="frmlogin"] input[name="user"]').val("");
            $j('form[name="frmlogin"] input[name="pass"]').val("");
            alert(data.banned_message);

          }else{
            $j('#loginloading').hide();
            $j('#loginfailedpanel').show();
          }
		  },
		  error: function(jqXHR, textStatus, errorThrown){
          alert(textStatus);
          $j('#logincontainer').show();
          $j('#loginloading').hide();
      },
      dataType: 'json'
	}); 
}

function retryLogin(){
  $j('form[name="frmlogin"] input[name="pass"]').val("");
  $j('#loginfailedpanel').hide();
  $j('#logincontainer').fadeIn(500);              
}

function retrievePassword(){
  var user = $j('form[name="frmlogin"] input[name="user"]').val();
  $j('#loginloadingtext').html("Invio richiesta in corso...");
  $j('#loginfailedpanel').hide();  
  $j('#loginloading').show();
  
  if (user != ""){
    $j.ajax({
		  type: "POST",
		  url: "/pages/login/ajaxLogin.php",
		  data: { "action" : "retrievePwd",
              "user"  : user },
		  timeout: 10000,
		  success: function(data) {
          if (data.success){
            $j('#loginloading').hide();
            $j('#loginmessagepanel span').html("Un'e-mail è stata inviata all'indirizzo e-mail associato con l'account: " + user);
            $j('#loginmessagepanel').fadeIn(500);                     
          }else{
            $j('#loginloading').hide();
            $j('#loginmessagepanel span').html("E' successo un errore durante il recupero della password. Contatta un amministratore.");
            $j('#loginmessagepanel').fadeIn(500);  
          }
		  },
		  error: function(jqXHR, textStatus, errorThrown){
          alert(textStatus);
          $j('#loginloading').hide();
          $j('#logincontainer').show();  
      },
      dataType: 'json'
	}); 
  }else{
    $j('#logincontainer').show();
    $j('#loginloading').hide();     
  }
}

function doLogout(){
  $j('#logintoppanel').hide();  
  $j('#logoutloading').show();
  $j('#bottombar').fadeOut(500);

    $j.ajax({
		  type: "POST",
		  url: "/pages/login/ajaxLogin.php",
		  data: { "action" : "logout" },
		  timeout: 10000,
		  success: function(data) {
          if (data.success){
            location.reload(true);            
          }
		  },
		  error: function(jqXHR, textStatus, errorThrown){
          alert(textStatus);
          $j('#bottombar').show();
          $j('#logintoppanel').show();  
          $j('#logoutloading').hide();
      },
      dataType: 'json'
	}); 
}
