/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

tuga_hash_dbgrid = new Object();

var tuga_dbgrid = function(nombre,data){ //data es el contenido de una linea
    this.nombrevariable = nombre;
    this.html = "<div id=\"" + nombre+ "\" class=\"dbgrid\">";
    this.linea = "";
    this.id = nombre;
    this.numeroLineas = 1;
    this.contador = 1;
    this.numeroColumnas = 0;
    this.currentCol = 0;
    this.encabezado = "";
    this.original = "";
    var campos;
    var tam;
    var detalle;
    var primer_vez = true;
    var i = 0;

    if(data.charAt(data.length -1 ) == ';'){
        data = data.substring(0, data.length - 1);
    }
    campos = data.split(";");
    tam = campos.length;

    this.encabezado = "<div id=\"tuga_dbgrid_linea_0\" class=\"tuga_dbgrid_linea\">";
    
    for (i=0;i<tam;i++) {
        detalle = campos[i].split(":"); //obtenemos un arreglo [etiqueta tipocampo variable obligatorio]
        if(detalle != null && primer_vez){
            this.encabezado += "<div id=\"tuga_dbgrid_papelera\"></div><div id=\"tuga_dbgrid_newline\"></div>";
            primer_vez = false;
        }
        if (detalle[1] == 'ENTERO'){
            this.encabezado += "<div id=\"tuga_dbgrid_campo_entero\">" + detalle[0] + "</div>";
        } else if (detalle[1] == 'TEXTO'){
            this.encabezado += "<div id=\"tuga_dbgrid_campo_texto\">" + detalle[0] + "</div>";
        } else if (detalle[1] == 'FECHA'){
            this.encabezado += "<div id=\"tuga_dbgrid_campo_fecha\">" + detalle[0] + "</div>";
        } else if (detalle[1].substring(0, 5) == 'COMBO'){
            this.encabezado += "<div id=\"tuga_dbgrid_campo_combo\">" + detalle[0] + "</div>";
        }else if (detalle[1].substring(0, 5) == 'FLOAT'){
            this.encabezado += "<div id=\"tuga_dbgrid_campo_float\">" + detalle[0] + "</div>";
        }
    }
    this.encabezado += "</div>";
    
    primer_vez = true;
    var onblur = "";
    var onfocus = "";
    var onblurfloat = "";
    var combodata = "";

    for (i=0;i<tam;i++) {
        detalle = campos[i].split(":"); //obtenemos un arreglo [etiqueta tipocampo variable obligatorio]
        if(detalle != null && primer_vez){
            this.linea += "<div id=\"tuga_dbgrid_papelera_img\" onclick=\"tuga_dbgrid_elimina_Linea(this,'" + nombre + "')\"></div>";
            this.linea += "<div id=\"tuga_dbgrid_newline_img\" onclick=\"tuga_dbgrid_validaNewLineButton('" + nombre + "',this.numeroLineas)\"></div>";
            primer_vez = false;
        }
        onblur = "";
        onblurfloat = "tuga_dbgrid_blurFloatBox(this);";
        if(detalle[3] == 'true'){
            onblur  = " onblur=\"tuga_blur(this,'" + detalle[0] + "'," + detalle[3] + ")\" ";
            onblurfloat += "tuga_blur(this,'" + detalle[0] + "'," + detalle[3] + ")";
        }
        onblurfloat = " onblur=\"" + onblurfloat + "\" ";

        onfocus = " onfocus=\"tuga_dbgrid_setCurrentCol('" + nombre + "'," + i + ")\" ";

        if (detalle[1] == 'ENTERO'){
            this.linea += "<div id=\"tuga_dbgrid_campo_entero\"><input type=\"text\" class=\"tugadbgridinput\" alt=\"" + detalle[0] + "\" id=\"" + nombre + "_" + detalle[2] + "_this.numeroLineas_" + i + "\" name=\"" + nombre + "_" + detalle[2] + "\" value=\"\" " + onblur + " " + onfocus + " onkeypress=\"return tuga_dbgrid_numberBox(event," + i + "," + tam + ",this)\"></div>";
        } else if (detalle[1] == 'TEXTO'){
            this.linea += "<div id=\"tuga_dbgrid_campo_texto\"><input type=\"text\"  class=\"tugadbgridinput\" alt=\"" + detalle[0] + "\" id=\"" + nombre + "_" + detalle[2] + "_this.numeroLineas_" + i + "\" name=\"" + nombre + "_" + detalle[2] + "\" value=\"\" " + onblur + "  " + onfocus + " onkeypress=\"return tuga_dbgrid_textBox(event," + i + "," + tam + ",this)\"></div>";
        } else if (detalle[1] == 'FECHA'){
            this.linea += "<div id=\"tuga_dbgrid_campo_fecha\"><table border=\"0\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td><input type=\"text\"  class=\"tugadbgridinput\" alt=\"" + detalle[0] + "\"  id=\"" + nombre + "_" + detalle[2] + "_this.numeroLineas_" + i + "\" name=\"" + nombre + "_" + detalle[2] + "\" value=\"\" " + onblur + " onfocus=\"tuga_dbgrid_setCurrentCol('" + nombre + "'," + i + "); return showCalendar('" + nombre + "_" + detalle[2] + "_this.numeroLineas_" + i + "', '%d-%m-%Y')\" onkeydown=\"return tuga_dbgrid_dateBox(event," + i + "," + tam + ",this)\"></td>";
            this.linea += "<td><A HREF=\"#\" onclick=\"return showCalendar('" + nombre + "_" + detalle[2] + "_this.numeroLineas_" + i + "', '%d-%m-%Y')\">";
            this.linea += "<div id=\"tuga_dbgrid_calendar_img\" onclick=\"return showCalendar('" + nombre + "_" + detalle[2] + "_this.numeroLineas_" + i + "', '%d-%m-%Y')\"></div></A></td>";
            this.linea += "</tr></table></div>";
        } else if (detalle[1].substring(0, 5) == 'COMBO'){
            combodata = "";
            new Ajax.Request("../ajaxSelect?c=" + detalle[1].substring(6, detalle[1].length ), {
                asynchronous : false,
                onSuccess: function(transport){
                    combodata = transport.responseText;
                }
            });
            this.linea += "<div id=\"tuga_dbgrid_campo_combo\"><select  class=\"tugadbgridinput\" title=\"" + detalle[0] + "\" id=\"" + nombre + "_" + detalle[2] + "_this.numeroLineas_" + i + "\" name=\"" + nombre + "_" + detalle[2] + "\" " + onblur + "  " + onfocus + ">" + combodata + "</select></div>";
        }else if(detalle[1] == 'FLOAT'){
            this.linea += "<div id=\"tuga_dbgrid_campo_float\"><input type=\"text\"  class=\"tugadbgridinput\" alt=\"" + detalle[0] + "\" id=\"" + nombre + "_" + detalle[2] + "_this.numeroLineas_" + i + "\" name=\"" + nombre + "_" + detalle[2] + "\" value=\"\" " + onblurfloat + " " + onfocus + " onkeypress=\"return tuga_dbgrid_floatBox(event," + i + "," + tam + ",this)\"></div>";
        }
    }
    
    this.html += this.encabezado;
    this.original += this.encabezado;
    this.html += "<div id=\"tuga_dbgrid_linea_" + this.contador + "\" class=\"tuga_dbgrid_linea\">";
    this.original += "<div id=\"tuga_dbgrid_linea_" + this.contador + "\" class=\"tuga_dbgrid_linea\">";
    this.html += this.linea.replace(/this.numeroLineas/g,this.contador);
    this.original += this.linea.replace(/this.numeroLineas/g,this.contador);
    this.html += "</div>";
    this.original += "</div>";
    
    this.html += "</div>";
    this.numeroColumnas = tam;
    this.render  = tuga_dbgrid_render;
    this.getData = tuga_dbgrid_getData;
    this.getRowSize = tuga_dbgrid_numeroLineas;
    this.getColSize = tuga_dbgrid_numeroColumnas;
    this.getCurrentCol = tuga_dbgrid_currentCol;
    this.getCurrentRow = tuga_dbgrid_currentRow;
    tuga_hash_dbgrid[nombre] = this;
}

