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

atual=0
function carrega(n){

    //Exibe o texto carregando no div conteúdo
    var conteudo=document.getElementById("conteudo")
    conteudo.innerHTML='<div class="carregando">Carregando, por favor aguarde.</div>'

    //Guarda a página escolhida na variável atual
    atual=n

    //Abre a url
    xmlhttp.open("GET", "backend.php?page="+n,true);

    //Executada quando o navegador obtiver o código
    xmlhttp.onreadystatechange=function() {

        if (xmlhttp.readyState==4){

            //Lê o texto
            var texto=xmlhttp.responseText

            //Desfaz o urlencode
            texto=texto.replace(/\+/g," ")
            texto=unescape(texto)

            //Exibe o texto no div conteúdo
            var conteudo=document.getElementById("conteudo")
            conteudo.innerHTML=texto
        }
    }
    xmlhttp.send(null)
}


function makePostRequest(url, parameters) {
    //Exibe o texto carregando no div conteúdo
    var conteudo=document.getElementById("conteudo")
    conteudo.innerHTML='<div class="carregando">Carregando, por favor aguarde.</div>'
    
    http_request = xmlhttp;
    http_request.onreadystatechange = alertContents;
    http_request.open('POST', url, true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);
}

function alertContents() {
    if (http_request.readyState == 4)
    {
        if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('conteudo').innerHTML = result;            
        }
    }
}

function login() {
    var poststr = "email=" + encodeURI(document.getElementById("email").value) +
                "&senha=" + encodeURI(document.getElementById("senha").value);
    makePostRequest('login.php', poststr);
}

function cadastro(id_form)
{   
    var elementos_form = BuscaElementosForm(id_form);
    //this.linkMenu(posturl);
    makePostRequest('backend.php?page=cadastro&submit=true', elementos_form);
}

function alterar_senha(id_form)
{
    var elementos_form = BuscaElementosForm(id_form);
    makePostRequest('backend.php?page=alterar_senha&onSubmit=true', elementos_form);
}

function livro(obj) {
    var poststr = "livro_nota=" + encodeURI(document.getElementById("livro_nota").value) +
                "&livro_mensagem=" + encodeURI(document.getElementById("livro_mensagem").value);
    makePostRequest('backend.php?page=livro&submit=true', poststr);
}

function livro2(obj) {
    var poststr = "livro_nick=" + encodeURI(document.getElementById("livro_nick").value) +
                "&livro_email=" + encodeURI(document.getElementById("livro_email").value) +
                "&livro_nota=" + encodeURI(document.getElementById("livro_nota").value) +
                "&livro_mensagem=" + encodeURI(document.getElementById("livro_mensagem").value);
    makePostRequest('backend.php?page=livro&submit=true', poststr);
}

function recado(obj, id) {
    var poststr = "remetente=" + encodeURI(document.getElementById("remetente").value) +
                "&destinatario=" + encodeURI(document.getElementById("destinatario").value) +
                "&mensagem=" + encodeURI(document.getElementById("mensagem").value);
    makePostRequest('backend.php?page=recados&cad_id='+id+'&submit=true', poststr);
}

function recado2(obj, id) {
    var poststr = "remetente=" + encodeURI(document.getElementById("remetente").value) +
                "&destinatario=" + encodeURI(document.getElementById("destinatario").value) +
                "&mensagem=" + encodeURI(document.getElementById("mensagem").value);
    makePostRequest('backend.php?page=perfil&cad_id='+id+'&submit=true', poststr);
}

function contato(obj) {
    var poststr = "nome=" + encodeURI(document.getElementById("nome").value) +
                "&email=" + encodeURI(document.getElementById("email").value) +
                "&telefone=" + encodeURI(document.getElementById("telefone").value) +
                "&mensagem=" + encodeURI(document.getElementById("mensagem").value);
    makePostRequest('backend.php?page=fale-conosco&submit=true', poststr);
}

function pesquisa(id_form) {

    var posturl = 'resultado-pesquisa&' +  BuscaElementosForm(id_form);
    //alert(posturl);
    //Salva No Histórico
    this.linkMenu(posturl);
   
}

