var matrixExplanationOld;
var matrixExplanationNew;
var matrix2ExplanationOld;
var matrix2ExplanationNew;

function matrixEditExplanation(a) {
	matrixExplanationOld = matrixExplanationNew; 
	matrixExplanationNew = document.getElementById(a);

	if (matrixExplanationOld) {
		matrixExplanationOld.style.visibility="hidden";
	}

	if (matrixExplanationNew) {
		matrixExplanationNew.style.visibility="visible";
	}
}

function matrix2EditExplanation(object) {
	matrix2ExplanationOld = matrix2ExplanationNew; 
	matrix2ExplanationNew = document.getElementById(object);

	if (matrix2ExplanationOld) {
		matrix2ExplanationOld.style.display = 'none';
	}

	if (matrix2ExplanationNew) {
		matrix2ExplanationNew.style.display = 'block';
	}
}

function matrixSetAnswer(row, column) {
//	alert('row = ' + row + '\ncolumn = ' + column);

	// select inputs
	var nodeList = document.getElementsByTagName('INPUT');
	for (var i = 0; i < nodeList.length; i++) {

		// subselect checkboxes
		if (nodeList[i].getAttribute('type') == 'checkbox') {
//			alert(nodeList[i].getAttribute('name'));

			// subselect checkboxes in this column
			var s1 = nodeList[i].getAttribute('name').substring(11, 12);
			var s2 = nodeList[i].getAttribute('name').substring(14, 15);
//			alert('s1 (current row) = ' + s1 + '\ns2 (current column) = ' + s2);

			if (s2 == column) {

				if (s1 == row) {
					// this is checkbox the user clicked;
					// leave it checked
				} else {

					if (nodeList[i].checked) {
						nodeList[i].checked = false;
					}
				}
			}
		}
	}
}

function matrix3SetAnswer(table, row, column) {
	// Special case: first table has unique answers per column, second table unique per table; default to unqiue per column;
	if (table == 1) {
		return matrixGenericSetAnswer(table, row, column, "table");
	}
	return matrixGenericSetAnswer(table, row, column, "column");
}

function matrixGenericSetAnswer(table, row, column, unique) {
	/*
		SetAnswer with options to select if the selected row should be unique per cell, row, column or table 
	*/
	var nodeList = document.getElementsByTagName("INPUT");
	for (var i=0; i<nodeList.length; i++) {
		if (nodeList[i].getAttribute("type") == "checkbox") {
			var nodeTablePos = nodeList[i].getAttribute("name").substring(11,12);
			var nodeRowPos = nodeList[i].getAttribute("name").substring(14,15);
			var nodeColPos = nodeList[i].getAttribute("name").substring(17,18);

			if (unique == "table") {
				// uncheck everything and set the selected item to checked.
				if (nodeTablePos == table) {
					if ((nodeRowPos == row) && (nodeColPos == column)) {
						// This is the checkbox the user clicked;
					} else {
						// Uncheck the rest in the same table;
						nodeList[i].checked = false;
					}
				}
			}
			if (unique == "row") {
				if ((nodeTablePos == table) && (nodeRowPos== row)) {
					if (nodeColPos == column) {
						// This is the checkbox the user clicked;
					} else {
						nodeList[i].checked = false;
					}
				}
			}
			if (unique == "column") {
				if ((nodeTablePos == table) && (nodeColPos == column)) {
					if (nodeRowPos== row) {
						// This is the checkbox the user clicked;
					} else {
						nodeList[i].checked = false;
					}
				}
			}
		}
	}
}

function matrixOpen(a) {
	obj = document.getElementById(a); 

	if (obj) {
		obj.style.visibility="visible";
	}
}

function matrixClose(a) {
	obj = document.getElementById(a); 
	if (obj) {
		obj.style.visibility="hidden";
	}
}

