/*

    grid.js

    Algunas funciones para trabajar con una grid al estilo de las
    de Excel y una variable, filaSeleccionada, que nos informa en
    todo momento de la fila seleccionada.

    Copyright (c) 2005, 2007 - Formauri, S.L. - Todos los derechos reservados.
    <http://www.formauri.es/>

*/


var
  F_INTEGER = 1
  , F_VARCHAR = 2
  , F_TEXT = 3
  , F_CHECKBOX = 4
  , F_RADIOBUTTON = 5
  , F_FILE = 6
  , F_IMAGE = 7
  , F_DATE = 8
  , F_TIME_HM = 9
  , F_TIME_HMS = 10
  , F_EMAIL = 11
  , F_URL = 12
  ;


var filaSeleccionada = -1;
var gridActiva = true;


function FilasConFondoBlanco(numFilas)
{
  for(var i = 0; i < numFilas; i++)
  {
    document.getElementById("fila" + i).className = "filaNormal";
  }
}

function FilaSeleccionada(queFila)
{
  filaSeleccionada = queFila;
  document.getElementById("fila" + queFila).className = "filaSeleccionada";
}


function EventoClickFilas(numFilas, queFila)
{
  if(gridActiva)
  {
    FilasConFondoBlanco(numFilas);
    FilaSeleccionada(queFila);
  }
  else
  {
  }
}

function Cursor(objeto, tipoCursor)
{
// objeto.style.cursor = (document.all) ? 'hand' : 'pointer';
  document.getElementById(objeto).style.cursor = tipoCursor;
}

function PincharFila(queFila, elForm)
{
  EventoClickFilas(losDatos.filas, queFila);
  ID = losDatos.datos[queFila][_ID_];
  elForm.idEntrada.value = ID;
}

function ObtenerFilaMatrizDatos(id)
{
  var fila = -1;

  for(var i = 0; i < losDatos.filas; i++)
  {
    if(losDatos.datos[i][_ID_] == id)
    {
      fila = i;
      break;
    }
  }

  return fila;
}



/*

  Recibe un objeto datosGrid con los siguientes datos:

  filas, columnas, ancho, alto, ancho_clip, alto_clip, columnaID, root_path, photo_path, photo_prefix,
  tituloColumnas, tipoDatoColumnas, visible, datos

  Ejemplo:

  datosGrid =
  {
    filas: 6,
    columnas: 5,
    ancho: 300,
    alto: 200,
    ancho_clip: 600,
    alto_clip: 200,
    columnaID: 10;
    root_path, photo_path, photo_prefix;
    tituloColumnas: ["Título 1", "Título 2", "Título 3", ...],
    tipoDatoColumnas: [F_INTEGER, F_EMAIL, ...],
    visible: [true, false, true, true,...],
    datos: [ ["Dato (1, 1)", "Dato (1, 2)", "Dato (1,3)", ...],[...], ...]
  };
*/

function StrHTML(str)
{
  return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/'/g, '&#39;').replace(/\n/g, '<br />\n');
}


