//fpv=1
function fp_loadoptionsondemand_option(){
	this.process_data=function(xmldata){
		
		this.label=xmldata.getElementsByTagName("label").item(0).firstChild.data;
		this.value=xmldata.getElementsByTagName("value").item(0).firstChild.data;
	}
	this.create_html_element=function(){
		var html_element=document.createElement("option");
		html_element.setAttribute("value",this.value);
		html_element.text=this.label;
		html_element.innerHTML=this.label;
		return html_element;
	}
	this.appendToParent=function(parent){
		var html_element=this.create_html_element();
		parent.appendChild(html_element);
	}
}
function fp_loadoptionsondemand_optgroup(){
	this.process_data=function(xmldata){
		this.label=xmldata.getElementsByTagName("label").item(0).firstChild.data;
		this.options=new Array;
		var ni=0;
		var optobj;
		var optnodes=xmldata.getElementsByTagName("options");
		if(optnodes){
			var optnode=optnodes[0];
			if(optnode){
				for (ni=0; ni<optnode.childNodes.length; ni++){
					if(optobj=this.manager.optionNode2option(optnode.childNodes[ni])){
						this.options.push(optobj);	
					}
				}
			}
				
		}
	}
	this.create_html_element=function(){
		var html_element=document.createElement("optgroup");
		html_element.setAttribute("label",this.label);
		if(this.options){
		   	for (ni=0; ni<this.options.length; ni++){
				this.options[ni].appendToParent(html_element);
			}
		}
		return html_element;
	}
	this.appendToParent=function(parent){
		var html_element=this.create_html_element();
		parent.appendChild(html_element);
	}
}


function fp_loadoptionsondemand_manager(cod,mainman){
	this.cod=cod;
	this.mainman=mainman;
	this.submited=false;
	this.loaded=false;
	this.selectinputs_to_populate_after_load=new Array();
	
	this.add_options_to_select=function(selectelem){
		//selectelem.style.display="none";
		var addempty=false;
		var addemptylbl="";
		var valselect=selectelem.value;
		if(selectelem["options"]){
			if(selectelem["options"][0]){
				if(	selectelem["options"][0].value==""){
					addempty=true;
					if(selectelem["options"][0].text){
						addemptylbl=selectelem["options"][0].text;	
					}
				}
			}
		}
		mw_select_removeAllOptions(selectelem);
		if(addempty){
			mw_select_addOption(selectelem,"",addemptylbl);	
		}
		var ni;
		
		for (ni=0; ni<this.options.length; ni++){
			this.options[ni].appendToParent(selectelem);
		}
		selectelem.value=valselect;
		//selectelem.style.display="";

		
	}
	this.add_select_and_load=function(selectelem){
		this.add_select(selectelem);
		this.submit();
	}
	this.submit=function(){
		if(this.submited){
			return true;	
		}
		
		
		this.submited=true;
		var params=new Object;
		params.cod=this.cod;
		this.urlajax= fp_get_modoutput_url("colecciondedatos","loadoptionsondemand",params);
		this.ajaxlauncher=new fp_ajax_launcher(this.urlajax);
		this.ajaxlauncher.setNextactionobj(this);
		this.ajaxlauncher.run();

	}
	this.process_responseXML=function(responseXML){
		this.responseXML=responseXML;
		var roots=this.responseXML.getElementsByTagName('root');
		if(!roots){
			return false;
		}
		this.xmlRootNode=roots[0];
		if(this.xmlRootNode.getElementsByTagName('responseok')[0].childNodes[0].nodeValue!=1){
			return false;
		}
		this.xmlOptionsNode=this.xmlRootNode.getElementsByTagName('optionsroot')[0];
		this.process_xmlOptionsNode();
	}
	this.process_xmlOptionsNode=function(){
		this.options=new Array;
		var ni=0;
		var optobj;
		for (ni=0; ni<this.xmlOptionsNode.childNodes.length; ni++){
			if(optobj=this.optionNode2option(this.xmlOptionsNode.childNodes[ni])){
				this.options.push(optobj);	
			}
		}
		this.loaded=true;
		this.populate_waiting_selects();
	}
	this.populate_waiting_selects=function(){
		var ni=0;
		for (ni=0; ni<this.selectinputs_to_populate_after_load.length; ni++){
			this.add_options_to_select(this.selectinputs_to_populate_after_load[ni]);		
		}
		
	}
	this.optionNode2option=function(node){
		if(node.nodeType!=1){
			return false;
		}
		var type=node.getAttribute("type");
		var obj;
		if(type=="option"){
			obj=new fp_loadoptionsondemand_option();
		}else if(type=="optgroup"){
			obj=new fp_loadoptionsondemand_optgroup();	
		}else{
			return false;	
		}
		obj.manager=this;
		obj.process_data(node);
		return obj;
	}
	this.donextaction=function(){
		this.process_responseXML(this.ajaxlauncher.req.responseXML);
	}
	this.add_select=function(selectelem){
		if(this.loaded){
			return this.add_options_to_select(selectelem);	
		}else{
			this.selectinputs_to_populate_after_load.push(selectelem);	
		}
	}
}
function fp_loadoptionsondemand_main_manager(){
	this.managers=new Object;
	this.get_manager=function(cod){
		if(!cod){
			return false;	
		}
		if(this.managers[cod]){
			return this.managers[cod];
		}else{
			this.managers[cod]=new fp_loadoptionsondemand_manager(cod,this);
			return this.managers[cod];
		}
	}
}
var __fp__fp_loadoptionsondemand_main_manager=new fp_loadoptionsondemand_main_manager();
function fp_loadoptionsondemand_get_manager(cod){
	return 	__fp__fp_loadoptionsondemand_main_manager.get_manager(cod);
}
function fp_loadoptionsondemand(selectelem){
	var cod=selectelem.getAttribute("fploadoptionsondemand");
	//cod=selectelem["name"];
	selectelem.onclick=function(){};
	if(cod){
		var man=fp_loadoptionsondemand_get_manager(cod);
		if(man){
			man.add_select_and_load(selectelem);	
		}
	}
}


