// onload
window.addEvent('domready', function() {

});

/* Formulario de insercion de asistentes reseteando valores */
function mostrar_formNuevo () {

  new Ajax('index.php', {
  postBody: 'ajax=1&mod=investigacion&exec=mostrar_formNuevo',
  update: 'outForm',
  evalScripts: true,
  onComplete:function() {

    $('outForm').setStyle('display','block');
    $('abrirForm').setStyle('display','none'); // Boton nuevo
    $('cerrarForm').setStyle('display','inline'); // Boton cerrar
    $('submit').setProperty ('value','Guardar');

    new Fx.Scroll(window).toElement('outForm');
  },
  method: 'POST'}).request();
}

/* Formulario de modificacion de asistentes obteniendo valores */
function mostrar_formEditar (id) {

  new Ajax('index.php', {
  postBody: 'ajax=1&mod=investigacion&exec=mostrar_formEditar&id=' + id,
  update: 'outForm',
  evalScripts: true,
  onComplete:function(){

    $('outForm').setStyle('display','block');
    $('abrirForm').setStyle('display','inline'); // Boton nuevo
    $('cerrarForm').setStyle('display','inline') // Boton cerrar

    $('submit').setProperty('value','Modificar');

    new Fx.Scroll(window).toElement('outForm');
  },
  method: 'POST'}).request();
}

function formCerrar () {
  $('cerrarForm').setStyle('display','none'); // Boton cerrar
  $('abrirForm').setStyle('display','inline'); // Boton nuevo
  $('outForm').setStyle('display','none');
  new Fx.Scroll(window).toElement('listOut');
}

function listar () {

  onLoading('listOut');

  new Ajax('index.php', {
  postBody: 'ajax=1&mod=investigacion&exec=index',
  update: 'listOut',
  evalScripts: true,
  onComplete:function(){
    if ($('listOut')) { new Fx.Style('listOut', 'opacity').start(0,1); }
  },
  method: 'POST'}).request();
}


/* Ordenar el listado de formaciones */
function OrdenarPor (order_by) {

  onLoading('listOut');

  var search_key = $('search_key').getValue();
  new Ajax('index.php', {
  postBody: 'ajax=1&mod=investigacion&exec=index&order_by=' + order_by + '&search_key=' + search_key,
  update: 'listOut',
  evalScripts: true,
  onComplete:function(){
    $(order_by).setStyle('text-decoration','underline');
  },
  method: 'POST'}).request();
}

/* Insercion/Actualizacion de asistentes */
function guardarPostQuery () {

  formCerrar();

  var id = $('id').value;
  var accion = $('accion').value;

  new Ajax('index.php', {
  postBody: $('form_default').toQueryString(),
  evalScripts: true,
  onComplete:function(){

    if (accion == 'update') {
      mostrar_formEditar(id);
    } else if (accion == 'insert') {
      mostrar_formNuevo();
    }

    listar ();

  },
  method: 'POST'}).request();
}

function eliminar (id) {

  var table = 'investigacion';

  if(confirm('¿Eliminar la investigación de accidente?')) {
    new Ajax('index.php', {
    postBody: 'ajax=1&mod='+table+'&exec=eliminar&id=' + id,
    evalScripts: true,
    onComplete:function(){
      formCerrar();
      listar();
    },
    method: 'POST'}).request();
  }
}


function searchDin () {
  if ($('search_key').getValue().length > 2 || $('search_key').getValue().length == 0) {

    onLoading('listOut');

    new Ajax('index.php', {
    postBody: $('search_form').toQueryString() + '&ajax=1&mod=investigacion&exec=index',
    update: 'listOut',
    evalScripts: true,
    onComplete:function(){

      $('search_key').addClass('eventout').setStyle('font-weight', 'bold');
      $('aexport_excel').setProperty('href',$('aexport_excel').getProperty('href')+'&search_key='+$('search_key').getValue());
      $('aexport_print').setProperty('href',$('aexport_print').getProperty('href')+'&search_key='+$('search_key').getValue());
    },
    method: 'POST'}).request();
  } else {
    kenda_msg ('Requiere un mínimo de 3 caracteres','error')
  }

}
