function GetNumeros(stEntrada)
{
    stSaida = "";
    for (inPosicao = 0 ; inPosicao < stEntrada.length ; inPosicao++)
    {
        if ((stEntrada.charCodeAt(inPosicao) >= "0".charCodeAt(0)) && (stEntrada.charCodeAt(inPosicao) <= "9".charCodeAt(0)))
            stSaida = stSaida.concat(stEntrada.charAt(inPosicao));
    }
    return stSaida;
}

function FormataCEP(obInputText)
{
    stTexto   = obInputText.value;
    stNumeros = GetNumeros(stTexto);
    stText = "";
    for (inPosicao = 0 ; ((inPosicao < stNumeros.length) && (inPosicao < 8)) ; inPosicao++)
    {
        if (inPosicao == 5)
            stText = stText.concat("-");
        stText = stText.concat(stNumeros.charAt(inPosicao));
    }
    obInputText.value = stText;
}

function FormataCPF(obInputText)
{
    stTexto   = obInputText.value;
    stNumeros = GetNumeros(stTexto);
    stText = "";
    for (inPosicao = 0 ; ((inPosicao < stNumeros.length) && (inPosicao < 11)) ; inPosicao++)
    {
        if ((inPosicao == 3) || (inPosicao == 6))
            stText = stText.concat(".");
        else if (inPosicao == 9)
            stText = stText.concat("-");
        stText = stText.concat(stNumeros.charAt(inPosicao));
    }
    obInputText.value = stText;
}

function FormataTelefone0800(obInputText)
{
    stTexto   = obInputText.value;
    stNumeros = GetNumeros(stTexto);
    stText = "";
    for (inPosicao = 0 ; ((inPosicao < stNumeros.length) && (inPosicao < 11)) ; inPosicao++)
    {
        if (inPosicao == 4)
            stText = stText.concat("-");
        stText = stText.concat(stNumeros.charAt(inPosicao));
    }
    obInputText.value = stText;
}

function FormataTelefone(obInputText)
{
    stTexto   = obInputText.value;
    stNumeros = "";
    for (inPosicao = 0 ; inPosicao < stTexto.length ; inPosicao++)
    {
        if ((stTexto.charCodeAt(inPosicao) >= "0".charCodeAt(0)) && (stTexto.charCodeAt(inPosicao) <= "9".charCodeAt(0)))
            stNumeros = stNumeros.concat(stTexto.charAt(inPosicao));
    }
    while (stNumeros.charCodeAt(0) == "0".charCodeAt(0))
        stNumeros = stNumeros.substr(1);
    stText = "";
    for (inPosicao = 0 ; ((inPosicao < stNumeros.length) && (inPosicao < 10)) ; inPosicao++)
    {
        if (inPosicao == 0)
            stText = stText.concat("(");
        if (inPosicao == 2)
            stText = stText.concat(") ");
        if ((inPosicao == 6))
            stText = stText.concat("-");
        stText = stText.concat(stNumeros.charAt(inPosicao));
    }
    obInputText.value = stText;
}

var obHTTP = null;

function GetXmlHttpObject()
{
    var obXML_HTTP_AUX = null;
    var aobIDs = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
    for(var i=0; !obXML_HTTP_AUX && i<aobIDs.length; i++) {
        try {
            obXML_HTTP_AUX = new ActiveXObject(aobIDs[i]);
        } catch(ex) {
            obXML_HTTP_AUX = null;
        }
    }
    if (!obXML_HTTP_AUX && typeof XMLHttpRequest!='undefined')
        obXML_HTTP_AUX = new XMLHttpRequest();
    return obXML_HTTP_AUX;
}

var inCountImagemContato = 1;

function ReloadImagem()
{
    document.getElementById("img-imagem-confirmacao").src = "imagem-confirmacao.php?" + inCountImagemContato++;
}

function LimitaDados(obText, inMaximoCaracteresPermitidos, obAvisaQuantidadeRestantes)
{
    if (obText.value.length > inMaximoCaracteresPermitidos)
        obText.value = obText.value.substr(0, inMaximoCaracteresPermitidos);
    if (obAvisaQuantidadeRestantes != null)
        obAvisaQuantidadeRestantes.innerHTML = (inMaximoCaracteresPermitidos - obText.value.length);
}

function Oculta(stIdElemento, inTempo) // Tempo em Millisegundos
{
    setTimeout("EsconteObjeto('" + stIdElemento + "')", inTempo);
}

function EsconteObjeto(stIdElemento)
{
    document.getElementById(stIdElemento).style.display = 'none';
}

var obXML_HTTP, obLogradouro, obBairro, obCidade, obEstado;

