
//	az objektumokat letrehozo Service
ponte.Classes.InstrumentChoose = {
    _choosers: new Array(),
    //	itt tarolodnak a frissitendo selectek
    _sourceIDs: new Array(),
    _destinationIDs: new Array(),
    _actionNames: new Array(),
    _args: new Array(),
    
    // itt kell beregisztralni a frissitendo selecteket
    registerSelect: function(sourceID, destinationID, actionName, args){
        this._sourceIDs.push(sourceID);
        this._destinationIDs.push(destinationID);
        this._actionNames.push(actionName);
        this._args.push(args);
    },
    
    createInstrumentChoosers: function(){
        for (i = 0; i < this._sourceIDs.length; i++) {
            chooser = new ponte.Classes.InstrumentChoose.Chooser();
            chooser.init(this._sourceIDs[i], this._destinationIDs[i],this._actionNames[i], this._args[i]);
        }
    },
	//	osszes chooser ujrainicializalasa
	refreshChoosers : function()
	{
		for (i = 0; i < this._sourceIDs.length; i++) {
            chooser = new ponte.Classes.InstrumentChoose.Chooser();
            chooser.init(this._sourceIDs[i], this._destinationIDs[i],this._actionNames[i], this._args[i]);
        }
	}
}

// a frissitest vegzo osztaly
ponte.Classes.InstrumentChoose.Chooser = function(){
    return {
        _Source: null,
        _DestSelect: null,
        _actionName: null,
        _args: null,
        
        // egy uj ajax kerest nyit a szerver fele es lekeri a kivalasztott szekciohoz tartozo termeket
        getInstruments: function(){
        
        },
        
        init: function(source_id, destSelect_id, actionName, args){
            this._Source = document.getElementById(source_id);
            this._DestSelect = document.getElementById(destSelect_id);
            this._actionName = actionName;
            this._args = args;
            
            function copyFunction(func, arg){
                return function(){
                    func(arg);
                }
            }
            this._Source.onchange = copyFunction(this.selectOnChange, this);
            
            var obj = this;
        },
        
        
        
        selectOnChange: function(obj){
            ajaxErrorHandler = function(){
            };
            
            onResponse = function(response){
				if (obj._DestSelect.tagName == "INPUT") {
					resp=response.responseXML;
					for(r=0; r<resp.lastChild.childNodes.length; r++)
					{
						if(resp.lastChild.childNodes[r].textContent)
						{
							input=document.getElementById(resp.lastChild.childNodes[r].nodeName);
							input.value=resp.lastChild.childNodes[r].textContent;
							try{input.onchange();}catch(e){}
						}
						else
						{
							input=document.getElementById(resp.lastChild.childNodes[r].nodeName);
							input.value=resp.lastChild.childNodes[r].text;
							try{input.onchange();}catch(e){}
						}
					}
				}
				else {
					resp = response.responseText;
					resp = resp.substr(resp.indexOf("<SELECT"), resp.length);
					obj._DestSelect.innerHTML = resp;
				}
				ponte.Classes.InstrumentChoose.refreshChoosers();
            }
            //	 a select erteke
            
            select_val = obj._Source.options[obj._Source.selectedIndex].value;
            
            ajaxURL = urlprefix + "?ajaxrequest=" + obj._actionName + "&select_value=" + select_val;
            for (p=0; p<obj._args.length; p++) {
            	try
            	{
				param=obj._args[p];
				value=document.getElementById(param.getValue()).value;
                ajaxURL += "&"+param.getName()+"="+value;
            	}catch(e){}
            }
            req = ponte.Services.AjaxService.createRequest(ajaxURL, onResponse, ajaxErrorHandler, 'GET', null);
            req.makeRequest();
        }
    }
}

/**
 * Megkapott stringet felparsolja es asszociativ tombot ad vissza.
 * Argumentumok megadasa : arg1=valami&arg2=valami2
 */
ponte.Classes.InstrumentChoose.parameter = function(name, value)
{
	return{
		_name : name,
		_value : value,
		getName : function(){return this._name;},
		getValue : function(){return this._value;}
	}
}
ponte.Classes.InstrumentChoose.createArgs = function(args){
	param_map = new Array();
	splitted = args.split("#");
	if (splitted.length >= 1) {
		for (s=0; s<splitted.length; s++) {
			param=splitted[s];
			p_name = param.split("=")[0];
			p_val = param.split("=")[1];
			param_map.push(new ponte.Classes.InstrumentChoose.parameter(p_name, p_val));
		}
	}
    
    return param_map;
}

// Load Event
ponte.Classes.InstrumentChoose.loadEvent = function(){
    try {
        ponte.Classes.InstrumentChoose.createInstrumentChoosers();
    } 
    catch (e) {
        alert(e);
    };
    }
addLoadEvent(ponte.Classes.InstrumentChoose.loadEvent);
