// JavaScript Document for Mike Behnke, Homework 7, INP271
var hw7 = {
	
	//object variables
	hdrDivs : [], hdrH3s : [], totalHdrs : null,
	tbutton : [], totalTbuttons : null,
	toggleBox : null, tboxButtons : [],	
	dataTables : [], totalDataTables : null, measure : [],
	
	mbinit : function () {
		if (!hw7.totalDataTables) {hw7.getTables();}
		if (!hw7.toggleBox) {hw7.createTogglebox();}
		if (!hw7.totalTbuttons) {hw7.createTogglebuttons();}
		hw7.modifyInputs();

		//Setup event triggers
		hw7.setToggles();

		//Check display status cookies and call appropriate function
		switch (jslib.findCookie('status')) {
			case 'hideall' : hw7.hideAll(); break;
			case 'showall' : hw7.showAll(); break;
			case 'mixed' : hw7.restoreToggles(); break;
			default : hw7.hideAll(); break;
		}
	},
	
	createTogglebox : function () {
		if (!document.getElementById && !document.getElementsByTagName && !document.createElement) {return;}
		var loginDiv = jslib.getObj('login');
		hw7.toggleBox = document.createElement('div');
		hw7.toggleBox.setAttribute('id', 'togglebox');
		var tbbv = ['Show All Measure Data','Hide All Measure Data'];
		for (var i=0; i<2; i++) {
			hw7.tboxButtons[i] = document.createElement('input');
			hw7.tboxButtons[i].setAttribute('type', 'button');
			hw7.tboxButtons[i].setAttribute('value', tbbv[i]);
			hw7.toggleBox.appendChild(hw7.tboxButtons[i]);
		}
		loginDiv.parentNode.insertBefore (hw7.toggleBox, loginDiv);
		
		//create buttons at bottom of form
		var changeButtons = [];
		var cbDiv = document.createElement('div');
		cbDiv.setAttribute('id','changeButtons');
		var cbbv = ['Add Subject','Add Measure','Save Changes'];
		for (var i=0;i<3;i++) {
			changeButtons[i] = document.createElement('input');
			if (i < 2) {changeButtons[i].setAttribute('type','button');}
			else {changeButtons[i].setAttribute('type','submit');}
			changeButtons[i].setAttribute('value',cbbv[i]);
			cbDiv.appendChild(changeButtons[i]);
		}
		loginDiv.parentNode.insertBefore(cbDiv, loginDiv);
	},

	createTogglebuttons : function () {
		if (!document.getElementByTagName && !document.createElement) {return;}
		hw7.hdrDivs = jslib.getElementsByClass('hdr',null,'div');
		hw7.totalHdrs = hw7.hdrDivs.length;
		for (var i=0; i<hw7.totalHdrs; i++) {
			hw7.hdrH3s[i] = hw7.hdrDivs[i].getElementsByTagName('h3')[0];
			hw7.tbutton[i] = document.createElement('input');

			// Find name of Measure without "Data"
			hw7.measure[i] = hw7.hdrH3s[i].firstChild.data.match(/[\w+\s+\-]+[^Data ]/);
			
			// Add to the table summaries and give the tables an ID
			hw7.dataTables[i].summary += ' in: ' + hw7.measure[i];
			hw7.dataTables[i].id = hw7.measure[i];
			hw7.dataTables[i].id = hw7.dataTables[i].id.replace(/\s/g,'_');
			
			hw7.tbutton[i].setAttribute('value', 'Show ' + hw7.measure[i] + ' Data');
			hw7.tbutton[i].setAttribute('type', 'button');
			hw7.hdrDivs[i].insertBefore(hw7.tbutton[i],hw7.hdrH3s[i]);
		}		
		//associate toggle buttons with tables
			for (var i=0;i<hw7.totalDataTables;i++) {
				hw7.dataTables[i].numb = i;
				hw7.tbutton[i].numb = i;
			}
	},
	
	getTables : function () {
		//get the data tables so they can be hidden
		if(document.getElementsByTagName) {
			hw7.dataTables = document.getElementsByTagName('table');
			hw7.totalDataTables = hw7.dataTables.length;
		}
	},
	
	setToggles : function () {
		jslib.addEvent(hw7.tboxButtons[0],'click',hw7.showAll);
		jslib.addEvent(hw7.tboxButtons[1],'click',hw7.hideAll);
		hw7.totalTbuttons = hw7.tbutton.length;
		for (i=0;i<hw7.totalTbuttons;i++) {
			jslib.addEvent(hw7.tbutton[i],'click',hw7.showHideOne);
		}
	},
	
	showAll : function () {
		for (var i=0;i<hw7.totalDataTables;i++) {
			hw7.dataTables[i].className = '';
			hw7.tbutton[i].setAttribute('value','Hide ' + hw7.measure[i] + ' Data');
			jslib.createCookie(hw7.dataTables[i].id,'show');
		}	
		jslib.createCookie('status','showall');
	},
	
	hideAll : function () {
		for (var i=0;i<hw7.totalDataTables;i++) {
			hw7.dataTables[i].className = 'hide';
			hw7.tbutton[i].setAttribute('value','Show ' + hw7.measure[i] + ' Data');
			jslib.createCookie(hw7.dataTables[i].id,'hide');
		}	
		jslib.createCookie('status','hideall');
	},
		
	showHideOne : function () {
		if (hw7.dataTables[this.numb].className == 'hide') {
			hw7.dataTables[this.numb].className = '';
			hw7.tbutton[this.numb].setAttribute('value','Hide ' + hw7.measure[this.numb] + ' Data');
			jslib.createCookie(hw7.dataTables[this.numb].id,'show');
		} else {
			hw7.dataTables[this.numb].className = 'hide';
			hw7.tbutton[this.numb].setAttribute('value','Show ' + hw7.measure[this.numb] + ' Data');
			jslib.createCookie(hw7.dataTables[this.numb].id, 'hide');
		}
		jslib.createCookie('status','mixed');
	},
	
	restoreToggles : function () {
		for (var i=0;i<hw7.totalDataTables;i++) {
			if (jslib.findCookie(hw7.dataTables[i].id) == 'show') {
				hw7.dataTables[i].className = '';
				hw7.tbutton[i].setAttribute('value','Hide ' + hw7.measure[i] + ' Data');
			} else {
				hw7.dataTables[i].className = 'hide';
				hw7.tbutton[i].setAttribute('value','Show ' + hw7.measure[i] + ' Data');
			}
		}
	},

  modifyInputs : function () {
    if (!document.getElementsByTagName && !document.createElement) {return;}

		var totalTab = 16;
		// Set tabindex for the Show All and Hide All buttons
		for (var k=0; k<2; k++) {
			totalTab++;
			hw7.tboxButtons[k].setAttribute('tabindex',totalTab);
		}

		//set Tabindex for Individual Show/Hide buttons and data inputs
		for (var i=0; i<hw7.totalDataTables; i++) {
		  totalTab++;
		  hw7.tbutton[i].setAttribute('tabindex', totalTab);
			var allInputs = hw7.dataTables[i].getElementsByTagName('input');
			for (var j=0, totalInputs=allInputs.length; j<totalInputs; j++) {

				
				totalTab++;
				allInputs[j].setAttribute('tabindex', totalTab);

				//create and set input IDs and Title
				var subjectPointer = allInputs[j].parentNode.previousSibling.firstChild;
				var subject = subjectPointer.data;
				var subjectID = hw7.dataTables[i].id + subject.substring(0,3) + subject.charAt(subject.length-2) + totalTab;
				allInputs[j].setAttribute('id', subjectID);
				allInputs[j].setAttribute('title', ('Study 1, ' + hw7.measure[i]));

				//create label for each input, place Subject within created label
				var theLabel = document.createElement('label');
				theLabel.htmlFor = subjectID;
				allInputs[j].parentNode.previousSibling.insertBefore(theLabel,subjectPointer);
				theLabel.appendChild(subjectPointer);
			}
		}
	}
		
}

jslib.addEvent(window,'load',hw7.mbinit);