

function troca_virg_por_ponto(S){
   // Deixa so' os digitos no numero
   var Digitos = ",";
   var temp = "";
   var digito = "";
   for (var i=0; i<S.length; i++){
      digito = S.charAt(i);
      if (Digitos.indexOf(digito)>=0) {
         temp=temp+'.';
         }
         else temp=temp+digito;
   }
   return temp
}


function em_cima(objeto,nome){

  objeto.src = nome + '_on.jpg';

  return true;
}

function saiu(objeto,nome){

  objeto.src = nome + '_off.jpg';

  return true;
}

function alterar(){

  if (confirm('Confirma a alteração?')) {
    document.frm.acao.value='ALTERAR';
    document.frm.submit();
  }
}

function excluir(){

  if (confirm('Confirma a deleção?')) {
    document.frm.acao.value='EXCLUIR';
    document.frm.submit();
  }

}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function abre_janela(pagina) {
  open(pagina,'order','toolbar=no,menubar=no,resizable=yes,status=no,location=no,directories=no,scrollbars=yes,height=390,width=380');
}

function atualiza_disciplina(cod) {

  top.parent.opener.document.frm.dscp_cod.value = cod;
  top.parent.opener.document.frm.enviado.value = '';
  top.parent.opener.document.frm.adicionar.value = '';
  top.parent.opener.document.frm.submit();
  window.close();

}


function exclui_orcamento_material(indice) {

  document.frm.pAcao.value='EXCLUIR_MATERIAL';
  document.frm.pAcao_Valor.value=indice;
  document.frm.pEnviado.value = document.frm.pEnviado.value - 1;
  document.frm.submit();

}

function adiciona_orcamento_material() {

  document.frm.pEnviado.value = document.frm.pEnviado.value - 1;
  document.frm.pNroItens.value = parseInt(document.frm.pNroItens.value) + 1;
  document.frm.submit();

}

function atualiza_subvencao(dt_ini_sub, dt_fim_sub, eens_cod, crso_cod, edfo_cod) {

  top.parent.opener.document.frm.dt_ini_sub.value = dt_ini_sub;
  top.parent.opener.document.frm.dt_fim_sub.value = dt_fim_sub;
  top.parent.opener.document.frm.eens_cod.value = eens_cod;
  top.parent.opener.document.frm.crso_cod.value = crso_cod;
  top.parent.opener.document.frm.edfo_cod.value = edfo_cod;
  top.parent.opener.document.frm.enviado.value = '';
  top.parent.opener.document.frm.submit();
  window.close();

}

function abre_janela_gg(pagina) {
  var nome = ''+RandomNumber();
  nome = nome.charAt(3) + nome.charAt(4);
  open(pagina,nome,'toolbar=no,menubar=no,resizable=yes,status=no,location=no,directories=no,scrollbars=yes,height=460,width=620');
}

function abre_janela_gg_exp(pagina) {
  var nome = ''+RandomNumber();
  nome = nome.charAt(3) + nome.charAt(4);
  open(pagina,nome,'toolbar=no,menubar=yes,resizable=yes,status=no,location=no,directories=no,scrollbars=yes,height=460,width=620');
}

function navega(pagina) {
  
  document.frm.pPagina.value=pagina;
  document.frm.submit();

}

function abre_janela_pp(pagina) {
  var nome = ''+RandomNumber();
  nome = nome.charAt(3) + nome.charAt(4) + 'pp';
  open(pagina,nome,'toolbar=no,menubar=no,resizable=yes,status=no,location=no,directories=no,scrollbars=yes,height=360,width=480');
}

  
function excluir_registro(pergunta) {
  
  if (confirm(pergunta)) {
     document.frm.pAcao.value='EXCLUIR';
     document.frm.submit();
  }
  return false;

}

function alterar_registro(pergunta) {
  
  if (confirm(pergunta)) {
     document.frm.pAcao.value='ALTERAR';
     document.frm.submit();
  }
  return false;

}

function envia_questiona(pergunta) {
  
  if (confirm(pergunta)) {
     document.frm.submit();
  }
  return false;

}

function RandomNumber() {
  today = new Date();
  num= Math.abs(Math.sin(today.getTime()));
  return num;  
}

function soma_nota_compra_estoque() {

  var total
  var tmpValor

  total = 0.0;

  tmpValor = parseFloat(document.frm.pIpi.value);
  if (!isNaN(tmpValor)) {
     total = total + parseFloat(document.frm.pIpi.value);
  }

  tmpValor = parseFloat(document.frm.pEmbalagem.value);
  if (!isNaN(tmpValor)) {
     total = total + parseFloat(document.frm.pEmbalagem.value);
  }

  tmpValor = parseFloat(document.frm.pIcm.value);
  if (!isNaN(tmpValor)) {
     total = total + parseFloat(document.frm.pIcm.value);
  }

  tmpValor = parseFloat(document.frm.pFrete.value);
  if (!isNaN(tmpValor)) {
     total = total + parseFloat(document.frm.pFrete.value);
  }

  //document.frm.pTotal.value = total;

}

function atualiza_pai() {

  opener.location.reload();

}

function atualiza_material_seleciona(form_name, input_id, input_nome, input_preco) {

  var campo_id;
  var campo_nome;
  var campo_preco;
  
  campo_id = eval('opener.document' + '.' + form_name + '.' + input_id);
  campo_nome = eval('opener.document' + '.' + form_name + '.' + input_nome);
  campo_preco = eval('opener.document' + '.' + form_name + '.' + input_preco);

  campo_id.value = document.frm.pMaterial_id.value;
  campo_nome.value = document.frm.pMaterial_nome.value;
  campo_preco.value = document.frm.pPreco.value;

}

function soma_orcamento() {

  var total;
  var tmpValor;
  var tmpQtde;
  var itens;
  var i;
  var tmpStr;

  total = 0.0;

  itens = parseInt(document.frm.pNroItens.value);

  //Calcula o total de material
  for (i=1;i<=itens;i++) {
    tmpQtde = parseFloat(troca_virg_por_ponto(eval('document.frm.pQuantidade'+i+'.value')));
    tmpValor = parseFloat(troca_virg_por_ponto(eval('document.frm.pPreco'+i+'.value')));
    if ((!isNaN(tmpValor))&&(!isNaN(tmpQtde))) {
       total = total + (tmpQtde * tmpValor);
    }
  }
  document.frm.pTotal_material.value = total;

  //Calcula o total de material mais maodeobra
  tmpValor = parseFloat(troca_virg_por_ponto(document.frm.pMao_de_obra.value));
  if (!isNaN(tmpValor)) {
     total = total + (tmpValor);
  }
  document.frm.pTotal_obra_si.value = total;
  
  //Calcula o ipi do total de material e mao de obra mais impostos
  tmpValor = parseFloat(document.frm.pBdi.value);
  if (!isNaN(tmpValor)) {     
     total = total + (total*(tmpValor/100));
  }

  //Calcula o total de material e mao de obra mais impostos
  tmpValor = parseFloat(troca_virg_por_ponto(document.frm.pIcm.value));
  if (!isNaN(tmpValor)) {
     total = total + (tmpValor);
  }
  tmpValor = parseFloat(troca_virg_por_ponto(document.frm.pIpi.value));
  if (!isNaN(tmpValor)) {
     total = total + (tmpValor);
  }
  document.frm.pTotal.value = total;
  

}


