// JavaScript Document

	
function paginar(pagina){
		document.form_busq.pag.value=pagina;
		document.form_busq.submit();
}

function ordenar(ordenador,direccion){
	document.form_busq.orden.value=ordenador;
	document.form_busq.direccion.value=direccion;
	document.form_busq.submit();
	//alert(ordenador+"-"+direccion);	
}

function cambiar_rango(){
		document.form_busq.pag.value=1;
		document.form_busq.submit();
}

function cambiar_rango_inm(valor){
	document.form_busq.pag.value=1;
	document.form_busq.intervalo_pag.value=valor;
	document.form_busq.submit();
}

function cambiar_rango_dir(valor){
	document.form_busq.pag.value=1;
	document.form_busq.intervalo_pag.value=valor;
	document.form_busq.submit();
}

function cambiar_rango_prod(valor){
	document.form_busq.pag.value=1;
	document.form_busq.intervalo_pag.value=valor;
	document.form_busq.submit();
}

function load_combo(codigo,nombre){

	var t_cod	=	codigo; 
	var t_tipo	=	nombre;
	var	t_estilo=	document.getElementById(nombre).className;
	clear_combo_distritos();
	if(t_tipo=='depto') {
		$.get("librerias/lb_ubicacion.php",{tipo:t_tipo,estilo:t_estilo},loading_depto);
		
		var y=$("#div_prov");
		y.html('<select name="cmb_prov" id="cmb_prov" class="'+t_estilo+'"><option value="null">-- Seleccionar --</option></select>');								

		var z=$("#div_dist");
		z.html('<select name="cmb_distrito" id="cmb_distrito" class="'+t_estilo+'"><option value="null">-- Seleccionar --</option></select>');								
		}

	if(t_tipo=='cmb_depto') {
		$.get("librerias/lb_ubicacion.php",{codigo:t_cod,tipo:t_tipo,estilo:t_estilo},loading_prov);
		var y=$("#div_dist");
		y.html('<select name="cmb_distrito" id="cmb_distrito" class="'+t_estilo+'"><option value="null">-- Seleccionar --</option></select>');								
		}
		
	if(t_tipo=='cmb_prov') { $.get("librerias/lb_ubicacion.php",{codigo:t_cod,tipo:t_tipo,estilo:t_estilo},loading_dist); }
	
	var x=$("#simulador_combo_ext");
 	x.html("<strong>- Seleccionar -</strong>");
	document.getElementById("distritos").value='';	
	document.getElementById("distritos_display").value='';	
	
	return false;
}


function loading_depto(data){
	var x=$("#div_depto");
 	x.html(data);
	
}
function loading_prov(data){
	var x=$("#div_prov");
 	x.html(data);
	document.getElementById("cmb_prov").focus();
	
}
function loading_dist(data){
	var x=$("#div_dist");
 	x.html(data);
	document.getElementById("cmb_distrito").focus();
	
}

function select_dist(){
	var count=0;
	var lista_dist="";
	var lista_dist_nombres="";
	for (var i=0;i < document.form_busq.elements.length;i++)
		{
		
		var elemento = document.form_busq.elements[i];
		
		if (elemento.type == "checkbox" && elemento.name=='chk_dist[]' && elemento.checked)
			{
			//alert(elemento.name+"-"+elemento.value+"-"+elemento.checked);
			lista_dist=lista_dist+elemento.value+"-";
			lista_dist_nombres=lista_dist_nombres+elemento.title+"-";
			//elemento.checked = chkbox.checked;
			count++;
			}
		}
	//document.getElementById("distritos").value=lista_dist;	
	var capa=$("#simulador_combo"); 
	if(count==0){
		capa.html("Seleccionar");
	} else {
		capa.html(lista_dist_nombres);
	}
}
function combo_distritos(){
	//var capa=$("#distritos"); 
	//alert(capa.display);
	var elemento=document.getElementById("selector_distritos_ext");	
	var clase	=elemento.className;
	if(clase=='combo_visible_ext'){
		//alert(visible);	
		elemento.className='combo_invisible_ext';
	} 
	if(clase=='combo_invisible_ext'){
		//alert(visible);	
		elemento.className='combo_visible_ext';
	}
}

