var controller_url="/custom_controllers/search_by_vehicle";

function add_opt_to_select(opt, sel)
{
	if(opt != null && sel != null){
		try{
			sel.add(opt,null);
		}
		catch(ex)
		{
			sel.add(opt);
		}
	}
}


function clear_select( sel)
{
    sel.options.length = 0;
}

function update_makes(o){
	if(o.responseText !== undefined){
		var data = YAHOO.lang.JSON.parse(o.responseText); 
		
		var make_select =document.getElementById("tg_make");
                clear_select(make_select);
                
		var opt = document.createElement('option');
		opt.text = " -- Select Make -- ";
		opt.value="";
		add_opt_to_select(opt, make_select);

		for(i=0; i<data.length;i++)
		{
			opt = document.createElement('option');
			opt.text = " " + data[i].make + " ";
			opt.value = data[i].make;
			add_opt_to_select(opt,make_select);
		}
                if(o.argument.def_value){
                    make_select.value=o.argument.def_value;
                }
                
		/*

		var options = '<option value=""> -- Select Make -- </option>';
		
		for(i=0;i<data.length;i++){
			//alert(data[i].make);
			options+='<option value="' + data[i].make + '"> ' + data[i].make + ' </option>'
		}
		
		make_select.innerHTML = options;
		*/
		
	}
}

function update_models(o){
	if(o.responseText !== undefined){
		var data = YAHOO.lang.JSON.parse(o.responseText); 
		
		var model_select =document.getElementById("tg_model");
                clear_select(model_select);
		

		var opt = document.createElement('option');
		opt.text = " -- Select Model -- ";
		opt.value="";
		add_opt_to_select(opt, model_select);
		
		for(i=0; i<data.length;i++)
		{
			opt = document.createElement('option');
			opt.text = " " + data[i].model + " ";
			opt.value = data[i].model;
			add_opt_to_select(opt,model_select);
		}
                if(o.argument.def_value){
                    model_select.value=o.argument.def_value;
                }
		/*
		var options = '<option value=""> -- Select Model -- </option>';
		for(i=0;i<data.length;i++){
			options+='<option value="' + data[i].model + '"> ' + data[i].model + ' </option>'
		}
		
		model_select.innerHTML = options;
		*/
	}
}

function do_custom_field_search(value)
{
	//value contains the custom field search criteria
	var url = base_index_url + "/browse/search/" + value;
	document.location = url;
}

function get_usage_id_from_name(split_name)
{
    var found = false;
    var i = 0;
    var returnVal;
    var name = split_name.split(" ");
    name = name[0];
    for(i = 0; i < usage_array.length && !found; i++)
    {
        if( usage_array[i][1].toUpperCase() == name.toUpperCase())
        {
            found = true;
            returnVal = usage_array[i][0];
        }
    }

    if(found)
    {
        return returnVal;
    }
    else
    {
        return null;
    }
}

