function xmlhttp() {
	// XMLHttpRequest para firefox e outros navegadores
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}

	// ActiveXObject para navegadores microsoft
	var versao = ['Microsoft.XMLHttp', 'Msxml2.XMLHttp', 'Msxml2.XMLHttp.6.0', 'Msxml2.XMLHttp.5.0', 'Msxml2.XMLHttp.4.0', 'Msxml2.XMLHttp.3.0','Msxml2.DOMDocument.3.0'];
	for (var i = 0; i < versao.length; i++) {
		try {
			return new ActiveXObject(versao[i]);
		} catch(e) {
			alert("Seu navegador não possui recursos para o uso do AJAX!");
		}
	} // fecha for
	return null;
} // fecha função xmlhttp


function carregar(pagina)
{
   ajax = xmlhttp();
   if (ajax)
   {
	   ajax.open('get',pagina, true);
	   ajax.onreadystatechange = trazconteudo; 
	   ajax.send(null);
   }
}

function trazconteudo() {
	if (ajax.readyState >= 0 && ajax.readyState <= 3) {
		document.getElementById('conteudo_marca').innerHTML = "<select class='select-um'><option>Carregando...</option></select><br><br>";
	}
	if (ajax.readyState==4) {
		if (ajax.status==200) {
			document.getElementById('conteudo_marca').innerHTML = ajax.responseText;
		}
	}
}
function trazconteudo2() {
	if (ajax.readyState >= 0 && ajax.readyState <= 3) {
		document.getElementById('conteudo_modelo').innerHTML = "<select class='select-um'><option>Carregando...</option></select><br><br>";
	}	
	if (ajax.readyState==4) {
		if (ajax.status==200) {
			document.getElementById('conteudo_modelo').innerHTML = ajax.responseText;
		}
	}
}

function montaMarca() {
	idtipo_veiculo = document.getElementById('idtipo_veiculo').value;
	ajax = xmlhttp();
	if (ajax) {
		ajax.open('get','monta_marca.php?idtipo_veiculo='+idtipo_veiculo, true);
		ajax.onreadystatechange = trazconteudo; 
		ajax.send(null);
	}
	//document.getElementById('conteudo').innerHTML = monta;
}
function montaModelo() {
	idmarca = document.getElementById('idmarca').value;
	ajax = xmlhttp();
	if (ajax) {
		ajax.open('get','monta_modelo.php?idmarca='+idmarca, true);
		ajax.onreadystatechange = trazconteudo2; 
		ajax.send(null);
	}
	//document.getElementById('conteudo').innerHTML = monta;
}

////////////////////////////////////////////////////////////////////
// função para montar a marca dos veículos no cadastro de anuncios
function montaMarcaCad() {
	idtipo_veiculo = document.getElementById('idtipo_v').value;
	ajax = xmlhttp();
	if (ajax) {
		ajax.open('get','monta_marca.php?cad=ok&idtipo_veiculo='+idtipo_veiculo, true);
		ajax.onreadystatechange = traz; 
		ajax.send(null);
	}
}
function traz() {
	if (ajax.readyState >= 0 && ajax.readyState <= 3) {
		document.getElementById('conteudo_cad_marca').innerHTML = "<select class='select-um'><option>Carregando...</option></select><br><br>";
	}
	if (ajax.readyState==4) {
		if (ajax.status==200) {
			document.getElementById('conteudo_cad_marca').innerHTML = ajax.responseText;
		}
	}
}


function montaApMarca() {
	idtipo_veiculo = document.getElementById('idtipo_v').value;
	ajax = xmlhttp();
	if (ajax) {
		ajax.open('get','monta_marca.php?cad=no&idtipo_veiculo='+idtipo_veiculo, true);
		ajax.onreadystatechange = traz; 
		ajax.send(null);
	}
}

////////////////////////////////////////////////////////////////////////////////////
// função para montar o modelo dos veículos no cadastro de anuncios
function montaModeloCad() {
	idmarca = document.getElementById('idmarcaCad').value;
	ajax = xmlhttp();
	if (ajax) {
		ajax.open('get','monta_modelo.php?idmarca='+idmarca, true);
		ajax.onreadystatechange = trazMod; 
		ajax.send(null);
	}
}
function trazMod() {
	if (ajax.readyState >= 0 && ajax.readyState <= 3) {
		document.getElementById('conteudo_cad_modelo').innerHTML = "<select class='select-um'><option>Carregando...</option></select><br><br>";
	}
	if (ajax.readyState==4) {
		if (ajax.status==200) {
			document.getElementById('conteudo_cad_modelo').innerHTML = ajax.responseText;
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////

function ValidaContato() {
	ajax = xmlhttp();
	if(ajax) {
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					captcha = ajax.responseText;
					if(captcha == 0) {
						alert('O código digitado não confere com a imagem!');
						document.formlogin.code.value='';
						document.formlogin.code.focus();
						return false;
					} else {
						document.formlogin.submit();
					}
				} else {
					alert(ajax.statusText);
				}
			}
		}

		code  = document.getElementById('code').value;
		dados = 'code='+code;
		ajax.open('POST', '../captcha/checa.php', true);
		ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ajax.send(dados);
		document.formlogin.code.value='';
	}
}

function ValidaDados() {    	
 	 if (document.getElementById('login').value ==""){
  		 alert('Informe o usuário!');
		 document.formlogin.login.focus();
 	 } else if (document.getElementById('senha').value == ''){    
     	window.alert('Informe a senha!');
		document.formlogin.senha.focus();
 	 } else {
	 	ValidaContato();
	 }
	 
}

