//set defaults
c='all'; t='all'; y=0; m='all';


if(typeof dealerId == 'undefined'){dealerId = 'na';}
if(typeof condLbl == 'undefined'){condLbl = '- ALL -';}
if(typeof typeLbl == 'undefined'){typeLbl = '- ALL -';}
if(typeof yearLbl == 'undefined'){yearLbl = '- ALL -';}
if(typeof makeLbl == 'undefined'){makeLbl = '- ALL -';}
if(typeof modelLbl == 'undefined'){modelLbl = '- ALL -';}

listCond(); listType(c); listYear(c,t); listMake(c,t,y); listModel(c,t,y,m);

$('#cond_sel').change(function(){
	if(typeof $('#cond_sel').val() == 'undefined'){var c = 'all';}else{var c = $('#cond_sel').val();}
	listType(c);
	listYear(c,t);
	listMake(c,t,y);
	listModel(c,t,y,m);
})

$('#type_sel').change(function(){
	if(typeof $('#cond_sel').val() == 'undefined'){var c = 'all';}else{var c = $('#cond_sel').val();}
	listYear(c, $(this).val());
	listMake(c,$(this).val(),y);
	listModel(c,$(this).val(),y,m);
})

$('#year_sel').change(function(){
	if(typeof $('#cond_sel').val() == 'undefined'){var c = 'all';}else{var c = $('#cond_sel').val();}		
	listMake(c,$('#type_sel').val(),$(this).val());
	listModel(c,$('#type_sel').val(),$(this).val(),m);
})

$('#make_sel').change(function(){
	if(typeof $('#cond_sel').val() == 'undefined'){var c = 'all';}else{var c = $('#cond_sel').val();}
	listModel(c,$('#type_sel').val(),$('#year_sel').val(),$('#make_sel').val());
})

function listCond(){
	$.getJSON('/cfc/grw.cfc?method=listCond&returnformat=json&dealerId='+dealerId, function(c){
		$.each(c.DATA, function(t,v){
			$('#cond_sel').children().end().append('<option value="'+v+'">'+v+'</option>');
		})
	})
}

function listType(c){
	$.getJSON('/cfc/grw.cfc?method=listType&returnformat=json&dealerId='+dealerId+'&vehCond='+c, function(t){
		typeList = t.split(',');
		if(typeList.length > 0) $('#type_sel').empty().append('<option value="all">'+typeLbl+'</option>');
		$.each(typeList, function(t,v){
			$('#type_sel').children().end().append('<option value="'+v+'">'+v+'</option>');
		})
	})
}

function listYear(c,t){
	$.getJSON('/cfc/grw.cfc?method=listYear&returnformat=json&dealerId='+dealerId+'&vehCond='+c+'&vehType='+t, function(y){
		if(y.DATA.length > 0) $('#year_sel').empty().append('<option value="0">'+yearLbl+'</option>');
		$.each(y.DATA, function(t,v){
			$('#year_sel').children().end().append('<option value="'+v+'">'+v+'</option>');
		})
	})
}

function listMake(c,t,y){
	$.getJSON('/cfc/grw.cfc?method=listMakes&returnforamt=json&dealerId='+dealerId+'&vehCond='+c+'&vehType='+t+'&vehYear='+y, function(m){
		if(m.DATA.length > 0) $('#make_sel').empty().append('<option value="all">'+makeLbl+'</option>');
		$.each(m.DATA, function(i,v){
			$('#make_sel').children().end().append('<option value="'+v+'">'+v+'</option>');
		})
	})
}

function listModel(c,t,y,m){
	$.getJSON('/cfc/grw.cfc?method=listModels&returnformat=json&dealerId='+dealerId+'&vehCond='+c+'&vehType='+t+'&vehYear='+y+'&vehMake='+m, function(m){
		if(m.DATA.length > 0) $('#model_sel').empty().append('<option value="all">'+modelLbl+'</option>');
		$.each(m.DATA, function(i,v){
			$('#model_sel').children().end().append('<option value="'+v+'">'+v+'</option>');
		})
	})
}
