showhide = new Array();
function toggle (switchid, showhide0, showhide1) { 
// this function will alternately hide or show one or more elements
// the hide or show will toggle but if switchid is a checkbox then
// the checkbox status (checked => show and not checked = hide)
// will override the toggle status
showhide[0] = showhide0;
	if (showhide1 != undefined) {showhide[1] = showhide1;} // ... more id's can be added

// alert("switchid = " + switchid + "; showhide[0] = " + showhide[0] + "; showhide[1] = " + showhide[1] + "\nshowhide.length = " + showhide.length); // debug

// switchid is the id of the element that will be clicked and unclicked
// showhide is an array of id's of what will show or not show
// ... more id's can be added

	for (var i=0; i<showhide.length; i++) {
	// alert("i = " + i); // debug
	// getElementById('elementID') returns the element with the id elementID as an object.
	var atableobject = document.getElementById(showhide[i]);
	// alert("i = " + i + "; showhide.length = " + showhide.length + "; atableobject.style.display = " + atableobject.style.display); // debug
		// check for element being shown (block or null) then hide
		if ((atableobject.style.display == 'block') || (atableobject.style.display == '')) {
		newstatus = 'none'; // hide 
		// alert("newstatus = " + newstatus); // debug
		// atableobject.style.backgroundColor='white'; // to unchecked white
		}
		// check for element being hidden (none) then show
		else if (atableobject.style.display == 'none') { // default is to not display
		// newstatus = 'block'; // show
		//  Firefox doesn't render display:block for tables as it is intended
		//  By setting display = '' it seems IE and FireFox make up their own mind
		newstatus = ''; // show (maybe this should be the default - 3Mar2009)
		// alert("newstatus = " + newstatus); // debug
		}
	atableobject.style.display = newstatus;
	}
//
	if (switchid) {
	override (switchid); // override newstatus if switch is a checkbox
	atableobject.style.display = newstatus;
	}
}

function override (switchid, hideviewid) {
// due to reloading, sometimes what is displayed is not what it should be
// following check and override for aswitchid being a checkbox added 31Jan2009
// check and override for switchid being a checkbox switch for hideviewid added 21Apr2010

// is element id provided a checkbox used to hide/view another id

	if (switchid && hideviewid) { // switchid is switch; hideviewid is id of view/hide element
	var varswitchid = document.getElementById (switchid);
	var varhideviewid = document.getElementById (hideviewid);

		if (varswitchid.type == "checkbox") { // is this is a checkbox
		// switchid is switch; hideviewid is id of viewed/hidden
		// var dummy1 = varswitchid.checked;  // debug
		// alert ("checkbox;\nswitchid = " + switchid + "\nvarswitchid.checked = " + dummy1);  // debug
		// var dummy2 = varhideviewid.checked;  // debug
		// alert ("checkbox;\nvarswitchid.checked = " + dummy1 + "\nvarhideviewid.checked = " + dummy2);  // debug
		// var dummy3 = varhideviewid.style.display;  // debug
		// alert ("checkbox;\nhideviewid = " + hideviewid + "\nvarhideviewid.style.display = " + dummy3);  // debug
		
			// if (varhideviewid.style.display == 'block') {
			// varswitchid.checked == true;
			if (varswitchid.checked == true) { // is switchid checked
			// alert ("checked");  // debug
			// varhideviewid.style.display = 'block'; // show
			varhideviewid.style.display = ''; // better for IE and FF to show
			}
			// else if (varhideviewid.style.display == 'none') {
			// varswitchid.checked == false;
			else if (varswitchid.checked == false) { // false - switchid not checked so do not show
			// alert ("not checked");  // debug
			varhideviewid.style.display = 'none'; // hide 
			}
		}
	}
	else if (switchid) { // is element id provided a checkbox
	var toggleid = document.getElementById (switchid);

		if (toggleid.type == "checkbox") { // is this is a checkbox
		// alert ("checkbox");  // debug
	
			if (toggleid.checked == true) { // is this checked
			// alert ("checked");  // debug
			// toggleid.style.display = 'block'; // show
			toggleid.style.display = ''; // better for IE and FF to show
			// newstatus = ''; // show
			}
			else { // false - not checked so do not show
			// alert ("not checked");  // debug
			toggleid.style.display = 'none'; // hide 
			}
		}
	}

}