function ValidaContato2() {
	ajax = xmlhttp();
	if(ajax) {
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					captcha = ajax.responseText;
					if(captcha == 0) {
						alert('O código digitado não confere com a imagem!');
						document.formlogin.code.value='';
						document.formlogin.code.focus();
						return false;
					} else {
						document.formlogin.submit();
					}
				} else {
					alert(ajax.statusText);
				}
			}
		}

		code  = document.getElementById('code').value;
		dados = 'code='+code;
		ajax.open('POST', 'captcha/checa.php', true);
		ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ajax.send(dados);
		document.formlogin.code.value='';
	}
}

function ValidaDadosEmail() {    	
 	 if (document.getElementById('nome').value ==""){
  		 alert('Informe seu nome!');
		 document.formlogin.nome.focus();
 	 } else if (document.getElementById('telefone').value == ''){    
     	window.alert('Informe seu telefone!');
		document.formlogin.telefone.focus();
 	 } else if (document.getElementById('mensagem').value == ''){    
     	window.alert('Informe a mensagem!');
		document.formlogin.mensagem.focus();
 	 }  else {
	 	ValidaContato2();
	 }
	 
}


function ValidaDadosCad() {    	
 	 if (document.getElementById('nome').value ==""){
  		 alert('Informe seu nome!');
		 document.formlogin.nome.focus();
 	 } else if (document.getElementById('email').value == ''){    
     	window.alert('Informe seu e-mail!');
		document.formlogin.email.focus();
 	 } else if (document.getElementById('telefones').value == ''){    
     	window.alert('Informe o telefone!');
		document.formlogin.telefones.focus();
 	 } else if (document.getElementById('endereco').value == ''){    
     	window.alert('Informe o endereço!');
		document.formlogin.endereco.focus();
 	 } else if (document.getElementById('senha1').value == ''){    
     	window.alert('Informe a senha1!');
		document.formlogin.senha1.focus();
 	 } else {
	 	ValidaContato2();
	 }
	 
}

function ValidaLog() {
	ajax = xmlhttp();
	if(ajax) {
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					captcha = ajax.responseText;
					if(captcha == 0) {
						alert('O código digitado não confere com a imagem!');
						document.formlg.code1.value='';
						document.formlg.code1.focus();
						return false;
					} else {
						document.formlg.submit();
					}
				} else {
					alert(ajax.statusText);
				}
			}
		}

		code  = document.getElementById('code1').value;
		dados = 'code='+code;
		ajax.open('POST', 'captcha/checa.php', true);
		ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ajax.send(dados);
		document.formlg.code1.value='';
	}
}

function ValidaDadosLog() {    	
 	 if (document.getElementById('login2').value ==""){
  		 alert('Informe seu E-mail!');
		 document.formlg.login2.focus();
 	 } else if (document.getElementById('senha2').value == ''){    
     	window.alert('Informe sua senha!');
		document.formlg.senha2.focus();
 	 } else {
	 	ValidaLog();
	 }
	 
}

function mascara(o,f)
   {
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
   }

function execmascara()
   {
    v_obj.value=v_fun(v_obj.value)
   }

function validadata(v)
   {
    v=v.replace(/\D/g,"")
    v=v.replace(/(\d{2})(\d)/,"$1/$2")
    v=v.replace(/(\d{2})(\d)/,"$1/$2")
    return v
   }

//função para ordenar os anuncios
function ordena2(ordem) {
	tipo = document.getElementById('id_tipo').value;
	document.location='anuncios.php?ordem='+ordem+'&tipo='+tipo;
}

function ordena(f1) {
	document.forms.f1.submit();
}

function ordena2(f3) {
	document.forms.f3.submit();
	document.forms.f1.submit();
}

function LimpaForm(fr) {
	document.getElementById(fr).submit();
	document.getElementById(fr).reset();
}

/////////////////////////////////////////////////////////////////////////////


//Função para dar efeito de zebra no grid
function zebra(id,tipo,cor) {
	if(tipo == 1) {
		//alert();
		document.getElementById(id).style.background ="#E6E6E6";
	} else {
		document.getElementById(id).style.background =cor;
	}
}

//funcao para validar compos

function validacampos() { 
	var x=0; /* contador */
	var campo; /* variavel que vai pegar o nome dos campos */
	var fim=0; /* variavel que determina o SUbmit ou não - início falso*/        
	
	while (x < arguments.length) {  
		campo=document.getElementById(arguments[x]); 
		if (campo.value=="" || campo.value == 0) {
			alert("O campo " + campo.id + " deve ser preenchido");
			x=arguments.length;                 
			fim=0;
		} else {
			fim=1;
		}
		x=x+1;
	}               
	
	/*Da focus no campo com problema*/
	campo.focus();
	/* Caso fim==true, submete o formulario */
	if (fim==1) {
		return true;
	} else {
		return false;
	}
}

/////////////////////////////////////////////////////////////////////////////

//Função para excluir registro
function excluir(pagina, id, num) {
	resp = window.confirm('Deseja realmente excluir o registro?');
	if(resp == true) {
		document.location= pagina+"?codigo="+id;	
		alert('Excluído com sucesso!');
	} else {
		return false;	
	}
}

/////////////////////////////////////////////////////////////////////////////
function atualizaContador(){
    setTimeout("carregar()",5000);
	setTimeout(atualizaContador, 5000);
}

function carregar()
{
   ajax = xmlhttp();
   if (ajax)
   {
	   ajax.open('get','contador.php', true);
	   ajax.onreadystatechange = trazContador; 
	   ajax.send(null);
   }
}

function trazContador()
{
	if (ajax.readyState==4)
	{
		if (ajax.status==200)
		{
			document.getElementById('cont').innerHTML = ajax.responseText;
		}
	}
}


