//fpv=5
function mw_lista_manager(id_lista){
	this.id_lista=id_lista;
	this.niveles=new Object();
	this.nivelmax=0;
	this.doaddafterloadmoevents=true;
	this.add_select_by_id=function(id,nivel,params){
		return this.add_select(fp_get_element_by_id(id),nivel,params);
	}
	this.abort_request=function(){
		if(this.req){
			this.req.abort();
			this.req=false;
		}
	}
	this.add_select=function(e,nivel,params){
		if(e){
			var no=this.get_nivel(nivel,params);
			if(no){
				no.select=e;
				return true;
			}
		}
	}
	this.get_nivel=function(nivel,params){
		if(!nivel){
			nivel=0;	
		}
		if(!this.niveles[nivel]){
			this.niveles[nivel]=new Object;
			if(typeof(params)!="object"){
				params=new Object;
			}
			this.niveles[nivel].params=params;
			this.niveles[nivel].nivel=nivel;
			if(this.nivelmax<nivel){
				this.nivelmax=nivel;
			}
		}
		return this.niveles[nivel];
	}
	this.inicializar=function(){
		if(this.doaddafterloadmoevents){
			this.addafterloadmoevents();	
		}
	}
	
	this.onselect_change=function(nivelobj){
		if(typeof(nivelobj)!="object"){
			return false;
		}
		this.abort_request();
		var val=false;
		if(nivelobj.select){
			val=nivelobj.select.value;	
		}
		var n=nivelobj.nivel;
		var nn;
		for (nn =n+1; nn<=this.nivelmax; nn++){
			this.vaciar_opciones_nivel(nn);
		}
		nn=n+1;
		this.do_request_items_opciones_nivel(nn,val);
		
	}
	this.onAjaxLoad=function(){
		if(!this.req){
			return false;	
		}
		if(this.req.readyState == 4){
			if(this.req.status == 200){
				if(this.loading_nivel_obj){
					this.do_fill_items_opciones_nivel_from_xml_response(this.loading_nivel_obj.nivel,this.req.responseXML);
				}
			}
		}
	}
	this.do_fill_items_opciones_nivel_from_xml_response=function(n,response){
		var no;
		var man=this;
		if(this.niveles[n]){
			no=	this.niveles[n];
		}else{
			return false;	
		}

		if (typeof(response)!="object"){
			return false;
		}
		if (response==null){
			return false;	
		}
		var items;
		var idata;
		if(!no.select){
			return false;
		}
		
		if(no.params){
			if(no.params.addnull){
				mw_select_addOption(no.select,"","");	
			}
		}
		if(items=response.getElementsByTagName('item')){
			for (var i=0;i<items.length;i++){
				idata=items(i);
				mw_select_addOption(no.select,idata.getAttribute("value"),idata.firstChild.data);	
			}
		}
		no.select.onchange=function(){man.onselect_change(no)};
	}
	this.do_request_items_opciones_nivel=function(n,parentval){
		
		var man=this;
		this.abort_request();
		this.loading_nivel_obj=false;
		if(!parentval){
			return false;	
		}
		var no;
		if(this.niveles[n]){
			no=	this.niveles[n];
			this.urlreq=mw_lista_getitemsxmlurl(this.id_lista,parentval,n);
			if(!this.urlreq){
				return false;	
			}
			this.loading_nivel_obj=no;
			this.req=fp_ajax_crear_Request();
			this.req.onreadystatechange = function(){man.onAjaxLoad()};
			
			this.req.open("GET", this.urlreq, true);
			this.req.send(null);

		}
	}
	this.vaciar_opciones_nivel=function(n){
		var no;
		if(this.niveles[n]){
			no=	this.niveles[n];
			if(no.select){
				no.select.onchange=function(){};
				mw_select_removeAllOptions(no.select);
				//no.select.onchange=function(){man.onselect_change(no)};
			}
		}
	}
	this.addafterloadmoevents=function(){
		var n;
		
		for (n =0; n<=this.nivelmax; n++){
			this.addafterloadmoevents_nivel(n);
		}
	}
	this.addafterloadmoevents_nivel=function(n){
		var no;
		var man=this;
		if(this.niveles[n]){
			no=	this.niveles[n];
			if(no.select){
				no.select.onchange=function(){man.onselect_change(no)};
			}
		}
	}
}


//////
function mw_lista_getitemsxmlurl(id_lista,id_parent,nivel){
	var url="/facipub/download/listas/itemsxml/ContentType/text-xml/ContentDisposition/inline/";
	if(!id_lista){
		return false;	
	}
	url=url+"id_lista/"+id_lista+"/";
	if(id_parent){
		url=url+"id_parent/"+id_parent+"/";	
		if(nivel){
			url=url+"nivel/"+nivel+"/";	
		}
	}
	url=url+"items.xml";
	
	return url;
}
function mw_lista_onchange(obj){
	//var id=obj.getAttribute('id');
	var id=obj.id;
	//alert (id);
	var actulizarlist=obj.getAttribute('fp_items_actualizar');
	var actulizarlist_a=actulizarlist.split(","); 
	
	var id_a=id.split("[");
	var idgeneral="";
	for (i=0;i<id_a.length;i++){
		if (i<(id_a.length-2)){
			if (i>0){
				idgeneral=idgeneral+"[";	
			}
			idgeneral=idgeneral+id_a[i];
		}
	}
	var fp_index=obj.selectedIndex;
	var valorsel=obj.options[fp_index].value
	for (i=0;i<actulizarlist_a.length;i++){
		
		//if (Number(actulizarlist_a[i])){
		if (actulizarlist_a[i].length>0){
			
			var id_elemento= idgeneral+"[input][" + actulizarlist_a[i] + "]";
			var id_contenedor= idgeneral+"[contenedor][" + actulizarlist_a[i] + "]";
			var objelemento=fp_get_element_by_id_siexiste(id_elemento);
			if (objelemento){
				var urlreload="lista_get_input.php?id_input=" + id_elemento;
				var class_elemento=objelemento.getAttribute('className');
				if (class_elemento==null){
					var class_elemento=objelemento.getAttribute('class');
				}
				if (class_elemento!=null){
					urlreload= urlreload + "&class=" + class_elemento;
				}
				var fp_lista_nivel_elemento=objelemento.getAttribute('fp_lista_nivel');
				if (fp_lista_nivel_elemento!=undefined){
					urlreload= urlreload + "&nivel=" + fp_lista_nivel_elemento;
				}
				var fp_lista_id_elemento=objelemento.getAttribute('fp_lista_id');
				if (fp_lista_id_elemento!=undefined){
					urlreload= urlreload + "&id_lista=" + fp_lista_id_elemento;
				}
				var fp_items_actualizar_elemento=objelemento.getAttribute('fp_items_actualizar');
				if (fp_items_actualizar_elemento!=undefined){
					urlreload= urlreload + "&fp_items_actualizar=" + fp_items_actualizar_elemento;
				}
				var desactivarsinopciones=objelemento.getAttribute('desactivarsinopciones');
				if (desactivarsinopciones!=undefined){
					urlreload= urlreload + "&desactivarsinopciones=" + desactivarsinopciones;
				}
				urlreload= urlreload + "&id_parent=" + valorsel;
				fp_get_output(urlreload,id_contenedor,"cont",true);
				//alert (urlreload);
			}
			//
		}
	}
	
}
