
function updateCriteria() {
	var filterset = new FilterSet();
	//filterset.styles = document.getElementById("filter_styles"); 
	filterset.colors = document.getElementById("filter_colors");
	filterset.materials = document.getElementById("filter_materials");
	filterset.shapes = document.getElementById("filter_shapes");
	var brand = document.getElementById("brand").value;
	var dept = document.getElementById("department").value;
	var uri = "NarrowCriteria.action?brand=" + escape(brand) 
		+ "&department=" + escape(dept);
	filterset.adjustFilterData(uri);
}

function FilterSet(styles, colors, materials, shapes) {
	this.styles = styles;
	this.colors = colors;
	this.materials = materials;
	this.shapes = shapes;
}

function FilterSet(){
	
}

FilterSet.prototype.adjustFilterData = function(baseURL) {
	var url = baseURL /* + this.getOptionParam("style", this.styles) no styles now  */  
		+ this.getOptionParam("color", this.colors)
		+ this.getOptionParam("material", this.materials)
		+ this.getOptionParam("shape", this.shapes);
	requestData(url, this);
}

FilterSet.prototype.handleResponseText = function(xmlReq, responseText) {
	//alert("Received response " + responseText);
	var criteria = JSON.parse(responseText);
	//this.setDropdownOptions("Styles", this.styles, criteria.styles);
	this.setDropdownOptions("Colors", this.colors, criteria.colors);
	this.setDropdownOptions("Materials", this.materials, criteria.materials);
	this.setDropdownOptions("Shapes", this.shapes, criteria.shapes);
}

FilterSet.prototype.setDropdownOptions = function(name, dropdown, valarray) {
	var selectedval = dropdown.options[dropdown.selectedIndex].value;
	dropdown.options.length = 0
	valarray.sort(); 
	
	this.addOption(dropdown, "-All " + name + "-", "")
	
	for (var i = 0; i < valarray.length; i++) {
		this.addOption(dropdown, valarray[i].value, valarray[i].key)

		if (valarray[i].key == selectedval) {
			dropdown.selectedIndex = i+1;
		}
	}
}

FilterSet.prototype.addOption = function(dropdown, text, value) {
	var oOption = document.createElement("option");
	oOption.text=text;
	oOption.value=value;
	if (navigator.appName == "Microsoft Internet Explorer") {
		dropdown.add(oOption)
	} else {
		dropdown.add(oOption, null)
	}
}

FilterSet.prototype.handleHttpFailure = function(xmlReq) {
	//alert("HTTP call failed: " + xmlReq.status);
}

FilterSet.prototype.getOptionParam = function(optionName, selectElement) {
	if (selectElement == undefined){
		return "";
	}
	if (selectElement.value.length > 0 && selectElement.value != "-1") {
		return "&" + optionName + "=" + encodeURIComponent(selectElement.value);
	} 
	return "";
}