function resultadoCancerMamaPrec(n){
   n = parseInt(n);
   if (n == 0) n = 1;
   if (n < 10) n = "0"+n;
   document.getElementById("resultado").innerHTML = "<a class='fechar' href='javascript:closeResultado();'></a><div style='height:22px;'><a id='back' class='voltar' href='javascript:menosGraf();'></a><a id='next' class='proxima' href='javascript:maisGraf();'></a></div><img class="+n+" id='imGraf' src='../images/graficos/"+n+".png' />";
   document.getElementById("resultado").style.display = "block";
   n += 1;
}

function closeResultado(){
   document.getElementById("resultado").innerHTML = "";
   document.getElementById("resultado").style.display = "none";

}

function maisGraf(){
   n = document.getElementById('imGraf').className;
   n = parseInt(n) + parseInt(1);
   if(n > 10){
      n = 0;
   }
   resultadoCancerMamaPrec(n);
}

function menosGraf(){
   n = parseInt(n) - parseInt(1);
   if(n < 0){
      n = 10;
   }
   resultadoCancerMamaPrec(n);
}

function controleNumber(nome, num) {
   document.getElementById(nome).value = num;
   
}

function controleResultado(nome, cont, fim) {
   if (fim == null && document.getElementById(nome+'Numero') != null) {
      cont  = parseInt(document.getElementById(nome+'Numero').value);
   }
   if (fim >= 1) {
      for (var i=1; i<=fim; i++) {
         if (i==cont){
            ElementFadeInOut(nome+i, 1);
            if (document.getElementById('idResultadoPaginacao') != null)
               document.getElementById('idResultadoPaginacao').getElementsByTagName('a')[i].className = 'texto_verde texto_negrito texto_13';
         } else {
            ElementShowHiddenSimple(nome+i, 0);
            if (document.getElementById('idResultadoPaginacao') != null)
               document.getElementById('idResultadoPaginacao').getElementsByTagName('a')[i].className = '';
         }
      }      
   }
   
}

function controleElement(nome, acrescimo, cont) {
   if (cont == null) cont = parseInt(acrescimo) + parseInt(document.getElementById(nome+'Controle').value);
   num  = parseInt(document.getElementById(nome+'Numero').value);
   if (num) {
      if (acrescimo > 0) {
         if (cont > num) cont = 1;
      } else {
         if (cont == 0) cont = num;
      }
      document.getElementById(nome+'Controle').value = cont;
      if (document.getElementById(nome+'ButtonAuto') != null) {
         document.getElementById(nome+'ButtonAuto').value = 'Stop';
      }
      for (var i = 1; i<=num; i++) {
         if (i == cont) {
            ElementFadeInOut(nome+i, 1);
            ElementClassChange(nome+'Button'+i, 'botaoLinkSelected');
            if (document.getElementById('idResultadoPaginacao') != null)
               document.getElementById('idResultadoPaginacao').getElementsByTagName('a')[i].className = 'texto_verde texto_negrito texto_13';
         } else {
            ElementShowHiddenSimple(nome+i, 0);
            ElementClassChange(nome+'Button'+i, 'botaoLink');
            if (document.getElementById('idResultadoPaginacao') != null)
               document.getElementById('idResultadoPaginacao').getElementsByTagName('a')[i].className = '';
         }
      }
   }
}

function controleElementAuto(nome, acrescimo) {
   var loopingTime = 3000;
   var checkStopEnd = false;
   var stopEnd = (document.getElementById(nome+'ButtonAuto')!=null&&document.getElementById(nome+'ButtonAuto').value=='Auto'?1:0);
   if (isNaN(parseInt(document.getElementById(nome+'Controle').value)) == true) {
      document.getElementById(nome+'Controle').value = 0;
   }
   if (stopEnd == '1') {
      cont = parseInt(document.getElementById(nome+'Controle').value) + parseInt(acrescimo);
      num  = parseInt(document.getElementById(nome+'Numero').value);
      if (num) {
         if (stopEnd == 1 && cont > num) {
            checkStopEnd = true;
            document.getElementById(nome+'ButtonAuto').value = 'Stop';
         }
         if (acrescimo > 0) if (cont > num) cont = 1;
         else if (cont == 0) cont = num;
         document.getElementById(nome+'Controle').value = cont;
         for (var i = 1; i<=num; i++) {
            if (i == cont) {
               ElementFadeInOut(nome+i, 1);
               ElementClassChange(nome+'Button'+i, 'botaoLinkSelected');
            } else {
               ElementShowHiddenSimple(nome+i, 0);
               ElementClassChange(nome+'Button'+i, 'botaoLink');
            }
         }
      }
      ElementInnerValue(nome+'ButtonAuto', 'Auto');
      setTimeout("controleElementAuto('"+nome+"', '"+acrescimo+"')", loopingTime);
   } else {
      ElementInnerValue(nome+'ButtonAuto', 'Stop');
   }
   
   //parar
   //ElementClassChange(nome+'ButtonAuto', 'botaoLink');
   //ElementClassChange(nome+'ButtonStop', 'botaoLinkSelected');
}

