var radiovalor = 0;

function cambioradio(valor){
	var radiovalorant = radiovalor;
	radiovalor = valor.value;
	if (((document.getElementById("categoria").value == "11") || (document.getElementById("categoria").value == "12") || ((document.getElementById("categoria").value >= "20") && (document.getElementById("categoria").value <= "29"))) && ((radiovalorant == 2) || (radiovalorant == 4) || (radiovalor == 2) || (radiovalor == 4))){
		buscadoravanzado('cambia');
	}
}

function buscadorcategoria(valor,div,tipos,tanuncios){

	var textoa = '<font class="letra">';
	var tipoactual = tipos[document.getElementById(valor).value];

	for (num = 0; num < tipoactual.length; num++) {
		if (num == 0){
			textoa =textoa + '<input type="radio" value="'+num+'" name="tipoa" id="'+tanuncios[num]+'" class="radio" onclick="cambioradio(this)">'+tanuncios[num]+'&nbsp;&nbsp;&nbsp;';
		}
		textoa =textoa + '<input type="radio" value="'+tipoactual.substring(num,num+1)+'" name="tipoa" id="'+tanuncios[tipoactual.substring(num,num+1)]+'" class="radio" onclick="cambioradio(this)">'+tanuncios[tipoactual.substring(num,num+1)]+'&nbsp;&nbsp;&nbsp;';
	}
	document.getElementById(div).innerHTML =textoa+'</font>';
	if (!document.getElementById([tanuncios[radiovalor]])){
		radiovalor = 0;
	}
	document.getElementById([tanuncios[radiovalor]]).checked = true;
	
	if (document.getElementById("bavanzado")!=null){
		if (document.getElementById("bavanzado").innerHTML!=""){
			buscadoravanzado('cambia');
		}
	}
}

function mprovincia(spanp) {
	if (document.getElementById(spanp)){
		if (document.getElementById('provincia').value == '0'){
			document.getElementById(spanp).innerHTML='<div id="spanprovbuscador"><p style="padding-top:3px;padding-bottom:2px;"><span style="margin-right:96px;"><b>MUNICIPIO:</b></span>Debe Seleccionar Una Provincia.</p><br /></div>';
		}else{
			var ajax = new ajaxFunction();
			ajax.onreadystatechange=function(){
				if(ajax.readyState==4) {
					document.getElementById(spanp).innerHTML=ajax.responseText;
				}
			}
			ajax.open("GET","/includes/datosinmobusc.php?municipio="+municipio+"&provincia="+document.getElementById('provincia').value,true);
			ajax.send(null);	
		}
	}
}

function guardarvalor(item,variable){
		window[variable]=item.value;
}

function borrarcuadroymas(item){
		if ((item.value=="0") || (item.value =="Maximo")){
			item.value='';
		}
		if (item.name == "habimax"){
			item.maxLength=2;
		}
}

