function fetchMakes(){
	var myAjax = new Request.JSON({
		url: 'Strathcom/components/search/make-model.php', 
		async: false,
		onComplete: function(resp){
			makes = resp;	
			loadMakes(makes);
		}}).post({
				'class': getCondition(), 
				'body_styles': $('body_type').value,
				'min_year': $('min_year').value, 
				'max_year': $('max_year').value
		});
}

function fetchBodyTypes(){
	var myAjax = new Request.JSON({
		url: 'Strathcom/components/search/body_styles.php', 
		async: false,
		onComplete: function(resp){
			bodyTypes = resp;	
			loadBodyTypes(bodyTypes);		
	}}).post({
				'class':getCondition()
	});
}

function fetchYears(){
	var myAjax = new Request.JSON({
		url: 'Strathcom/components/search/years.php', 
		async: false,
		onComplete: function(resp){
			years = resp;	
			loadYears(years);		
	}}).post({
		all_years: 'yes', 
		'class': getCondition(),
		'make': $('make').value,
		'model': $('model').value,
		'body_styles': $('body_type').value
	});
}

function fetchTrims(){
	var myAjax = new Request.JSON({
		url: 'Strathcom/components/search/trims.php', 
		async: false,
		onComplete: function(resp){
			trims = resp;	
			loadTrims(trims);		
		}
	}).post({
		'class': getCondition(),
		'make': $('make').value,
		'model': $('model').value,
		'min_year': $('min_year').value, 
		'max_year': $('max_year').value,
		'body_styles': $('body_type').value
	});
}

function loadMakes(makes){
	var selectedMake = $('make').value;
	var selectedModel = $('model').value;
	$('make').selectedIndex = 0;
	$('make').options.length = 1;
	$('model').selectedIndex = 0;
	$('model').options.length = 1;
	makes.each(function(make,i){
		$('make').options[$('make').options.length] = new Option(make.name, make.name);
		if(make.name == selectedMake){
			$('make').options[$('make').options.length - 1].selected = true;
			//session.make = make.id;
			loadModels(make.models, selectedModel);
		}
	});
	//if($('make').selectedIndex == 0){
	//	$('trims').selectedIndex = 0;
	//	$('trims').options.length = 1;
	//	session.trim = '';
	//}
	//if($('model').selectedIndex == 0){
	//	$('trims').selectedIndex = 0;
	//	$('trims').options.length = 1;
	//	session.trim = '';
	//}
}

function loadModels(models, model_id){
	var selectedModel = '';
	if(model_id){selectedModel = model_id};
	models.each(function(model,i){
		$('model').options[$('model').options.length] = new Option(model.name, model.name);
		if(model.name == selectedModel){
			$('model').options[$('model').options.length - 1].selected = true;
			//session.model = model.id;
		}
	});
	if(selectedModel == 0){
		//$('trim').selectedIndex = 0;
		//$('trim').options.length = 1;
		//session.trim = '';
	}else{
		//fetchTrims(session.condition, session.make, session.model);
	}
}

function loadTrims(trims){
	selectedTrim = ('trim').value;
	$('trim').selectedIndex = 0;
	$('trim').options.length = 1;
	trims.each(function(trim,i){
		$('trim').options[$('trim').options.length] = new Option(trim.value, trim.value);
		if(trim.value == selectedTrim){
			$('trim').options[$('trim').options.length - 1].selected = true;
		}
	});
}

function loadBodyTypes(types){
	selectedType = $('body_type').value;
	$('body_type').selectedIndex = 0;
	$('body_type').options.length = 1;
	types.each(function(type,i){
		$('body_type').options[$('body_type').options.length] = new Option(type.value, type.value);
		if(type.value == selectedType){
			$('body_type').options[$('body_type').options.length - 1].selected = true;
		}
	});
}

function loadYears(){
	var selected = years.length -1;
	var selectedMax = $('max_year').value;
	var selectedMin = $('min_year').value;
	$('min_year').options.length = 0;
	years.each(function(year,i){
		$('min_year').options[$('min_year').options.length] = new Option(year.year, year.year);
		if(year.year == selectedMin){
			selected = $('min_year').options.length - 1;
		}
	});
	$('min_year').selectedIndex = selected;
	selected = 0;
	$('max_year').options.length = 0;
	years.each(function(year,i){
		$('max_year').options[$('max_year').options.length] = new Option(year.year, year.year);
		if(year.year == selectedMax){
			selected = $('max_year').options.length - 1;
		}
	});
	$('max_year').selectedIndex = selected;
}

function getCondition(){
	var cond = '';
	if($('status1').checked){
		cond = $('status1').value;
	}
	if($('status2').checked){
		cond = $('status2').value;
	}
	return cond;
}

window.addEvent('domready', function(e){
	updateSearch();
	$$('.set_drop_down').addEvent('change', function(e){
		updateSearch();
	});
	$$('.set_status').addEvent('click', function(e){
		updateSearch();
	});
});

function updateSearch(){
	fetchBodyTypes();
	fetchMakes();
}
