try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}


function texto(este)
{
	if (este.title == este.value)
		este.value = '';
}

function texto_blur(este)
{
	return;
	if (este.value == '')
		este.value = este.title;
}

function index(url)
{
  xmlhttp.open("GET", url, true);
  xmlhttp.onreadystatechange=function() {
        document.getElementById("div_index").style.display = 'block';
      document.getElementById("div_index").innerHTML = xmlhttp.responseText;
  }
  xmlhttp.send(null)
}

function formataCep(){
  query(document.dados.cep.value);
}

function envia(){



  url = "fontes/envia.php";
  xmlhttp.open("GET", url, true);
  xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4){

        win = window.open(xmlhttp.responseText,'emailWindow');
       if (win && win.open &&!win.closed) win.close();


      }
  }
  xmlhttp.send(null);
}

function setZero(campo){
  if (campo.value = '')
  {
    campo.value = '0';
  }
}

function query(cep){
  document.dados.consultando.value = "...carregando";
  var numero;
  var complemento;
	var tabela;
  numero = document.dados.numero.value;
  complemento = document.dados.complemento.value;
  tabela = document.dados.tabela.value;
  url = "cep.php?cep="+cep+"&numero="+numero+"&complemento="+complemento;
  xmlhttp.open("GET", url, true);
  xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4){
        document.getElementById("div_cep").style.display = 'block';
      document.getElementById("div_cep").innerHTML = xmlhttp.responseText;
      }
  }
  xmlhttp.send(null)
  document.dados.consultando.value = "consultar";

}

function ccep(cep, numero, complemento){
  url = "fontes/cep.php?cep="+cep+"&numero="+numero+"&complemento="+complemento;
  xmlhttp.open("GET", url, true);
  xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4){
        document.getElementById("div_cep").style.display = 'block';
      document.getElementById("div_cep").innerHTML = xmlhttp.responseText;
      }
  }
  xmlhttp.send(null)

}

function setFocus(aForm){
  if( aForm.elements[0]!=null) {
   var i;
   var max = aForm.length;
   for( i = 0; i < max; i++ ) {
    if( aForm.elements[ i ].type != "hidden"
     && !aForm.elements[ i ].disabled
     && !aForm.elements[ i ].readOnly ) {
      aForm.elements[ i ].focus();
      break;
     }
   }
  }
 }




function validaData(campo)
{
  dia = (campo.value.substring(0,2));
  mes = (campo.value.substring(3,5));
  ano = (campo.value.substring(6,10));

  situacao = "";
  // verifica o dia valido para cada mes
  if ((dia < 01)||(dia < 01 || dia > 30) && (  mes == 04 || mes == 06 || mes == 09 || mes == 11 ) || dia > 31) {
      situacao = "falsa";
  }

  // verifica se o mes e valido
  if (mes < 01 || mes > 12 ) {
      situacao = "falsa";
  }

  // verifica se e ano bissexto
  if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) {
      situacao = "falsa";
  }

  if (campo.value == "") {
      return true;
  }

  if (situacao == "falsa") {
      alert("Data inválida!");
      campo.value = '';
      campo.focus();
      return false;
  }
}


function mascara(campo,mascara,i){
//campo: local do objeto --> this;
//mascara: máscara;
//i: caractere inicial (posição zero da máscara;
var iCount = campo.value.length;

//Digite os símbolos especiais que permanecerão em seus locais corretos:
var simbolosEspeciais = "()/-.";

    if (iCount < mascara.length)
    {
         //Verificando se foi passado o parâmetro inicial "i":
        if (iCount == 1 && i != "")
        {
            campo.value = i+campo.value;
        }
        else
        {
            if (simbolosEspeciais.indexOf(mascara.substr(iCount,1)) != -1)
            {
                campo.value = campo.value+mascara.substr(iCount,1);
                if (mascara.substr(iCount+1,1) == " ")
                {
                  campo.value = campo.value+" ";
                }
            }
            if (mascara.substr(iCount,1) == " ")
            {
                campo.value = campo.value+" ";
            }
          }

    }
    else
    {
        campo.value = campo.value.substr(0,mascara.length);
    }
}




function validaNumero(string) {
    if (!string) return false;
    if (string.name.substr(0,4) == 'data')
      tipo = 'd';
    else
      tipo = 'n';
    if (tipo == 'n')
    {
      var Chars = "0123456789-.";
    }
    if (tipo == 'd')
    {
      var Chars = "0123456789/";
    }
    for (var i = 0; i < Chars.length; i++)
    {
      var s =  string.value.substr(string.value.length-1,1);
      if (Chars.indexOf(s) == -1)
        string.value = string.value.substr(0, string.value.length-1);
    }
    return true;
}



function OnChange(dropdown)
{
    var myindex  = dropdown.selectedIndex
    var SelValue = dropdown.options[myindex].value
  alert(SelValue);
    var baseURL  = 'teste.php'
  top.location.hash = '#quadro';
    top.location.href = baseURL;

    return true;
}



function changeSelect(valor, valor2)
{
  alert(valor);
  alert(valor2);
  return true;
}