function viewdiv (tableid, checkboxid) { // this function will alternately hide or show a DIV,
// TABLE, or TR; the hide or show will toggle with an OnClick or OnMouseOver/OnMouseOut
// alert("tableid = " + tableid); // debug

// tableid is the id of what will show or not show (view/hide)
// checkboxid is the optional id of a checkbox if it is the toggling element

// getElementById('elementID') returns the element with the id elementID as an object.
var atableobject = document.getElementById(tableid);
// check for block or default of null
// alert("atableobject.style.display = " + atableobject.style.display); // debug

	if ((atableobject.style.display == 'block') || (atableobject.style.display == '')) {
	// alert("newstatus = " + newstatus); // debug
	atableobject.style.display = 'none'; // hide 
	// atableobject.style.backgroundColor='white'; // to unchecked white
	}
	else if (atableobject.style.display == 'none') { // default is to not display
	// alert("newstatus = " + newstatus); // debug
	//  Firefox doesn't render display:block for tables as it is intended
	//  By setting display = '' it seems IE and FireFox make up their own mind
	atableobject.style.display = ''; // better for IE and FF to show
	}

// if checkboxid (the switch id) is a checkbox then hide/view tableid (the element switched)
// based on the status of checkboxid (the switch id) being checked or not

	if (checkboxid) { // special use
	// override hide/view if switch is a checkbox and based on being checked or not
	// override (checkboxid, tableid); // checkboxid is switch; tableid is id of view/hide element
	override (checkboxid); // checkboxid is switch; tableid is id of view/hide element
	}
	
}