function tdgb_esObj(objeto, nombrecampo){
    //Compara si el objeto es el indicado por el nombre
    //retorna verdadero en este caso
    var tmpnombre = objeto.name;
    var tmppos = tmpnombre.indexOf('_');
    var nombregrid = tmpnombre.substring(0, tmppos +1);
    if(objeto.name == (nombregrid + nombrecampo)){
        return true;
    }
    return false;
}

function tdbg_esObj(objeto, nombrecampo){
    //Compara si el objeto es el indicado por el nombre
    //retorna verdadero en este caso
    return tdgb_esObj(objeto, nombrecampo);
}

function tdbg_esGrid(objeto, nombregrid){
    //Compara si el objeto hace parte del dbgrid con el nombre indicado,
    //retorna verdadero en este caso
    var tmpnombre = objeto.name;
    var tmppos = tmpnombre.indexOf('_');
    var nombregridtemp = tmpnombre.substring(0, tmppos);
    if(nombregrid == (nombregridtemp)){
        return true;
    }
    return false;
}

function tdbg_getCellValue(campoactual,nombrecampo){
    var tmpnombre = campoactual.name;
    var tmppos = tmpnombre.indexOf('_');
    var nombregrid = tmpnombre.substring(0, tmppos +1);

    var lineaActual = tuga_dbgrid_currentRow(campoactual);
    if(lineaActual == 0){
        return campoactual.form[nombregrid + nombrecampo].value;
    }
    return campoactual.form[nombregrid + nombrecampo][lineaActual].value;

}