function clear_combo_distritos(){
	//var capa=$("#distritos"); 
	//alert(capa.display);
	var elemento=document.getElementById("selector_distritos_ext");	
	elemento.className='combo_invisible_ext';
}

function clickeado(elemento){
	alert(elemento.className);
}

function combo_distritos_ext(){
	//var capa=$("#distritos"); 
	//alert(capa.display);
	var id_prov=document.getElementById("cmb_prov").value;	
	if(id_prov=='null'){
		alert("Selecciona una Provincia!");	
	} else {
	
	var capa=$("#selector_distritos_ext");
 	//capa.html("Cargando...");	
	
	t_tipo='distritos';
	var dist_selec=document.getElementById("distritos").value;
	//if(distritos_seleccionados==''){
		var x=$("#selector_distritos_ext");
	 	x.html('<span class="tit_opciones">Cargando...</span>');	
		$.get("librerias/lb_ubicacion.php",{codigo:id_prov,tipo:t_tipo,seleccion:dist_selec},loading_distritos);
	//}
	
	// Obtiene en una matriz los distritos seleccionados
	/*
	var arr_dist_selec=distritos_seleccionados.split("-");
	for(j=0;j<arr_dist_selec.length-1;j++){
		//alert(arr_dist_selec[j]);	
		var ttt=j;
	}
	*/
	//alert(document.getElementById('chk_dist').elements[1]);
	// Recorre la matriz de checkbox creada para cada distrito de provincia
	/*
	var count=0;
	for (var i=0;i < document.form_busq.elements.length;i++)
		{
		var elemento = document.form_busq.elements[i];
		alert(elemento.type);
		if (elemento.type == "checkbox")
			{
				alert(elemento.value);
			}
		}
	*/
	//
	
	var elemento=document.getElementById("selector_distritos_ext");	
	var clase	=elemento.className;
	if(clase=='combo_visible_ext'){
		//alert(visible);	
		elemento.className='combo_invisible_ext';
	} 
	if(clase=='combo_invisible_ext'){
		//alert(visible);	
		elemento.className='combo_visible_ext';
	}
	}
}
function select_dist_ext(){
	var count=0;
	var lista_dist="";
	var lista_dist_nombres="";
	for (var i=0;i < document.form_busq.elements.length;i++)
		{
		
		var elemento = document.form_busq.elements[i];
		if (elemento.type == "checkbox" && elemento.name=='chk_dist[]' && elemento.checked)
			{
			//alert(elemento.name+"-"+elemento.value+"-"+elemento.checked);
			lista_dist=lista_dist+elemento.value+"-";
			//lista_dist_nombres=lista_dist_nombres+elemento.title+" / ";
			titulo=elemento.title;
			titulo=titulo.substring(0,3);
			if(count<2){
				lista_dist_nombres=lista_dist_nombres+titulo+"/ ";
			} 
			if(count==2){
				lista_dist_nombres=lista_dist_nombres+titulo+" ... ";
			}
			//elemento.checked = chkbox.checked;
			count++;
			}
		}
	document.getElementById("distritos").value=lista_dist;	
	var capa=$("#simulador_combo_ext"); 
	if(count==0){
		capa.html("-- Seleccionar --");
		document.getElementById("distritos_display").value="-- Seleccionar --";	
	} else {
		capa.html(lista_dist_nombres);
		document.getElementById("distritos_display").value=lista_dist_nombres;	
	}
	
}

function loading_distritos(data){
	var x=$("#selector_distritos_ext");
 	x.html(data);	
}

function load_prov_dist(){
	clear_combo_distritos();
	var x=$("#simulador_combo_ext");
 	x.html("<strong>- Seleccionar -</strong>");
	document.getElementById("distritos").value='';	
	//select_dist_ext():
}

