// JavaScript Document ASG TecnoInmueble ScriptGenerales
$(document).ready(function(){	
	var category=($('#categoria_inmueble').val()==undefined)?"":$('#categoria_inmueble').val();
	$.ajax({
		type: "POST",
		url: "include/buscar_inmueble_sql.php",
		data: "op=cargar&categoria_inmueble="+category,
		success: function(msg){
		msg=unescape(msg);
		msg=msg.replace('+',' ');
		var opciones="<option value='' selected></option>";
		var opc = msg.split(",");
		for (var i=0; i<opc.length;i++)
		opciones += "<option value='"+opc[i]+"'>"+opc[i]+"</option>";
		$("#state").html(opciones);
		$("#state").attr("value","");
		}
	});
});
function enviar(){
	var state=($('#state').val()==undefined)?"":$('#state').val();
	var zone=($('#zona').val()==undefined)?"":$('#zona').val();
	var category=($('#categoria_inmueble').val()==undefined)?"":$('#categoria_inmueble').val();
	var inmueble='';
	var pclass='';
	
	
	$("#clas_type").find('option').each(function(){
		
		if($("#clas_type").val()==$(this).val()){
			inmueble=($(this).html()==undefined)?"":$(this).html();
			
			pclass +='&pclass[]='+$(this).val();
		}
	});
	
	var nombreRes=($('#nom_resid').val()==undefined)?"":$('#nom_resid').val(); 
	
	var b=($('#code').attr("value")).split(',');

	var code=(b[0]==undefined)?"":b[0]; 
	
	window.location.href = "index.php?action=searchresults&categoria_inmueble="+category+pclass+"&state_ven="+state+"&zona="+zone+"&searchtext="+nombreRes+"&code="+code;
	//alert ("index.php?action=searchresults&categoria_inmueble="+category+"&inmueble="+inmueble+"&"+vacio());
}

function enviar_mercado_secundario(){
	var state=($('#state').val()==undefined)?"":$('#state').val();
	var zone=($('#zona').val()==undefined)?"":$('#zona').val();
	var category=($('#categoria_inmueble').val()==undefined)?"":$('#categoria_inmueble').val();
	var inmueble='';
	var pclass='';
	
	
	$("#clas_type").find('option').each(function(){
		
		if($("#clas_type").val()==$(this).val()){
			inmueble=($(this).html()==undefined)?"":$(this).html();
			
			pclass +='&pclass[]='+$(this).val();
		}
	});
	
	var n=($('#nom_resid').val()==undefined)?"":($('#nom_resid').val()).split(','); 
	var nombreRes=(n[0]==undefined)?"":n[0];
	
	var b=($('#code').attr("value")).split(',');

	var code=(b[0]==undefined)?"":b[0];
	
//	alert($('#habitacion').attr("value"));
	var c=($('#Habitaciones').attr("value")).split(',');
	var habitacion=(c[0]==undefined)?"":c[0];
	 
	var d=($('#Banios').attr("value")).split(',');
	var banios=(d[0]==undefined)?"":d[0];
	
	var e=($('#P_Esta').attr("value")).split(',');
	var p_esta=(e[0]==undefined)?"":e[0];
	
	window.location.href = "index.php?action=searchresults&categoria_inmueble="+category+pclass+"&state_ven="+state+"&zona="+zone+"&searchtext="+nombreRes+"&code="+code+"&beds="+habitacion+"&baths="+banios+"&garage_size="+p_esta;
	//alert ("index.php?action=searchresults&categoria_inmueble="+category+"&inmueble="+inmueble+"&"+vacio());
}

 function vacio(){
	 var bool=[];
	 var resultado="";
	  if(document.listado.state.value!="" && document.listado.zona.value==""){
		  var hijos = $("#zona").find("option");
			  for(var i=1;i<hijos.length;i++){
			  bool[i-1]= "pclass[]="+hijos[i].value;
			 }
			 //bool[bool.length] = "inmueble=";
	  }else{
		//bool[bool.length] = "inmueble="+document.listado.inmueble.options[document.listado.inmueble.selectedIndex].text;
		bool[bool.length] = "pclass[]="+document.listado.zona.value;
	  }
		
	   bool[bool.length] = "state="+document.listado.state.value;
	   //bool[bool.length] = "state="+document.listado.state.value;
		
	   bool[bool.length] = "clas_type[]="+document.listado.clas_type.value;	
		
	   
	 
		return (bool.toString()).replace('/\,/gi',"&");		
 }
