
var c_year = "Chọn năm";
var c_brand = "Chọn nhà sản xuất";
var c_model = "Chọn tên xe";
var c_serie = "Chọn dòng xe";

function select_type_search(obj, val) {
	$obj = obj.parentNode.getElementsByTagName('div');
	for (i =0; i < $obj.length ; i++) {
		if($obj[i].className == 'cate-title' || $obj[i].className == 'cate-title active')
			$obj[i].className = 'cate-title';
	}
	obj.className = 'cate-title active';
	$('typesearch').value = val;
}

function clear_combo(obj){
		var i=0;
		for (i=0; i < obj.options.length; i++) {
			obj.options[i] = null;
		}
		obj.length = 0;
	}

	function fill_data_2_combo(obj,sdata, def){
		clear_combo(obj);
		var txts = sdata.split(',');
		var i=0;
		opt = new Option();
		opt.value = ""; 
		opt.text = def;
		obj.options[i]=opt;
		for(i=1;i<=txts.length;i++){
			opt = new Option();
			opt.value = txts[i-1]; 
			opt.text = txts[i-1];
			obj.options[i]=opt;
		}

	}

	function fill_data_2_combo_arr(obj,sdata, def) {
		clear_combo(obj);
		var i=0;
		opt = new Option();
		opt.value = ""; 
		opt.text = def;
		obj.options[i]=opt;
		
		for(i=1;i<=sdata.length;i++){
			opt = new Option();
			opt.value = sdata[i-1]; 
			opt.text = sdata[i-1];
			obj.options[i]=opt;
		}
	}

	function do_change(o_current,o_next, o_text){

		if (o_text == "year") {
			fill_data_2_combo(o_next, year[o_current.options[o_current.selectedIndex].value] ,  "Chọn nhà sản xuất");
		}
		else if (o_text == "brand")
		{
			fill_data_2_combo($('searchserie'), brand2serie[o_current.options[o_current.selectedIndex].value] ,  "Chọn dòng xe");
			fill_data_2_combo($('searchmodel'), brand2model[o_current.options[o_current.selectedIndex].value] , "Chọn tên xe");
		}
	}

	function init_auto_compare(obj,o_load_next, o_text){
		obj.onchange = function(o){
			do_change(obj, o_load_next, o_text);
		}
	}

	function motor_do_change(o_current,o_next, o_text) {
		if (o_text == "year") {
			fill_data_2_combo(o_next, motor_year[o_current.options[o_current.selectedIndex].value], "Chọn nhà sản xuất");
		}
		else if (o_text == "brand") {
			fill_data_2_combo($('msearchserie'), motor_brand2serie[o_current.options[o_current.selectedIndex].value], "Chọn dòng xe");
			fill_data_2_combo($('msearchmodel'), motor_brand2model[o_current.options[o_current.selectedIndex].value], "Chọn tên xe");
		}
	}

	function init_motor_compare(obj,o_load_next, o_text) {
		obj.onchange = function(o) {
			motor_do_change(obj, o_load_next, o_text);
		}
	}

	function change_search_display(name) {
	if (name == 'tk_auto')
	{
		$('tk_auto').style.display = "";
		$('tk_motor').style.display = "none";
	}
	else
	{
		$('tk_auto').style.display = "none";
		$('tk_motor').style.display = "";
	}
}

function search_giaxe() {
	var $url = "";
	switch (parseInt($('typesearch').value)) {
		case 0:
			$url = '/banggia/trongnuoc/?' +processSearch();
			break;
		case 1:
			$url = '/banggia/nhapkhau/?' +processSearch();
			break;
		case 2:
			$url = '/banggia/?' +processSearch();
			break;
		case 3:
			$url = '/banggia/xemay/?' +processSearchMoto();
			break;
	}
	location.href = $url ;
}


function processSearch() {
	var $url = "";
	if ($('searchyear').value != "") {
		$url += '&ty=year&ky=' + $('searchyear').value;
	}

	if ($('searchbrand').value != "") {
		$url += '&tb=brand&kb=' + $('searchbrand').value;
	}

	if ($('searchmodel').value != "") {
		$url += '&tm=model&km=' + $('searchmodel').value;
	}

	if ($('searchserie').value != "") {
		$url += '&ts=serie&ks=' + $('searchserie').value;
	}
	return $url;
}

function processSearchMoto() {
	var $url = "";
	if ($('msearchyear').value != "") {
		$url += '&ty=year&ky=' + $('msearchyear').value;
	}

	if ($('msearchbrand').value != "") {
		$url += '&tb=brand&kb=' + $('msearchbrand').value;
	}

	if ($('msearchmodel').value != "") {
		$url += '&tm=model&km=' + $('msearchmodel').value;
	}

	if ($('msearchserie').value != "") {
		$url += '&ts=serie&ks=' + $('msearchserie').value;
	}
	return $url;
}