function tdbg_setCellValue(campoactual, nombrecampo, valor){
    var tmpnombre = campoactual.name;
    var tmppos = tmpnombre.indexOf('_');
    var nombregrid = tmpnombre.substring(0, tmppos +1);

    var lineaActual = tuga_dbgrid_currentRow(campoactual);
    if(lineaActual == 0){
        campoactual.form[nombregrid + nombrecampo].value = valor;
    }
    else {
        campoactual.form[nombregrid + nombrecampo][lineaActual].value = valor;
    }
}

function tuga_dbgrid_setCurrentCol(nombre,pos){
    //establece la posicion actual de columna (posicion en la que esta trabajando el usuario)
    tuga_hash_dbgrid[nombre].currentCol = pos;
}

function tuga_dbgrid_numeroLineas(){
    //metodo del objeto
    return this.numeroLineas;
}

function tuga_dbgrid_currentCol(){
    //metodo del objeto
    return this.currentCol;
}

function tuga_dbgrid_currentRow(obj){
    //metodo del objeto
    var i, j;
    var retorno = 0;
    for ( i = 0; i < document.forms.length; i ++)
    {
        if(document.forms[i][obj.name]){
            for ( j = 0; j < document.forms[i][obj.name].length; j++)
            {
                if(document.forms[i][obj.name][j].id == obj.id){
                    retorno = j;
                }
            }
        }
    }
    return retorno;
}

function tuga_dbgrid_numeroColumnas(){
    //metodo del objeto
    return this.numeroColumnas;
}

function tuga_dbgrid_getData(campo){
    //metodo del objeto
    var i, j;
    var retorno = "+";
    var valor = "";
    for ( i = 0; i < document.forms.length; i ++)
    {
        for ( j = 0; j < document.forms[i].length; j ++)
        {
            if(document.forms[i][j].name == this.nombrevariable + "_" + campo && document.forms[i][j].type == "text"){
                valor = document.forms[i][j].value.replace(/\+/g,"%2B").replace(/,/g,"%2C");
                if(valor == ''){
                    valor = "%20";
                }
                retorno += valor + "+";
            } else if (document.forms[i][j].name == this.nombrevariable + "_" + campo && document.forms[i][j].type == "select-one"){
                valor = document.forms[i][j][document.forms[i][j].selectedIndex].value.replace(/\+/g,"%2B").replace(/,/g,"%2C");
                if(valor == ''){
                    valor = "%20";
                }
                retorno += valor + "+";
            }
        }
    }
    return retorno;
}