function buscar_zona(valor){
	$.ajax({
		type: "POST",
		url: "include/buscar_inmueble_sql.php",
		data: "op=buscar_zona&zona="+escape(valor),
		success: function(msg){
		
		var opciones='<option selected="selected"></option>';
		var opc;
		eval('opc='+msg);
		for (var i=0; i<opc.length;i++){
		opciones += "<option value='"+opc[i]+"'>"+opc[i]
		+"</option>";
		}
		$("#zona").html(opciones);
		$("#zona").attr("value","");
		}
	});
 }
 	


	 function buscar_tipo(valor){
	 	var state=escape($('#state').val());
		var zone=escape($('#zona').val());
		var category=escape($('#categoria_inmueble').val());

		$.ajax({
		type: "POST",
		url: "include/buscar_inmueble_sql.php",
		data: "op=buscar_tipo&clas_type="+valor+'&state='+state+'&zone='+zone+'&categoria_inmueble='+category,
		success: function(msg){
		var opciones='<option selected="selected"></option>';
		var opc;
		eval('opc='+msg);
		for (var i=0; i<opc.length;i++)
		  opciones += "<option value=\'"+opc[i][0]+"\'>"+opc[i][1]+"</option>";
		  $("#clas_type").html(opciones);
		  $("#clas_type").attr("value","");
		},
		error:function(de,t,e){
			alert(t);
		}});
	 }
	 
	  function buscar_tipo_ms(valor){
	 	var state=$('#state').val();
		var zone=$('#zona').val();
		var category=$('#categoria_inmueble').val();
		var urbaz=$('#urbanizacion').val();
		var habt=$('#habitacion').val();
		var banio=$('#banio').val();
		var estacio=$('#estacionamiento').val();
		var metraje=$('#metraje').val();
		//alert("op=buscar_tipo&clas_type="+valor+'&state='+state+'&zone='+zone+'&categoria_inmueble='+category+'&urbanizacion='+urbaz+'&habitacion='+habt+'&banio='+banio+'&estacionamiento='+estacio+'&metraje='+metraje);
		$.ajax({
		type: "POST",
		url: "include/buscar_inmueble_sql.php",
		data: "op=buscar_tipo&clas_type="+valor+'&state='+state+'&zone='+zone+'&categoria_inmueble='+category+'&urbanizacion='+urbaz+'&habitacion='+habt+'&banio='+banio+'&estacionamiento='+estacio+'&metraje='+metraje,
		success: function(msg){
		//alert(msg);
		var opciones='<option selected="selected"></option>';
		var opc;
		eval('opc='+msg);
		for (var i=0; i<opc.length;i++)
		  opciones += "<option value=\'"+opc[i][0]+"\'>"+opc[i][1]+"</option>";
		  $("#clas_type").html(opciones);
		  $("#clas_type").attr("value","");
		}
		});
	 }
	 
	 function buscar_residencia_code(valor){
	 	var state=$('#state').val();
		var zone=$('#zona').val();
		var category=$('#categoria_inmueble').val();
	 	//alert(valor);   
		$.ajax({
		type: "POST",
		url: "include/buscar_inmueble_sql.php",
		data: "op=buscar_residencia_code&categoria_inmueble="+escape(category)+"&nom_resid="+valor+"&zone="+escape(zone)+"&state="+escape(state),
		success: function(msg,t){
			//alert(msg);
		
		var opciones='<option selected="selected"></option>';

		var opc;
		eval('opc='+msg);
		for (var i=0; i<opc.length;i++){
		opciones += "<option value='"+opc[i][0]+"'>"+opc[i][0]+", "+opc[i][2]+"</option>";
		
		}
		$("#nom_resid").html(opciones);
		$("#nom_resid").attr("value",""); 		
		opciones='<option selected="selected"></option>';
		for (var i=0; i<opc.length;i++){
		opciones += "<option value='"+opc[i][2]+','+opc[i][0]+"'>"+opc[i][2]+"</option>";
		
		}
		$("#code").html(opciones);
		$("#code").attr("value",""); 		
		}
		
		});
		
	 }
	 
	  function buscar_tipo_neg(valor){
	 	var state=$('#state').val();
		var zone=$('#zona').val();
		var category=$('#categoria_inmueble').val();
	 	//alert(valor);   
		$.ajax({
		type: "POST",
		url: "include/buscar_inmueble_sql.php",
		data: "op=buscar_tipo_neg&categoria_inmueble="+escape(category)+"&nom_resid="+valor+"&zone="+escape(zone)+"&state="+escape(state),
		success: function(msg,t){
			//alert(msg);
		var opciones='<option selected="selected"></option>';

		var opc;
		eval('opc='+msg);
		for (var i=0; i<opc.length;i++){
		opciones += "<option value='"+opc[i][0]+"'>"+opc[i][1]+" ("+opc[i][2]+")</option>";
		
		}
		$("#tipo_negocio").html(opciones);
		$("#tipo_negocio").attr("value",""); 		
		opciones='<option selected="selected"></option>';
		for (var i=0; i<opc.length;i++){
		opciones += "<option value='"+opc[i][2]+','+opc[i][0]+"'>"+opc[i][2]+"</option>";
		
		}
		}
		
		});
		
	 }
	 
	 function buscar_residencia_code_ms(valor){
	
	 	var state=escape($('#state').val());
		var zone=escape($('#zona').val());
		var category=$('#categoria_inmueble').val();
		var tipo_negocio=$('#tipo_negocio').val();
		var clas_type=$('#clas_type').val();
	 	//alert(valor.value);   
		$.ajax({
		type: "POST",
		url: "include/buscar_inmueble_sql.php",
		data: "op=buscar_residencia_code_ms&categoria_inmueble="+escape(category)+"&nom_resid="+escape(valor.value)+"&zone="+escape(zone)+"&state="+escape(state)+"&clas_type="+escape(clas_type),
		success: function(msg,t){
		msg = unescape(msg);
		//	alert(msg);
		var opciones='<option selected="selected"></option>';

		var opc;
		eval('opc='+msg);
		for (var i=0; i<opc.length;i++){
		opciones += "<option value='"+opc[i][0]+","+opc[i][4]+"'>"+unescape(opc[i][0])+", "+opc[i][2]+"</option>";
		
		}
		$("#nom_resid").html(opciones);
		$("#nom_resid").attr("value",""); 		
		opciones='<option selected="selected"></option>';
		for (var i=0; i<opc.length;i++){
		opciones += "<option value='"+opc[i][2]+','+opc[i][4]+"'>"+opc[i][2]+"</option>";
		
		}
		$("#code").html(opciones);
		$("#code").attr("value",""); 	
		
		opciones='<option selected="selected"></option>';
		for (var i=0; i<opc.length;i++){
		opciones += "<option value='"+opc[i][3]+','+opc[i][4]+"'>"+opc[i][3]+"</option>";
		
		}
		$("#Habitaciones").html(opciones);
		$("#Habitaciones").attr("value",""); 	
		
		opciones='<option selected="selected"></option>';
		for (var i=0; i<opc.length;i++){
		opciones += "<option value='"+opc[i][5]+','+opc[i][4]+"'>"+opc[i][5]+"</option>";
		
		}
		$("#Banios").html(opciones);
		$("#Banios").attr("value",""); 	
		
		opciones='<option selected="selected"></option>';
		for (var i=0; i<opc.length;i++){
		opciones += "<option value='"+opc[i][6]+','+opc[i][4]+"'>"+opc[i][6]+"</option>";
		
		}
		$("#P_Esta").html(opciones);
		$("#P_Esta").attr("value",""); 
		}

		});
		
	 }
	 
	 function buscar_code(obj){
	 	
	 	$("#code").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+obj.value);
	 		if(b[1]==obj.value){
	 			$(this).attr('selected','selected');
	 		}
	 	})
	 		 	
	 }
	 
	function buscar_code_ms(obj){
	 	var parse=obj.value.split(',');
	 	$("#code").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+obj.value);
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 		}
	 	})
	 	$("#Habitaciones").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+obj.value);
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 		}
	 	})
	 	
	 	$("#Banios").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+obj.value);
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 		}
	 	})
	 	$("#P_Esta").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+obj.value);
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 		}
	 	})
	 		 	
	 }
	 
	 function cambiarCode(obj){
	 		
	 	$("#nom_resid").find("option").each(function(){
	 		var b=(obj).split(',');
	 		//alert(b[1]+'=='+$(this).attr('value'));
	 		if(b[1]==$(this).attr('value')){
	 			$(this).attr('selected','selected');
	 		}
	 	})
	 }
	 
	 function cambiarCode_ms(obj){
	 	var parse=obj.value.split(',');
	 	//alert( parse[1]);
	 	$("#nom_resid").find("option").each(function(){
			var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+$(this).attr('value'));
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 			}
	 	})
	 	$("#Habitaciones").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+$(this).attr('value'));
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 			}
	 	})
	 	$("#Banios").find("option").each(function(){
			var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+$(this).attr('value'));
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 			}
	 	})
	 	$("#P_Esta").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+obj.value);
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 		}
	 	})
	 }
	 
	 function cambiarCodeHab_ms(obj){
	 	var parse=obj.value.split(',');
	 	$("#nom_resid").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+$(this).attr('value'));
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 			}
	 	})
	 	$("#code").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+obj.value);
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 		}
	 	})
	 	$("#Banios").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+$(this).attr('value'));
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 			}
	 	})
	 	$("#P_Esta").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+obj.value);
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 		}
	 	})
	 }
	 
	 function cambiarCodeBan_ms(obj){
	 	var parse=obj.value.split(',');
	 	$("#nom_resid").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+$(this).attr('value'));
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 			}
	 	})
	 	$("#code").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+obj.value);
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 		}
	 	})
	 	$("#Habitaciones").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+$(this).attr('value'));
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 			}
	 	})
	 	$("#P_Esta").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+obj.value);
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 		}
	 	})
	 	
	 }
	 
	 function cambiarCodeEst_ms(obj){
	 	var parse=obj.value.split(',');
	 	$("#nom_resid").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+$(this).attr('value'));
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 			}
	 	})
	 	$("#code").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+obj.value);
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 		}
	 	})
	 	$("#Habitaciones").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+$(this).attr('value'));
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 			}
	 	})
	 	$("#Banios").find("option").each(function(){
	 		var b=($(this).attr("value")).split(',');
	 		//alert(b[1]+'=='+obj.value);
	 		if(b[1]==parse[1]){
	 			$(this).attr('selected','selected');
	 		}
	 	})
	 }

function ValidarEmail(campo)
{
  var sw = true;
  if (campo.value.indexOf('@') == -1 || campo.value.indexOf('.') == -1 || campo.value.length<6)
	sw=false;

  return sw;
}
