﻿/**
 * SQS Technologies
* Bogota - Colombia
* 7416418
*/
var filas = 1;
var ids = 1;
function finaNueva()
{
    var objTbody=document.getElementById('datatable');
    var txtIds=document.getElementById(idhidenTXT);
    var nuevaFila = objTbody.firstChild.cloneNode(true);
    nuevaFila.id = "idFila" + ids;
    if(nuevaFila.style)
    {
  nuevaFila.style.backgroundColor='#ffffff';
    }
    objTbody.appendChild(nuevaFila);
    var opt = document.createElement("IMG");
    //Empresa en la que labora
    var txtEmpresa = document.createElement("INPUT");
    txtEmpresa.className =  "box";
    txtEmpresa.size = 18;
    objTbody.rows[filas].cells[0].innerText = "";
    objTbody.rows[filas].cells[0].appendChild(txtEmpresa);
    objTbody.rows[filas].cells[0].align="center";
    
    //Fecha de inicio
    var txtIni = document.createElement("INPUT");
    txtIni.className =  "box";
    txtIni.size = 11;
    txtIni.id = "txtIni" + ids;
    objTbody.rows[filas].cells[1].innerText= "";
    objTbody.rows[filas].cells[1].appendChild(txtIni);
    objTbody.rows[filas].cells[1].align="center";
   new Mask("dd/mm/yyyy", "date").attach(txtIni);
    
    //Fecha de Fin
    var txtFin = document.createElement("INPUT");
    txtFin.className =  "box";
    txtFin.size = 11;
    txtFin.id = "txtFin" + ids;
    objTbody.rows[filas].cells[2].innerText= "";
    objTbody.rows[filas].cells[2].appendChild(txtFin);
    objTbody.rows[filas].cells[2].align="center";
   new Mask("dd/mm/yyyy", "date").attach(txtFin); 
    //eval('Calendar.setup({inputField : "' + txtFin.id + '", ifFormat : "%d/%m/%Y",showsTime : false,timeFormat : "24"});');
    //Dias deducibles
    var txtDeducibles = document.createElement("INPUT");
    txtDeducibles.className =  "box";
    txtDeducibles.size = 3;
    objTbody.rows[filas].cells[3].innerText= "";
    objTbody.rows[filas].cells[3].appendChild(txtDeducibles);
    objTbody.rows[filas].cells[3].align="center";
    //Tiempo a cargo
    var txtTCargo = document.createElement("INPUT");
    txtTCargo.type="checkbox";
    txtTCargo.check=true;
    txtTCargo.size = 3;
    objTbody.rows[filas].cells[4].innerText= "";
    objTbody.rows[filas].cells[4].appendChild(txtTCargo);
    objTbody.rows[filas].cells[4].align="center";
    //Imagen para borrar
    objTbody.rows[filas].cells[5].innerText= "";
    objTbody.rows[filas].cells[5].appendChild(opt);
    objTbody.rows[filas].cells[5].align="center";
    opt.id = ids;
    opt.onclick = function delRowid(){borrarFila("idFila" + this.id);borrarNodo(this.id);}
    opt.src = "imagenes/Eliminar.gif"
    opt.alt="Quitar esta fila del listado de periodos a procesar";
    opt.style.cursor = "hand";
    var nodo = crearNodo(ids, new Array(txtEmpresa, txtIni, txtFin, txtDeducibles, txtTCargo));
    txtEmpresa.nodoProcess = nodo;
    txtIni.nodoProcess = nodo;
    txtFin.nodoProcess = nodo;
    txtDeducibles.nodoProcess = nodo;
    txtTCargo.nodoProcess = nodo;
    
    txtEmpresa.tipo = 's';
    txtIni.tipo = 'f';
    txtFin.tipo = 'f';
    txtDeducibles.tipo = 'n';
    txtTCargo.tipo = 'o';
    /*
    txtEmpresa.value = 'Empresa ' + filas;
    txtIni.value = '01/01/1980';
    txtFin.value = '12/12/2007';
    txtDeducibles.value = '0';
    txtTCargo.value = '0';
    */
    txtEmpresa.nombre = 'Entidad';
    txtIni.nombre = 'Fecha inicio';
    txtFin.nombre = 'Fecha fin';
    txtDeducibles.nombre = 'Deducibles';
    txtTCargo.nombre = 'Tiempo a cargo';
    filas++;
    ids++;
}

function mostrar()
{
    var txt = document.getElementById(idhidenTXT);
    var retorno = true;
    if(txt)
    {
        txt.value = mostrarNodos();
        if(errores!='')
        {
            alert('Debe coregir estos errores en los periodos antes de continuar:\n--+------------------------------------+---\n' + errores);
            retorno = false;
        }
        else if(txt.value == "|" || txt.value == "")
        {
            alert('Debe definir los periodos de tiempo del emplado.');
            retorno = false;
        }
    }
    return retorno;
}

function borrarFila(id)
{
    var txtIds=document.getElementById(idhidenTXT);
    var objTbody=document.getElementById('datatable');
    for( var x = 0; objTbody.childNodes[x]; x++ )
    {
        var nodo = objTbody.childNodes[x];
        if(nodo.id && nodo.id == id)
        {
            objTbody.removeChild(nodo);
            filas--;
            break;
        }
    }
    var ind = txtIds.value.indexOf("|" + id.replace("idFila", "") + "°");
    if(ind>=0)//Siempre deberia encontrarlo
    {
        var fin = txtIds.value.indexOf("|", ind + 1);
        txtIds.value = txtIds.value.substring(0, ind) + txtIds.value.substring(fin);
    }
}