function tuga_dbgrid_render(){
    //metodo del objeto
    document.write( this.html );
}

function tuga_dbgrid_elimina_Linea(nombrediv, nombrevar){
    //elimina una linea del control
    var Linea = nombrediv.parentNode; //Nodo es Linea
    var Nodo = Linea.parentNode;
    var id = Linea.id;
    var numerol = id.lastIndexOf("_");
    numerol = id.substring(id.lastIndexOf("_") + 1, id.length);
    var tempcnt = tuga_hash_dbgrid[nombrevar].numeroLineas;
    if( tempcnt == 1){
        alert("Debe haber por lo menos un registro");
    }
    else{
        Nodo.removeChild(Linea);
        tuga_hash_dbgrid[nombrevar].numeroLineas--;

        if(numerol == ("" + tuga_hash_dbgrid[nombrevar].contador)){
            tuga_hash_dbgrid[nombrevar].contador --;
        }
    }
}

function tuga_dbgrid_nueva_Linea(nombrevar)
{    
    //agrega una nueva linea al control
    var temp = tuga_hash_dbgrid[nombrevar];
    tuga_hash_dbgrid[nombrevar].numeroLineas++;
    tuga_hash_dbgrid[nombrevar].contador++;
    var d = document.getElementById(nombrevar);
    var divNode = document.createElement("div");
    divNode.setAttribute('id','tuga_dbgrid_linea_' + temp.contador);
    divNode.setAttribute('class','tuga_dbgrid_linea');
    divNode.setAttribute('className','tuga_dbgrid_linea');
    divNode.innerHTML = temp.linea.replace(/this.numeroLineas/g,temp.contador);
    d.appendChild(divNode);
}

function tuga_blur(textbox, nombre, validaNulo)
{
    //evento que se dispara cuando uno de los campos deja de estar seleccionado
    var retorno = true;
    //si se quita el siguiente bloque de comentarios, cuando se tratan de cajas de texto mostrará un
    //alerta por javascript

    /*if(validaNulo == true){
        retorno = tuga_dbgrid_validaNulo(textbox,nombre);
    }*/

    return retorno;
}

function tuga_dbgrid_validaNulo(textbox,nombre)
{
    //valida si una caja de txto esta vacia o no
    var texto = textbox.value;
    texto = tuga_trim(texto);
    if (texto == "")
    {
        alert("Escriba un valor para el campo " + nombre);
        return (false);
    }
    return true;
}

function tuga_dbgrid_validaSelecc(combo, nombre)
{
    //valida si un combo tiene seleccion o no
    var valor = combo[combo.selectedIndex].value;
    valor = tuga_trim(valor);
    if (valor == "")
    {
        alert("Seleccione un valor para el campo " + nombre);
        return (false);
    }
    return true;
}

function tuga_dbgrid_validaEnviar(nombrevar)
{
    //Este metodo se usa para validar toda la dbgrid
    var retorno = true;
    var i, j;
    for ( i = 0; i < document.forms.length; i ++)
    {
        for ( j = 0; j < document.forms[i].length && retorno; j ++)
        {
            if (document.forms[i][j].onblur){
                if(document.forms[i][j].name.indexOf(nombrevar + "_",0) != -1 && document.forms[i][j].type == "text"){
                    retorno = tuga_dbgrid_validaNulo(document.forms[i][j],document.forms[i][j].alt);
                } else if(document.forms[i][j].name.indexOf(nombrevar + "_",0) != -1 && document.forms[i][j].type == "select-one"){
                    retorno = tuga_dbgrid_validaSelecc(document.forms[i][j],document.forms[i][j].title);
                }
            }
        }
    }
    return retorno;
}

function tuga_dbgrid_validaNewLineButton(dbgridname, linenumber)
/* Funcion que se dispara cuando se presiona el icono de nueva linea */
{
    var retorno = false;
    retorno = tuga_dbgrid_sub_validaForma_nonewline(dbgridname);
    if(window.tuga_campo_newline_user_function){
        window.tuga_campo_newline_user_function(dbgridname, linenumber);
    }else if(retorno){
        tuga_dbgrid_nueva_Linea(dbgridname);
    }
}