//controlar botao
function controleElementButton(nome, d) {
   
   if (d != null && nome != null) {
      if (d.value == 'Auto') {
         d.value = 'Stop'; 
      } else {
         d.value = 'Auto'; 
         controleElementAuto(nome, '1');
      }
   }
}

//checar checkboxs
function checkboxChildCheck(id, option) {
   var validationTema = false;
   var cont = 1;
   while (validationTema != true) {
      if (document.getElementById(id+cont)) {
         if (option == true) {
            document.getElementById(id+cont).checked = true;
         } else {
            document.getElementById(id+cont).checked = false;
         }
      } else {
         validationTema = true;
      }
      cont++;
   }
}

function checkboxChildCheck2(id, option) {
   if (document.getElementById(id) != null) {
      for (var i=0; i<document.getElementById(id).getElementsByTagName('input').length; i++) {
         if (document.getElementById(id).getElementsByTagName('input')[i] != null) {
            if (option == true) {
               document.getElementById(id).getElementsByTagName('input')[i].checked = true;
            } else {
               document.getElementById(id).getElementsByTagName('input')[i].checked = false;
            }
         }
      }
   }
}

//carregar definicao
function loadDefinition(orig, idDest, link) {
   //altera posicao de destino para origem
   changePosition(orig, idDest);  
   ElementInner(idDest, '');
   //carrega link
   refreshPage(link, idDest);
}

//busca por localizacao
function findAddress() {
   var checkCep = false;
   if (document.getElementById('idCepOrigem') != null && document.getElementById('idPais') != null) {
      if (document.getElementById('idPais').value == "1") {
         var cep = document.getElementById('idCepOrigem').value;
         if (cep.length == 9) {
            var doc = document.formCorreio;
            cep = cep.replace('-', '');
            window.open('../geral/validacao_cep.php?cep='+cep, 'CORREIO', 'toolbar=0,menubar=0,resizable=0,status=0,scrollbars=0,width=1,height=1');
            self.focus();
            checkCep = true;
         }
      } else {
         alert("Somente dentro do Brasil");
         return false;
      }
   }
   if (checkCep == true) {
      //doc.submit();
   } else {
      var cep = document.getElementById('idCepOrigem').value;
      if (cep.length < 9) {
         alert('Campo CEP inválido. Este campo precisa conter 8 números.');
      }
      
   }
}

//busca por localizacao2
function findAddress2() {
   var checkCep = false;
   if (document.getElementById('idCepOrigem') != null && document.getElementById('idPais') != null) {
      if (document.getElementById('idPais').value == "1") {
         var cep = document.getElementById('idCepOrigem').value;
         if (cep.length == 9) {
            insertAddressManual();
            var doc = document.formCorreio;
            window.open('../geral/validacao_cep.php?cep='+cep, 'CORREIO', 'toolbar=0,menubar=0,resizable=0,status=0,scrollbars=0,width=550,height=500');
            checkCep = true;
         }
      } else {
         alert("Somente dentro do Brasil");
         return false;
      }
   }
   if (checkCep == true) {
      //doc.submit();
   } else {
      var cep = document.getElementById('idCepOrigem').value;
      if (cep.length < 9) {
         alert('Campo CEP inválido. Este campo precisa conter 8 números.');
      }
      
   }
}

//zerar checkValidacao
function validationCepChange() {
   finishAddress('','','','','');
}