function viewtree (listtype, tableordivname, listitemclass) { // alternately hide or show a sub list
// the hide or show will toggle with an OnClick
// listtype is the format style of items using either TR or DIV
// tableordivname is the id of the table or DIV which contains the list items (listtype)
// listitemclass is the string of characters starting the class of all items in the subset

// alert ("listtype = " + listtype + "; tableordivname = " + tableordivname + "; and listitemclass = " + listitemclass); // debug
// getElementsByTagName('tag') returns all elements with the name tag as an array
// var checkboxes = document.getElementsByTagName('checkbox');
// var tables=document.getElementsByType('table');

var row_regexp = new RegExp ("^" + listitemclass,""); // 
// alert ("row_regexp = " + row_regexp); // debug

// get the id of the table or DIV containing the list
var table = document.getElementById (tableordivname); //
// var rows = table_data.getElementsByType('checkbox'); // 
// var rows = table_data.getElementsByTagName ('TABLE'); // gets all TR tags
var rows = table.getElementsByTagName (listtype); // gets list type tags (TR or DIV)
// alert("rows = " + rows.length); // debug

	for (var i=0; i<rows.length; i++) {
	// alert ("line 69: outer loop i = " + i + "; listitemclass = '" + listitemclass + "';\nrows[" + i + "].className = '" + rows[i].className + "'; and rows[" + i + "].id = '" + rows[i].id + "' "); // debug
	// check the class which will have a unique data identifier (YM-Guide..., Rank-..., or PQR-...)
	var test = row_regexp.test(rows[i].className); // use regex string
	// alert ("line 41: test = " + test + " for listitemclass = " + listitemclass  + " compare with className = "  ); // debug
	// the first match should be the row which was checked or unchecked (highest level)
	// all additional matches will be members of a subset which should be hidden or viewed
	var breakflag = false;
	
	// be careful to not identify the original row as a subset of listitemclass (class)
	// this would happen if an EO was selected that had no PR's
		// if (rows[i].className != listitemclass && test) {
	
		if (test) { // if true then this row is a subset of listitemclass (class)
		// alert ("line 78: outer loop i = " + i + " and is a match"); // debug
		// every data row has ID="group$n_row" and the row checkbox has ID="box$n_row"
		var rowsid = rows[i].id; // get the row id (.id has to start with lowercase) ie group1YM
		// alert ("outer loop i = " + i + " and rowsid = " + rowsid + "\nthe items wanted should follow this item which was clicked"); // debug
		var boxid = rowsid.replace (/group/i, 'box'); // get checkbox id using String.replace (pattern,string)
		// alert ("outer loop i = " + i + " and boxid = " + boxid); // debug
		var box = document.getElementById (boxid); // get the checkbox in this row
		// alert ("outer loop i = " + i + " and box = " + box); // debug
		var userunchecked = false; // user unchecked so uncheck sub and show
		var userchecked = false; // for user checked so uncheck sub and hide

			//if (box.type=="checkbox") {
			// alert ("we have a checkbox!"); // debug
			//}
			
			if (box.checked != true) { // checkbox is not checked
			// highest level checkbox is unchecked so need to uncheck and show all sub elements
			// all boxes should already be unchecked but will uncheck anyway
			// alert ("the checkbox was just unchecked!"); // debug
			userunchecked = true; // uncheck the checkboxes and show the rows
			}
			else if (box.checked == true) {
			// highest level checkbox is checked so need to check and hide all sub elements
			// alert ("the checkbox was just checked!"); // debug
			userchecked = true; // check the checkboxes and hide the rows
			}
			
		// take advantage of matches being consecutive to restrict search
		// as soon as there is not a match we can stop checking for subsets
			for (var j=i+1; j<rows.length; j++) {
			// alert ("line 112: inner loop j = " + j + "; rows.length = " + rows.length + "; rows[j].className = " + rows[j].className); // debug
			// class will have a unique data identifier (YM-Guide..., Rank-..., or PQR-...)
			var subtest = row_regexp.test(rows[j].className); // use regex string
			// alert ("line 80: subtest = " + subtest); // debug
				if (subtest) { // if true then this row is a subset of listitemclass (class)
				// every data row has ID="group$n_row" and the row checkbox has ID="box$n_row"
				var subrowsid = rows[j].id; // get the row id (id has to start with lowercase)
				// alert ("inner loop j = " + j + " and subrowsid = " + subrowsid); // debug
				var subboxid = subrowsid.replace (/group/i, 'box'); // construct the checkbox id 
				// alert ("inner loop j = " + j + " and subboxid = " + subboxid); // debug
				var subbox = document.getElementById (subboxid); // get the checkbox in this row
				// alert ("line 88: subbox.type = " + subbox.type); // debug
				// we want to skip any valid class codes which do not have a checkbox
				
					if (subbox.type == "checkbox") { // check if this is a blank or a checkbox
					// alert ("we have a checkbox!"); // debug
					var subrowsname = subbox.name; // get the checkbox/row name (ie Rows002)
					// alert ("inner loop j = " + j + " and subrowsname = " + subrowsname); // debug
					// var subrowsnumber = subrowsname.replace (/Rows00/i, ''); // leave row number
					var subrowsnumber = subrowsname.replace (/^.+00/i, ''); // leave row number
					// alert ("line 102: inner loop j = " + j + " ; subrowsnumber = " + subrowsnumber  + "; and subrowsname = " + subrowsname); // debug
					
						if (userunchecked == true) { // uncheck and show all sub elements
						subbox.checked = false; // sub elements should not be checked
						rows[j].style.display = ''; // show the row
						rows[j].style.backgroundColor='white'; // to unchecked white
						// alert ("line 108: inner loop userunchecked = " + userunchecked + " and rows[j].style.display = '" + rows[j].style.display + "' and should be '';\nsubrowsnumber = " + subrowsnumber + "; subrowsname = " + subrowsname); // debug
						//
						// now need to use function CheckUncheck to delete data otherwise
						// because this row was not clicked CheckUncheck will not be run
						// (CheckUncheck must have been run on main row before viewtree)
						// function CheckUncheck(row,rowname) ie CheckUncheck(1, 'Rows001')
						CheckUncheck (subrowsnumber, subrowsname);
						}
						else if (userchecked == true) { // check and hide all sub elements
						subbox.checked = true; // sub elements should be checked
						rows[j].style.display = 'none'; // hide the row
						// rows[j].style.backgroundColor='white'; // why bother to change
						// alert ("line 120: inner loop userchecked = " + userchecked + " and rows[j].style.display = '" + rows[j].style.display + "' and should be 'none';\nsubrowsnumber = " + subrowsnumber + "; subrowsname = " + subrowsname); // debug
						//
						// now need to use function CheckUncheck to store data otherwise
						// because this row was not clicked CheckUncheck will not be run
						// (CheckUncheck must have been run on main row before viewtree)
						// function CheckUncheck(row,rowname) ie CheckUncheck(1, 'Rows001')
						CheckUncheck (subrowsnumber, subrowsname);
						}
					}
				}
				else {
				breakflag = true;
				// alert ("breakflag = true;"); // debug
				break; // done with subset elements so end loop
				}
			}
		}
	// if the set of elements was found then we are done 
		if (breakflag == true) {break;}
	// do not repeat through outer loop
	}
}