function tuga_dbgrid_validaForma(obj)
{
    if(window.tuga_campo_user_function){
        window.tuga_campo_user_function(obj);
    }
    else {
        tuga_dbgrid_sub_validaForma(obj);
    }
}

function tuga_dbgrid_sub_validaForma(obj){
    //Este metodo se usa para validar una linea antes de crear otra
    //Si la validacion es correcta se crea una nueva linea   
    var tmpnombre = obj.name;
    var tmppos = tmpnombre.indexOf('_');
    var nombrevar = tmpnombre.substring(0, tmppos);  
    tuga_dbgrid_sub_validaForma_n(nombrevar);
}

function tuga_dbgrid_getObj(dbgridname, nombrecampo, linea){
    //dbgridoname es el nombre del objeto dbgrid
    //Este metodo devuelve el objeto de nombre 'nombrecampo' que esta ubicado en
    //el numero de linea indicado por el parametro 'linea'
    var temp = tuga_hash_dbgrid[dbgridname];
    var tempnm;
    for(i = 0; i < temp.numeroColumnas; i++){
        tempnm = temp.nombrevariable + "_" + nombrecampo + "_" + linea + "_" + i;
        if(document.getElementById(tempnm)){
            return document.getElementById(tempnm);
        }
    }
    return null;
}

function tuga_dbgrid_sub_validaForma_nonewline(nm){
    //Este metodo se usa para validar una linea pero no crea la linea
    var retorno = true;
    var i, j;
    var nombrevar = nm;

    for ( i = 0; i < document.forms.length && retorno ; i ++)
    {
        for ( j = 0; j < document.forms[i].length && retorno; j ++)
        {
            if (document.forms[i][j].onblur){
                if(document.forms[i][j].name.indexOf(nombrevar + "_",0) != -1 && document.forms[i][j].type == "text"){
                    retorno = tuga_dbgrid_validaNulo(document.forms[i][j],document.forms[i][j].alt);
                } else if(document.forms[i][j].name.indexOf(nombrevar + "_",0) != -1 && document.forms[i][j].type == "select-one"){
                    retorno = tuga_dbgrid_validaSelecc(document.forms[i][j],document.forms[i][j].title);
                }
            }
        }
    }
    return retorno;
}

function tuga_dbgrid_sub_validaForma_n(nm){
    //Este metodo se usa para validar una linea antes de crear otra
    //Si la validacion es correcta se crea una nueva linea
    var retorno = true;
    var i, j;
    //var tmpnombre = obj.name;
    //var tmppos = tmpnombre.indexOf('_');
    //var nombrevar = tmpnombre.substring(0, tmppos);
    var nombrevar = nm;

    for ( i = 0; i < document.forms.length && retorno ; i ++)
    {
        for ( j = 0; j < document.forms[i].length && retorno; j ++)
        {
            if (document.forms[i][j].onblur){
                if(document.forms[i][j].name.indexOf(nombrevar + "_",0) != -1 && document.forms[i][j].type == "text"){
                    retorno = tuga_dbgrid_validaNulo(document.forms[i][j],document.forms[i][j].alt);
                } else if(document.forms[i][j].name.indexOf(nombrevar + "_",0) != -1 && document.forms[i][j].type == "select-one"){
                    retorno = tuga_dbgrid_validaSelecc(document.forms[i][j],document.forms[i][j].title);
                }
            }
        }
    }
    if(retorno){
        tuga_dbgrid_nueva_Linea(nombrevar);
    }
}