//para outro pais
function validationCountryChange(op, clear) {
   if (clear == null) clear = true;
   if (op == "1") {
      //brasil
      if (clear == true) finishAddress('','','','','');
      
      ElementShowHidden('idSpanEndereco', 0);
      ElementShowHidden('idSpanNumero', 1);
      ElementShowHidden('idSpanComplemento', 1);
      ElementShowHidden('idSpanBairro', 0);
      ElementShowHidden('idSpanCidade', 0);
      ElementShowHidden('idSpanEstadoNome', 0);
      ElementShowHidden('idSpanNomeEstado', 1);
      ElementShowHidden('idSpanNomeBairro', 1);
      ElementShowHidden('idEnderecoEncontrado', 1);
      ElementShowHidden('idBairroEncontrado', 1);
      ElementShowHidden('idCidadeEncontrado', 1);
      ElementShowHidden('idEstadoEncontrado', 1);
      ElementInner('idEnderecoEncontrado', '---');
      ElementInner('idSpanNomeEndereco', 'Endereço');
      ElementInner('idSpanNomeCep', 'CEP');
      ElementInner('idSpanNomeCidade', 'Cidade');
      ElementInner('idSpanNomeNumero', 'Número');
      ElementInner('idSpanNomeComplemento', 'Complemento');
      ElementInner('idSpanNomeEstado', 'Estado');
      //endereco nao adicionado manualmente
      ElementInnerValue('idEnderecoManual', '0');
      ElementDisable('idButtonCep', 0, 'slow', 'botao');
   } else {
      //demais paises
      ElementShowHidden('idSpanEndereco', 1);
      ElementShowHidden('idSpanNumero', 0);
      ElementShowHidden('idSpanComplemento', 0);
      ElementShowHidden('idSpanBairro', 0);
      ElementShowHidden('idSpanNomeBairro', 0);
      ElementShowHidden('idSpanCidade', 1);
      ElementShowHidden('idSpanEstadoNome', 0);
      ElementShowHidden('idEnderecoEncontrado', 0);
      ElementShowHidden('idBairroEncontrado', 0);
      ElementShowHidden('idSpanNomeBairro', 0);
      ElementShowHidden('idCidadeEncontrado', 0);
      ElementShowHidden('idSpanNomeBairro', 0);
      ElementShowHidden('idEstadoEncontrado', 0);
      ElementShowHidden('idSpanNomeEstado', 0);
      
      ElementInner('idSpanNomeEndereco', 'Address');
      ElementInner('idSpanNomeCep', 'Zip Code');
      ElementInner('idSpanNomeCidade', 'City');
      ElementInner('idSpanNomeNumero', '');
      ElementInner('idSpanNomeComplemento', '');
      ElementInner('idSpanNomeEstado', '');
      ElementInner('idEnderecoEncontrado', '');
      //endereco adicionado manualmente
      ElementInnerValue('idEnderecoManual', '1');
      //esconder opcao - alterar manualmente
      ElementInner('idSpanEnderecoManual', '');
      ElementDisable('idButtonCep', 1, 'slow', 'botaoDisabled');
   }
   document.getElementById('idCheckValidacao').value = '0';
}

//adicionar localizacao para opener
function finishAddress(address, neighborhood, city, state, idState) {
   if (document.getElementById('idCheckValidacao')) {
      var doc = document;
   } else {
      var doc = window.opener.document;
   }
   if (address && neighborhood && city && state) {
      //inserir textos
      if (doc.getElementById('idCheckValidacao') != null) {
         insertAddressManual();
         //endereco
         doc.getElementById('idEnderecoEncontrado').innerHTML = address;
         //bairro
         doc.getElementById('idBairroEncontrado').innerHTML = neighborhood;
         //cidade uf
         doc.getElementById('idCidadeEncontrado').innerHTML = city;
         doc.getElementById('idEstadoEncontrado').innerHTML = state;
         //adicionar valores
         doc.getElementById('idEndereco').value = address;
         doc.getElementById('idBairro').value = neighborhood;
         doc.getElementById('idCidade').value = city;
         doc.getElementById('idEstado').value = idState;
         doc.getElementById('idEstadoNome').value = state;
         doc.getElementById('idCheckValidacao').value = '1';
         //endereco nao adicionado manualmente
         doc.getElementById('idEnderecoManual').value = '0';
         //exibir opcao - alterar manualmente
         //doc.getElementById('idSpanEnderecoManual').style.display = 'block';
         return true;
      } 
   } else {
      //limpar textos
      if (doc.getElementById('idEnderecoEncontrado') != null) {
         //endereco
         doc.getElementById('idEnderecoEncontrado').innerHTML = '';
         //bairro
         doc.getElementById('idBairroEncontrado').innerHTML = '';
         //cidade
         doc.getElementById('idCidadeEncontrado').innerHTML = '';
         //estado
         doc.getElementById('idEstadoEncontrado').innerHTML = '';
         //valores
         doc.getElementById('idEndereco').value = '';
         doc.getElementById('idBairro').value = '';
         doc.getElementById('idCidade').value = '';
         doc.getElementById('idEstado').value = '';
         doc.getElementById('idCheckValidacao').value = '';
         return true;
      }
   }
}

