
var wheel_search_fields=Array("Wheel_Dia","Wheel_Bolt");
var tire_search_fields=Array("AspectRati","RimDiamete","SectionWit");

var keep_out_list;

var id_prefix = "custom_field_";

var cookie_name = "search_type";
var expiry_in_days = 30;//in days


var tires_by_vehicle = "tires_by_vehicle";
var wheels_by_size = "wheels_by_size";
var tires_by_size = "tires_by_size";

function search(search_type)
{
	var url = base_index_url + "/browse/search";
	//browse_filter_string = browse_filter_string.replace(/id/, "");
	//browse_filter_string = browse_filter_string.replace("/", "");
	//alert(browse_filter_string);
	//if(browse_type == "category" && browse_filter_string != "")
	//{
		//url += "/browse/search/product_category_id/" + browse_filter_string;
	//}
	var query_string = "";
	var id = "";

	if(search_type=="wheels_by_size")
	{
		keep_out_list = tire_search_fields;

		var el = document.getElementById("usage_ids");
		var us = null;
		if(el){
			us = el.value;
		}
		if(us != null && us != "")
		{
			query_string = "/must_have_usages/"+us;
		}
		
	}
	else if(search_type == "tires_by_size")
	{
		keep_out_list = wheel_search_fields;
	}

	//get values for each of the custom field
	for(var i = 0; i < custom_field_list.length; i++)
	{
		var use_field = true;
		for(var j = 0; j < keep_out_list.length; j++)
		{
			if(custom_field_list[i] == keep_out_list[j])
			{
				use_field = false;
			}
		}

		if(use_field)
		{
			//product_custom_field[SectionWit]/175
			//get value for each and add to query string
			id = id_prefix + i;
			select = document.getElementById(id);
			if(select != null)
			{
				index = select.selectedIndex;
				if(index != 0)
				{
					//only use the option if its not = 0
					option = select.options[index];
					value = option.value;
					if(value != "")
					{
						//only use the value in the search if its not empty
						query_string += "/product_custom_field[" + custom_field_list[i] + "]/" + value;
					}
				}
			}
		}
	}

        query_string+="/search_type/"+search_type ;
	//alert(url + query_string);
	document.location = url + query_string;
}

function set_search_cookie(search_type)
{
	if(search_type == "")
	{
		search_type = tires_by_vehicle;
	}
	//sets a cookie for the search type
	//when you load the page again it will load the last used search
	var expires = new Date();
	expires.setDate(expires.getDate() + expiry_in_days);//expires in 30 days
	document.cookie= cookie_name+"="+escape(search_type)+";expires="+expires.toUTCString();
	return;
}

function load_initial_search()
{
	//get value from cookie (if it exists)
	var cookie_start = -1;
	var cookie_end = -1;
	var search_type = "";
	if(document.cookie.length > 0)
	{
		cookie_start = document.cookie.indexOf(cookie_name+"=");
		if(cookie_start != -1)
		{
			cookie_start = cookie_start + cookie_name.length+1;
			//alert("asdfasdfasdfasdfasdfsadfasdasdfsdaf\nasdfasdfasdf\n"+cookie_name);
			cookie_end = document.cookie.indexOf(";", cookie_start);
			if(cookie_end == -1)
			{
				cookie_end = document.cookie.length;
			}
			search_type = unescape(document.cookie.substring(cookie_start, cookie_end));
		}
	}
	if(search_type != "")
	{
		if(search_type == tires_by_size || search_type == tires_by_vehicle || search_type == wheels_by_size)
		{
			//change_search(search_type);
			//alert("good search\n"+search_type);
			var to_hide = document.getElementsByClassName("search");
			//hide these
			for(i=0; i < to_hide.length; i++)
			{
				YAHOO.util.Dom.setStyle(to_hide[i], "display", "none");
			}
			var to_show = document.getElementsByClassName(search_type);
			//show these elements
			for(i=0; i < to_show.length; i++)
			{
				YAHOO.util.Dom.setStyle(to_show[i], "display", "block");
			}
		}
		else
		{
			//alert("invalid search selection");
		}
	}
	else
	{
		//no cookie set so load the search by vehicle
		//change_search(tires_by_vehicle);
		//alert("default search");
	}
}

function load_search(search_type, category_id)
{
	set_search_cookie(new_search);
	document.location = base_index_url + "/browse/category/id/" + category_id;
	return;
}


function change_search(new_search)
{
	/*
	index = new_search.toString().search(/wheel/i);
	if(index != -1)
	{
		//redirect to Tires category
		load_search(new_search, "WHEELS");
		return;
	}

	index = new_search.toString().search(/tire/i);
	if(index != -1)
	{
		//redirect to Tires category
		load_search(new_search, "TIRES");
		return;
	}
	return;
	*/
	//change_search(search_type);
	var to_hide = elByClass("search");
	//hide these
	for(i=0; i < to_hide.length; i++)
	{
		YAHOO.util.Dom.setStyle(to_hide[i], "display", "none");
	}
	var to_show = elByClass(new_search);
	//show these elements
	for(i=0; i < to_show.length; i++)
	{
		YAHOO.util.Dom.setStyle(to_show[i], "display", "block");
	}
}

/*
YAHOO.util.Event.onDOMReady(function(){
	change_search("tires_by_vehicle");
});
*/
function elByClass(cl)
{
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = document.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)){
			retnode.push(elem[i]);
		}
	}
	return retnode;
}

document.c = function(cl) {
var retnode = [];
var myclass = new RegExp('\\b'+cl+'\\b');
var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++) {
var classes = elem[i].className;
if (myclass.test(classes)) retnode.push(elem[i]);
}
return retnode;
}; 