function viewhiderows (listtype, tableordivname, listitemclass, action) { // alternately hide or show
// the hide or show rows of a table will toggle with an OnClick
// listtype is the format style of items using either TR or DIV
// tableordivname is the id of the table or DIV which contains the list items (listtype)
// listitemclass is the string of characters starting the class of all items in the subset
// action is 'show', hide', or 'toggle' 

// every row which is a subset of listitemclass must have a row id

// alert ("listtype = " + listtype + "; tableordivname = " + tableordivname + ";  listitemclass = " + listitemclass + "; and action = " + action); // debug
// getElementsByTagName('tag') returns all elements with the name tag as an array
// var checkboxes = document.getElementsByTagName('checkbox');
// var tables=document.getElementsByType('table');

var row_regexp = new RegExp ("^" + listitemclass,""); // 
// alert ("row_regexp = " + row_regexp); // debug

// get the id of the table or DIV containing the list
var table = document.getElementById (tableordivname); //
// var rows = table_data.getElementsByType('checkbox'); // 
// var rows = table_data.getElementsByTagName ('TABLE'); // gets all TR tags
var rows = table.getElementsByTagName (listtype); // gets list type tags (TR or DIV)
// alert("rows = " + rows.length); // debug

	if (action != '') { // a legitimate action is 'show', hide', or 'toggle' 
	for (var i=0; i<rows.length; i++) {
	// alert ("line 38: outer loop i = " + i + "; listitemclass = '" + listitemclass + "';\nrows[" + i + "].className = '" + rows[i].className + "'; and rows[" + i + "].id = '" + rows[i].id + "' "); // debug
	// check the class which will have a unique data identifier (YM-Guide..., Rank-..., or PQR-...)
	var test = row_regexp.test(rows[i].className); // use regex string
	// alert ("line 198: test = " + test + "\n for listitemclass = " + listitemclass  + "\n compare with className = " + rows[i].className ); // debug
	
		if (test) { // if true then this row is a subset of listitemclass (class)
		// alert ("line 201: loop i = " + i + " and is a match"); // debug
		// every data row has ID="group$n_row" and the row checkbox has ID="box$n_row"
		var rowid = rows[i].id; // get the row id (.id has to start with lowercase) ie group1YM
		// alert ("outer loop i = " + i + " and rowsid = " + rowsid + "\nthe items wanted should follow this item which was clicked"); // debug

		// tableid is the id of what will show or not show
		// getElementById('elementID') returns the element with the id elementID as an object.
		var atableobject = document.getElementById(rowid);
		// alert ("line 210: loop i = " + i + " and rowid = " + rowid); // debug
		
			if (action == 'show') { // a legitimate action is 'show', hide', or 'toggle' 
			atableobject.style.display = ''; // better for IE and FF to show
			}
			else if (action == 'hide') { // a legitimate action is 'show', hide', or 'toggle' 
			atableobject.style.display = 'none'; // hide 
			}
			else if (action == 'toggle') { // a legitimate action is 'show', hide', or 'toggle' 
			// check for block or default of null
				if ((atableobject.style.display == 'block') || (atableobject.style.display == '')) {
				newstatus = 'none'; // hide 
				// atableobject.style.backgroundColor='white'; // to unchecked white
				}
				else if (atableobject.style.display == 'none') { // default is to not display
				newstatus = ''; // show
				}
			atableobject.style.display = newstatus;
			}
			else { // error
			
			}
		}
	}
	}
}