function tuga_dbgrid_dateBox(e, num, tam, obj)
{
    var retorno = true;
    var ultimo_campo = false;
    //8: Backspace, 13: Enter, 9: Tab,
    if (e.which)
    {
        if(e.which != 9 && e.which != 13 && e.which != 8 && e.which != 46)
            retorno = false;
        //se quita de la condicion && num == (tam -1)
        if((e.which == 13 || e.which == 9))
            ultimo_campo = true;
    }
    else if(e.keyCode)
    {
        if(e.keyCode != 9 && e.keyCode != 13 && e.keyCode != 8 && e.keyCode != 46)
            retorno =  false;
        //se quita de la condicion && num == (tam -1)
        if((e.keyCode == 13 || e.keyCode == 9))
            ultimo_campo = true;
    }

    //se agrega como parte de la condicion && window.tuga_campo_user_function
    if(ultimo_campo && retorno && window.tuga_campo_user_function){
        //tuga_dbgrid_validaForma(obj);
        window.tuga_campo_user_function(obj);
    }
    return retorno;
}

function tuga_dbgrid_numberBox(e, num, tam, obj)
{
    //devuelve true si el caracter es valido, falso de lo contrario
    var retorno = true;
    var ultimo_campo = false;
    if (e.which)
    {
        if(e.which != 8 && e.which != 13 && e.which != 9 && (e.which < 48 || e.which > 57))
            retorno = false;
        //se quita de la condicion && num == (tam -1)
        if((e.which == 13 || e.which == 9))
            ultimo_campo = true;
    }
    else if(e.keyCode)
    {
        //8: Backspace, 13: Enter, 9: Tab,
        // entre el 48 y el 57 entan los numeros
        if(e.keyCode != 8 && e.keyCode != 13 && e.keyCode != 9 && (e.keyCode < 48 || e.keyCode > 57))
            retorno = false;
        //se quita de la condicion && num == (tam -1)
        if((e.keyCode == 13 || e.keyCode == 9))
            ultimo_campo = true;
    }
    //se agrega como parte de la condicion && window.tuga_campo_user_function
    if(ultimo_campo && retorno && window.tuga_campo_user_function){
        //tuga_dbgrid_validaForma(obj);
        window.tuga_campo_user_function(obj);
    }
    return retorno;
}

function tuga_dbgrid_floatBox(e, num, tam, obj){
    var temp;
    if (e.which){
        if(e.which == 46){
            temp = obj.value;
            if(temp.indexOf(".") != -1){
                return false;
            }else{
                return true;
            }
        }
        if(e.which != 8 && e.which != 9 && (e.which < 48 || e.which > 57)){
            return false;
        }
    }
    else if(e.keyCode){
        if(e.keyCode == 46){
            temp = obj.value;
            if(temp.indexOf(".") != -1){
                return false;
            }else{
                return true;
            }
        }
        if(e.keyCode != 8 && e.keyCode != 9 && (e.keyCode < 48 || e.keyCode > 57)){
            return false;
        }
    }
    return true;
}

function tuga_dbgrid_blurFloatBox(obj){
    var temp = obj.value;
    var tam;
    if(temp.charAt(0) == '.'){
        obj.value = "0" + obj.value;
    }
    temp = obj.value;
    tam = temp.length;
    if(temp.charAt(tam-1) == '.'){
        obj.value = obj.value + "0";
    }
}

function tuga_dbgrid_textBox(e, num, tam, obj)
{
    var retorno = true;
    var ultimo_campo = false;
    if (e.which)
    {
        //se quita de la condicion && num == (tam -1)
        if((e.which == 13 || e.which == 9))
            ultimo_campo = true;
    }
    else if(e.keyCode)
    {
        //se quita de la condicion && num == (tam -1)
        if((e.keyCode == 13 || e.keyCode == 9))
            ultimo_campo = true;
    }
    //se agrega como parte de la condicion && window.tuga_campo_user_function
    if(ultimo_campo && retorno && window.tuga_campo_user_function){
        //tuga_dbgrid_validaForma(obj);
        window.tuga_campo_user_function(obj);
    }
    return retorno;
}