function buscadoravanzado(item){
	if ((document.getElementById("bavanzado").innerHTML=="") || (item =="cambia")){
		if (((document.getElementById("categoria").value == "11") || (document.getElementById("categoria").value == "12")) && (radiovalor != 2)){
			var ajax = new ajaxFunction();
			ajax.onreadystatechange=function(){
				if(ajax.readyState==4) {
					document.getElementById("bavanzado").innerHTML='<span style="margin-right:112px;"><b>PRECIO:</b></span>Desde: <input type="text" name="preciomin" maxlength="9" size="9" value="'+preciomin+'" style="text-align: right;" onclick="borrarcuadroymas(this);" onblur="vnumero(this,0,\'si\');guardarvalor(this,\'preciomin\');" onkeypress="vnumero(this,0,\'no\');" onkeyup="vnumero(this,0,\'no\');" /> € &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hasta: <input type="text" name="preciomax" maxlength="9" size="9" value="'+preciomax+'" style="text-align: right;" onclick="borrarcuadroymas(this);" onblur="vnumero(this,\'Maximo\',\'si\');guardarvalor(this,\'preciomax\');" onkeypress="vnumero(this,\'Maximo\',\'no\');" onkeyup="vnumero(this,\'Maximo\',\'no\');" /> €<br /><br /><span style="margin-right:79px;"><b>KILOMETROS:</b></span>Desde: <input type="text" name="kilomin" maxlength="6" size="9" value="'+kilomin+'" style="text-align: right;" onclick="borrarcuadroymas(this);" onblur="vnumero(this,0,\'si\');guardarvalor(this,\'kilomin\');" onkeypress="vnumero(this,0,\'no\');" onkeyup="vnumero(this,0,\'no\');" /> Km. &nbsp;&nbsp;&nbsp;Hasta: <input type="text" name="kilomax" maxlength="6" size="9" value="'+kilomax+'" style="text-align: right;" onclick="borrarcuadroymas(this);" onblur="vnumero(this,\'Maximo\',\'si\');guardarvalor(this,\'kilomax\');" onkeypress="vnumero(this,\'Maximo\',\'no\');" onkeyup="vnumero(this,\'Maximo\',\'no\');" /> Km.<br /><br />'+ajax.responseText+'';
				}
			}
			ajax.open("GET","/includes/datosivehiculosbusc.php?combustible="+combustible+"&anomin="+anomin+"&anomax="+anomax,true);
			ajax.send(null);
		}else{
			if (((document.getElementById("categoria").value == "20") || (document.getElementById("categoria").value == "21") || (document.getElementById("categoria").value == "22") || (document.getElementById("categoria").value == "23") || (document.getElementById("categoria").value == "24") || (document.getElementById("categoria").value == "25") || (document.getElementById("categoria").value == "26") || (document.getElementById("categoria").value == "27")) && (radiovalor != 2) && (radiovalor != 4)){
				var ajax = new ajaxFunction();
				ajax.onreadystatechange=function(){
					if(ajax.readyState==4) {
						document.getElementById("bavanzado").innerHTML='<span style="margin-right:112px;"><b>PRECIO:</b></span>Desde: <input type="text" name="preciomin" maxlength="9" size="9" value="'+preciomin+'" style="text-align: right;" onclick="borrarcuadroymas(this);" onblur="vnumero(this,0,\'si\');guardarvalor(this,\'preciomin\');" onkeypress="vnumero(this,0,\'no\');" onkeyup="vnumero(this,0,\'no\');" /> € &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hasta: <input type="text" name="preciomax" maxlength="9" size="9" value="'+preciomax+'" style="text-align: right;" onclick="borrarcuadroymas(this);" onblur="vnumero(this,\'Maximo\',\'si\');guardarvalor(this,\'preciomax\');" onkeypress="vnumero(this,\'Maximo\',\'no\');" onkeyup="vnumero(this,\'Maximo\',\'no\');" /> €<br /><br />';
						if (document.getElementById('provincia').value != '0'){
							document.getElementById("bavanzado").innerHTML=document.getElementById("bavanzado").innerHTML+'<div id="spanprovbuscador">'+ajax.responseText+'</div>';
						}else{
							document.getElementById("bavanzado").innerHTML=document.getElementById("bavanzado").innerHTML+'<div id="spanprovbuscador"><p style="padding-top:3px;padding-bottom:2px;"><span style="margin-right:96px;"><b>MUNICIPIO:</b></span>Debe Seleccionar Una Provincia.</p><br /></div>';
						}
						if (document.getElementById("categoria").value != "20"){
							if ((document.getElementById("categoria").value == "21") || (document.getElementById("categoria").value == "22") || (document.getElementById("categoria").value == "26")){
								var documentohtml='<div id="spanhabitacionesb"><span style="margin-right:67px;"><b>HABITACIONES:</b></span> Desde: <input type="text" name="habimin" maxlength="2" size="9" value="'+habimin+'" style="text-align: right;margin-right:37px;" onclick="borrarcuadroymas(this);" onblur="vnumero(this,0,\'si\');guardarvalor(this,\'habimin\');" onkeypress="vnumero(this,0,\'no\');" onkeyup="vnumero(this,0,\'no\');" />Hasta: <input type="text" name="habimax" maxlength="';
								if (habimax=='Maximo'){
									documentohtml=documentohtml+'6';
								}else{
									documentohtml=documentohtml+'2';
								}
								document.getElementById("bavanzado").innerHTML=document.getElementById("bavanzado").innerHTML+documentohtml+'" size="9" value="'+habimax+'" style="text-align: right;" onclick="borrarcuadroymas(this);" onblur="vnumero(this,\'Maximo\',\'si\');guardarvalor(this,\'habimax\');" onkeypress="vnumero(this,\'Maximo\',\'no\');" onkeyup="vnumero(this,\'Maximo\',\'no\');" /><br /><br /></div>';
							}else{
								document.getElementById("bavanzado").innerHTML=document.getElementById("bavanzado").innerHTML+'<div id="spanhabitacionesb"></span>';
							}
							document.getElementById("bavanzado").innerHTML=document.getElementById("bavanzado").innerHTML+'<span style="margin-right:86px;"><b>SUPERFICIE:</b></span> Desde: <input type="text" name="supmin" maxlength="10" size="9" value="'+supmin+'" style="text-align: right;" onclick="borrarcuadroymas(this);" onblur="vnumero(this,0,\'si\');guardarvalor(this,\'supmin\');" onkeypress="vnumero(this,0,\'no\');" onkeyup="vnumero(this,0,\'no\');" /> m<sup>2</sup> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hasta: <input type="text" name="supmax" maxlength="10" size="9" value="'+supmax+'" style="text-align: right;" onclick="borrarcuadroymas(this);" onblur="vnumero(this,\'Maximo\',\'si\');guardarvalor(this,\'supmax\');" onkeypress="vnumero(this,\'Maximo\',\'no\');" onkeyup="vnumero(this,\'Maximo\',\'no\');" /> m<sup>2</sup><br /><br />';
						}
					}
				}
				ajax.open("GET","/includes/datosinmobusc.php?municipio="+municipio+"&provincia="+document.getElementById('provincia').value,true);
				ajax.send(null);
			}else{
				document.getElementById("bavanzado").innerHTML='<span style="margin-right:112px;"><b>PRECIO:</b></span>Desde: <input type="text" name="preciomin" maxlength="9" size="9" value="'+preciomin+'" style="text-align: right;" onclick="borrarcuadroymas(this);" onblur="vnumero(this,0,\'si\');guardarvalor(this,\'preciomin\');" onkeypress="vnumero(this,0,\'no\');" onkeyup="vnumero(this,0,\'no\');" /> € &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hasta: <input type="text" name="preciomax" maxlength="9" size="9" value="'+preciomax+'" style="text-align: right;" onclick="borrarcuadroymas(this);" onblur="vnumero(this,\'Maximo\',\'si\');guardarvalor(this,\'preciomax\');" onkeypress="vnumero(this,\'Maximo\',\'no\');" onkeyup="vnumero(this,\'Maximo\',\'no\');" /> €';
			}
		}
		
		document.getElementById("bavanzado").style.cssFloat="left";
		document.getElementById("bavanzado").style.styleFloat="left";
		document.getElementById("bavanzado").style.margin="10px 0px 0px 11px";
		document.getElementById("imagenbav").src="/imagenes/buscaa2.gif";
	}else{
		document.getElementById("bavanzado").innerHTML="";
		document.getElementById("bavanzado").style.cssFloat="none";
		document.getElementById("bavanzado").style.styleFloat="none";
		document.getElementById("bavanzado").style.margin="0px";
		document.getElementById("imagenbav").src="/imagenes/buscaa.gif";
	}
}

function vnumero(f,defecto,pondefecto){
	while(f.value.match(/[^0-9]/)){
		var i
		for (i=0;i<=f.value.length;i++) {
			if(f.value.substring(i,1).match(/[^0-9]/)){
				if (i==0){
					f.value=f.value.substring(0,i)+f.value.substring(i+1,f.value.length);
				}else{
					f.value=f.value.substring(0,i-1)+f.value.substring(i,f.value.length);
				}
			}
		} 
	}
	if (pondefecto == "si"){
		if (f.value == ""){
			if (f.name == "habimax"){
				f.maxLength=6;
			}
			f.value=defecto;
		}
	}
}

function ajaxFunction()
  { var xmlHttp;
  try { xmlHttp=new XMLHttpRequest();return xmlHttp; }
  catch (e) { try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");return xmlHttp; }
  catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");return xmlHttp; }
  catch (e) { alert("Your browser does not support AJAX!");return false; }
}}}