function list_options(o){
	if(o.responseText !== undefined){
		var data = YAHOO.lang.JSON.parse(o.responseText); 
		//var options_box = document.getElementById('tg_options');
		options_box = document.getElementById('tg_trim');
                clear_select(options_box);

		var opt = document.createElement('option');
		opt.text = " -- Select Trim -- ";
		opt.value="";
		add_opt_to_select(opt, options_box);

		for(i=0; i<data.length;i++)
		{
			opt = document.createElement('option');
			//opt.text = " " + data[i].standard_or_option + " -- " + data[i].name + " ";
			opt.text = " " + data[i].name + " ";
			search_value = "";
                        
                        if(o.argument.wheel){
                            //product_custom_field[Wheel_Bolt]/5*112/product_custom_field[Wheel_Dia]/14
                            search_value += "product_custom_field[Wheel_Bolt]/" + data[i].bolt_pattern + "/";
                            search_value += "product_custom_field[Wheel_Dia]/" + data[i].rim + "/";
                            usage_id = get_usage_id_from_name(data[i].Make)
                            if(usage_id != null)
                            {
                                search_value += "must_have_usages/" + usage_id + "/";
                            }
                            search_value += "search_type/wheels_by_vehicle/";

                        }else{
                            search_value += "product_custom_field[AspectRati]/" + data[i].aspect_ratio + "/";
                            search_value += "product_custom_field[RimDiamete]/" + data[i].rim + "/";
                            search_value += "product_custom_field[SectionWit]/" + data[i].section_width + "/";
                            search_value += "search_type/tires_by_vehicle/";
                        }
			//example url to follow:
			// index.php/browse/search/custom_fields%5BAspectRati%5D/1050/custom_fields%5BRimDiamete%5D/13/custom_fields%5BSectionWit%5D/19

			/*options += "<tr><td><a href=" + base_index_url + "/browse/search/product_custom_field[AspectRati]/" + data[i].aspect_ratio + "/product_custom_field[RimDiamete]/" + data[i].rim + "/product_custom_field[SectionWit]/" + data[i].section_width +">" + data[i].name + "</a> </td>";
			options += "<td>" + data[i].standard_or_option + "</td>";
			options += "<td>" + data[i].tire_size + "</td>";
			options += "<td>" + data[i].section_width + "</td>";
			options += "<td>" + data[i].aspect_ratio + "</td>";
			options += "<td>" + data[i].rim + "</td> </tr>";*/
			//options += "<option value='" + search_value + "'>" + data[i].standard_or_option + " -- " + data[i].name + "</option>";
			opt.value = search_value;
			add_opt_to_select(opt,options_box);
		}
                if(o.argument.def_value){
                    options_box.value=o.argument.def_value;
                }

		/*
		//var options = '<table><tr><th> Option </th> <th> Std or Opt </th> <th> Tire Size </th> <th> Section Width </th><th>Aspect Ratio</th> <th> Rim </th></tr>';
		var options = '<option value=""> -- Select Trim --</option>';
		var search_value = "";
		if(data != null){
			for(i=0;i<data.length;i++){
				//example url to follow:
				// index.php/browse/search/custom_fields%5BAspectRati%5D/1050/custom_fields%5BRimDiamete%5D/13/custom_fields%5BSectionWit%5D/19

				/*options += "<tr><td><a href=" + base_index_url + "/browse/search/product_custom_field[AspectRati]/" + data[i].aspect_ratio + "/product_custom_field[RimDiamete]/" + data[i].rim + "/product_custom_field[SectionWit]/" + data[i].section_width +">" + data[i].name + "</a> </td>";
				options += "<td>" + data[i].standard_or_option + "</td>";
				options += "<td>" + data[i].tire_size + "</td>";
				options += "<td>" + data[i].section_width + "</td>";
				options += "<td>" + data[i].aspect_ratio + "</td>";
				options += "<td>" + data[i].rim + "</td> </tr>";*/
		/*
				search_value = "";
				search_value = "product_custom_field[AspectRati]/" + data[i].aspect_ratio + "/";
				search_value += "product_custom_field[RimDiamete]/" + data[i].rim + "/";
				search_value += "product_custom_field[SectionWit]/" + data[i].section_width + "/";
				
				options += "<option value='" + search_value + "'>" + data[i].standard_or_option + " -- " + data[i].name + "</option>";
			}
		}
		else
		{
			options += "<tr><td colspan='60'>";
			options += "No Results Found";
			options += "</td></tr>";
		}
		options += "</table>";
		options_box.innerHTML = options;
		*/

	}
}

function handle_error(o){
	if(o.responseText !== undefined){
		alert("error loading " + o.argument.data + ", please re-try!")
	}
}


function get_makes_by_year(year, deflt){
	var callback =
	{
		success:update_makes,
		failure:handle_error,
		argument: { data:"makes", def_value:deflt}
	};
	YAHOO.util.Connect.asyncRequest('GET', base_index_url + controller_url + "/get_makes_by_year/" +year, callback);
}

function get_model_by_year_make(year, make, deflt){
	var callback =
	{
		success:update_models,
		failure:handle_error,
		argument: { data:"models", def_value:deflt}
	};
	
	YAHOO.util.Connect.asyncRequest('GET', base_index_url + controller_url + "/get_model_by_year_make/" +year + "/" + make, callback);
}

function get_selected_options(year, make, model, deflt, is_wheel){
	var callback =
	{
		success:list_options,
		failure:handle_error,
		argument: { data:"options", def_value:deflt, wheel:is_wheel }
	};
	
	YAHOO.util.Connect.asyncRequest('GET', base_index_url + controller_url + "/get_options/" +year + "/" + make + "/" + model.replace('/', '~~'), callback);
	
}