function tuga_trim(texto)
{
    if (texto == "")
    {
        return "";
    }

    for (i = 0;  i < texto.length;  i++)
    {
        if (texto.charAt(i) == ' ' && (i+1) < texto.length )
        {
            texto = texto.substring(i+1,texto.length);
        }
        else
        {
            break;
        }
    }

    for (i = texto.length - 1;  i > 0;  i--)
    {
        if (texto.charAt(i) == ' ')
        {
            texto = texto.substring(0,i);
        }
        else
        {
            break;
        }
    }
    if (texto.charAt(0) == ' ') return "";
    return texto;
}


function tuga_dbGridGetRowSize(dbgridname){
    /*obtiene el numero de filas del dbgrid*/
    var dbgridactual = tuga_hash_dbgrid[dbgridname];
    var lineaactual = dbgridactual.numeroLineas;
    return lineaactual;
}

function tuga_dbGridSetCellValue(dbgridname, fieldname, fieldvalue){
    /*Asigna un valor al campo fieldname de la ultima fila */
    var lineaactual = tuga_hash_dbgrid[dbgridname].contador;
    var obj = tuga_dbgrid_getObj(dbgridname, fieldname, lineaactual);
    if(obj != null){
        tdbg_setCellValue(obj, fieldname, fieldvalue);
    }
}

function tuga_dbGridGetCellValue(dbgridname, fieldname){
    /*Obtiene el valor del campo fieldname de la ultima fila */
    /*ingresa: el nombre del dbgrid y el nombre del campo a obtener su valor */
    var lineaactual = tuga_hash_dbgrid[dbgridname].contador;
    var obj = tuga_dbgrid_getObj(dbgridname, fieldname, lineaactual);
    if(obj != null){
        return obj.value;
    //return tdbg_getCellValue(obj, fieldname);
    }
    return null;
}

function tuga_dbGridNewLine(dbgridname){
    /*Agrega una nueva linea al dbgrid */
    tuga_dbgrid_nueva_Linea(dbgridname);
}

function tuga_dbGridDisableLastField(dbgridname, fieldname){
    /*Deshabilita el campo fieldname de la ultima fila */
    var lineaactual = tuga_hash_dbgrid[dbgridname].contador;
    var obj = tuga_dbgrid_getObj(dbgridname, fieldname, lineaactual);
    tuga_disable(obj.id);
}

function tuga_dbGridClean(dbgridname){
    var obj = document.getElementById(dbgridname);
    obj.innerHTML = tuga_hash_dbgrid[dbgridname].original;
    tuga_hash_dbgrid[dbgridname].numeroLineas = 1;
    tuga_hash_dbgrid[dbgridname].contador = 1;
    tuga_hash_dbgrid[dbgridname].currentCol = 0;
}


function tuga_dbGridHaveDuplicate(dbgridname, fields, testnull){
    //fields es un array con el id de los campos que no se pueden repetir
    //testnull : si es true, tiene en cuenta los valores nulos para buscar duplicados,
    //           false de lo contrario
    var tam = fields.length;
    var iguales = 0, i = 0, j = 0, k = 0;
    var nlineas = tuga_hash_dbgrid[dbgridname].numeroLineas;
    var temp1 = new Array();
    var temp2 = new Array();
    if(nlineas == 1){
        return false;
    }
    for(i = 1; i <= nlineas; i++){
        for(j=0; j < tam; j++){
            temp1[j] = tuga_dbgrid_getObj(dbgridname, fields[j], i);
            if(temp1[j]){
                temp1[j] = temp1[j].value;
            }
        }
        for(k = 1; k <= nlineas; k++){
            if(k != i){
                for(j=0; j < tam; j++){
                    temp2[j] = tuga_dbgrid_getObj(dbgridname, fields[j], k);
                    if(temp2[j]){
                        temp2[j] = temp2[j].value;
                    }
                }
                iguales = 0
                for(j=0; j < tam; j++){
                    if(testnull){ //si incluye en la comparacion los nulos
                        if(temp1[j] == temp2[j]){
                            iguales ++;
                        }
                    } else { //si no incluye la comparacion de nulos y vacios
                        if(temp1[j] == temp2[j] && temp2[j] != null && temp2[j] != ''){
                            iguales ++;
                        }
                    }
                }
                if(iguales == tam){
                    return true;
                }
            }
        }
    }
    return false;
}
