var navegador = navigator.userAgent.toLowerCase(); //Cria e atribui à variável global 'navegador' (em caracteres minúsculos) o nome e a versão do navegador
   g_iCount = 5;
function startCountdown(){
                                           if((g_iCount - 1) >= 0){
                                                   g_iCount = g_iCount - 1;
                                                   setTimeout('startCountdown()',1000);
                                           }else{
                                             window.location.reload( true );
                                           }
                                    }
//Cria uma variável global chamada 'xmlhttp'
var xmlhttp;
function setarCampos(acao) {
    if(acao == "cadastro"){
        campos = "nome="+encodeURI(document.getElementById('txtNome').value)+
        "&cpf="+encodeURI(document.getElementById('txtCPF').value)+
        "&data_nascimento="+encodeURI(document.getElementById('txtNascimento').value)+
        "&email="+encodeURI(document.getElementById('txtEmail').value)+
        "&endereco="+encodeURI(document.getElementById('txtEndereco').value+", "+document.getElementById('txtNumero').value+", "+document.getElementById('txtComplemento').value)+
        "&telefone="+encodeURI("("+document.getElementById('txtDDDTel').value+")"+document.getElementById('txtTelefone').value)+
        "&celular="+encodeURI("("+document.getElementById('txtDDDCel').value+")"+document.getElementById('txtCelular').value)+
        "&cep="+encodeURI(document.getElementById('cep').value)+
        "&bairro="+encodeURI(document.getElementById('bairro').value)+
        "&cidade="+encodeURI(document.getElementById('txtCidade').value)+
        "&estado="+encodeURI(document.getElementById('txtEstado').value)+
        "&pag="+encodeURI(document.getElementById('pag_cadastro').value)+
        "&senha="+encodeURI(document.getElementById('senha').value)+
        "&confirma_senha="+encodeURI(document.getElementById('confirma_senha').value+
        "&captcha="+encodeURI(document.getElementById('captcha').value));
    }else if(acao=="dados_cliente"){
         campos = "nome="+encodeURI(document.getElementById('txtNome2').value)+"&data_nascimento="+encodeURI(document.getElementById('txtNascimento').value)+
        "&email="+encodeURI(document.getElementById('txtEmail2').value)+
        "&cpf="+encodeURI(document.getElementById('txtCPF2').value)+
        "&endereco="+encodeURI(document.getElementById('txtEndereco2').value+", "+document.getElementById('txtNumero2').value+", "+document.getElementById('txtComplemento2').value)+
        "&telefone="+encodeURI(document.getElementById('txtTelefone2').value)+
        "&celular="+encodeURI(document.getElementById('txtCelular2').value)+
        "&cep="+encodeURI(document.getElementById('cep2').value)+
        "&bairro="+encodeURI(document.getElementById('txtBairro2').value)+
        "&cidade="+encodeURI(document.getElementById('txtCidade2').value)+
        "&estado="+encodeURI(document.getElementById('txtEstado2').value);
    }else if(acao=="dados_cliente_senha"){
        campos = "senha_atual="+encodeURI(document.getElementById('senha_atual').value)+
        "&nova_senha="+encodeURI(document.getElementById('nova_senha').value)+
        "&confirma_senha="+encodeURI(document.getElementById('confirma_senha2').value);
    }else if(acao=="contato"){
        campos = "nome="+encodeURI(document.getElementById('contatoNome').value)+
        "&sobrenome="+encodeURI(document.getElementById('contatoSobreNome').value)+
        "&dddtel="+encodeURI(document.getElementById('contatoDDDTel').value)+
        "&telefone="+encodeURI(document.getElementById('contatoTelefone').value)+
        "&email="+encodeURI(document.getElementById('contatoEmail').value)+
        "&pag="+encodeURI(document.getElementById('pag_contato').value)+
        "&mensagem="+encodeURI(document.getElementById('contatoMsg').value);
    }else{
        campos = "email="+encodeURI(document.getElementById('email_esqueceu').value)+"&pag="+encodeURI(document.getElementById('pag').value);
    }
}

//Função que inicia o objeto XMLHttpRequest

function objetoXML() {

	if (navegador.indexOf('msie') != -1) { //Internet Explorer

		var controle = (navegador.indexOf('msie 5') != -1) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'; //Operador ternário que adiciona o objeto padrão do seu navegador (caso for o IE) à variável 'controle'

		try {

			xmlhttp = new ActiveXObject(controle); //Inicia o objeto no IE

		} catch (e) { }

	} else { //Firefox, Safari, Mozilla

		xmlhttp = new XMLHttpRequest(); //Inicia o objeto no Firefox, Safari, Mozilla

	}

}

//Função que envia o formulário

function enviarForm(url, campos, destino) {

	//Atribui à variável 'elemento' o elemento que irá receber a página postada
	var elemento = document.getElementById(destino);

	//Executa a função objetoXML()
	objetoXML();

	//Se o objeto de 'xmlhttp' não estiver true
	if (!xmlhttp) {

		//Insere no 'elemento' o texto atribuído
		elemento.innerHTML = 'Impossível iniciar o objeto XMLHttpRequest.';

		return;

	} else {

		//Insere no 'elemento' o texto atribuído
		elemento.innerHTML = 'Carregando...';

	}

	xmlhttp.onreadystatechange = function () {

		//Se a requisição estiver completada
		if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {

			//Se o status da requisição estiver OK
			if (xmlhttp.status == 200) {

				//Insere no 'elemento' a página postada
				elemento.innerHTML = xmlhttp.responseText;
                                if(elemento.innerHTML.indexOf("A pagina será redirecionada em 5 segundos.")!= -1){
                                   

                                    // de 30 a 0 //
                                   g_iCount = 5;

                                   
                                    startCountdown();
                                }



			} else {

				//Insere no 'elemento' o texto atribuído
				elemento.innerHMTL = 'Página não encontrada!';

			}

		}

	}

	//Abre a página que receberá os campos do formulário
	xmlhttp.open('POST', url+'?'+campos, true);

	//Envia o formulário com dados da variável 'campos' (passado por parâmetro)
	xmlhttp.send(campos);

}


