function toggleMoreFilters (the_button, filter_type, is_neo) {
	//alert('x');
	if ((! filter_type) || (! the_button)) {
		return false;
	}
	if ((! is_neo) && (filter_type == "date_code")) {
		var n = document.getElementById("theNextButton__date_code");
		var p = document.getElementById("thePrevButton__date_code");
		
		var np = document.getElementById("theCalNextPrev");	
		
		if (the_button == "start") {
			
			if (cal_months.length > 1) {
				if (np != null) np.style.display = "table";
				if (n != null) n.style.display = "inline";
				if (p != null) p.style.display = "inline";
				
				var y = 0;
				while(y < cal_months.length) {
					if (cal_months[y] == cal_today) {
						next = cal_months[y+1];
						prev = cal_months[y-1];
						if (next != null) {
							n.name = next;
							n.innerHTML = next.substring(4)+" &raquo;";
							n.style.display = "inline";
						} else {
							n.innerHTML = "no";
							n.name = "no";
							n.style.display = "none";
						}
						if (prev != null) {
							p.name = prev;
							p.innerHTML = "&laquo; "+prev.substring(4);
							p.style.display = "inline";
						} else {
							p.innerHTML = "no";
							p.name = "no";
							p.style.display = "none";
						}
					}
					y++;
				}
			}
			
			var f = getElementsByClassName('listingsCalMonth');
			var x = 0;
			while(x < f.length) {
				if (f[x].parentNode.className == "deadMonth")  f[x].style.display = "none";
				x++;
			}
		} else {
			var the_name = the_button.name
			var f = getElementsByClassName('listingsCalMonth');
			var x = 0;
			
			while(x < f.length) {		
				if (f[x].id == the_button.name) {
					f[x].style.display = "table";
					f[x].parentNode.className = "activeMonth";
				} else {
					f[x].style.display = "none";
					f[x].parentNode.className = "deadMonth";
				}
				x++;
			}
			
			var y = 0;
			while(y < cal_months.length) {
				if (cal_months[y] == the_name) {
					next = cal_months[y+1];
					prev = cal_months[y-1];
					if (next != null) {
						n.name = next;
						n.innerHTML = next.substring(4)+" &raquo;";
						n.style.display = "inline";
					} else {
						n.innerHTML = "no";
						n.name = "no";
						n.style.display = "none";
					}
					if (prev != null) {
						p.name = prev;
						p.innerHTML = "&laquo; "+prev.substring(4);
						p.style.display = "inline";
					} else {
						p.innerHTML = "no";
						p.name = "no";
						p.style.display = "none";
					}
				}
				y++;
			}
			
		}
		return false;
	}

	//First: toggle between Fewer and More buttons
	if (the_button.className == "moreButton") {
		var new_disp = "block";
		the_button.className = "fewerButton";
		
		var new_type = "Fewer";
		var img_type = "fewer";
		if (is_neo) img_type = "open";
		the_button.href = "#" + new_type;
		
	} else {
		var new_disp = "none";
		the_button.className = "moreButton";
		var new_type = "More";
		var img_type = "closed";
		the_button.href = "#" + new_type;
	}
	var ta = the_button.innerHTML.split(" ");
	
	var new_thing = '<img src="/assets/images/' + img_type + '8.png"> ';
	var x = 3; 
	if (! is_neo) new_thing = new_thing + new_type + " ";
	else x = 2;
	
	while(x < ta.length) {
		//alert(new_thing);
		new_thing = new_thing + ta[x] + " ";
		x++;
	}
	//alert(new_thing);
	the_button.innerHTML = new_thing;
	
	//Now: collect the links to be shown or hidden 
	var f = getElementsByClassName(filter_type + '_hide');
	x = 0; 
	if (new_disp == 'block') var new_class = 'filter';
	else var new_class = 'deadFilter';
	while(x < f.length) {
		f[x].style.display = new_disp;
		f[x].parentNode.className = new_class;
		x++;
	}
	return false;
}

function toggleAllMoreFilters() {
	var i = 0;
	while (i < filter_types.length) {
		var m = document.getElementById("theMoreButton__" + filter_types[i]);
		if (m != null) m.style.display = "inline";
		if (filter_types[i] == "date_code") m = "start";
		toggleMoreFilters(m, filter_types[i]);
		
		i++;
	}
}

var process_listings_response = function (xml) {
	// only if req shows "loaded"
	if (xml.readyState == 4) {
		// only if "OK"
		if ((xml.status == 200) && (xml.responseXML)) {
			var items = xml.responseXML.getElementsByTagName("column");
			var column = get_xml_text_node(items[0]);
			var column_spot = document.getElementById("theListingsColumnSpot");
			if (column_spot != null) column_spot.innerHTML = column;
			
			items = xml.responseXML.getElementsByTagName("bar");
			var bar = get_xml_text_node(items[0]);
			var bar_spot = document.getElementById("theBarOfBarsSpot");
			if (bar_spot != null) bar_spot.innerHTML = bar;
			
			var searchy = document.getElementById("theListingsSearcher");
			if (searchy != null) searchy.style.display = "none";
			
			toggleAllMoreFilters();
			
			if (megaMapInit != null) megaMapInit();
			
		} else {
			alert("Error " + xml.status + "\n" + xml.responseText);
		}
	}
	return;
}

