var url = 'ajax_en.php';

function BuildSelect(target,values) {
	if(!values) return;
	$(target).options.length = 0;
	
	for(i=0;i<values.length;i+=2) {
		$(target).options[i/2] = new Option(values[i],values[i+1]);
	}
}

function changeTipo(e) {
	if(e.value != '') {
		var pars = 's=1&m='+e.value;
		var target = 'c01_id';
		
		new Ajax.Request("ajax_en.php", { 
		 onSuccess : function(resp) { 
		   var opt = resp.responseText.split(','); 
				BuildSelect(target,opt)
		 }, 
		 onFailure : function(resp) { 
		   alert("Oops, there's been an error."); 
		 }, 
		 parameters : pars,
		 method : 'get'
		});
	}
}

function changeUbicacion(e) {
	if(e.value != '') {
		var tipo = $('accion').value;
		var pars = 's=2&m='+tipo+'&c='+e.value;
		var target = 't01_ubicacion';
		
		new Ajax.Request("ajax_en.php", { 
		 onSuccess : function(resp) { 
		   var opt = resp.responseText.split(','); 
				BuildSelect(target,opt)
		 }, 
		 onFailure : function(resp) { 
		   alert("Oops, there's been an error."); 
		 }, 
		 parameters : pars,
		 method : 'get'
		});
	}
}

function changeMunicipio(e) {
	var target = '';
	var tipo = $('accion').value;
	var inm = $('c01_id').value;
	
	$('c02_id').options.selectedIndex = 0;
	$('c03_id').options.selectedIndex = 0;
	
	if(e.value == '1') {
		Element.show('tr_delegaciones');
		Element.hide('tr_municipios');
		target = 'c02_id';
	} 
	
	if(e.value == '2') {
		Element.show('tr_municipios');
		Element.hide('tr_delegaciones');
		target = 'c03_id';
	}
	
	if(e.value != '') {
		var pars = 's=3&m='+tipo+'&c='+inm+'&l='+e.value;
		
		new Ajax.Request("ajax_en.php", { 
		 onSuccess : function(resp) { 
		   var opt = resp.responseText.split(','); 
				BuildSelect(target,opt)
		 }, 
		 onFailure : function(resp) { 
		   alert("Oops, there's been an error."); 
		 }, 
		 parameters : pars,
		 method : 'get'
		});
	}
}

function changeVentaRenta(e) {
	var target = '';
	var tipo = $('accion').value;
	var inm = $('c01_id').value;
	var ub = $('t01_ubicacion').value;
	var dd = e.value;
	
	if(tipo == '1') {
		Element.show('tr_precio_venta');
		Element.hide('tr_precio_renta');
		target = 't01_precio_venta';
	}
	
	if(tipo == '2') {
		Element.show('tr_precio_renta');
		Element.hide('tr_precio_venta');
		target = 't01_precio_renta';
	}
	
	if(e.value != '') {
		var pars = 's=4&m='+tipo+'&c='+inm+'&l='+ub+'&p='+e.value;
		
		new Ajax.Request("ajax_en.php", { 
		 onSuccess : function(resp) { 
		   var opt = resp.responseText.split('|'); 
				BuildSelect(target,opt)
		 }, 
		 onFailure : function(resp) { 
		   alert("Oops, there's been an error."); 
		 }, 
		 parameters : pars,
		 method : 'get'
		});
	}
}

function init() {
	Element.hide('tr_municipios');
	Element.hide('tr_delegaciones');
	Element.hide('tr_precio_venta');
	Element.hide('tr_precio_renta');
	
	$('accion').options.selectedIndex = 0;
	$('c01_id').options.selectedIndex = 0;
	$('t01_ubicacion').options.selectedIndex = 0;
	$('c02_id').options.selectedIndex = 0;
	$('c03_id').options.selectedIndex = 0;
	$('t01_precio_venta').options.selectedIndex = 0;
	$('t01_precio_renta').options.selectedIndex = 0;
	
	new Form.Element.EventObserver($("accion"), changeTipo);
	new Form.Element.EventObserver($("c01_id"), changeUbicacion);
	new Form.Element.EventObserver($("t01_ubicacion"), changeMunicipio);
	new Form.Element.EventObserver($("c02_id"), changeVentaRenta);
	new Form.Element.EventObserver($("c03_id"), changeVentaRenta);
	//new Form.Element.EventObserver($("t01_ubicacion"), changeUbicacion);
}

function frmValidar() {
	var frm = document.frmBuscar;
	if(frm.accion.value == '') {
		if(frm.t01_id_compuesto.value != '') {
			return true;
		}
		window.alert('Please, select at least one of the options before start searching');
		return false;
	} else {
		return true;
	}
}

Event.observe(window, 'load', init, false);
