
/* 
saves the make of vehicle to a cookie
*/
function savemake(value, desc) {
	desc = desc.replace("&amp;", "and");
	createCookie("makevalue", value, 14);
	createCookie("makedesc", desc, 14);
}

/*
saves the yesr of vehicle to a cookie
*/
function saveyear(value) {
	createCookie("year", value, 14);
}

/*
saves the model of vehicle to a cookie
*/
function savemodel(value, desc) {
	desc = desc.replace("&amp;", "and");
	createCookie("modelvalue", value, 14);
	createCookie("modeldesc", desc, 14);
}

/*
clear the make, model and year values in cookies
*/
function reset_all() {
	eraseCookie("makevalue");
	eraseCookie("makedesc");
	eraseCookie("modelvalue");
	eraseCookie("modeldesc");
	eraseCookie("year");
}

/*
makes a query string based on the variables passed to this function
*/
function createQueryString(make, year, model) {
	var pairs = new Array();
	if (make) { pairs.push("make=" + make); }
	if (year) { pairs.push("year=" + year); }
	if (model) { pairs.push("model=" + model); }
	return pairs.join("&");
}

/*
sends the browser off to the product finder, include vehicle details if available
*/
function gotoProductFinder(gotocategories) {
	var url = "/car_parts/product-finder.php";
	var makev  = readCookie("makevalue");
	var modelv = readCookie("modelvalue");
	var year   = readCookie("year");
	var querystring = createQueryString(makev, year, modelv);
	
	if (querystring) { 
		url += "?" + querystring; 
		if (gotocategories) {
			url += "#choosecategory"; 
		}
	}
	
	location.href = url;
	return false; // so that the link doesn't fire off...
}