var handle_wire_maintenance = function (xml) {
	// only if req shows "loaded"
	if (xml.readyState == 4) {
		// only if "OK"
		if ((xml.status == 200) && (xml.responseXML)) {
			return;
		} else {
			alert("Error " + xml.status + "\n" + xml.responseText);
		}
	}
	return;
}

function changeFilters (query) {
	if (! query) query = "?listings_type=" + listings_type;
	else query = query + "&listings_type=" + listings_type;
	//alert(query);
	
	get_xml_doc("/assets/scripts/lijax.x" + query, process_listings_response);
	return false;
}

function changeWires () {
	//alert('d');
	get_xml_doc("/assets/scripts/wire.x", handle_wire_maintenance);
	return false;
}

function addPluralField (button) {
	var n = button.name;
	
	var d = document.getElementById("newfields__" + n);
	var div = document.createElement('div');
	
	if (n == 'manytimes') {
		div.innerHTML = 'Date (M/D/Y): <input type="text" name="manytimes__month[]" value="" size="3" style="width:1.8em"> / <input type="text" name="manytimes__day[]" value="" size="3" style="width:1.8em"> / <input type="text" name="manytimes__year[]" value="" size="5" style="width:3em"> Time: <input type="text" name="manytimes__time[]" value="" size="7"><br>';
		/*
		div.appendChild(document.createTextNode('Date (M/D/Y): '));
		
		var input1 = document.createElement('input');
		input1.type = "text";
		input1.size = "3";
		input1.style.width = "1.8em";
		input1.name = "manytimes__month[]";
		div.appendChild(input1);
		
		div.appendChild(document.createTextNode(' / '));
		
		var input2 = document.createElement('input');
		input2.type = "text";
		input2.size = "3";
		input2.style.width = "1.8em";
		input2.name = "manytimes__day[]";
		div.appendChild(input2);
		
		div.appendChild(document.createTextNode(' / '));
				
		var input3 = document.createElement('input');
		input3.type = "text";
		input3.size = "5";
		input3.style.width = "3em";
		input3.name = "manytimes__year[]";
		div.appendChild(input3);
		
		div.appendChild(document.createTextNode(' Time: '));
		
		var input4 = document.createElement('input');
		input4.type = "text";
		input4.size = "7";
		input4.name = "manytimes__time[]";
		div.appendChild(input4);
		
		div.appendChild(document.createElement("<br>"));*/
		
	} else {
		div.innerHTML = '<input type="text" name="' + n + '[]" value=""><br>';		
		/*var input = document.createElement('input');
		input.type = "text";
		input.name = n + '[]';
		div.appendChild(input);
		div.appendChild(document.createElement("<br>"));*/
	}
	d.appendChild(div);
	
	return true;
}

function checkSubmit (canonical_name, month, day, year) {
	if (canonical_name == null) return true;
	
	if (canonical_name.value == '') {
		alert("Please fill in a "+canonical_name.name+" for this entry.");
		return false;
	}
	if (month == null) return true;
	if (day == null) return true;
	if (year == null) return true;
	
	if ((month.value == '') || (day.value == '') || (year.value == '')) {
		alert("Please fill in a date for this entry.");
		return false;
	}
}

function variety_swap (newstate) {
	var theEndDate = document.getElementById("theEndDate");
	var theStartDate = document.getElementById("theStartDate");
	var manytimes = document.getElementById("plural__manytimes");
	var weeklyboxes = document.getElementById("weekly_boxes");
	var theDateName = document.getElementById("theDateName");

	switch (newstate) {
		case "weekly":
			if (theEndDate) theEndDate.style.display = 'block';
			if (theStartDate) theStartDate.style.display = 'block';
			if (manytimes) manytimes.style.display = 'none';
			if (weeklyboxes) weeklyboxes.style.display = 'block';
			if (theDateName) theDateName.innerHTML = 'Start';
			break;
			
		case "Monday":
		case "Tuesday":
		case "Wednesday":
		case "Thursday":
		case "Friday":
		case "Saturday":
		case "Sunday":
		
		case "daily":
			if (theEndDate) theEndDate.style.display = 'block';
			if (theStartDate) theStartDate.style.display = 'block';
			if (manytimes) manytimes.style.display = 'none';
			if (weeklyboxes) weeklyboxes.style.display = 'none';
			if (theDateName) theDateName.innerHTML = 'Start';
			break;

		case "arbitrarily":
			if (theEndDate) theEndDate.style.display = 'none';
			if (theStartDate) theStartDate.style.display = 'none';
			if (manytimes) manytimes.style.display='block';
			if (weeklyboxes) weeklyboxes.style.display = 'none';
			break;

		default:
			if (theEndDate) theEndDate.style.display = 'none';
			if (theStartDate) theStartDate.style.display = 'block';
			if (theDateName) theDateName.innerHTML = 'Event';
			if (manytimes) manytimes.style.display = 'none';
			if (weeklyboxes) weeklyboxes.style.display = 'none';
			break;
	}
}