function insertAddressManual() {
   ElementShowHidden('idSpanEndereco', 1);
   ElementShowHidden('idSpanNumero', 1);
   ElementShowHidden('idSpanComplemento', 1);
   ElementShowHidden('idSpanBairro', 1);
   ElementShowHidden('idSpanCidade', 1);
   ElementShowHidden('idSpanEstadoNome', 1);
   ElementShowHidden('idEnderecoEncontrado', 0);
   ElementShowHidden('idBairroEncontrado', 0);
   ElementShowHidden('idCidadeEncontrado', 0);
   ElementShowHidden('idEstadoEncontrado', 0);
   ElementInnerValue('idEnderecoManual', 1);
}

function calcularBlocoMedia(curso, bloco, pg) {
   var mediaFinal       = 0.0;
   var mediaFinalGeral  = 0.0;
   var numProcessado    = 0;
   var num              = 0;
   if (pg == 'adm') {
      var doc  = document.getElementsByTagName('select');
   } else {
      var doc  = document.getElementsByTagName('input');
   }
   var doc2 = document.getElementById('idNota'+curso+'Bloco'+bloco+'Media');
   var doc3 = document.getElementsByTagName('input');
   var doc4 = document.getElementById('idDivNota'+curso+'Bloco'+bloco+'Media');
   var doc5 = document.getElementById('idDivNota'+curso+'BlocoMediaFinal');
   if (doc != null && doc2 != null) {
      num = doc.length;
      numProcessado = 0;
      for (var i=0; i<num; i++) {
         if (doc[i].className == 'nota'+curso+'Bloco'+bloco) {
            if (doc[i].value != 'N/C') {
               numProcessado++;
               valor = parseFloat(ElementConvertNumber(doc[i].value, 2, 'us'));
               if (!isNaN(valor)) {
                  mediaFinal += valor;
               }
            }
         }
      }
      if (!isNaN(mediaFinal) && mediaFinal > 0) {
         mediaFinal = mediaFinal / numProcessado;
         doc2.value = ElementConvertNumber(mediaFinal, 2, 'br');
         doc4.innerHTML = 'Média do bloco : '+ElementConvertNumber(mediaFinal, 2, 'br');
      } else {
         doc2.value = '';
         doc4.innerHTML = 'Média do bloco : N/C';
      }
   }
   
   //calcular media total do bloco
   if (doc3 != null) {
      num = doc3.length;
      numProcessado = 0;
      for (var i=0; i<num; i++) {
         if (doc3[i].className == 'nota'+curso+'BlocoMedia') {
            if (doc3[i].value != 'N/C' && doc3[i].value != '') {
               numProcessado++;
               valor = parseFloat(ElementConvertNumber(doc3[i].value, 2, 'us'));
               if (!isNaN(valor)) {
                  mediaFinalGeral += valor;
               }
            }
         }
      }
      if (!isNaN(mediaFinalGeral)) {
         mediaFinalGeral = mediaFinalGeral / numProcessado;
         doc5.innerHTML = 'Média final : '+ElementConvertNumber(mediaFinalGeral, 2, 'br');
      } else {
         doc5.innerHTML = 'Média final : N/C';
      }
   }
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function exibirEsconderBarra(id, op, id2, link){
   doc = document.getElementById(id).getElementsByTagName('li');
   if (op != null) {
      for (var i=0; i<5; i++) {
         if (doc[i] != null) {
            if (i!=op) {
               doc[i].className = '';
            } else {
               doc[i].className = 'selected';
            }
         }
      }
   }
   if (id2 && link) {
      refreshPage(link, id2);
   }
}

function showDescription(url, content) {
   ElementFullScreen('idFullScreen'); 
   $('#idDivDescription').animate({width:"540px",'height': '70%'}, 1000 );
   if (content) ElementInner('idDivDescription', "<div style=\"width:520px;text-align:right;margin:5px 5px;font-weight:bold;\"><a href=\"javascript:void('0');\"  class=\"bordaArredondada\" onclick=\"javascript:hideDescription();\" style=\"text-decoration:none;color:#FFFFFF;background-color:black;padding:2px 10px; font-size:10px;\">X</a></div><div class=\"bordaArredondada\" style=\"background-color:#CCCCCC;padding:6px 6px;\"><div class=\"bordaArredondada\" id=\"idDivDescription2\" style=\"background-color:#EEEEEE;\">"+content+"</div></div>");
   if (url) refreshPage(url, 'idDivDescription');
}

function hideDescription() {
   $('#idDivDescription').animate({'height': '0px'}, 500 );
   ElementInner('idDivDescription', '');
   ElementSlideUpDown('idDivDescription', '0');
   ElementShowHiddenSimple('idDivDescription', '0');
   ElementFadeInOut('idFullScreen', '0'); 
}