var format_id = ''; // to store the id of ribbon format BR if any
// receive the element id
function viewawards (awardid) { // this function will alternately hide or show

// there are three tables in the web page:
// 1) descriptions (ID=[awardid]), // award number = awardid
// 2) arranged (IMG ID= ["ribbon-" + awardid] and BR ID= ["ribbon-" + awardid + "br"]),
// 3) details (ID=["award-" + awardid])

// first check the status of the checkbox then either hide the ribbon and BR
// if it is not checked

	if (awardid) { // was an award number (checkbox id) provided
	var toggleid = document.getElementById (awardid);

		if (toggleid.type == "checkbox") { // redundant check for a checkbox
		// alert ("checkbox");  // debug
		
		// if checkbox is not checked then take actions
			if (toggleid.checked == false) { // is this not checked
			// alert ("not checked awardid = " + awardid);  // debug
			// get BR ID= [awardid + "br"] and award ID = ["award-" + awardid]
			// change ribbon, ribbon format, and details to not show
			//
			var hide = "ribbon-" + awardid;
			// alert ("hide ribbon = " + hide);  // debug
			var hideobject = document.getElementById(hide);
			hideobject.style.display = 'none'; // do not show BR format
			//
			// hide = "ribbon-" + awardid + "br";
			// alert ("hide ribbon format = " + hide);  // debug
			// hideobject = document.getElementById(hide);
			// hideobject.style.display = 'none'; // do not show BR format
			//
			hide = "award-" + awardid;
			// alert ("hide award details = " + hide);  // debug
			hideobject = document.getElementById(hide);
			hideobject.style.display = 'none'; // do not show details
			}
			else { // checked so show ribbon and description
			// alert ("checked awardid = " + awardid);  // debug
			var show = "ribbon-" + awardid;
			var showobject = document.getElementById(show);
			showobject.style.display = ''; // show ribbon
			//
			show = "award-" + awardid;
			showobject = document.getElementById(show);
			showobject.style.display = ''; // show details
			}
		}
	}

// now get all table descriptions checked checkboxes to show ribbons and details
// alert("getting checkboxes checked"); // debug

// awardid is the award number and id of a checkbox if it is the toggling element

// object is the name of a form, table, or div containing all rows of data submitted
// to use the following the (table) object descriptions is hardcoded into js below
// for (i = 1; i < descriptions.elements.length; i++) { // n elements in descriptions
	
// getElementsByTagName('tag') returns all elements with the name tag as an array
// var tables=document.getElementsByType('table');

var checkboxes = descriptions.getElementsByTagName('INPUT');
// alert ("line 291: checkboxes.length = " + checkboxes.length); // debug

var n_checked = 0;
var ribbon_id  = new Array();

	for (i = 1; i < checkboxes.length; i++) { // n elements in descriptions
	//
	// need to count each ribbon checkbox checked then take actions
		if (checkboxes[i].checked == true) { // is this not checked
		// alert("line 310: i =" + i); // debug
		//
		// store award number which is also the checkbox ID in TABLE ID='arranged'
		ribbon_id[n_checked] = checkboxes[i].id;
		// alert ("checkbox checked: ID = ribbon number = " + ribbon_id[n_checked]);  // debug
		n_checked++; // count number of checkboxes checked
		}
	}
// how many checkboxes were checked?
// alert ("n_checked = " + n_checked);  // debug

// if more than thre ribbons selected calculate the ribbon format BR to show
	if (n_checked > 3) {
	var n_floating = n_checked - (Math.floor (n_checked/3) * 3);
	// alert ("top row number; n_floating = " + n_floating);  // debug number of extra ribbons
	//
	
	// hide the old ribbon format BR if any
		if (format_id != '') {
		var br = "ribbon-" + format_id + "br"; // ribbon format BR ID
		// alert ("hideold ribbon format = " + hideold);  // debug
		var hideformat = document.getElementById(br);
		hideformat.style.display = 'none'; // do not show BR format
		format_id = ''; // clear id of ribbon
		}
	//
	// iff not all rows of three ribbons then modify display
		if (n_floating > 0) {
		// store and show the new ribbon format BR
		var adj_index = n_floating - 1; // index of id in array ribbon_id
		format_id = ribbon_id[adj_index]; // store award number 
		// alert ("ribbon_id[adj_index] = " + format_id);  // debug
		var br = "ribbon-" + format_id + "br"; // create id of new ribbon format BR
		// alert ("new ribbon format ID = " + format_id);  // debug
		var hideformat = document.getElementById(br);
		hideformat.style.display = ''; // show BR format
		}
	}
}