function BuscarEndereco(obCEP, obLogradouroAux, obBairroAux, obCidadeAux, obEstadoAux)
{
    FormataCEP(obCEP);
    stCEP = obCEP.value;
    if (stCEP.length == 0)
        return;
    stCEP = GetNumeros(stCEP);
    if (stCEP.length != 8)
    {
        alert ('CEP [' + stCEP + '] inválido');
        return;
    }
    var stURL = "busca_cep.php?cep=" + stCEP + "&formato=xml";
    obXML_HTTP = GetXmlHttpObject();
    if (obXML_HTTP != null)
    {
        if (document.getElementById("aviso_cep") != null)
        {
            document.getElementById("aviso_cep").innerHTML = "&nbsp;Buscando Endereço...";
        }
        obLogradouro       = obLogradouroAux;
        obBairro           = obBairroAux;
        obCidade           = obCidadeAux;
        obEstado           = obEstadoAux;
        obXML_HTTP.onreadystatechange = BuscarEndereco_state_Change;
        obXML_HTTP.open("GET", stURL, true);
        obXML_HTTP.send(null);
    }
    else
    {
        alert("Esse Browser não possui suporte para XMLHTTP.");
    }
}

function BuscarEndereco_state_Change()
{
    try
    {
        if ((obXML_HTTP.readyState == 4) || (obXML_HTTP.readyState == "complete"))
        {// 4 = "loaded"
            obXML_DOC                  = obXML_HTTP.responseXML;
            inResultado                = obXML_DOC.getElementsByTagName("resultado")[0].childNodes[0].nodeValue;
            stLogradouro       = obLogradouro.value;
            stBairro           = obBairro.value;
            stCidade           = obCidade.value;
            stEstado           = obEstado.value;
            if (inResultado == 1)
            {
                stLogradouro               = obXML_DOC.getElementsByTagName("tipo_logradouro")[0].childNodes[0].nodeValue + ' ' + obXML_DOC.getElementsByTagName("logradouro")[0].childNodes[0].nodeValue;
                stBairro                   = obXML_DOC.getElementsByTagName("bairro")[0].childNodes[0].nodeValue;
                stCidade                   = obXML_DOC.getElementsByTagName("cidade")[0].childNodes[0].nodeValue;
                stEstado                   = obXML_DOC.getElementsByTagName("uf")[0].childNodes[0].nodeValue;
            }
            else if (inResultado == 2)
            {
                stCidade = obXML_DOC.getElementsByTagName("cidade")[0].childNodes[0].nodeValue;
                stEstado = obXML_DOC.getElementsByTagName("uf")[0].childNodes[0].nodeValue;
            }
            else
            {
                alert(obXML_DOC.getElementsByTagName("resultado_txt")[0].childNodes[0].nodeValue + '\nErro Número ' + inResultado);
            }

            aobElementos = document.getElementsByName("formulario-cadastro-endereco");
            for (inCount = 0 ; inCount < aobElementos.length ; inCount++)
            {
                aobElementos[inCount].style.display = "inline-block";
            }

            obLogradouro.value   = stLogradouro;
            obBairro.value       = stBairro;
            obCidade.value       = stCidade;
            for (inCount = 0 ; inCount < obEstado.options.length ; inCount++)
            {
                if (obEstado.options[inCount].value == stEstado)
                    obEstado.selectedIndex = inCount;
            }

            if (document.getElementById("aviso_cep") != null)
            {
                document.getElementById("aviso_cep").innerHTML = "&nbsp;Somente números";
            }
        }
    } catch (e)
    {
        alert (e);
        if (document.getElementById("aviso_cep") != null)
        {
            document.getElementById("aviso_cep").innerHTML = "&nbsp;Somente números";
        }
    }
}

function SalvaPedido(){
    var stURL = "salva_pedido.php";
    obXML_HTTP_Aux = GetXmlHttpObject();
    if (obXML_HTTP_Aux != null)
    {
        obXML_HTTP_Aux.open("GET", stURL, true);
        obXML_HTTP_Aux.send(null);
    }
}

function DisplayTipoPagamento(){
    try
    {
        var obSelect = document.getElementById('aviso_tipo');
        document.getElementById('div-aviso-1').style.display = 'none';
        document.getElementById('div-aviso-2').style.display = 'none';
        document.getElementById('div-aviso-3').style.display = 'none';
        document.getElementById('div-aviso-4').style.display = 'none';
        document.getElementById('div-aviso-' + obSelect[obSelect.selectedIndex].value).style.display = 'block';
    } catch (e)
    {
        alert (e);
    }
}
