var RelatedSelect = Class.create(
	{
	  inputId: '',
	  input: null,
	  inputHidden: null,
	  inputForm: null,
	  
	  inputRelatedId: '',
	  inputRelated: null,
	  
	  urlQueryString: '',
	  weight: 0,
	  valuesHash: new Hash(),
		
	  relatedSelectedValue: '',
	  
	  
	  singularString:'',
	  pluralString:'',
	  
	  initialize:
	  function( id, relatedId, urlQueryString )
	  {
		  this.inputId 		= id;
		  this.input 		= $( this.inputId );
		  this.weight		= weightG;	
		  
		  this.inputRelatedId = relatedId;
		  this.inputRelated   = $( this.inputRelatedId );

		  this.urlQueryString = urlQueryString;
		  
		  //this.singularString = singularString;
		  //this.pluralString = pluralString;

		  this.inputRelated.observe( 'change', this.inputRelatedOnChange.bind(this) );
		  this.input.observe( 'change', this.inputOnChange.bind(this) );
		  this.inputRelatedOnChange( null );
		  
		  
     },
	  
	  inputRelatedOnChange:
	  function( event )
	  {
		  this.relatedSelectedValue = this.inputRelated.options[this.inputRelated.selectedIndex].value;
		  var urlParams = this.urlQueryString.replace( '|WEIGHT|', '' + ( parseInt( $('qty').value ) * this.weight  ) ) +  this.relatedSelectedValue;
		  this.input.update();
		  var thisObject = this;
		  req = new Ajax.Request( WS_ROOT + '/ajax.php', {
					 evalJSON  : true,	   
					 method    : 'post',
					 parameters: urlParams,
					 onSuccess : function(transport) 
								{
									var nrValues = transport.responseJSON.nrValues;
									var values = transport.responseJSON.values;									
									
									
									if( transport.responseJSON.nrValues > 0 )
									{
										thisObject.input.show();
										thisObject.displayValues( values );
									}
									else
									{
										var option = new Option( 'No Entries', '0' );
			 							thisObject.input.options[0] = option;
										
										$('estimatedCost').update( ' No cost ' );
										$('estimatedTime').update( ' ' );
									}
									
								}
					});
	  },
	  
	  inputOnChange:
	  function( event )
	  {
	  		var aux;
	  		aux = this.input.value.split( "|||" );
	  		if ( aux[0] != '') $('estimatedCost').update( aux[0] ); else $('estimatedCost').update( ' ' );
	  		if ( aux[1] != '') $('estimatedTime').update( aux[1] ); else $('estimatedTime').update( ' ' );
	  },
	  
	  displayValues:
	  function( values )
	  {
		  var thisObject = this;
		  
		  if( values != null )
		  {
			  //var option = new Option( 'Select a product', '0' );
			  //thisObject.input.options[0] = option;
			  
			  values.each( 
						function( o )
						{
							var option = new Option( o.name, o.value );
							thisObject.input.options[thisObject.input.options.length] = option;
						}
				);
			  this.inputOnChange( null );
		  }
	  }
	  
	}
);
