function rimChange( target, type )
{
	if ( !type )
		type = 'alloy';

	if ( type == 'scrub' )
	{
		if ( target == 'marka' )
		{
			removeOptions( 'seria_id' );
			addOptions( 'marka', 'seria', type );
			submitDisabled();
		}
		else if ( target == 'seria' )
		{
			if ( $('seria_id').options[$('seria_id').selectedIndex].value > 0 )
				submitEnabled();	
			else
				submitDisabled();
		}
	}
	else if ( target == 'marka' )
	{
		removeOptions( 'seria_id' );
		removeOptions( 'model_id' );
		removeOptions( 'srednica_id' );
		addOptions( 'marka', 'seria', type );
		submitDisabled();
	}
	else if ( target == 'seria' )
	{
		removeOptions( 'model_id' );
		removeOptions( 'srednica_id' );
		addOptions( 'seria', 'model', type );
		submitDisabled();
	}
	else if ( target == 'model' )
	{
		removeOptions( 'srednica_id' );
		addOptions( 'model', 'srednica', type );
		submitDisabled();
	}
	else if ( target == 'srednica' )
	{
		if ( $('srednica_id').options[$('srednica_id').selectedIndex].value > 0 )
			submitEnabled();	
		else
			submitDisabled();
	}
}

function rimCheck()
{
	if (
			(
				( $('rozstaw') )
				&&
				(
					(
						( $('rozstaw').options[$('rozstaw').selectedIndex].value != '' )
						&&
						( $('producent_id2').options[$('producent_id2').selectedIndex].value != '' )
					)
					||
					(
						( $('rozstaw').options[$('rozstaw').selectedIndex].value != '' ) 
						&&
						( $('srednica_id2').options[$('srednica_id2').selectedIndex].value > 0 ) 
					)
					||
					(
						( $('producent_id2').options[$('producent_id2').selectedIndex].value != '' )
						&&
						( $('srednica_id2').options[$('srednica_id2').selectedIndex].value > 0 ) 
					)
				)
			)
			||
			(
				( $('producent_id2').options[$('producent_id2').selectedIndex].value != '' )
				&&
				( $('srednica_id2').options[$('srednica_id2').selectedIndex].value > 0 ) 
			)
		)
		submitEnabled( '2' );	
	else
		submitDisabled( '2' );
}

function removeOptions( target )
{
	for ( i = $( target ).length - 1; i >= 0; i-- )
	{
		$( target ).remove(i);
	}
	$( target ).update('<option value="">-- wybierz --</option>');
	$( target ).disabled = 'disabled'; 
}

function addOptions( source, target, type )
{
	id = $(source+'_id').options[$(source+'_id').selectedIndex].value;
	
	if ( id > 0 )
	{
		// alert( '/ajax/rimparam/type/'+type+'/selected_param/'+target+'/id/' + id );
		new Ajax.Updater( target+'_id', '/ajax/rimparam/type/'+type+'/selected_param/'+target+'/id/' + id );
		$( target+'_id' ).disabled = false;
	}
}

function submitDisabled( suffix )
{
	if ( !suffix )
		suffix = '';
	$( 'szukaj_felgi'+suffix ).disabled = 'disabled';
	$( 'szukaj_felgi'+suffix ).className = 'szukaj_disabled';
}

function submitEnabled( suffix )
{
	if ( !suffix )
		suffix = '';
	$( 'szukaj_felgi'+suffix ).disabled = false;
	$( 'szukaj_felgi'+suffix ).className = 'szukaj_enabled';
}