function delImg() {
    var poststr = "ids=" + encodeURI(document.getElementById("cb").value);
    makePostRequest('backend.php?page=perfil&del=true', poststr);
}

function UploadFull(){
    
    this.linkMenu('perfil');
    
}

function getCity() {
    
    var elemento_dest = document.getElementById('div_cidade');
    var elemento_orig = document.getElementById('cad_estado');
    
    if (xmlhttp) {
        
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                // Se OK
                if (xmlhttp.status == 200) {						
                    elemento_dest.innerHTML=xmlhttp.responseText;						
                } else {
                    alert("There was a problem while using XMLHTTP:\n" + xmlhttp.statusText);
                }
            }				
        }			
        xmlhttp.open("GET", "combo/cidades.php?cad_estado="+elemento_orig.value, true);
        xmlhttp.send(null);
    }

            
}

function onChangeTipo() {
    var value   = document.getElementById('cad_tipo_cadastro').value;
    var divMasc = document.getElementById('masc');
    var divFem  = document.getElementById('fem');
    
    if (value == 1) {
        divMasc.style.display='block';
        divFem.style.display='block';
    }
    else if (value == 2)
    {
        divMasc.style.display='block';
        divFem.style.display='none';
        // limpa os campos
        document.getElementById('cad_nasc_fem').value='';
        document.getElementById('cad_opcao_sexual_fem').selectedIndex=0;
    }
    else if (value == 3)
    {
        divMasc.style.display='none';
        divFem.style.display='block';
        // limpa os campos
        document.getElementById('cad_nasc_masc').value='';
        document.getElementById('cad_opcao_sexual_masc').selectedIndex=0;
    }
}

function onChangeTipoPesquisar() {
    var divMasc = document.getElementById('masc');
    var divFem  = document.getElementById('fem');
    if (document.getElementById('tipo_casal').checked) {
        divMasc.style.display='block';
        divFem.style.display='block';
    } else if (document.getElementById('tipo_homem').checked)
    {
        divMasc.style.display='block';
        divFem.style.display='none';
    } else if (document.getElementById('tipo_mulher').checked)
    {
        divMasc.style.display='none';
        divFem.style.display='block';
    }
}


function lostPassword() {
    var poststr = 'email=' + encodeURI(document.getElementById('email').value);
    makePostRequest('backend.php?page=esqueceu_senha&onSubmit=true', poststr);
}


//FUNCAO PARA PEGAR OS ELEMENTOS DO FORM
function BuscaElementosForm(idForm) {
        var elementosFormulario = document.getElementById(idForm).elements;
        var qtdElementos = elementosFormulario.length;
        var queryString = "";
        var elemento;

        //Cria uma funcao interna para concatenar os elementos do form
        this.ConcatenaElemento = function(nome,valor) {
            if (queryString.length>0) {
                    queryString += "&";
            }
            //queryString += encodeURIComponent(nome) + "=" + encodeURIComponent(valor);
            queryString += encodeURI(nome) + "=" + encodeURI(valor).replace("&", "%26");
         };

        //Loop para percorrer todos os elementos
        for (var i=0; i<qtdElementos; i++) {
                //Pega o elemento
                elemento = elementosFormulario[i];
                if (!elemento.disabled) {
                        //Trabalha com o elemento caso ele nao esteja desabilitado
                        switch(elemento.type) {
                                //Realiza a acao dependendo do tipo de elemento
                                case 'text': case 'password': case 'hidden': case 'textarea':
                                        this.ConcatenaElemento(elemento.name,elemento.value);
                                        break;
                                case 'select-one':
                                        if (elemento.selectedIndex>=0) {
                                                this.ConcatenaElemento(elemento.name,elemento.options[elemento.selectedIndex].value);
                                        }
                                        break;
                                case 'select-multiple':
                                        for (var j=0; j<elemento.options.length; j++) {
                                                if (elemento.options[j].selected) {
                                                        this.ConcatenaElemento(elemento.name,elemento.options[j].value);
                                                }
                                        }
                                        break;
                                case 'checkbox': case 'radio':
                                        if (elemento.checked) {
                                                this.ConcatenaElemento(elemento.name,elemento.value);
                                        }
                                        break;
                        }
                }
        }
        return queryString;
}

