function toggleDisplay(elem) {  
	var style = elem.style;
	if (style.display == "none") { style.display = "";
	} else { style.display = "none" }
} 

function ocultarDisplay(elem) {  
	var style = elem.style;
	style.display = "none";
} 
function mostrarDisplay(elem) {  
	var style = elem.style;
	style.display = "";
} 

function nuevaventananombre(nombre,nombreventana,w,h){
	var opciones="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width="+w+",height="+h+",top=150,left=150";
	window.open(nombre,nombreventana,opciones);
}

function CambiaImg(img_nueva){ 	
	img_grande.src = img_nueva; 
	}
	
function AmpliarImagen(img_grande){
	img_grande = img_grande.substring(0, img_grande.indexOf("_med.jpg"));
	img_grande = img_grande + '.jpg';
	//window.open("popup.asp?"+img_grande,"","width=200,height=200,toolbar=no,
	//location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,
	//resizable=yes");
	window.open("popup.php?"+img_grande, "",'resizable=1,HEIGHT=200,WIDTH=200,status=no,location=no,menubar=no,toolbar=no');

}

function extractFilename(fullpath){
	var lastSlash = fullpath.lastIndexOf("/");
	var filename = fullpath.substring(lastSlash+1,fullpath.length);
	return filename;
	}

function AbrirGaleria(idfichainmueble,imagen){
	imagen = imagen.substring(0, imagen.indexOf("_med.jpg"));
	imagen = imagen + '.jpg';
	
	imagen = extractFilename(imagen);
	window.open("popup.php?idfichainmueble="+idfichainmueble+ "&i="+imagen, "",'resizable=1,HEIGHT=510,WIDTH=880,status=no,location=no,menubar=no,toolbar=no');
}

function getHTTPObject() {
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)
       try {
          xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (e) {
          try {
             xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (E) { xmlhttp = false; }
       }
    @else
    xmlhttp = false;
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
       try {
          xmlhttp = new XMLHttpRequest();
       } catch (e) { xmlhttp = false; }
    }

	return xmlhttp;
}

function handleHttpResponse() {
	//alert("http.ReadyState = " + http.readyState);
	if (http.readyState == 4) {
       //alert("http.Status = " + http.status);
	   if (http.status == 200) {
          
		  if (http.responseText.indexOf('invalid') == -1) {
		  	// Armamos un array, usando la coma para separar elementos
			 //alert("entro al if");
			 var posbarra;
		 	 var texto = new Array();
		 	 var valor = new Array();
             var results = http.responseText.split(",");
			 for (i=0;i<=results.length-1;i++) {
				 posbarra = results[i].indexOf("|");
				 texto[i] =  results[i].substring(0, posbarra);
				 valor[i] = results[i].substring(posbarra+1,100);
			}
			// new Option(text, value, defaultSelected, selected)
			 for (i=0;i<=results.length-1;i++) {
//				document.form_buscador.selectLocalidad[i] = new Option(texto[i], valor[i], false);
				combo[i+1] = new Option(texto[i], valor[i], false);
			}
			
//           document.getElementById("localidadesCargando").innerHTML = "";
//			 document.getElementById("zonasCargando").innerHTML = "";
			 document.getElementById("localidadesCargando").style.display = "none";
			 document.getElementById("zonasCargando").style.display = "none";
			 
			 enProceso = false;
          }
       }
    }
}

function leerLocalidades() {
	if (!enProceso && http) {	
 	   combo = document.form_buscador.selectlocalidad;
    	limpiarSelect(combo);
		limpiarSelect(document.form_buscador.selectzona);
		

	   var i = document.form_buscador.selectprovincia.selectedIndex;
	   var valor =document.form_buscador.selectprovincia.options[i].value;
	   var url = "ajax_querylocalidades.php?idprovincia="+ valor;
	   http.open("GET", url, true);
       http.onreadystatechange = handleHttpResponse;
//       document.getElementById("localidadesCargando").innerHTML = "Cargando...";
       document.getElementById("localidadesCargando").style.display = "";
	   enProceso = true;
       http.send(null);
    }
}
function leerZonas() {
	if (!enProceso && http) {	
	   combo = document.form_buscador.selectzona;
	   limpiarSelect(combo);
	   var i = document.form_buscador.selectlocalidad.selectedIndex;
	   var valor =document.form_buscador.selectlocalidad.options[i].value;
	   var url = "ajax_queryzonas.php?idlocalidad="+ valor;
	   http.open("GET", url, true);
       http.onreadystatechange = handleHttpResponse;
//       document.getElementById("zonasCargando").innerHTML = "Cargando...";
       document.getElementById("zonasCargando").style.display = "";
	   enProceso = true;
       http.send(null);
    }
}

function limpiarSelect(combo){	
	combo.options.length = 0;
	combo[0] = new Option("Indistinto","", false);
}

var enProceso = false; // lo usamos para ver si hay un proceso activo
var http = getHTTPObject(); // Creamos el objeto XMLHttpRequest
var combo;
