function show_years() {
// purpose: prepopulates year selection combo box when the year is present
	if (document.getElementById) {
		var yearX	=(window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
		
		//year
		if (yearX) {
			yearX.onreadystatechange = function() {
				if (yearX.readyState==4 && yearX.status==200) {
					var el=document.getElementById("year-box");
					el.innerHTML=years_combo_box(yearX.responseText,"year","");
				}
			}
			yearX.open("GET", "/uniroyal/tire-selector/data/ymmo/years.txt", true);
			yearX.send(null);
		}
	}
}

function years_combo_box(text,name,value) {
// purpose: generates the lists of options for the vehicle year combo box
	var o='<select id="toolform" name="'+name+'" onchange="document.vehicle.submit()">';
	if (text.indexOf("|")>0) {
    	var r=text.split("||");
		if (r.length>1) {
			o += '\n<option value="-">Please select</option>';
			for (var i=0; i<r.length; i++) {
				o += '\n<option value="'+r[i]+'"';
				if (filter(r[i])==filter(value)) o += ' selected="selected"';
				o += '>'+r[i]+'</option>';
			}
		}
	}else{
		o += '\n<option value="'+text+'">'+text+'</option>';
	}
	o += '</select>';
	return o;
}

function filter(v) {
// purpose: converts the specified value to lower case and replaces non-alphanumeric characters with underscores
	if (v) v=v.toLowerCase().replace(/[^a-zA-Z0-9]/g, "_");
	return v;
}

if (window.addEventListener) {
	window.addEventListener("load", show_years, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", show_years);
}
