
function CRelatedSelect(id,url){
	//properies
    this.parentValue = null;
	this.value = 0;
	this.id = id;
	this.url = url;
	this.optionsList = null;
	this.optionsList = [{id:1, name:'name1'}, {id:2, name:'name2'} ];
	this.listeners = new Array();
	
	//methods
	this.addListener = function(obj){
	    this.listeners.push(obj);
	}
	this.deleteListener = function(obj){
	   for(i=0;i<this.listeners.length;i++){
	       if(obj == this.listeners[i])
	           this.listeners[i] = null;
	   }
	}

	this.getElement = function(){
		return $(this.id);
		//return document.getElementById(this.id);
	}
	this.getValue = function(){
	    if(!(element = this.getElement())) return false;
		this.value = parseInt(typeobj.options[typeobj.selectedIndex].value);
		return this.value;
	}
	
	this.setValue = function(value){
	    if(!(element = this.getElement())) return false;
		this.value = 0;
		for(i=0;i<element.options.length;i++){
			if(element.options[i].value == value){
			    element.options[i].focused = true;
    			element.options[i].selected = true;
				this.value = value;
				break;
			}
			element.options[i].selected = false;
		}
        this.changed();
		return true;
	}
	
	this.populateInternal = function( value ){
	    var element = null;
        if(!(element = this.getElement())) return false;
		element.options.length = 0;
		for(var i=0;i<this.optionsList.length;i++)
		{
		    var item = this.optionsList[i];
		    element.options[i] = new Option(item.name, item.id);
		}
		if( value )
		  element.value = value;
	}
	
	this.populate = function(parentValue, value){
	    var oThis = this;
		//new Ajax.Updater(this.id,this.url);
        new Ajax.Request(this.url,
            {   method:'get',
                parameters: {id: parentValue},
                onComplete:function(transport, json){
                    json = eval(transport.responseText);
                    //alert(json ? Object.inspect(json) : "no JSON object");
                    oThis.optionsList = json;
                    oThis.populateInternal( value );
                },
                evalScripts:true
            });
        this.parentValue = parentValue;
	    return true;
	}
	
	this.onParentChanged = function(objParent){
	    if(!(parentElement = objParent.getElement())) return false;
	    
	    var newValue = parentElement.getValue();
	    if(newValue != this.parentValue || this.parentValue == null)
	    {
            this.parentValue = newValue;
            this.populate(this.parentValue);
            this.setValue(0);
	    }
	}

	
	this.changed = function(){
	   for(i=0;i<this.listeners.length;i++){
	       if(this.listeners[i])
	           this.listeners[i].onParentChanged(this);
	   }
	}
	
	this.init = function(){
		var oThis = this;
	    if(!(element = this.getElement())) return false;
		element.onchange = function(){oThis.changed();}
        //element.changed();
	}
	
	//init code
	this.init();
	return this;
}
