//Gorka - Se ve cuál ha sido la celda actualizada, para localizar
function actualizarCelda(gridName, cellId){    
    
    var seleccion = document.getElementById("seleccion").value;
    var grid = igtbl_getGridById(gridName);	    
    var band = grid.Bands[0];	  
    var cell = igtbl_getCellById(cellId);  
    var column = band.getColumnFromKey("campoClave");
    var row=igtbl_getActiveRow(gridName);
    
    var columnaAsociada = cellId.split("_")[cellId.split("_").length - 1];
    if (columnaAsociada == "0"){    
        if (cell.getValue() == true){	        
            document.getElementById("seleccion").value = seleccion + row.getCellByColumn(column).getValue() + ";";
        }else{
            document.getElementById("seleccion").value = document.getElementById("seleccion").value.replace(";" + row.getCellByColumn(column).getValue() + ";",";");
        }
    }else{
        var esFormatoCorrecto = comprobarFormatoCampos(cell);
        //Si no se quiere que vaya a servidor (comprobacion erronea)
        if (esFormatoCorrecto == false){
            //grid.CancelPostBack = !esFormatoCorrecto;
            document.getElementById("formatoCorrecto").value = "false";
            PonEstado(document.getElementById("errorFormatoIncorrecto").value);
        }else{
            PonEstado('');
        }
        document.getElementById("accionARealizar").value = "edicion" 
    }
}

//Gorka - Se lanza cuando se actualiza una fila, y sirve para que en caso de que se haya modificado
// la columna "Localizar", no se lance la petición al servidor
function actualizarFila(gridName, rowId){
    var grid = igtbl_getGridById(gridName);
    var row = igtbl_getRowById(rowId);
    var numColums = grid.Bands[0].Columns.length;
    var formatoCorrecto = true;
    if (document.getElementById("accionARealizar").value == "edicion"){
        for (var j=4;j<numColums;j++){
            formatoCorrecto = comprobarFormatoCampos(row.getCell(j));
            if (!formatoCorrecto){
                j = numColums;                
            }
        }
        if (formatoCorrecto){
            grid.CancelPostBack = false;
        }else{
            grid.CancelPostBack = true;
        }
        
    }else{
        grid.CancelPostBack = true;
    }
}

function comprobarFormatoCampos(cell){
    //Comprobar formatos antes de redirigir al servidor
    var longitudCorrecta = false;
    var formatoCorrecto = false;
    if (cell.getValue() == null){
        return true;
    }else{ 
        var tipoCampos = document.getElementById("tipoCampos").value;
        var tipoCampo = tipoCampos.split("*")[cell.Column.Index].split("/")[0];
        var longitudCampo = tipoCampos.split("*")[cell.Column.Index].split("/")[1];        
        if (longitudCampo == "-1"){
            longitudCorrecta = true;
        }else{
            longitudCorrecta = comprobarLongitud(cell.getValue(),longitudCampo);
        }   
        if (longitudCorrecta){
            if (tipoCampo == 1){
                //tipoCampo texto (nada que comprobar)
                formatoCorrecto = true;
            }else if(tipoCampo == 2){
                //tipoCampo integer
                formatoCorrecto = IsNumeric2(cell.getValue());
            }else if(tipoCampo == 3){
                //tipoCampo double
                formatoCorrecto = IsNumeric(cell.getValue());
            }else if(tipoCampo == 4){
                //tipoCampo booblean (nada que comprobar)
                formatoCorrecto = true;
            }else if(tipoCampo == 5){
                //tipoCampo date
                var rtdoFecha = CheckDate(cell.getValue());
                if (rtdoFecha == false){
                    formatoCorrecto = false;    
                }else{
                    cell.setValue(rtdoFecha);
                    formatoCorrecto = true;
                }
            }else if(tipoCampo == 6){
                //tipoCampo byte (comprobar??)
                formatoCorrecto = true;
            }else{
                formatoCorrecto = false;
            }
        }
        return (longitudCorrecta && formatoCorrecto);
     }
     
}

function localizarResultadoHijo(){   
      document.getElementById("accionARealizar").value = "localizar";    
      document.form1.submit();
}

function borrarResultadoHijo(){ 
      if (document.getElementById("seleccion").value != ";"){
          document.getElementById("accionARealizar").value = "borrar" ;     
          document.form1.submit();
      }
}

function guardarResultadoHijo(){
    document.getElementById("accionARealizar").value = "guardar"      
    document.form1.submit();
}

function activarLocalizarCalle(gridName, cellId){
    var grid=igtbl_getGridById("UltraWebGrid1");
    var cell=igtbl_getCellById(cellId);
    var row=igtbl_getActiveRow(gridName);
    var c=row.getIndex();
    
    document.getElementById("seleccion").value = c;
    
    if(cell.Index==0)
    {
       if (cell.getValue()==false)
       {
            grid.Rows.getRow(c).setSelected(false); 
       }
    }
}

