// viewdiv_viewtree javascript

// receive the element id
function viewdiv (tableid, checkboxid) { // this function will alternately hide or show a DIV 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
// 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

	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 = 'block'; // show
	// newstatus = ''; // show
	}

// due to reloading, sometimes what is displayed is not what it should be
// following conditions for checkboxes added 31Jan2009

	if (checkboxid) { // was a toggling element id provided
	var toggleid = document.getElementById (checkboxid);

		if (toggleid.type == "checkbox") { // is this is a checkbox
		// alert ("checkbox");  // debug
	
			if (toggleid.checked == true) { // is this checked
			// alert ("checked");  // debug
			newstatus = 'block'; // show
			// newstatus = ''; // show
			}
			else { // false - not checked so do not show
			// alert ("not checked");  // debug
			newstatus = 'none'; // hide 
			}
		}
	}

atableobject.style.display = newstatus;
}

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 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 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;
	
		if (test) { // if true then this row is a subset of listitemclass (class)
		// alert ("line 47: 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 77: 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
	}
}
