var aparelhosCache = { };
var viewed = -1;

var danosCache = { };
var viewedDanos = -1;

var isIE60 =(navigator.userAgent && navigator.userAgent.indexOf("MSIE 6") != -1) ||
            (navigator.userAgent && navigator.userAgent.indexOf("MSIE 7") != -1);

function initRessarcimento() {
     showDivDanosAparelho();
  
  fillTableDanos();
  fillTable();
}

//
// Funcoes para declaracao de aparelhos
//
function fillTable() {

  RessarcimentoDanos.getAparelhosRessarcimento(function(aparelho) {
    
    // Delete all the rows except for the "pattern" row
    dwr.util.removeAllRows("aparelhobody", { filter:function(tr) {
    
      return (tr.id != "pattern") && (tr.id != "semAparelhos");
    }});
    
    // Create a new set cloned from the pattern row
    var apar, id;
    aparelho.sort(function(p1, p2) { return (p1.id >p2.id); } );
    for (var i = 0; i < aparelho.length; i++) {
      apar = aparelho[i];
      id = i + 1;
      dwr.util.cloneNode("pattern", { idSuffix:id });
      dwr.util.setValue("descricaoTbl"+id, apar.descricao);
      dwr.util.setValue("voltagemTbl"+id, apar.voltagem);
      dwr.util.setValue("bifasicoTbl"+id, apar.bifasico);
      dwr.util.setValue("anoFabricacaoTbl"+id, apar.anoFabricacao);
      dwr.util.setValue("descTempoUsoTbl"+id, apar.descTempoUso);
      dwr.util.setValue("reparadoAntesTbl"+id, apar.statusReparoAntes);
      dwr.util.setValue("reparadoDepoisTbl"+id, apar.statusReparoDepois);
     
      // Workaround para IE6 que nao suporta o valor table-row
      if(isIE60){
            document.getElementById("pattern"+id).style.display = "";
      } else {
            document.getElementById("pattern"+id).style.display = "table-row";
      }
      
      aparelhosCache[id] = apar;
    }
    
    // Workaround para IE6 que nao suporta o valor table-row
    if(isIE60){
      if(aparelho.length == 0){
        document.getElementById("semAparelhos").style.display = "";
      } else {
        document.getElementById("semAparelhos").style.display = "none";
      }
    } else {
      if(aparelho.length == 0){
        document.getElementById("semAparelhos").style.display = "table-row";
      } else {
        document.getElementById("semAparelhos").style.display = "none";
      }
    }
  });
}

function editClicked(eleid) {
  var apar = aparelhosCache[eleid.substring(4)];
  dwr.util.setValues(apar);
  viewed = apar.id;
}

function deleteClicked(eleid) {
  var apar = aparelhosCache[eleid.substring(6)];
  dwr.engine.beginBatch();
  RessarcimentoDanos.deleteAparelhoRessarcimento(apar);
  fillTable();
  dwr.engine.endBatch();
}

function writeAparelho() {
  
  var apar = { id:viewed , descricao:null, voltagem:null, bifasico:null, 
               anoFabricacao:null, codigoTempoUso:null, statusReparoAntes:null, statusReparoDepois:null};
  dwr.util.getValues(apar);
  
  if(apar.descricao == ''){
    alert('Informe a descrição do aparelho.');
    return false;
  } else if(apar.voltagem == ''){
    alert('Informe a voltagem do aparelho.');
    return false;
  } else if(apar.bifasico == ''){
    alert('Informe se aparelho é bifásico.');
    return false;
  } else if(apar.anoFabricacao == ''){
    alert('Informe o ano de fabricação do aparelho.');
    return false;
  } else if(apar.anoFabricacao < 1900){
    alert('Informe um ano de fabricação válido do aparelho.');
    return false;
  } else if(apar.codigoTempoUso == ''){
    alert('Informe o tempo de uso do aparelho.');
    return false;
  } else if(apar.statusReparoAntes == ''){
    alert('Informe se o aparelho foi reparado antes.');
    return false;
  } else if(apar.statusReparoDepois == ''){
    alert('Informe se o aparelho foi reparado depois.');
    return false;
  }
  
  dwr.engine.beginBatch();
  apar.descricao = apar.descricao.toUpperCase();
  RessarcimentoDanos.setAparelhoRessarcimento(apar);
  fillTable();
  clearAparelho();
  dwr.engine.endBatch();
}

function clearAparelho() {
  viewed = -1;
  dwr.util.setValues({ id:-1 , descricao:null, voltagem:null, bifasico:'NAO', 
               anoFabricacao:null, codigoTempoUso:'0', statusReparoAntes:'NAO SEI', statusReparoDepois:'NAO SEI'});
}

function showDivAparelho(){
    document.getElementById('divAparelho').style.display = '';
    document.getElementById('divDanos').style.display = 'none';
}
function showDivDanos(){
    document.getElementById('divAparelho').style.display = 'none';
    document.getElementById('divDanos').style.display = '';
}
function showDivDanosAparelho(){
    document.getElementById('divAparelho').style.display = '';
    document.getElementById('divDanos').style.display = '';
}

//
// Funcoes para declaracao de danos
//
function fillTableDanos() {
  RessarcimentoDanos.getDanosRessarcimento(function(danos) {
    
    // Delete all the rows except for the "pattern" row
    dwr.util.removeAllRows("danosbody", { filter:function(tr) {
    
      return (tr.id != "patternDanos") && (tr.id != "semDanos");
    }});
    
    var dno, id;
    danos.sort(function(p1, p2) { return (p1.codigo >p2.codigo); } );
    for (var i = 0; i < danos.length; i++) {
      dno = danos[i];
      id = i + 1;
      dwr.util.cloneNode("patternDanos", { idSuffix:id });
      dwr.util.setValue("descricaoDanoTbl"+id, dno.descDanos);
      
      // Workaround para IE6 que nao suporta o valor table-row
      if(isIE60){
            document.getElementById("patternDanos"+id).style.display = "";
      } else {
            document.getElementById("patternDanos"+id).style.display = "table-row";
      }
      
      danosCache[id] = dno;
    }
    
    // Workaround para IE6 que nao suporta o valor table-row
    if(isIE60){
      if(danos.length == 0){
        document.getElementById("semDanos").style.display = "";
      } else {
        document.getElementById("semDanos").style.display = "none";
      }
    } else {
      if(danos.length == 0){
        document.getElementById("semDanos").style.display = "table-row";
      } else {
        document.getElementById("semDanos").style.display = "none";
      }
    }
  });
}

function editClickedDanos(eleid) {
  var dano = danosCache[eleid.substring(4)];
  dwr.util.setValues(dano);
  viewedDanos = dano.codigo;
}

function deleteClickedDanos(eleid) {
  var dano = danosCache[eleid.substring(6)];
  dwr.engine.beginBatch();
  RessarcimentoDanos.deleteDanoRessarcimento(dano);
  fillTableDanos();
  dwr.engine.endBatch();
}


function writeDanos() {
  var dano = { codigo:viewedDanos , descDanos:null };
  dwr.util.getValues(dano);
  
  if(dano.descDanos == ''){
    alert('Informe a descrição do dano.');
    return false;
  } 
  
  dwr.engine.beginBatch();
  dano.descDanos = dano.descDanos.toUpperCase();
  RessarcimentoDanos.setDanoRessarcimento(dano);
  fillTableDanos();
  clearDanos();
  dwr.engine.endBatch();
}

function clearDanos() {
  viewedDanos = -1;
  dwr.util.setValues({ codigo:-1 , descDanos:null});
  document.getElementById("descDanos").focus();
}

