function change_part(part, use_image) {
	if (part == "") {
		alert("Please select a part type.");
	} else {
		load_image(part, use_image);
		load_results();
	}
}

function choose_shop(part_num) {
	var part = document.getElementById("part").value;
	var popup = window.open("choose_shop.php?pn=" + part_num + "&pt=" + part, "choose_shop", "left=20,height=250,scrollbars=1,top=20,width=500");
	popup.focus();
}

function fillEmptyOption(id, name) {
	var sel = document.getElementById(id);
	sel.length = 0;
	sel.options[0] = new Option("--- " + name + " ---", 0);
}

function load_image(part, use_image) {
	// If all of the items are chosen, the user is just trying to change the part. Don't refresh the message.
	var sm = (document.getElementById("year").selectedIndex > 0 && document.getElementById("make").selectedIndex > 0 && document.getElementById("model").selectedIndex > 0 && document.getElementById("engine").selectedIndex > 0) ? "0" : "1";
	if (use_image) {
		cursor_wait();
		$.ajax({
			type: "get",
			processData: false,
			url: "../functions/catalog_load_image.php",
			data: "part=" + part + "&sm=" + sm,
			cache: false,
			success: function(data) {
				$("#image").hide().html(data).fadeIn("slow");
			}
		});
	}
}

function load_makes(year) {
	if (!(year > 0)) {
		showDialog("Error in Catalog Search", "You must select a make to proceed. Please try again.");
	} else {
		cursor_wait();
		var url = "../functions/catalog_load_makes.php";
		var params = "y=" + year;
		ajaxUpdater(url, params, "make", "get");
	}
}

function load_models(make) {
	var year = document.getElementById("year").value;
	if (year <= 0 || make == "") {
		showDialog("Error in Catalog Search", "Please select a valid year and make to continue. Please try again.");
	} else {
		cursor_wait();
		var url = "../functions/catalog_load_models.php";
		var params = "y=" + year + "&m=" + escape(make);
		ajaxUpdater(url, params, "model", "get");
	}
}

function load_engines(model) {
	var year = document.getElementById("year").value;
	var make = document.getElementById("make").value;
	if (year <= 0 || make == "" || model == "") {
		alert("Please select a valid year, make, and model to continue.");
	} else {
		cursor_wait();
		var url = "../functions/catalog_load_engines.php";
		var params = "y=" + year + "&m=" + escape(make) + "&d=" + escape(model);
		ajaxUpdater(url, params, "engine", "get");
	}
}

function load_results() {
	var items = new Array();
	var part = document.getElementById("part");
	if (part.options.length > 1) {
		for (var i = 0; i < part.options.length; i++) {
			if (part.options[i].selected == true)
				items.push('p[]=' + part.options[i].value);
		}
	} else {
		items.push('p[]=' + part.value);
	}
	items.push("y=" + document.getElementById("year").value);
	items.push("m=" + escape(document.getElementById("make").value));
	items.push("d=" + escape(document.getElementById("model").value));
	items.push("e=" + escape(document.getElementById("engine").value));
	items.push("a=" + ((document.getElementById("advanced").checked) ? 1 : 0));
	// Only send the request if all items are chosen.
	if (document.getElementById("engine").selectedIndex > 0) {
		cursor_wait();
		var url = "../functions/catalog_load_results.php";
		var params = items.join("&");
		ajaxUpdater(url, params, "results", "get");
	}
}

function open_image(id, part) {
	var f = document.forme;
	var popup = window.open("product_image.php?id=" + id + "&part=" + part, "popup", "left=20,height=450,scrollbars=1,top=20,width=400");
	popup.focus();
}

function show_lineart(id, part) {
	var popup = window.open("product_lineart.php?id=" + id + "&part=" + part, "popup", "left=20,height=450,scrollbars=1,top=20,width=400");
	popup.focus();
}

function validateMake(f) {
	if (f.make.value == "") {
		highlight(f.make, 1);
		showDialog("Errors in Small Engine Search", "You must select a make to proceed. Please try again.");
	} else {
		highlight(f.make, 0);
		cursor_wait();
		$("#indicator").css("display", "block");
		var url = "../functions/catalog_show_smallengine.php";
		var params = "m=" + f.make.value;
		ajaxUpdater(url, params, "results", "get");
	}
	return false;
}