/*
the main function loaded when the page loads
performs major functionality such as loading cookie variables, checking links to send visitors to the product finder etc...
*/
function loadFrontend() {
	// get the cookie values
	var maked  = readCookie("makedesc");
	var makev  = readCookie("makevalue");
	var modeld = readCookie("modeldesc");
	var modelv = readCookie("modelvalue");
	var year   = readCookie("year");
	var querystring = createQueryString(makev, year, modelv);
	
	// // get the frontend element
	div = document.getElementById("astoria-frontend");
	if (!div)
	{
		alert("Error: Unable to load frontend extensions");
		return false;
	}
	else
	{
		div.innerHTML = '<a name="af"></a><table width="604" align="center" border="0" cellspacing="0" cellpadding="0" style="margin:10px 0px 0px 0px;"><tr><td id="mht">&nbsp;</td></tr><tr><td id="mhm"><div id="frontend-content"></div></td></tr><tr><td id="mhb">&nbsp;</td></tr></table>';
		
		fe = document.getElementById("frontend-content");
		
		if (makev != null  &&  modelv != null  &&  year != null)
		{
			fe.innerHTML = '<a href="/car_parts/product-finder.php?make=' + readCookie("makevalue") + '&year=' + readCookie("year") + '&model=' + readCookie("modelvalue") + '#choosecategory">Return to products for "' + readCookie("year") + " " + readCookie("makedesc") + " " + readCookie("modeldesc") + '"...</a>';
		}
		else
		{
			fe.innerHTML = '<a href="/car_parts/product-finder.php">Find accessories for your vehicle...</a>';
		}
		
		sr = document.getElementById("searchresultsummary");
		if (sr)
		{
			var search_paragraphs = document.getElementById('searchresultscell').getElementsByTagName('p');
			if (search_paragraphs.length == 1)
			{
				var links = search_paragraphs[0].getElementsByTagName('a');
				if (links[0].href != "")
				{
					createCookie("searchredirect", "yes", 7);
					window.location = links[0].href;
				}
			}
		}
	}
	// end frontend div manipulation
	
	
	// redirect from a search?
	var redirected = readCookie("searchredirect");
	if (redirected == "yes")
	{
		eraseCookie("searchredirect");
		window.location.hash = "#"; // af = astoria frontend anchor
	}
	// end redirecting from a search
	
	
	// ie / ff -> different ways to add 'onclick'
	var btype = 0;
    if (window.attachEvent) { btype = 1; } // ie
	else if (window.addEventListener) { btype = 2; } // ff
	
	
	// product finder url
	/*
	var productfinderurl = "/car_parts/product-finder.php";
	if (querystring) { productfinderurl += "?" + querystring; }
	alert(productfinderurl);
	*/
	
	// pages to exclude from adjustments
	var excluded = new Array(); // links to ignore. full path is required.
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/info.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/sitemap.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/search.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/Loading_Zone_Cargo_Gate.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/AmericanVehicleParts_WeatherTech__Universal_Rubber_Cargo_Mat_1.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/AmericanVehicleParts_WeatherTech__Pet_Barrier_835.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/AmericanVehicleParts_WeatherTech__RackSack__833.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/SVR_Batteries__SVR3478DT_.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/SVR48.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/SVR_Batteries__SVR49_.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/Loading_Zone_Cargo_Gate__1101_.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/Loading_Zone_Cargo_Gate__1301_.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/Loading_Zone_Cargo_Gate__1601_.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/Loading_Zone_Cargo_Gate__1501_.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/Loading_Zone_Cargo_Gate__1302_.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/WeatherTech__Universal_Mats.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/AmericanVehicleParts_WeatherTech__Pet_Barrier__60012__836.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/AmericanVehicleParts_WeatherTech__RackSack___60001__834.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/AmericanVehicleParts_WeatherTech__Universal_Rubber_Cargo_Mat__39999__2.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/AmericanVehicleParts_WeatherTech__Universal_Rubber_Cargo_Mat__40000__3.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/AmericanVehicleParts_WeatherTech__RackSack__2096.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/AmericanVehicleParts_WeatherTech__Pet_Barrier_2094.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/SVR_Batteries.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/AmericanVehicleParts_WeatherTech__Universal_Rubber_Cargo_Mat_3745.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/AmericanVehicleParts_WeatherTech__Universal_Rubber_Cargo_Mat__39999__3746.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/AmericanVehicleParts_WeatherTech__Universal_Rubber_Cargo_Mat__40000__3747.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/AmericanVehicleParts_WeatherTech__Pet_Barrier__60012__2095.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/AmericanVehicleParts_WeatherTech__RackSack___60001__2097.html");
	excluded.push(location.protocol+"//"+location.host+"/car_parts/acatalog/AmericanVehicleParts_WeatherTech__Side_Window_Deflectors__Light__2475.html");
	var excluded_links = excluded.toString();
	//alert(excluded_links);
	
	// process /acatalog/ links
	var links = document.getElementsByTagName("a");
	//alert(links.length);
	for (i = 0; i < links.length; i++)
	{
		var pos = links[i].href.search(/\/acatalog\//)
		if (pos != -1) // link is for a file in the acatalog folder
		{ 
			if (excluded_links.search(links[i].href) == -1) // if the acatalog link isn't to be excluded... 
			{ 
				links[i].style.textDecoration='underline'; // for showing which links will be affected
				switch (btype) {
					case 1:
						links[i].attachEvent("onclick", gotoProductFinder);
						break;
					case 2:
						links[i].onclick = function() { gotoProductFinder(); return false; }; // addEventListener didn't seem to work!
						//links[i].addEventListener("click", function(){ alert(productfinderurl); location.href=productfinderurl; return false; }, false );
						break;
					default:
						//do nothing
            	} // end switch
			}// end link not excluded from onclick adjustment
		} // end link is in /acatalog/
	} // end links
}

/* REDUNDANT
function productfinderload()
{
	var maked = readCookie("makedesc");
	var makev = readCookie("makevalue");
	var modeld = readCookie("modeldesc");
	var modelv = readCookie("modelvalue");
	var year = readCookie("year");
	
	if (makev != ""  &&  modelv != ""  && year != "") {
		location.href = '/car_parts/product-finder.php?make=' + makev + '&year=' + year + '&model=' + modelv + '#choosecategory';
	}
}
*/

function getStartEnd(cat)
{
	//alert("GSE for " + cat);
	var startend = document.forms["startend" + cat].elements;
	//alert("startend length : " + startend.length);
	var se = new Array(2);
	for (i = 0; i < startend.length; i++)
	{
		//alert(startend[i].name);
		if (startend[i].name == "from")
		{
			se[0] = startend[i].value;
		}
		else if (startend[i].name == "to")
		{
			se[1] = startend[i].value;
		}
	}
	return se;
}


/* 
funciton to populate the filters in the showproducts page
*/
function getFilters(cat)
{
	s = document.getElementById("span" + cat);
	
	startend = getStartEnd(cat);
	//alert("GF s/e = " + startend[0] + "/" + startend[1]);
	
	s.innerHTML = "";
	
	// get the inputs
	inputs = new Array();
	for (i = startend[0]; i <= startend[1]; i++)
	{
		elements = document.forms["formrow" + i].elements;
		for (e = 0; e < elements.length; e++)
		{
			if (elements[e].type == "hidden")
			{
				//document.write("input = " + elements[e].name + "/" + elements[e].value);
				inputs.push(elements[e]);
			}
		}
	}
	//alert("GF inputs = " + inputs.length);
	
	// got the inputs, find all filters
	var filters = new Array();
	for (i = 0; i < inputs.length; i++)
	{
		var found = false;
		for (j = 0; j < filters.length; j++)
		{
			if (inputs[i].name == filters[j])
			{
				found = true;
				break;
			}
		}
		if (!found)
		{
			filters.push(inputs[i].name);
		}
	}
	//alert("GF filters = " + filters.length);
	
	// got the filters, create select elements
	for (i = 0; i < filters.length; i++)
	{
		// changed!
		//s.innerHTML += "<select id=\"" + filters[i] + cat + "\" name=\"" + filters[i] + "\" style=\"margin-right:10px\" onchange=\"if (this.selectedIndex > 0) { filterCategory('" + cat + "'); } else { resetCategory('" + cat + "'); }\"></select>";
		s.innerHTML += "<select id=\"" + filters[i] + cat + "\" name=\"" + filters[i] + "\" style=\"margin-right:10px\" onchange=\"filterCategory('" + cat + "');\"></select>";
		
		// get a unique list of options for a filter
		var opts = new Array();
		for (j = 0; j < inputs.length; j++)
		{
			//document.write("inputj" + j + ".value = " + inputs[j].value + " ");
			if (inputs[j].name == filters[i])
			{
				var found = false;
				for (k = 0; k < opts.length; k++)
				{
					if (inputs[j].value == opts[k])
					{
						found = true;
						break;
					}
				}
				if (!found)
				{
					opts.push(inputs[j].value);
				}
			}
		}
		
		// sort the array of options...
		opts.sort();
		
		// get the select for the filter
		sel = document.getElementById(filters[i] + cat);
		
		// first option...
		o = document.createElement('option');
		if (filters[i].substring((filters[i].length - 1)) == "s")
		{
			// already ends with an s, don't add it again!
			o.text = "-- All " + filters[i] + " --";
		}
		else
		{
			// no e, go ahead
			o.text = "-- All " + filters[i] + "s --";
		}
		o.value = "";
		try
		{
			sel.add(o,null); // standards compliant
		}
		catch(ex)
		{
			sel.add(o); // IE only
		}
		
		// options...
		for (j = 0; j < opts.length; j++)
		{
			o = document.createElement('option');
			o.text = opts[j];
			o.value = opts[j];
			try
			{
				sel.add(o,null); // standards compliant
			}
			catch(ex)
			{
				sel.add(o); // IE only
			}
		}
	}
	
	if (filters.length < 1) // no filters
	{
		document.getElementById("filtertablerow" + cat).style.display = "none";
	}
}

/*
filters a category on the showproducts page
*/
function filterCategory(cat)
{
	//alert("filtering...");
	resetCategory(cat);
	
	// get the selects
	selects = document.forms["selects" + cat].elements;
	//alert(selects.length + " selects found");
	
	var filters = new Array();
	
	for (i = 0; i < selects.length; i++)
	{
		if (selects[i].type == "select-one")
		{
			if (selects[i].selectedIndex != 0) // if a filter has been chosen
			{
				filters.push( new Array(selects[i].name, selects[i].value) );
			}
		}
	}
	
	//alert("Filters = " + filters.toString());
	
	var startend = getStartEnd(cat);
	//alert(startend[0] + " " + startend[1]);
	
	var notmatching = 0;
	// go through each row
	for (i = startend[0]; i <= startend[1]; i++)
	{
		//document.write(i + " " + " formrow" + i);
		elements = document.forms["formrow" + i].elements;
		
		var matches = true;
		// foreach filter
		for (f = 0; f < filters.length; f++)
		{
			// go through the elements
			for (e = 0; e < elements.length; e++)
			{
				// if the row has the filter, does it match?
				if (elements[e].name == filters[f][0])
				{
					if (elements[e].value != filters[f][1])
					{
						matches = false;
						notmatching++;
					}
					//alert(elements[e].value + " " + filters[f][1] + " matches = " + matches.toString());
				}
			}
		}

		if (matches == false)
		{
			document.getElementById("tablerow" + i).style.display = "none";
			//document.getElementById("tablerow" + cat).className = "blackborder";
		}
		
	}
}

/*
resets a category on the showproducts page
*/
function resetCategory(cat)
{
	// get the rows
	startend = getStartEnd(cat);
	
	for (i = startend[0]; i <= startend[1]; i++)
	{
		document.getElementById("tablerow" + i).style.display = "";
	}
}

