//---------------------------------------------------------------
// Residência de Estudantes da Beira
// by Ricardo Otero dos Santos <oterosantos@gmail.com>
//--------------------------------------------------------------

function show_form(id) {
  if ($(id).style.display == "none")
    Effect.BlindDown(id);
  else
    Effect.BlindUp(id);
}

function delete_testemunho(id) {
  if (confirm('Tem a certeza que quer apagar este testemunho?')) {
    var options = {
      method: 'get',
      parameters: 'apagar='+id,
      onComplete: function(t) {
        Effect.Fade('list_'+id);
      }
  }
	new Ajax.Request('ajax_testemunhos.php', options);
  }  
}

function delete_utilizador(id) {
  if (confirm('Tem a certeza que quer apagar este utilizador?')) {
    var options = {
      method: 'get',
      parameters: 'apagar='+id,
      onComplete: function(t) {
        Effect.Fade('list_'+id);
      }
  }
	new Ajax.Request('ajax_utilizadores.php', options);
  }  
}

function add_testemunho() {  
  var nome = $('nome').value;
  var intro = $('intro').value;
  var body = $('body').value;
  var curso = $('curso').value;
  
  Element.show('loading');
  $('nome').disable();
  $('intro').disable();
  $('body').disable();
  $('curso').disable();
    
  var options = {
    method: 'get',
    parameters: 'nome='+nome+'&intro='+intro+'&body='+body+'&curso='+curso,
    onComplete: function(t) {
      newID = t.responseText;  
      Element.hide('loading');
      $('nome').enable();
      $('intro').enable();
      $('body').enable();
      $('curso').enable();
      refresh_testemunhos(newID);
    }
  }
	new Ajax.Request('ajax_testemunhos.php', options);
}

function add_utilizador() {
  var nome = $('nome').value;
  var username = $('username').value;
  var password = $('password').value;
  var nivel = document.getElementById("nivel");
  nivel = nivel.selectedIndex+1;
 
  Element.show('loading');
  $('nome').disable();
  $('username').disable();
  $('password').disable();
  $('nivel').disable();
    
  var options = {
    method: 'get',
    parameters: 'add=1'+'&nome='+nome+'&username='+username+'&password='+password+'&nivel='+nivel,
    onComplete: function(t) {
      newID = t.responseText;  
      Element.hide('loading');
      $('nome').enable();
      $('username').enable();
      $('password').enable();
      $('nivel').enable();
      refresh_utilizadores(newID);
    }
  }
	new Ajax.Request('ajax_utilizadores.php', options);
}

function edit_form(userID) {
  var options = {
    method: 'get',
    parameters: 'editform='+userID,
    onComplete: function(t) {
      var text = t.responseText;
      $('edit_'+userID).innerHTML = t.responseText;
      Effect.Appear('edit_'+userID);
    }
  }
  new Ajax.Request('ajax_utilizadores.php', options);
}

function edit_utilizador(userID) {
  var nome = $('edit_nome').value;
  var username = $('edit_username').value;
  var password = $('edit_password').value;
  var nivel = document.getElementById("edit_nivel");
  nivel = nivel.selectedIndex+1;
  
  $('edit_nome').disable();
  $('edit_username').disable();
  $('edit_password').disable();
  $('edit_nivel').disable();
  $('edit_aceitar').disable();
  $('edit_cancelar').disable();
    
  var options = {
    method: 'get',
    parameters: 'edit='+userID+'&nome='+nome+'&username='+username+'&password='+password+'&nivel='+nivel,
    onComplete: function(t) {
      window.location.reload();
    }
  }
	new Ajax.Request('ajax_utilizadores.php', options);
}

function refresh_testemunhos(newID) {
  Element.hide('list');
  Element.show('loading_list');
  var options = {
    method: 'get',
    parameters: 'list',
    onComplete: function(t) {
      $('list').innerHTML = t.responseText;
      Element.show('list');
      Element.hide('loading_list');
      Effect.Pulsate('list_'+newID, { duration: 1.5 });
    }
  }
	new Ajax.Request('ajax_testemunhos.php', options);
}

function refresh_utilizadores(newID) {
  Element.hide('list');
  Element.show('loading_list');
  var options = {
    method: 'get',
    parameters: 'list',
    onComplete: function(t) {
      $('list').innerHTML = t.responseText;
      Element.show('list');
      Element.hide('loading_list');
      Effect.Pulsate('list_'+newID, { duration: 1.5 });
    }
  }
	new Ajax.Request('ajax_utilizadores.php', options);
}