$(document).ready(function(){
	// bind functionality to main dropdown
	$('#type').bind('change', function(){
		$('p.classification, p.category, p.manufacturer').css('display', 'none');
		// think about resetting select boxes
		// and disabling irrelevant inputs
		if (this.value == 'truck')
		{
			$('p.classification, p.category, p.manufacturer').css('display', 'block');
			loadCategories(this.value);
			loadManufacturers(this.value);
		}
		else if (this.value == 'trailer')
		{
			$('p.category, p.manufacturer').css('display', 'block');
			loadCategories(this.value);
			loadManufacturers(this.value);
		}
		else if (this.value == 'part')
		{
			$('p.manufacturer').css('display', 'block');
			loadManufacturers(this.value);
		}
	});

	$('#type').trigger('change');
});

function loadCategories(type)
{
	$.get('/scripts/company', { action: 'loadCategories', type: type },
		function(data)
		{
			$('#category').empty();
			for (var index in data)
			{
				$('#category').append($('<option></option>').val(data[index][0]).html(data[index][1]));
			}

		}, 'json'
	);
}

function loadManufacturers(type)
{
	$.get('/scripts/company', { action: 'loadManufacturers', type: type },
		function(data)
		{
			$('#manufacturer').empty();
			for (var index in data)
			{
				$('#manufacturer').append($('<option></option>').val(data[index][0]).html(data[index][1]));
			}

		}, 'json'
	);
}
