function ManejaAction(modo,frm){
	//Si alquileres estado_modo = 1
	if(modo.indexOf("TA")>-1){
		if(modo_buscador==0){
			document.getElementById(frm).action = "locations.asp";
			CreaControlesAlquiler();
			modo_buscador = 1;
		}
	} else {
		if(modo_buscador==1){
			document.getElementById(frm).action = "properties.asp";
			CreaControles();
			modo_buscador = 0;
		}
	}
}
function CreaControlesAlquiler(){
		var box = document.getElementById("caja_cambia_buscador");
		while(box.hasChildNodes()) box.removeChild(box.lastChild);
		var label = document.createElement("label");
		label.setAttribute("for", "personas");
		label.appendChild(document.createTextNode(nPersonas));
		box.appendChild(label);
		var txt = document.createElement("input");
		txt.setAttribute("type", "text");
		txt.setAttribute("id", "personas");
		txt.setAttribute("name", "personas");
		box.appendChild(txt);
		document.getElementById("lblVistas").style.display = '';
		document.getElementById("lblAnimales").style.display = '';
		document.getElementById("lblAccesible").style.display = '';
}
function CreaControles(){
		var box = document.getElementById("caja_cambia_buscador");
		while(box.hasChildNodes()) box.removeChild(box.lastChild);
		var label = document.createElement("label");
		label.setAttribute("for", "preciomax");
		label.appendChild(document.createTextNode(precioMax));
		box.appendChild(label);
		var sel = document.createElement("select");
		sel.setAttribute("id", "preciomax");
		sel.setAttribute("name", "preciomax");
		var opt = document.createElement("option");
		opt.value = "";
		opt.appendChild(document.createTextNode(val_todos));
		sel.appendChild(opt);
		for(i=60;i<301;i+=30){
			opt = document.createElement("option");
			opt.setAttribute("style", "padding-left:8px");
			opt.value = String(i)+"000";
			opt.appendChild(document.createTextNode("  " + formatCurrency(String(i)+"000")));
			sel.appendChild(opt);
		}
		for(i=360;i<481;i+=60){
			opt = document.createElement("option");
			opt.setAttribute("style", "padding-left:8px");
			opt.value = String(i)+"000";
			opt.appendChild(document.createTextNode("  " + formatCurrency(String(i)+"000")));
			sel.appendChild(opt);
		}
		for(i=600;i<1001;i+=400){
			opt = document.createElement("option");
			opt.setAttribute("style", "padding-left:8px");
			opt.value = String(i)+"000";
			opt.appendChild(document.createTextNode("  " + formatCurrency(String(i)+"000")));
			sel.appendChild(opt);
		}
		for(i=2000;i<3001;i+=1000){
			opt = document.createElement("option");
			opt.setAttribute("style", "padding-left:8px");
			opt.value = String(i)+"000";
			opt.appendChild(document.createTextNode("  " + formatCurrency(String(i)+"000")));
			sel.appendChild(opt);
		}
		sel.appendChild(opt);
		box.appendChild(sel);
		document.getElementById("lblVistas").style.display = 'none';
		document.getElementById("lblAnimales").style.display = 'none';
		document.getElementById("lblAccesible").style.display = 'none';
}
function ManejaSelects(modo,tipo,provincia){
	var precarga = true;
	var req = new XMLHttpRequest();
	if(!tipo) tipo = 0;
	if(!provincia) provincia = 0;
	if(req){
		req.onreadystatechange = function(){
			if (req.readyState == 4 && (req.status == 200 || req.status == 304)){
				try { 
					if(req.responseXML)
						if(tipo==0)LimpiaSelect("tipo");
						if(provincia==0) LimpiaSelect("provincia");
						var root = req.responseXML.documentElement;
						CreaOptions(root,true);
						Loading(false);
				} catch (ex){ alert("ERROR: " + ex); }
			} else if (precarga && req.readyState == 1) {
					precarga = false;
					Loading(true);
			}
		}
	}
	var requestStr = 'includes_web/db_selects_AJAX.asp?modo=' + modo + '&tipo=' + tipo;
	req.open('GET', requestStr);
	req.send(null);
}
function isIElt7(){
	if(navigator.appVersion.indexOf("MSIE")!=-1) if(parseFloat(navigator.appVersion.split("MSIE")[1])<7) return true;
	return false;
}
function Loading(mostrar){
	if(isIElt7()){
		var arr = document.getElementsByTagName("select");
		for(i=0;i<arr.length;i++)arr[i].disabled=mostrar;
	} else {
		var l = document.getElementById("loader");
		l.style.display = (mostrar) ? "block" : "";
	}
}
function CambiaPoblacion(poblacion){
	var idPoblacion = 0;
	var idProvincia = 0;
	var index = -1;
	var ctrl_provincia = document.getElementById('provincia');
	var provincia = parseInt(ctrl_provincia.options[ctrl_provincia.selectedIndex].value);

	if(poblacion) index = poblacion.indexOf("_");
	if(index>-1){
		
		idPoblacion = parseInt(poblacion.substring(index + 1));
		idProvincia = parseInt(poblacion.substring(0, index));
		
		if(provincia != idProvincia){
			var index_p = findOption(ctrl_provincia,idProvincia,"value");
			ctrl_provincia.selectedIndex = index_p;
			//Precarga aqui
		}
	}
}
function ManejaSelectsInterno(modo,tipo,provincia){	
	var precarga = true;
	var req = new XMLHttpRequest();
	if(!modo) modo = "TS";
	if(!tipo) tipo = 0;
	if(!provincia) provincia = 0;

	if(req){
		req.onreadystatechange = function(){
			if (req.readyState == 4 && (req.status == 200 || req.status == 304)){
				try { 
					if(req.responseXML)
						if(tipo==0) LimpiaSelect("tipo");
						if(provincia==0) LimpiaSelect("provincia");
						var root = req.responseXML.documentElement;
						CreaOptions(root,true);
						Loading(false);
				} catch (ex){ alert("ERROR: " + ex); }
			} else if (precarga && req.readyState == 1) {
					precarga = false;
					Loading(true);
			}
		}
	}
	var requestStr = 'includes_web/db_selects_AJAX.asp?vert=1&modo=' + modo + '&tipo=' + tipo + '&provincia=' + provincia;
	req.open('GET', requestStr);
	req.send(null);
}
function LimpiaSelect(id_ctrl){
	var ctrl = document.getElementById(id_ctrl);
	ctrl.disabled = false;
	while (ctrl.childNodes.length >= 1) ctrl.removeChild(ctrl.firstChild);
	//añadimos la opcion todos
	ctrl.options[0] = new Option(val_todos, "", false, true);
	ctrl.options[0].selected = true;
}
function CreaOptions(root,provincias){
	var nOpt;
	//Tipos
	var arrTipos = root.getElementsByTagName("t");
	if(arrTipos){
		for(i=0; i<arrTipos.length; i++){
			nOpt = document.createElement("option");
			nOpt.value = arrTipos[i].attributes[0].nodeValue;
			nOpt.appendChild(document.createTextNode(arrTipos[i].firstChild.nodeValue));
			document.getElementById("tipo").appendChild(nOpt);
		}
	}
	//Provincias
	if(provincias){
		var arrPoblaciones = root.getElementsByTagName("p");
		for(i=0; i<arrPoblaciones.length; i++){
			nOpt = document.createElement("option");
			nOpt.value = arrPoblaciones[i].attributes[0].nodeValue;
			nOpt.appendChild(document.createTextNode(arrPoblaciones[i].firstChild.nodeValue));
			document.getElementById("provincia").appendChild(nOpt);
		}
	}
	//Poblaciones
	var poblacion = document.getElementById("poblacion");
	if(poblacion){
		LimpiaSelect("poblacion");
		var arrPoblaciones = root.getElementsByTagName("l");
		var tmp_idZona = 0;
		var nOptgroup;
		for(i=0; i<arrPoblaciones.length; i++){
			if(tmp_idZona != parseInt(arrPoblaciones[i].attributes[1].nodeValue)){
				tmp_idZona = parseInt(arrPoblaciones[i].attributes[1].nodeValue);
				nOptgroup = document.createElement("optgroup");
				nOptgroup.label = arrPoblaciones[i].attributes[2].nodeValue;
				poblacion.appendChild(nOptgroup);
			}
			
			nOpt = document.createElement("option");
			nOpt.value = arrPoblaciones[i].attributes[0].nodeValue;
			nOpt.appendChild(document.createTextNode(arrPoblaciones[i].firstChild.nodeValue));
			
			nOptgroup.appendChild(nOpt);
		}
	}
}
function findOption($objSelect, $strNeedle, $strSearch) { 
	var $_found = -1; 
	if (($objSelect) && ($strNeedle) && (($strSearch == 'text') || ($strSearch == 'value'))){ 
		for (var $i = 0; $i < $objSelect.options.length; $i++){ 
			if ( $strSearch == 'value' ) 
				$objHayStack = $objSelect[$i].value; 
			else if ( $strSearch == 'text' ) 
				$objHayStack = $objSelect[$i].text; 
			if ( $objHayStack == $strNeedle ){ 
				$_found = parseInt($i);
				break; 
			} 
		} 
	} 
	return $_found; 
}
function formatCurrency(num) {
	var num = num.toString().replace(/\$|\,/g,'');
	var CurrencySymbol = "€";
	if(isNaN(num)) num = "0";
	var sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	num = Math.floor(num/100).toString();
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+'.'+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + ' ' + CurrencySymbol);
}
