$(document).ready(function() {
	$('div#waitimage').css('visibility', 'hidden');
	$('form.searchImperial :input').attr('disabled', 'disabled');
	$('#straporder').removeAttr("disabled");
	$('form.searchImperial select#select_brand').removeAttr('disabled');
	
	// Brand.
	$('select#select_brand').change(function() {
		
		$('form.searchImperial :input').attr('disabled', 'disabled');
		$('form.searchImperial select#select_brand').removeAttr('disabled');
		
		if ($(this).val() != '') {
			
			$('div#waitimage').css('visibility', 'visible');
			
			$.getJSON('index.php?id=' + ajaxID + '&L=' + L + '&type=1512&command=getModelByBrand', {brand: $(this).val()}, function(json) {
				$('select#select_model option').remove();
				
				$('select#select_model').append('<option value="">' + defaultModelText + '</option>');
				for(i=0; i<json.length;i++) {
					$('select#select_model').append('<option value="' + json[i]['uid'] + '">'+ json[i]['name'] +'</option>');
				}
				
				$('div#waitimage').css('visibility', 'hidden');
				$('form.searchImperial select#select_model').removeAttr('disabled');
			});
		}
	});
	
	
	// Model.
	$('select#select_model').change(function() {
		
		$('form.searchImperial :input').attr('disabled', 'disabled');
		$('form.searchImperial select#select_brand').removeAttr('disabled');
		$('form.searchImperial select#select_model').removeAttr('disabled');
		
		if ($(this).val() != '') {
			
			$('div#waitimage').css('visibility', 'visible');
			
			$.getJSON('index.php?id=' + ajaxID + '&L=' + L + '&type=1512&command=getWheelbaseByModel', {model: $(this).val()}, function(json) {
				$('select#select_wheelbase option').remove();
				
				$('select#select_wheelbase').append('<option value="">' + defaultWheelbaseText + '</option>');
				for(i=0; i<json.length;i++) {
					$('select#select_wheelbase').append('<option value="' + json[i]['wielbasis'] + '">'+ json[i]['wielbasis'] +'</option>');
				}
				
				$('div#waitimage').css('visibility', 'hidden');
				$('form.searchImperial select#select_wheelbase').removeAttr('disabled');
			});
		}
	});
	
	
	// Wheelbase.
	$('select#select_wheelbase').change(function() {
		
		$('form.searchImperial :input').attr('disabled', 'disabled');
		$('form.searchImperial select#select_brand').removeAttr('disabled');
		$('form.searchImperial select#select_model').removeAttr('disabled');
		$('form.searchImperial select#select_wheelbase').removeAttr('disabled');
		
		if ($(this).val() != '') {
			
			$('div#waitimage').css('visibility', 'visible');
			
			$.getJSON('index.php?id=' + ajaxID + '&L=' + L + '&type=1512&command=getRoofheightByModelAndWheelbase', {model: $('select#select_model').val(), wheelbase: $(this).val()}, function(json) {
				$('select#select_roofheight option').remove();
				
				$('select#select_roofheight').append('<option value="">' + defaultRoofHeightText + '</option>');
				for(i=0; i<json.length;i++) {
					$('select#select_roofheight').append('<option value="' + json[i]['dakhoogte'] + '">'+ json[i]['dakhoogte'] +'</option>');
				}
				
				$('div#waitimage').css('visibility', 'hidden');
				$('form.searchImperial select#select_roofheight').removeAttr('disabled');
			});
		}
	});
	
	// Roofheight.
	$('select#select_roofheight').change(function() {
		$('form.searchImperial select#select_type').attr('disabled', 'disabled');
		$('form.searchImperial div.buttons input').attr('disabled', 'disabled');
		
		if ($(this).val() != '') {
			
			$('div#waitimage').css('visibility', 'visible');
			
			//alert('ping');
			//alert('index.php?id=' + ajaxID + '&L=' + L + '&type=1512&command=getTypeByModelWheelbaseAndRoofheight&roofheight='+ escape($(this).val()) +'&model='+$('select#select_model').val() +'&wheelbase=' + $('select#select_wheelbase').val());
			
			$.getJSON('index.php?id=' + ajaxID + '&L=' + L + '&type=1512&command=getTypeByModelWheelbaseAndRoofheight&roofheight='+ escape($(this).val()), {model: $('select#select_model').val(), wheelbase: $('select#select_wheelbase').val()}, function(json) {
				$('select#select_type option').remove();

				$('select#select_type').append('<option value="">' + defaultTypeText + '</option>');
				for(i=0; i<json.length;i++) {
					$('select#select_type').append('<option value="' + json[i]['value'] + '">'+ json[i]['text'] +'</option>');
				}
				
				$('div#waitimage').css('visibility', 'hidden');
				$('form.searchImperial select#select_type').removeAttr('disabled');
			});
		}
	});
	
	// Type.
	$('select#select_type').change(function() {
		$('form.searchImperial div.buttons input').attr('disabled', 'disabled');
		
		if ($(this).val() != '') {
			$('form.searchImperial div.buttons input').removeAttr('disabled');
		}
	});
});
