$().ready(function(){
	var product = '';

	$.get('./autocad/index.php?api/lines/'+$(this).val(), function(data) {
	  $('#lines').replaceWith(data);
	});

	$('#lines').live('change', function() {
		product = $('#lines option:selected').text();
		
		if ( product == 'Flanges' )
		{
			$.get('./autocad/index.php?api/classes/'+$(this).val(), function(data) {
				$('#lines').nextUntil('p').remove();			
			
				$('#lines').after(data);
				$('#classes').after('<select id="types" disabled="disabled"></select>');
				$('#types').after('<select id="bores" disabled="disabled"></select>');
				$('#bores').after('<select id="sizes" disabled="disabled"></select>');
				$('#sizes').after('<input type="submit" id="download" value="Download" disabled="disabled" />');			
			});
		}
		else
		{
			$.get('./autocad/index.php?api/types/'+$(this).val(), function(data) {
				$('#lines').nextUntil('p').remove();			
				
				$('#lines').after(data);
				$('#types').after('<select id="schedules" disabled="disabled"></select>');
				$('#schedules').after('<select id="sizes" disabled="disabled"></select>');
				$('#sizes').after('<input type="submit" id="download" value="Download" disabled="disabled" />');		  
			});		
		}
	});
	
	$('#classes').live('change', function() {
		$.get('./autocad/index.php?api/types/'+$('#lines').val()+'/'+$(this).val(), function(data) {		  
			$('#classes').nextUntil('p').remove();			
			
			
			$('#classes').after(data);
			$('#types').after('<select id="bores" disabled="disabled"></select>');
			$('#bores').after('<select id="sizes" disabled="disabled"></select>');
			$('#sizes').after('<input type="submit" id="download" value="Download" disabled="disabled" />');		
		});				
	});	
	
	$('#types').live('change', function() {
		if ( product == 'Flanges' )
		{
			$.get('./autocad/index.php?api/bores/'+$('#lines').val()+'/'+$('#classes').val()+'/'+$(this).val(), function(data) {
				$('#types').nextUntil('p').remove();			
			
				$('#types').after(data);
				$('#bores').after('<select id="sizes" disabled="disabled"></select>');
				$('#sizes').after('<input type="submit" id="download" value="Download" disabled="disabled" />');
			});		
		}
		else
		{
			$.get('./autocad/index.php?api/schedules/'+$('#lines').val()+'/'+$(this).val(), function(data) {		  
				$('#types').nextUntil('p').remove();			
				
				$('#types').after(data);
				$('#schedules').after('<select id="sizes" disabled="disabled"></select>');
				$('#sizes').after('<input type="submit" id="download" value="Download" disabled="disabled" />');		
			});		
		}		
	});

	$('#bores').live('change', function() {
		$.get('./autocad/index.php?api/flanges/'+$('#lines').val()+'/'+$('#classes').val()+'/'+$('#types').val()+'/'+$(this).val(), function(data) {
			$('#bores').nextUntil('p').remove();			
			
			$('#bores').after(data);
			$('#sizes').after('<input type="submit" id="download" value="Download" disabled="disabled" />');		
		});		
	});

	$('#schedules').live('change', function() {
		$.get('./autocad/index.php?api/sizes/'+$('#lines').val()+'/'+$('#types').val()+'/'+$(this).val(), function(data) {
			$('#schedules').nextUntil('p').remove();			
			
			$('#schedules').after(data);
			$('#sizes').after('<input type="submit" id="download" value="Download" disabled="disabled" />');
		});			
	});

	$('#sizes').live('change', function() {
		$(this).nextUntil('p').remove();

		$('#sizes').after('<input type="submit" id="download" value="Download" ref="'+$(this).val()+'" />');
	});
	
	$('#download').live('click', function() {
		var ref = $(this).attr('ref')	
		if ( ref) window.open($(this).attr('ref'));
		
		return false;
	});

});