function corta_cadena(cadena,long){
	
	for(a=0;a<long; a++){
		alert(cadena);
	}
	
}

function submit_form(nombre_form){
	document.getElementById(nombre_form).submit();
}

function busqueda_rapida(elemento){
	//alert(elemento.value);
	valor_actual=elemento.value;
	if(valor_actual=='Búsqueda rápida de Inmuebles'){
		elemento.value='';
	} else if(valor_actual==''){
		elemento.value='Búsqueda rápida de Inmuebles';
	}
}

function agregar_a_lista(codigo,j_operacion)
{
	
	//var x=$("#div_mensaje");
	//x.html("agregando ...");
	var j_accion="add-delet-favoritos";
	$.post("f_favoritos.php",{cod:codigo,operacion:j_operacion,accion:j_accion}, function(data){
		x=$("#opcion_favorito_"+codigo);
		x.html(data);
		mostrar_num_favoritos();
	}); 
}

function agregar_prod_a_lista(codigo,j_operacion)
{
	
	//var x=$("#div_mensaje");
	//x.html("agregando ...");
	var j_accion="add-delet-favoritos";
	$.post("f_favoritos_prod.php",{cod:codigo,operacion:j_operacion,accion:j_accion}, function(data){
		x=$("#opcion_favorito_"+codigo);
		x.html(data);
		//mostrar_num_favoritos();
	}); 
}

function resultadoFavoritos(datos)
{
	var x=$("#opcion_favorito");
 	x.html(datos);
	mostrar_num_favoritos();
}

function mostrar_num_favoritos()
{
	
	var j_accion="contador-favoritos";
	$.post("f_favoritos.php",{accion:j_accion},function(data){ 
		var capa=$("#total_favoritos");
		capa.html(data);
		}); 
}

function vista_de_playa(objeto){
	var estado=objeto.checked;

	if(estado){
	document.getElementById("div_busq_no_playa").style.display='none';
	document.getElementById("div_busq_playa").style.display='block';
	document.getElementById("form_busq").action="playas.php";
	} else {
	document.getElementById("div_busq_no_playa").style.display='block';
	document.getElementById("div_busq_playa").style.display='none';
	document.getElementById("form_busq").action="inmuebles.php";
	}
}


function combo_playas_ext(){
	
	var elemento=document.getElementById("selector_playas_ext");	
	var clase	=elemento.className;
	if(clase=='combo_visible_ext'){
		//alert(visible);	
		elemento.className='combo_invisible_ext';
	} 
	if(clase=='combo_invisible_ext'){
		//alert(visible);	
		elemento.className='combo_visible_ext';
	}

}

function select_playa_ext(){
	var count=0;
	var lista_playa="";
	var lista_playa_nombres="";
	for (var i=0;i < document.form_busq.elements.length;i++)
		{
		
		var elemento = document.form_busq.elements[i];
		if (elemento.type == "checkbox" && elemento.name=='chk_playa[]' && elemento.checked)
			{
			//alert(elemento.name+"-"+elemento.value+"-"+elemento.checked);
			lista_playa=lista_playa+elemento.value+"-";
			//lista_dist_nombres=lista_dist_nombres+elemento.title+" / ";
			titulo=elemento.title;
			titulo=titulo.substring(0,3);
			if(count<2){
				lista_playa_nombres=lista_playa_nombres+titulo+"/ ";
			} 
			if(count==2){
				lista_playa_nombres=lista_playa_nombres+titulo+" ... ";
			}
			//elemento.checked = chkbox.checked;
			count++;
			}
		}
	document.getElementById("playas").value=lista_playa;	
	var capa=$("#simulador_combo_ext_playa"); 
	if(count==0){
		capa.html("--- Todas ---");
		document.getElementById("playas_display").value="--- Todas ---";	
	} else {
		capa.html(lista_playa_nombres);
		document.getElementById("playas_display").value=lista_playa_nombres;	
	}
	
}
