$(function() {
   /* FAZ LOGIN MAIN */
   $('#btnLogar').click(function() {
      var user = $('#user').val();
      var pswd = $('#pswd').val();
      var erro = 0;
      var msg  = 'Preencha o(s) campo(s):\n';
      if (user.length<1||user=='Usuário') {
         msg += '» Usuário\n';
         erro = 1;
      }
      if (pswd.length<1||pswd=='suasenh') {
         msg += '» Senha\n';
         erro = 1;
      }
      if (erro) { alert(msg); }
      else {
         $.ajax({
            type: 'POST',
            dataType: 'text',
            cache: false,
            url: './modules/login.php',
            data: {'user':user, 'pswd':pswd},
            beforeSend: function() {
               $('#statusLogar').fadeIn('slow').html('<img src="./images/load.gif" class="img"> &nbsp;<b>Validando usu&aacute;rio, aguarde...</b>');
            },
            success: function(data) {
               data = data.split('&');
               switch (data[0]) {
                  case 'invalid':
                       $('#statusLogar').hide().fadeIn('slow').html('<img src="./images/stop.png" class="img"> &nbsp;<b>Acesso Negado:</b> Usu&aacute;rio ou Senha invalido!');
                  break;
                  case 'valid':
                       document.location.href = data[1];
                  break;
                  default:
                      document.location.reload();
               }
            },
            error: function() {
               $('#statusLogar').html('<img src="./images/servidor_c.png" class="img"> &nbsp;Erro, nenhum retorno estabelecido.');
            }
         });
      }
   });
});

