function gopag(red){
  		marc = $('#marcados').val();
		marc2 = $('#marcados2').val();
		window.location = red+"/"+marc+"/"+marc2;
}

function checados(valor){
  	checks = $('#marcados');
	valini = checks.val();
	if(valini.indexOf(valor) == -1){
		valini+=valor+"-";
		checks.val(valini);		
	}else {
	
		valini = valini.replace(valor+"-","");
		checks.val(valini);
	}

  }
  function checados2(valor){
  	checks = $('#marcados2');
	valini = checks.val();
	if(valini.indexOf(valor) == -1){
		valini+=valor+"-";
		checks.val(valini);		
	}else {	
		valini = valini.replace(valor+"-","");
		checks.val(valini);
	}

  }
  
 function popup(tip,local) {
  	 checks = $('#marcados').val();
	 checks2 = $('#marcados2').val();
    url = local+"index.php/imoveis/lista/"+tip+"/"+checks+"/"+checks2; 
    window.open(url,'page','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=800,height=600');
  }
  function popupCaracteristica(tip,local) {
  	checks = $('#marcados').val();
	checks2 = $('#marcados2').val();
	url = local+"index.php/imoveis/listaCaracteristica/"+tip+"/"+checks+"/"+checks2;
	window.open(url,'page','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=800,height=600');
	 
  }
  
function consultaropc(op,local) {
   if(op>0){
		form = document.formavancada;
		cid =  document.getElementById("cidadeav");
		tip =  document.getElementById("tipoav");
		bai =  document.getElementById("bairroav");				
		
		$.ajax({
					 type: "POST",
					 url: local+"index.php/ajax/buscavop",
					 dataType: "xml",
					 data: "opcao="+op,
					 success: function(msg){
						var retorno = msg.getElementsByTagName('retorno')[0];
									 if(retorno.getElementsByTagName('msg')[0].firstChild.nodeValue == '1'){												
										 cid.options.length = 0;
										 results = retorno.getElementsByTagName('cid');						 
										 for( i = 0; i < results.length; i++ ){ 
											string = results[i].firstChild.nodeValue.split( "|" );
											cid.options[i] = new Option( string[0], string[1] );
										 }
										 
										 tip.options.length = 0;
										 results = retorno.getElementsByTagName('op');						 
										 for( i = 0; i < results.length; i++ ){ 
											string = results[i].firstChild.nodeValue.split( "|" );
											tip.options[i] = new Option( string[0], string[1] );
										 }
										 bai.options.length = 0;
										 bai.options[0] = new Option("selec.cidade","0");
										 $("#caracs").html("");	
										  $("#Buscar").val("Buscar");
										 mudaCampos(form,1);											 
				
									 }else{
										  alert(retorno.getElementsByTagName('alerta')[0].firstChild.nodeValue);												
										  cid.options.length = 0;
										  cid.options[0] = new Option("selec.opcão","0");				  
										  tip.options.length = 0;
										  tip.options[0] = new Option("selec.opção","0");
										  bai.options.length = 0;
										  bai.options[0] = new Option("selec.cidade","0");	
										  $("#comav").val("");
										  $("#Buscar").val("Buscar");
										  $("#caracs").html("");										  
										  mudaCampos(form,1);												
										 
									 }
								 },
								 beforeSend: function(){												
										cid.options.length = 0;
										cid.options[0] = new Option("Carregando...","0");				  
										tip.options.length = 0;
										tip.options[0] = new Option("Carregando...","0");
										bai.options.length = 0;
										bai.options[0] = new Option("Carregando...","0");
										$("#caracs").html("");		
										$("#Buscar").val("Carregando...");
										$("#comav").val("");
										mudaCampos(form,0);												
										
									}
			});
	}
}

function consultarBairroAvc(cid,local) {
	 if(cid != "0"){
		form = document.formavancada;
		opcao = document.getElementById("opcoesav").value;
		campo_select = document.getElementById("bairroav");
		$.ajax({
					type: "POST",
					url:  local+"index.php/ajax/consultarBairros",
					dataType: "html",
					 data: "opcao="+opcao+"&cidade="+cid,
					 success: function(msg){
						 if(msg){																	
							campo_select.options.length = 0;
							results = msg.split(",");	 
							for( i = 0; i < results.length; i++ ){ 
							  string = results[i].split( "|" );
							  campo_select.options[i] = new Option( string[0], string[1] );
							  if(i==0){
							  	campo_select.options[i].selected = true;
							  }
							}
							mudaCampos(form,1); 	 	 									 
				
						 }else{
							alert("Erro na aplicação, favor tentar em alguns minutos!");																		
							campo_select.options.length = 0;
							campo_select.options[0] = new Option("Escolha uma cidade","0");
							mudaCampos(form,1);			 												
										 
						 }
					 },
					 beforeSend: function(){
						campo_select.options.length = 0;												
						campo_select.options[0] = new Option("Carregando...","0");							
						mudaCampos(form,0);													
										
					 }
			});	
	 }
}

function submeterComp(tipo,local) {
 if(tipo>0){
	form = document.formavancada;
	div = $("#caracs");
	$.ajax({
			 type: "POST",
			 url: local+"index.php/ajax/desenhacaracs",
			 dataType: "html",
			 data: "tipo="+tipo+"&opcao="+$("#opcoesav").val(),
			 success: function(msg){
				 if(msg){								 
					 div.html(msg);
														
				 }else{
					 alert("Erro na ação, Favor tentar em alguns minutos!");
					 div.html("");
					 
				 }
			 },
			 beforeSend: function(){
					div.html("<center><label style='font-size:12px;font-weight:bold;color:#FF0000;font-family:tahoma;'>Caregando...</label></center>");
					
				}
	});	
 }
}

function validaAv() {
 op = document.formavancada.opcoesav.value;
 tipo = document.formavancada.tipoav.value;
 vali = document.formavancada.valoriniav.value;
 valf = document.formavancada.valorfimav.value;
 cid  = document.formavancada.cidadeav;
 
 if(cid.value == 0){
	alert("Informe a cidade da busca!");
	cid.focus();
	return false;
	
 }else if(tipo == 0) {
	alert("É necessário escolher um tipo de imóvel");
	return document.formavancada.tipoav.focus();
 }else if(op == 0) {
	alert("É necessário escolher uma opção de busca");
 }else if(op == 1) {
	document.formavancada.submit();
 }else if(op == 2) {
	if(document.formavancada.cidadeav.value == "") {
	   alert("É necessário escolher uma cidade");
	}else {
	   document.formavancada.submit();
	}
 }
}
