/* assumes all restricts are select elements whose initial values are an empty string or 0 */
var filter = {
	selects: [ // 'id' should match the attribute of the same name in the markup and 'key' specifies the key portion of the key/value pair in the query string
		{ id: "restrict_month", key: "month" },
		{ id: "restrict_year", key: "year" },
		{ id: "issues-list", key: "rid" },
		{ id: "session_select", key: "c" },
		{ id: "county-restrict", key: "county" },
		{ id: "region-restrict", key: "region" }
	],

	query: null,

	exists: function(obj) {
		return !!(obj || obj === 0);
	},

	evaluate: function(member) {
		var select = document.getElementById(member.id);
		if (this.exists(select)) {
			var val = select.options[select.options.selectedIndex].value
			if (val != "" && val != 0) { this.query.push({ "key": member.key, "val": val }); }
		}
	},

	construct_query: function() {
		var q = "";
		for (var x = 0; x < this.query.length; x++) {
			if (x == 0) { q += "?" } else { q += "&"; }
			q += this.query[x].key + "=" + encodeURIComponent(this.query[x].val);
		}

		return q;
	},
	
	run: function(page) {
		this.query = [];
		
		for (var x = 0; x < this.selects.length; x++) {
			this.evaluate(this.selects[x]);
		}

		window.location.href = document.location.pathname + this.construct_query();
	}
};

