//fpv=2

// JavaScript Document
function mw_selecttree(){
	this.id_input=false;
	this.opcionesdir=new Array();
	this.dirindex=0;
	this.opcioneselem=new Array();
	this.nivelessel=new Array();
	this.elemindex=0;
	this.nivel=0;
	this.dirtxt="";
	this.adddir=function(txt,val){
		indexop=this.dirindex;
		this.dirindex++;
		this.opcionesdir[indexop]=new mw_selecttree_opciondir(txt,val);
		return this.opcionesdir[indexop];
	}
	this.addelem=function(txt,val){
		indexelem=this.elemindex;
		this.elemindex++;
		this.opcioneselem[indexelem]=new mw_selecttree_opcionelem(txt,val);
		return this.opcioneselem[indexelem];
	}
	this.set_val=function(val){
		//
	}
	this.set_val_frominput=function(){
		if (this.id_input){
			this.set_val(this.inputobj.value);	
		}
	}
	this.set_objs=function(id_input){
		if (inputobj=fp_get_element_by_id_siexiste(id_input)){
			this.id_input=id_input;
			this.inputobj=inputobj;
			this.areaobj=this.inputobj.parentNode;
			this.selectobj=mw_dom_get_child_by_id(this.areaobj,"elemsel");
			this.diractualdiv=mw_dom_get_child_by_id(this.areaobj,"diractual");
			this.selectobjdir=mw_dom_get_child_by_id(this.areaobj,"dirsel");
			//this.set_obj_events();
			return true;
		}
			
	}
	this.resetdatos=function(){
		this.dirtxt="";
		this.nivelessel=new Array();
		this.opcionesdirsel=this.opcionesdir;
		this.opcioneselemsel=this.opcioneselem;
		this.profundidad=0;
		
		
	}
	this.poblar=function(){
		if(!this.id_input){
			return false;	
		}
		this.diractualdiv.innerHTML=this.dirtxt;
		this.inputobj.value="";
		mw_select_borrarop(this.selectobjdir);
		mw_select_borrarop(this.selectobj);
		mw_selecttree_addoption(this.selectobjdir,".",".");
		if(this.profundidad>0){
			mw_selecttree_addoption(this.selectobjdir,"..","..");
		}
		if(this.opcionesdirsel){
			for (idir=0;idir<this.opcionesdirsel.length;idir++){
				mw_selecttree_addoption(this.selectobjdir,this.opcionesdirsel[idir].val,this.opcionesdirsel[idir].txt);
			}
		}
		mw_selecttree_addoption(this.selectobj,"","");
		if(this.opcioneselemsel){
			for (ielem=0;ielem<this.opcioneselemsel.length;ielem++){
				mw_selecttree_addoption(this.selectobj,this.opcioneselemsel[ielem].val,this.opcioneselemsel[ielem].txt);
			}
		}
		this.selectobj.size=this.selectobj.options.length;
		this.selectobjdir.size=this.selectobjdir.options.length;
	}
	this.onchange_dirsel=function (){
		indexcorresponde=this.selectobjdir.selectedIndex-1;
		if(this.profundidad>0){
			if(indexcorresponde==0){
				return this.retroceder();	
			}
			indexcorresponde=indexcorresponde-1;
		}
		if(this.opcionesdirsel[indexcorresponde]){
			this.profundidad+=1;
			old_opcionesdirsel=this.opcionesdirsel;
			this.opcionesdirsel=old_opcionesdirsel[indexcorresponde].opcionesdir;
			this.opcioneselemsel=old_opcionesdirsel[indexcorresponde].opcioneselem;
			this.nivelessel[this.profundidad-1]=indexcorresponde;
			this.dirtxt=old_opcionesdirsel[indexcorresponde].val;
			this.poblar();
			
		}
		//alert(this.selectobjdir.options[this.selectobjdir.selectedIndex].value);	
	}
	this.set_dirselindex=function(dindex){
		sidxx=dindex+1;
		if(this.profundidad>0){
			sidxx=sidxx+1;
		}
		this.selectobjdir.selectedIndex=sidxx;
		this.onchange_dirsel();
	}
	this.set_selindex=function(eindex){
		this.selectobj.selectedIndex=eindex+1;
		this.onchange_elemsel();
	}

	
	this.retroceder=function(){
		if(this.profundidad>0){
			this.profundidad-=1;
		}
		if(this.profundidad<=0){
			this.resetdatos();
			this.poblar();
			return true;
		}
		this.set_nivelessel();
		this.poblar();
		return true;
	}
	this.set_nivelessel=function(){
		old_nivelessel=this.nivelessel;
		this.nivelessel=new Array();
		opcionesdirsel=this.opcionesdir;
		//alert(this.profundidad);
		if(this.profundidad<=0){
			this.resetdatos();
			return true;
		}
		this.dirtxt="";
		for (p=0;p<this.profundidad;p++){
			this.nivelessel[p]=old_nivelessel[p];
			objopciondir=opcionesdirsel[this.nivelessel[p]];
			if(!objopciondir){
				this.resetdatos();
				return false;
			}
			this.dirtxt=objopciondir.val;
			opcionesdirsel=objopciondir.opcionesdir;
			//alert(p+"="+this.nivelessel[p]);
		}
		this.opcionesdirsel=objopciondir.opcionesdir;
		this.opcioneselemsel=objopciondir.opcioneselem;
	}
	this.onchange_elemsel=function (){
		this.inputobj.value="";
		elmv=this.selectobj.options[this.selectobj.selectedIndex].value;
		if(!elmv){
			return false;	
		}
		if(elmv==""){
			return false;	
		}
		if(elmv=="."){
			return false;	
		}
		this.inputobj.value=elmv;
		return true;
	}
	
	

}
function mw_selecttree_opciondir(txt,val){
	this.txt=txt;
	this.val=val;
	this.opcionesdir=new Array();
	this.dirindex=0;
	this.opcioneselem=new Array();
	this.elemindex=0;
	this.adddir=function(txt,val){
		indexop=this.dirindex;
		this.dirindex++;
		this.opcionesdir[indexop]=new mw_selecttree_opciondir(txt,val);
		return this.opcionesdir[indexop];
	}
	this.addelem=function(txt,val){
		indexelem=this.elemindex;
		this.elemindex++;
		this.opcioneselem[indexelem]=new mw_selecttree_opcionelem(txt,val);
		return this.opcioneselem[indexelem];
	}
}
function mw_selecttree_opcionelem(txt,val){
	this.txt=txt;
	this.val=val;
}
function mw_select_borrarop(selectobj){
	if(!selectobj.options){
		return false;	
	}
	
	selectobj.selectedIndex=false;
	for (i = selectobj.options.length - 1; i>=0; i--) {
		selectobj.remove(i);
	}
	//selectobj.size=1;
}
function mw_selecttree_addoption(selobj,val,txt){
	elOptNew = document.createElement('option');
 	elOptNew.text = txt;
	elOptNew.value = val;
	try {
		selobj.add(elOptNew, null); // standards compliant; doesnt work in IE
	}catch(ex) {
    	selobj.add(elOptNew); // IE only
	}
}


