$(document).ready(function () {
	$('#product_button').click(function(){
		var go = false;
		var score = 0;
		$('.req').each(function(){
			if($(this).val()){
				$(this).parent().parent().removeClass('required');
			}else{
				alert('Required fields are empty');
				$(this).focus();
				$(this).parent().parent().addClass('required');
				score--;
				return false;
			}
		});
		$('input:checkbox').each(function(){
			if($(this).is(':checked')){
				//
			} else {
				$(this).val(0);
			}	
		});
		
		if(score >= 0){
			go = true;
		}
		if(go){
			doAdd();
		}
	});
	$('#cars').change(function () {
		if($('#cars').val()) {
			$.ajax({
				type: 'get',
				url: 'index.php?route=account/vehicle/callback&vehicle_id='+escape($('#cars').val()),
				dataType: 'json',
				success: function(data) {
					$('#make').val(data['make']);
					$('#model').val(data['model']);
					$('#engine-capacity').val(data['capacity']);
					$('select[name=category]').val(data['category_id']);
					$('#supercharged').attr('checked',false);
					$('#turbocharged').attr('checked',false);
					$('#rotary').attr('checked',false);
					if(data['supercharged']==1){
						$('#supercharged').attr('checked',true);
					}
					if(data['turbocharged']==1){
						$('#turbocharged').attr('checked',true);
					}
					if(data['supercharged']==1){
						$('#rotary').attr('checked',true);
					}
				}
			});
		}		
	});	
});
