var groupIDs = ["A", "B", "J", "I", "S", "P"];
var groupNames = ["ALL", "Beginning", "Junior", "Intermediate", "Senior", "Parents"];

function showcalendar() {
	var ridx = 0;
	try {
		var calgrps = "A";
		var mybody =document.getElementById("calendardata");
		if (!mybody) return;
        if (document.getElementById("calendargroups")) {
       		calgrps = document.getElementById("calendargroupselect").value;
    	} else {
			// create our list
			// update select list.
			var selhtml = 
			'<p style="text-align: center;">Show calendar for ' + 
			'<select id="calendargroupselect" onchange="showcalendar()" name="groups">' +
			'<option selected="selected" value="A">ALL</option>' +
			'<option value="B">Beginning</option>' +
			'<option value="J">Junior</option>' +
			'<option value="I">Intermediate</option>' +
			'<option value="S">Senior</option>' +
			'<option value="P">Parents</option>' +
			'</select>orchestra(s)</p>'; 
			var caldiv = document.createElement('div');
			caldiv.setAttribute('id','calendargroups');
			caldiv.innerHTML = selhtml;
			mybody.parentNode.insertBefore(caldiv, mybody);
		}
		
		var mybody =document.getElementById("calendardata");
		var rows = mybody.getElementsByTagName("tr");
		for(var ridx = 0; ridx < (rows.length-1)/2; ridx++) {
			var hdr = rows[ridx*2];
			var detail = rows[ridx*2+1];
			if (!detail || !detail.getElementsByTagName("td")) {
				continue;	// Nothing for this row..
			}
			var cols = hdr.getElementsByTagName("td");
			if (cols.length != 4) {
				hdr.style.backgroundColor = "yellow";
				alert("Row " + ridx*2 + " (colored yellow) doesn't have 4 columns, content error found");
				return;
			}
			var haveContent = false;
			for (var cidx=0; cidx<cols.length; cidx++) {
				if (cols[cidx].innerHTML && cols[cidx].innerHTML != ' ' && cols[cidx].innerHTML != '' &&
					cols[cidx].innerHTML != '&nbsp;') {
					haveContent = true;
					break;
				}
			}
			if (!haveContent) {
				hdr.style.display = 'none';
				detail.style.display = 'none';
				continue;
			}
			
			var group = cols[3].innerHTML.toUpperCase();
			group = group.replace("&NBSP;", "");
			var haveall = true;
			var grptext = "";
			for (idx=0; idx<groupIDs.length; idx++) {
				if (group.indexOf(groupIDs[idx]) != -1) {
					// have this one
					if (grptext != "") grptext += ", ";
					grptext += groupNames[idx];
				} else {
					haveall = false;
				}
			}
			if (haveall) {
				grptext = "All";
			}
			cols[2].className = "SmallText"; 
			cols[0].noWrap = true;
			cols[0].style.paddingRight = "10px";
			cols[1].noWrap = true;
			cols[1].style.paddingRight = "10px";
			cols[2].noWrap = true;
			cols[3].style.display = "none";
			var detailcell = detail.getElementsByTagName("td")[0];
			if (!detailcell.innerHTML) continue;
			if (grptext == "") {
				if (group != "N") {
					hdr.style.backgroundColor = "yellow";
					alert("Row " + ridx*2 + " (colored yellow) doesn't specify which groups in the last column, please specify one or more group letters");
					return;
				}
			} else {
				grptext = "(" + grptext + ")";
			}
			cols[2].innerHTML = grptext;
			detailcell.style.paddingBottom = "20px";
			detailcell.style.paddingLeft = "20px";
			// Ok, hide/show based on groups.
			if (calgrps == "A" || group.indexOf(calgrps) != -1) {
				// show
				hdr.style.display = "";
				detail.style.display = "";
			} else {
				// hide
				hdr.style.display = "none";
				detail.style.display = "none";
			}
		}
	} catch (e) {
		alert(e);
	}
}