function desactivarLocalizarCalle(gridName, rowId){           
   var grid=igtbl_getGridById("UltraWebGrid1");
   var row=igtbl_getActiveRow(gridName);
   var cell=row.getCell(0);            
   var c=row.getIndex();    
   
   if (cell.getValue()==false)
   {                
        var length = grid.Rows.length;
        var i = 0;
        for(i; i < length; i++){                                  
            if(i == c){                                                
               grid.Rows.getRow(c).activate();                       
               grid.Rows.getRow(c).setSelected(true);
               document.getElementById("seleccion").value = c;                       
            }else{                                               
                grid.Rows.getRow(i).getCell(0).setValue(false);
                grid.Rows.getRow(i).setSelected(false);                         
            }
        } 
   } else{               
       grid.Rows.getRow(c).setSelected(false);               
   }
}

function activarLocalizar(activar){
    if (parent.document.getElementById("localizarRtdo") != null) {
        if (activar){
            parent.document.getElementById("localizarRtdo").src = "./imagenes/editbarbuttons/localizar_on.gif";
        }else{
            parent.document.getElementById("localizarRtdo").src = "./imagenes/editbarbuttons/localizar_off.gif";
        }
    }    
}

function activarGuardar(activar){
    if (parent.document.getElementById("guardarRtdo") != null){
        if (activar){
            parent.document.getElementById("guardarRtdo").src = "./imagenes/editbarbuttons/guardar_cambios_on.gif";
        }else{
            parent.document.getElementById("guardarRtdo").src = "./imagenes/editbarbuttons/guardar_cambios_off.gif";
        }
    }
}

function activarInsertar(activar){
    if (parent.document.getElementById("insertarRtdo") != null){
        if (activar){
            parent.document.getElementById("insertarRtdo").src = "./imagenes/editbarbuttons/nuevo_on.gif";
        }else{
            parent.document.getElementById("insertarRtdo").src = "./imagenes/editbarbuttons/nuevo_off.gif";
        }
    }
}

function activarBorrar(activar){
    if (parent.document.getElementById("borrarRtdo") != null){
        if (activar){
            parent.document.getElementById("borrarRtdo").src = "./imagenes/editbarbuttons/eliminar_on.gif";
        }else{
            parent.document.getElementById("borrarRtdo").src = "./imagenes/editbarbuttons/eliminar_off.gif";
        }        
    }
}

function activarExcel(act) {

    activar(act);  

}

function activar(act) {

    if (parent.document.getElementById("ExportarExcel") != null) {
        if (act) {
            parent.document.getElementById("ExportarExcel").src = "./imagenes/editbarbuttons/excel_on.gif";
        } else {
            parent.document.getElementById("ExportarExcel").src = "./imagenes/editbarbuttons/excel_off.gif";
        }
    } 
}

function activarPaginasPorHoja(activar){
    if (document.getElementById("WebSplitter1_tmpl1_WebSplitter2_tmpl1_ddlPaginasPorHoja") != null){
        document.getElementById("WebSplitter1_tmpl1_WebSplitter2_tmpl1_ddlPaginasPorHoja").disabled = !(activar);  
    }else if(parent.document.getElementById("WebSplitter1_tmpl1_WebSplitter2_tmpl1_ddlPaginasPorHoja") != null) {
        parent.document.getElementById("WebSplitter1_tmpl1_WebSplitter2_tmpl1_ddlPaginasPorHoja").disabled = !(activar);        
    }    
}

function desactivarBotones(){
    activarLocalizar(false);
    activarInsertar(false);
    activarBorrar(false);
    activarExcel(false);
    activarPaginasPorHoja(false);
}

function desactivarLocalizados(){
    var grid=igtbl_getGridById("UltraWebGrid1");
    
    var length = grid.Rows.length;
    var i = 0;
    
    for(i; i < length; i++){      
        grid.Rows.getRow(i).getCell(0).setValue(false);
        grid.Rows.getRow(i).setSelected(false);
    } 
}

function desactivarFilas(){
   var grid=igtbl_getGridById("UltraWebGrid1");
   for(var i =0; i < grid.Rows.length; i++) { 
        for(var j=0; j < grid.Rows.getRow(i).cells.length; j++) { 
            grid.Rows.getRow(i).getCell(j).setEditable(false);
        }
   }
}

function activarFilas(){
   var grid=igtbl_getGridById("UltraWebGrid1");
   for(var i =0; i < grid.Rows.length; i++) { 
        for(var j=0; j < grid.Rows.getRow(i).cells.length; j++) { 
            grid.Rows.getRow(i).getCell(j).setEditable(true);
        }
   }
}

function verResultado(key)
{
    document.getElementById("seleccion").value = ";" + key + ";";
    document.getElementById("accionARealizar").value = "localizar";    
    document.form1.submit();
}

function activarSeleccionados(seleccionados){
    var grid=igtbl_getGridById("UltraWebGrid1");
    
    var length = grid.Rows.length;
    var i = 0;
    
    //var seleccion = document.getElementById("seleccion").value;
    var seleccion = ";";
    
    var partesSeleccion = seleccionados.split(";");
    if (partesSeleccion.length > 2){
        for(i; i < length; i++){ 
            for(j=1;j<partesSeleccion.length-1;j++){
                if (grid.Rows.getRow(i).getCell(2).getValue() == partesSeleccion[j]){
                    grid.Rows.getRow(i).getCell(0).setValue(true);
                    seleccion = seleccion + grid.Rows.getRow(i).getCell(2).getValue() + ";";
                }
            }
        } 
        document.getElementById("seleccion").value = seleccion
    } 
}