function CrearGrid(datosGrid)
{
  var txtGrid =
    "<div style=\"width:" + datosGrid.ancho + ";height:" + datosGrid.alto + ";"
    + "clip:rect(0, " + datosGrid.ancho_clip + ", 0, " + datosGrid.alto_clip + ");"
    + "overflow:auto;text-align:center;margin-left:auto;margin-right:auto\">"
    ;

  txtGrid += "<table id=\"tblDatos\" cellspacing=\"0\" cellpadding=\"0\""
    + " style=\"margin-left:auto;margin-right:auto;text-align:left;\">";

  txtGrid += "<tr>";
  for(var i = 0; i < datosGrid.columnas; i++)
  {
    if( datosGrid.visible[i] )
    {
      txtGrid += "<th class=\"columna-" + i + "\">" + datosGrid.tituloColumnas[i] + "</th>";
    }
  }
  txtGrid += "</tr>";

  for(i = 0; i < datosGrid.filas; i++)
  {
    txtGrid +=
      "<tr id=\"fila" + i + "\" onclick=\"EventoClickFilas("
      + datosGrid.filas + ", " + i + ");\""
      + " onmouseover=\"Cursor('fila" + i + "', 'default');\">"
      ;
    for(var j = 0; j < datosGrid.columnas; j++)
    {
      if( datosGrid.visible[j] )
      {
        tipoCampo = datosGrid.tipoDatoColumnas[j];

        if(tipoCampo == F_INTEGER)
          txtCelda = "<span style=\"display:block;margin:0;text-align:right\">" + datosGrid.datos[i][j] + "</span>";
        else if(tipoCampo == F_CHECKBOX)
          txtCelda = "<span style=\"display:block;margin:0;text-align:center\">"
            + (datosGrid.datos[i][j] == 1 ? "Sí" : "No") + "</span>"
            ;
        else if(tipoCampo == F_RADIOBUTTON)
          txtCelda = "";
        else if(tipoCampo == F_FILE)
          txtCelda = "";
        else if(tipoCampo == F_IMAGE)
          txtCelda = "<img style=\"display:block;margin:0;text-align:center;padding-bottom:1px\""
            + " src=\"" + datosGrid.root_path + "util/show-image.php?w=75&amp;"
            + "f=" + datosGrid.photo_path + datosGrid.photo_prefix
            + datosGrid.datos[i][datosGrid.columnaID] + ".jpg\""
            + " alt=\"Miniatura\" title=\"Miniatura\" />"
            ;
        else if(tipoCampo == F_DATE)
        {
          if (datosGrid.datos[i][j] != '')
          {
            txtCelda = "<span style=\"display:block;margin:0;text-align:center\">"
              + datosGrid.datos[i][j].substring(6, 8)
              + "/" + datosGrid.datos[i][j].substring(4, 6)
              + "/" + datosGrid.datos[i][j].substring(0, 4)
              + "</span>"
              ;
          }
          else
          {
            txtCelda = "<span style=\"display:block;margin:0;text-align:center\"> - </span>";
          }
        }
        else if(tipoCampo == F_TIME_HM)
          txtCelda = "<span style=\"display:block;margin:0;text-align:center\">"
            + datosGrid.datos[i][j].substring(0, 2)
            + ":" + datosGrid.datos[i][j].substring(2, 4)
            + "</span>"
            ;
        else if(tipoCampo == F_TIME_HMS)
          txtCelda = "<span style=\"display:block;margin:0;text-align:center\">"
            + datosGrid.datos[i][j].substring(0, 2)
            + ":" + datosGrid.datos[i][j].substring(2, 4)
            + ":" + datosGrid.datos[i][j].substring(4, 6)
            + "</span>"
            ;
        else if(tipoCampo == F_EMAIL)
          txtCelda = "<a href=\"mailto:" + datosGrid.datos[i][j] + "\">" + datosGrid.datos[i][j] + "</a>";
        else if(tipoCampo == F_URL)
          txtCelda = "<a href=\"" + datosGrid.datos[i][j] + "\">" + datosGrid.datos[i][j] + "</a>";
        else
          txtCelda = StrHTML(datosGrid.datos[i][j]);

        txtGrid += "<td class=\"columna-" + j + "\">" + txtCelda + "</td>";
      }
    }
    txtGrid += "</tr>";
  }
  if(datosGrid.filas == 0)
  {
    txtGrid += "<tr><td colspan=\"" + datosGrid.columnas + "\" style=\"text-align:center;font-style:italic;font-weight:bold;\">No hay datos para mostrar, la tabla está vacía</td></tr>";
  }
  txtGrid += "</table>";
  txtGrid += "</div>";

  document.open();
  document.write(txtGrid);
  document.close();
}

