var GeckoDependantSelects = Class.create();
GeckoDependantSelects.prototype = {
	initialize: function( control, childControl, gateway ) {
		this.controlName = control;
		this.control = $('select_' + control );
		
		if( this.control == null ) {
			throw ("Control not found, unable to register" );
		}
		
		this.childName = childControl;
		this.gateway = gateway;
		this.value = 0;
		
		var self = this;
		if( this.childName != "" ) {
			this.control.onchange = function() {
				self.updateChild();
			}
		}
	},
	
	updateChild: function() {
		var child = this.getChildObject();
		if( child == null ) return;
		
		child.reset();
		
		this.value = this.control.options[this.control.selectedIndex].value;
		
		if( this.control.selectedIndex == 0 ) {
			return; // No value
		}
		
		var pars = "parentControl=" + this.controlName + "&childControl=" + this.childName + "&parentValue=" + this.value;
		var self = this;
		var theAjax = new Ajax.Request(
			this.gateway,
			{
				method: 'get',
				parameters: pars,
				onLoading: function() {
					Element.show( 'loading_' + self.childName );
				},
				onComplete: function( req ) {
					//alert( req.responseText );
					//alert( self.childName );
					Element.hide( 'loading_' + self.childName );
					self.updateChildOptions( req.responseText );
				}
			});
	},
	
	reset: function() {
		var theFirstOption = this.control.options[0];
		
		this.control.options.length = 0;
		//this.control.options.add( theFirstOption );
		this.control.options[this.control.length] = theFirstOption;
		
		var child = this.getChildObject();
		if( child == null ) return;
		
		child.reset();
	},
	
	getChildObject: function() {
		if( this.childName == "" ) {
			return; // No child
		}
		evalStr = "var child = dependantSelect_" + this.childName + ";";
		//alert( evalStr );
		try  {
		eval( evalStr );
		} catch( e ) {}
		
		//alert( typeof child );
		
		if( typeof child == "undefined" ) {
			return; // No child
		}
		
		return child;
	},
	
	updateChildOptions: function( data, value ) {
		
		var child = this.getChildObject();
		if( child == null ) return;
		
		child.fillComboData( data );
		
		if( value != null ) {
			child.selectValue( value );
		}
	},
	
	fillComboData: function( data ) {
		if( data == "" ) return;
		
		var theOptions = data.split( "|" );
		var theFirstOption = this.control.options[0];
		
		this.control.options.length = 0;
		this.control.options[this.control.length] = theFirstOption;
		
		for( var i = 0; i < theOptions.length; i++ ) {
			var aOption = theOptions[i].split("~");
			
			this.control.options[this.control.length] = new Option( aOption[1], aOption[0] );
		}
	},
	
	selectValue: function( optionVal ) {
		var lst = this.control;
		
		for( var i = 0; i< lst.options.length; i++ ){
			if( lst.options[i].value == optionVal ){
				lst.selectedIndex = i;
				return;
			}
		}
	},
	
	preSelect: function( parentValue, childValue ) {
		this.selectValue( parentValue );
		
		var child = this.getChildObject();
		if( child == null ) return;
		
		child.reset();
		
		pars = "parentControl=" + this.controlName + "&childControl=" + this.childName + "&parentValue=" + parentValue;
		
		var self = this;
		var theAjax = new Ajax.Request(
			this.gateway,
			{
				method: 'get',
				parameters: pars,
				onLoading: function() {
					Element.show( 'loading_' + self.childName );
				},
				onComplete: function( req ) {
					Element.hide( 'loading_' + self.childName );
					self.updateChildOptions( req.responseText, childValue );
				}
			});
	}
};