var get_models = new sack(ajax_delegate_get_models_url);
var get_years  = new sack(ajax_delegate_get_years);
var get_parts  = new sack(ajax_delegate_get_parts);
var get_makes  = new sack(ajax_delegate_get_makes);
var selected_make = "";
var selected_category = ""
var default_make = "";

function set_default_make(make) {
	default_make = make;
}

function make_model_year_submit_check() {
	if (document.make_model_year_brand.year.value == "") { 
		alert("You must select a year to continue!");
		return false;
	}	
	return true;
}


function make_year_submit_check() {
	if (document.make_model_year_brand.year.value == "") { 
		alert("You must select a year to continue!");
		return false;
	}	
	return true;
}


function brand_submit_check() {
	if (document.brand.brand_name.value == "") { 
		alert("You must select a brand to continue!");
		return false;
	}	
	return true;
}


function part_number_submit_check() {
	if (document.part_search.part_number.value == "") { 
		alert("You must enter a part # to continue!");
		return false;
	}	
	return true;
}

function category_submit_check() {
	if (document.category.part_name.value == "") { 
		alert("You must select a category/part name to continue!");
		return false;
	}	
	return true;
}

function defaultDropDowns() {
	selected_make = document.make_model_year_brand.make_text.value;
	if (typeof document.category != "undefined") {
		selected_category = document.category.category_name.value;
	}
	if (selected_make != "") {
		get_lists();
	}
	if (typeof document.category != "undefined") { 
		if (selected_category != "") {
			get_part_lists();
		}
	}
}


function get_part_lists() { 
	if (validate_category()) {
	 	selected_category = document.category.category_name.value;
		get_part_list();
		show_part();
	} else { 
		document.category.category_name.value = selected_category;
	}
}

function get_lists() { 
	if (validate()) {
	 	selected_make = document.make_model_year_brand.make_text.value;
	 	//document.getElementById("unified_dropdown_main").setAttribute("className", "unified_main_dropdown_flipped");
	 	//document.getElementById("unified_dropdown_main").setAttribute("class", "unified_main_dropdown_flipped");
		//get_model_list();
	 	get_year_list();
	} else { 
		document.make_model_year_brand.make_text.value = selected_make;
	}
}


function get_make_list() {
	
	get_makes.method = "GET";
	get_makes.onCompletion = load_makes;
	get_makes.runAJAX();
}


function get_part_list() {
	
	param = 'category_name=' +  document.category.category_name.value.replace("&", "-and");
	get_parts.method = "GET";
	get_parts.onCompletion = load_parts;
	get_parts.runAJAX(param);
}


function get_model_list() {
	
	param = 'make_text=' + document.make_model_year_brand.make_text.value;
	get_models.method = "GET";
	get_models.onCompletion = load_models;
	get_models.runAJAX(param);
}

function get_year_list() {
	param = 'make_text=' + document.make_model_year_brand.make_text.value;
	get_years.method = "GET";
	get_years.onCompletion = load_years;
	get_years.runAJAX(param);
}

function load_parts() { 
	results = get_parts.response.split(",");
	field = document.category.part_name;
	field.options.length = 1;
	for (i=0; i<results.length; i++) {
			field.options[i+1] = new Option(results[i], results[i]);
	}
}

function load_makes() { 
	selectedMake = false;
	results = get_makes.response.split(",");
	field = document.make_model_year_brand.make_text;
	field.options.length = 1;
	
	for (i=0; i<results.length; i++) {
		field.options[i+1] = new Option(results[i], results[i]);
		if(results[i].toLowerCase().trim() == default_make.toLowerCase().trim()) { 	
			field.selectedIndex = i+1;	
			selectedMake = true;
		}
	}
	try{
		document.make_model_year_brand.year.selectedIndex = 0;
	} catch(e) {}
	if (selectedMake) { 
		get_year_list();
	}
	//document.make_model_year_brand.model_text.disabled = false;
}


function load_models() { 
	results = get_models.response.split(",");
	field = document.make_model_year_brand.model_text;
	field.options.length = 1;
	for (i=0; i<results.length; i++) {
			field.options[i+1] = new Option(results[i], results[i]);
	}
	//document.make_model_year_brand.model_text.disabled = false;
}

function load_years() { 
	results = get_years.response.split(",");
	field = document.make_model_year_brand.year;
	field.options.length = 1;
	for (i=0; i<results.length; i++) {
			field.options[i+1] = new Option(results[i], results[i]);
	}
	document.make_model_year_brand.year.disabled = false;
	show_year();
	//document.getElementById("instr").innerHTML = "<b>Step 2:</b> Next, Please Select a Year or a Model to Continue";
}

function disable_dd() {
	document.make_model_year_brand.year.disabled = true;
	document.make_model_year_brand.model_text.disabled = true;
}

function validate() { 
	if (document.make_model_year_brand.make_text.value == "") { 
		alert("You must select a make to continue!");
		return false;
	}	
	return true;
}

function validate_category() { 
	if (document.category.category_name.value == "") { 
		alert("You must select a category to continue!");
		return false;
	}	
	return true;
}

function show_year() {
	var t_element = document.getElementById("year_field");
	if (t_element) { 
		document.getElementById("year_field").setAttribute("className", "unified_dropdown");
	
		document.getElementById("year_field").setAttribute("class", "unified_dropdown");
	}
	//document.getElementById("model_field").setAttribute("className", "unified_dropdown_disabled");
	//document.getElementById("model_field").setAttribute("class", "unified_dropdown_disabled");
	//document.getElementById("model_field").setAttribute("class", "unified_dropdown_disabled");
	//document.make_model_year_brand.chc_year.checked = true;
	//document.make_model_year_brand.chc_model.checked = false;
}

function show_model() {
	var t_element = document.getElementById("model_field");
	if (tt_element) {
		document.getElementById("model_field").setAttribute("className", "unified_dropdown");
		document.getElementById("model_field").setAttribute("class", "unified_dropdown");
	}
	t_element = document.getElementById("year_field");
	if (t_element) {
		document.getElementById("year_field").setAttribute("className", "unified_dropdown_disabled");
		document.getElementById("year_field").setAttribute("class", "unified_dropdown_disabled");
	}
	//document.make_model_year_brand.chc_year.checked = false;
	//document.make_model_year_brand.chc_model.checked = true;
}


function show_part() {
	document.getElementById("part_field").setAttribute("className", "unified_dropdown");
	document.getElementById("part_field").setAttribute("class", "unified_dropdown");
}