function apagatel(valor)
{
  var res = confirm('Confirma Exclusão do Telefone?');
  if (res)
    window.location.href = "apagafone.php?tabela=gw_telefone&codigo=" + valor;
  return res;
}


function exibe(id)
{
  document.getElementById(id).style.display = "inline";
  document.getElementById('dados').style.color = "red";
}
function oculta(id)
{
  document.getElementById(id).style.display = "none";
  document.getElementById('pesquisa').style.color = "green";
}



function calculaJuros(){
  var valor = parseFloat(document.dados.valor.value);
  var data_vencimento = document.dados.data_vencimento.value;
  var juros = parseFloat(document.dados.valor_multa.value);
  var juros_ao_dia = parseFloat(document.dados.juros_ao_dia.value);

if (valor.value == 'Nan')
    valor.value = 0;
if (juros_ao_dia.value == 'Nan')
    juros_ao_dia.value = 0;
if (juros.value == 'Nan')
    juros.value = 0;

  var data_baixa = document.dados.data_baixa.value;
  calcula(data_vencimento, juros, juros_ao_dia, data_baixa, valor);
}



function calcula(data_vencimento, juros, juros_ao_dia, data_baixa, valor)
{
  url = "date_diff.php?datai="+data_vencimento+"&dataf="+data_baixa;
  xmlhttp.open("GET", url, true);
  xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4){
        var dias = parseInt(xmlhttp.responseText);
        var valorcomjuros;
        if (dias <= 0)
          valorcomjuros = valor;
        else
          valorcomjuros = (valor + ((valor/100)*juros));
        for (var j=0; j<dias; j++)
          valorcomjuros +=  (valorcomjuros/100) * juros_ao_dia
        document.getElementById("valor_baixa").value = valorcomjuros;
        document.getElementById("calcular").value = dias + ' dias';
      }
  }
  xmlhttp.send(null)
}


function zero(campo)
{
  if (campo.value == '') campo.value = 0;;
}


/*
        echo 'changeSelect(';
        echo  'this.form.'. $tabela .'.options[this.form.'. $tabela .'.selectedIndex].value, ';
        echo  'this.form.'. $tabela .'.name';
        echo ');">';
*/


function ajax_select(valor, tabela, campo)
{
  campo.value = "...carregando";
  var url = "monta_select.php?tabela="+tabela+"&ordem=nome&codigo=0&campo="+campo+"&alternativo="+campo+"&filtro="+valor;
xmlhttp.open("GET", url, true);
	var div_id;
	div_id = "div_" + campo
  xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4){
        document.getElementById(div_id).style.display = 'block';
      document.getElementById(div_id).innerHTML = xmlhttp.responseText;
      }
  }
  xmlhttp.send(null)
  campo.value = "";
}

function ajax_select_rel(valor, tabela, cp, campo, codigo)
{
//	alert(valor + ' - ' + tabela + ' - ' + campo+ ' - ' + cp + ' - ' + codigo);
  campo.value = "...carregando";
  var url = "monta_select_rel.php?tabela="+tabela+"&ordem=2&codigo=0&campo="+cp+"&alternativo="+campo+"&filtro="+valor;
  xmlhttp.open("GET", url, true);
  xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4){
        document.getElementById('div_select_rel_' + cp).style.display = 'block';
      document.getElementById('div_select_rel_' + cp).innerHTML = xmlhttp.responseText;
      }
  }
  xmlhttp.send(null);
  campo.value = "";
}

function readCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return "";
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length;
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function edit(tabela, codigo)
{
var x = readCookie('conectado_publico');
	if (x == '1')
		window.location.href="fontes/conteudo.php?tabela="+tabela+"&codigo=0&pessoacodigo="+codigo;
	else
		window.location.href="conteudo.php?tabela="+tabela+"&codigo="+codigo+"&pessoacodigo="+codigo;
}


function change_select(valor)
{
	campo = valor.id.substring(7);
	document.getElementById(campo).value = valor.value;
}


function mostra_div_pesquisa(valor)
{
		var vl = document.getElementById("div_pesquisa_"+valor).style.display;
	if (vl == 'none')
	  document.getElementById("div_pesquisa_"+valor).style.display = "inline";
	else
	  document.getElementById("div_pesquisa_"+valor).style.display = "none";
}

function mostra_div_aba_pesquisa(valor,codigo)
{

	if (document.getElementById(valor).style.display == 'none')
	  document.getElementById(valor).style.display = "inline";
	else
	  document.getElementById(valor).style.display = "none";


}


function vertical() {

   var navItems = document.getElementById("nav").getElementsByTagName("li");

   for (var i=0; i< navItems.length; i++) {
      if(navItems[i].className == "submenu") {
         navItems[i].onmouseover=function() {this.getElementsByTagName('ul')[0].style.display="block";this.style.backgroundColor = "#f9f9f9";}
         navItems[i].onmouseout=function() {this.getElementsByTagName('ul')[0].style.display="none";this.style.backgroundColor = "#FFFFFF";}
      }
   }

}

