// JavaScript Document
//saved 3/9/07 version 1.2
//saved 5/28/07 version 1.3 debugged for IE

function hi() {alert("hi");}
function version() {alert("This is version 5/28/07");}
function debug(msg, num) {alert("DEBUG: " + msg + ": " + num);}
//function undef(varname) {return(typeof(varname)=="undefined");} //might cause errors
function btw(x1,num,x2) {return(x1<=num && num<x2);}
function round(num, dec) {return Math.round(Math.pow(10, dec) * num)	/ Math.pow(10, dec);}

function getById(id) {
	if(typeof(id)=="string") return(document.getElementById(id));
	return(id);
	}
function getByTag(tag,el) {
	if(!el) return(document.getElementsByTagName(tag));
	if((typeof el) == "string") return(document.getElementById(el).getElementsByTagName(tag));
	return(el.getElementsByTagName(tag));
	}
//can pass id, el, table cell containing el, etc	
//assumes you want the FIRST of wantedTag -- really for when there's only 1
function findNode(given, wantedTag) {
	var theNode;
	if (typeof given == "string") theNode = document.getElementById(given);
		else theNode=given;
	if (theNode.nodeName != wantedTag.toUpperCase()) 
		theNode = theNode.getElementsByTagName(wantedTag)[0];
	return(theNode);
}

String.prototype.trim = function() {
	var regexExtraSpace = /^\s+(.*?)\s+$/;
	return this.replace(regexExtraSpace, "$1");
}
//----------takeOutReplaceable----------------------------------------------------------

function takeOutReplaceable(id) {
	takeOutReplaceableFromElement(getById(id), 0);
}

function takeOutReplaceableFromElement(el, depth) {
	//alert("takeout");
	var theList = el.childNodes;
	var numNodes = theList.length;
	for (var i=numNodes-1; i >= 0; --i) {
		if (theList[i].hasChildNodes) takeOutReplaceableFromElement(theList[i], depth+1);
		if (theList[i].className == "replaceable") {
			removeNode(theList[i]);
			}
		}
	
}

//-------------manipulating tables------------------------------------------//
function getCell(id, r, c) {
	if (typeof(id)=="string") return(document.getElementById(id).getElementsByTagName("tr")[r].getElementsByTagName("td")[c]);
	return(id.getElementsByTagName("tr")[r].getElementsByTagName("td")[c]);
	}
function getCellText(id, r, c) {
	if(getCell(id, r, c).nodeValue) return(getCell(id, r, c).nodeValue); 
	return(getCell(id, r, c).firstChild.nodeValue);
	}

function getCellAlt(id, r, c) {
	if (getCell(id, r, c).getElementsByTagName("input")[0])
		return(getCell(id, r, c).getElementsByTagName("input")[0].getAttribute("alt"));
	if (getCell(id, r, c).getElementsByTagName("span")[0])
		return(getCell(id, r, c).getElementsByTagName("span")[0].innerHTML);
	//alert("using nodeValue");
	return(getCell(id, r, c).innerHTML);
	}

//-------------------manipulating nodes------------------------------------//
function insertBeforeNode(oldElement, newElement) {
	if (typeof oldElement == "string") oldElement = document.getElementById(oldElement);
	oldElement.parentNode.insertBefore(newElement, oldElement);
}

function insertAfterNode(oldElement, newElement) {
	if (typeof oldElement == "string") oldElement = document.getElementById(oldElement);
	var parent = oldElement.parentNode;
	if (parent.lastChild == oldElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement, oldElement.nextSibling);
	}
}

function removeNode(id) {
	if(typeof id == "string")
		{document.getElementById(id).parentNode.removeChild(document.getElementById(id));}
	else id.parentNode.removeChild(id);
}

function replaceNode(oldnode, newnode) {
	if(typeof oldnode == "string") oldnode = document.getElementById(oldnode);
	oldnode.parentNode.replaceChild(newnode, oldnode);
	}
//---------------------randomizing----------------------------------------------------//
function randInt(from, to) {
	return(from + Math.floor(Math.random()*(to-from)));
	}

function randColor(r1,r2,g1,g2,b1,b2) {
 	var hD=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];
	var h = "#";
	h += hD[randInt(r1,r2)];
	h += hD[randInt(r1,r2)];
	h += hD[randInt(g1,g2)];
	h += hD[randInt(g1,g2)];
	h += hD[randInt(b1,b2)];
	h += hD[randInt(b1,b2)];
	return h;
	}
	
function randNormal(mn, sd) {//uses Box-Muller tr/f
	var x1=Math.max(0.01, Math.random());
	var x2=Math.random();
	var y = mn+ (sd*Math.sqrt( - 2.0 *Math.log(x1) ) * Math.cos( 2.0 * Math.PI *x2 ));
	return y;
}

function incSpanInt(spanId, incVal) {
	var span = document.getElementById(spanId);
	var spanVal = parseInt(span.innerHTML);
	span.innerHTML = spanVal + incVal;	
	}	

//---------------------adding buttons, images, text-------------------------------------//

//"stuff" can contain HTML
function makeWindow(stuff, title, ht) {
	if (!ht) ht = 200+20*stuff.length/40;
	var newwin = window.open("", "", "width=300,height="+ht+",left=300,screenX=300,top=300,screenY=300");
	
	var titleNode = newwin.document.createElement("h1");
	titleNode.appendChild(newwin.document.createTextNode(title));
	titleNode.style.color = randColor(2,10,4,10,4,10);
	
	var divNode = newwin.document.createElement("div");
	divNode.innerHTML = stuff;
	
	var buttonNode = newwin.document.createElement("div");
	buttonNode.setAttribute("align","center");
	buttonNode.setAttribute("verticalAlign","baseline");
	var button = newwin.document.createElement("input");
	button.setAttribute("type", "button");
	button.setAttribute("value", "Close");
	button.onclick = function() {newwin.close();};
	buttonNode.appendChild(button);

	newwin.document.body.appendChild(titleNode);
	newwin.document.body.appendChild(divNode);
	newwin.document.body.appendChild(buttonNode);
	}
	
function makeButton(text, id) {
	var theNode = document.createElement("input");
	theNode.value = text;
	if(id) theNode.id = id;
	theNode.type = "button";
	return(theNode);
	}
	
//check for suffix	
function makeImage(src, w, h, alt) {
	var theNode = document.createElement("img");
	theNode.src = src;
	if (h) theNode.width = w;
	if (w) theNode.height = h;
	if (alt) theNode.alt = alt;
	return(theNode);
	}	

//image should look like a button
//if w&h < image, IE pays attention, FF doesn't --> removed w & h
//extra parameters OK
function makeImageButton(img) {
	var theNode = document.createElement("input");
	theNode.src = img;
	theNode.type = "image";
	return(theNode);
	}
	
function makeAbsImage(src, x, y, w, h, alt) {
	var theNode = document.createElement("img");
	theNode.src = src;
	theNode.style.position = "absolute";
	theNode.style.left = x+"px";
	theNode.style.top = y+"px";
	if (h) theNode.width = w;
	if (w) theNode.height = h;
	if (alt) theNode.alt = alt;
	return(theNode);
	}

function makeTextNode(text, classname, tagName) {
	if (!tagName) {tagName="span";}
	var theElNode = document.createElement(tagName);
	theElNode.appendChild(document.createTextNode(text));
	if (classname) theElNode.className = classname;
	return(theElNode);
	}
function makeAbsTextNode(text, x, y, classname) {
	var theElNode = document.createElement("span");
	theElNode.style.position = "absolute";
	theElNode.style.left = x+"px";
	theElNode.style.top = y+"px";
	theElNode.appendChild(document.createTextNode(text));
	if (classname) theElNode.className = classname;
	return(theElNode);
	}

//returns a node, which must be added to the dom
//do not pass an existing node or ID
function makeCentered(el) {
	var cen = document.createElement("div");
	cen.align = "center";
	cen.appendChild(el);
	return(cen);
}

//----------------------------nodeInfo-----------------------------------------
function arrayNodeInfo(arr, msg) {
	debug("#  nodes in " + msg + ": ", arr.length);
	for (var i=0; i < arr.length; ++i) {
		alert(nodeInfo(arr[i]));
		}
	}

//table has 2 children in FF, 1 in IE (tbody)
function childNodeInfo(node, msg) {
	if (typeof node == "string") node = document.getElementById(node);
	debug("# child nodes in " + msg + ": ", node.childNodes.length);
	for (var i=0; i < node.childNodes.length; ++i) {
		alert(nodeInfo(node.childNodes[i]));
		}
	}
		
function nodeInfo(node) {
	if (typeof node == "string") node = document.getElementById(node);
	if (node.nodeType==3) return("TEXT: " + node.nodeValue.slice(0,10));
	else if (node.nodeType==1) return("ELEMENT: " + node.nodeName + " class=" + node.className);
	else if (node.nodeType==2) return("ATTR: " + node.nodeName + " value=" + node.nodeValue);
	return("NODE type=" + node.nodeType);
	}

//------------------styles-----------------------------------

//colors returned as rgb(xxx,xxx,xxx) in FF, #xxxxxx in IE
function getStyle(el, style) {
	var i=0;
   if(!document.getElementById) return("undef"); 
   if ((typeof el) == "string") el = document.getElementById(el);
   var value = el.style[toCamelCase(style)];
   if(!value)
        if(document.defaultView)
            value = document.defaultView.getComputedStyle(el, "").getPropertyValue(style);      
        else if(el.currentStyle)
            value = el.currentStyle[toCamelCase(style)];     
     return value;
}

//OK is value has dash, colors can be #xxxxxx for either browser
function setStyle(el, style, value) {
	if(typeof el == "string") el = document.getElementById(el);
    el.style[toCamelCase(style)] = value;
}

function toCamelCase( sInput ) {
    var oStringList = sInput.split('-');
    if(oStringList.length == 1)   
        return oStringList[0];
    var ret = sInput.indexOf("-") == 0 ?
       oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) : oStringList[0];
    for(var i = 1, len = oStringList.length; i < len; i++){
        var s = oStringList[i];
        ret += s.charAt(0).toUpperCase() + s.substring(1)
    }
    return ret;
} 

//------elevators-------------------------------------------------------------
//-----------hook: a div with button, textbox, button
function Elevator(divid, minn, maxx, inc, dec) {
	this.downBut = getByTag("input", divid)[0];
	this.currTxt = getByTag("input", divid)[1];
	this.upBut = getByTag("input", divid)[2];
	
	this.minn = minn; this.maxx = maxx;
	this.inc = inc; this.dec = dec;
	this.currVal = parseFloat(this.currTxt.value);
	var elev = this;
	
	this.downBut.onclick = function() {elev.setCurr(elev.currVal-elev.inc);}
	this.currTxt.onblur = function() {elev.setCurr(parseFloat(elev.currTxt.value));}
	this.upBut.onclick = function() {elev.setCurr(elev.currVal+elev.inc);}
}

Elevator.prototype.setCurr = function(num) {
	var newval = round(num, this.dec);
	if (newval > this.maxx) newval = this.maxx;
	if (newval < this.minn) newval = this.minn;
	this.currTxt.value = newval;
	this.currVal = newval;
	}

//------------ReplaceButtonByAlt----------------------------------------------------------
//-----------------hook: a button id or node ---------------------------------------------

function prepareReplaceButtonByAlt(id, className) {
	var theButton = findNode(id, "input");
		theButton.onclick = function() {
			replaceButtonByAlt(id, className);
			};
	
	}

function replaceButtonByString(labelNode, str, className) {
	if (!className) className = "right";
	var theButton =  findNode(labelNode, "input");
	if (theButton) {	
		var newNode = document.createTextNode(str);
		var parent = theButton.parentNode;
		var spanNode = document.createElement("span");
		spanNode.setAttribute("class", className);
		spanNode.appendChild(newNode);
		insertAfterNode(theButton, spanNode);
		parent.removeChild(theButton);
		}		
	}	

function replaceButtonByString(labelNode, str, className) {
	if (!className) className = "right";
	var theButton =  findNode(labelNode, "input");
	if (theButton) {	
		var newNode = makeTextNode(str, className, "span");
		replaceNode(theButton, newNode);
		}		
	}	

function replaceButtonByAlt(labelNode, className) {
	labelNode = findNode(labelNode, "input");
	if (labelNode) 
		replaceButtonByString(labelNode, labelNode.getAttribute("alt"), className);
	}
	
//----------------------------------------------------------------------------------
//tol is absolute value of tolerance
function Blank(id, ans, type, tol, hasCheck, hasHint, hintStr, rightClassName, wrongClassName) {
	if (typeof id == "string") {this.el = getById(id);}
		else this.el = id;
	this.type = type;
	this.ans = ans;
	if (tol) this.tol = tol; else this.tol=0;
	this.hasCheck = hasCheck;
	this.hasHint = hasHint;
	this.hintStr = hintStr;
	if (rightClassName) this.rightClassName = rightClassName; else this.rightClassName = "right";
	if (wrongClassName) this.wrongClassName = wrongClassName; else this.wrongClassName = "wrong";
	this.done = false;
	this.prepare();
}

Blank.prototype.alert = function() {
	alert("Blank should be "+this.ans+" ("+this.type+" "+this.tol+" tol)");
}

Blank.prototype.prepare = function() {
	var blank = this.el;
	if (this.hasCheck) {
		var buttonCheck = makeButton("!");
		this.buttonCheck = buttonCheck;		
		var myBlank = this;
		buttonCheck.onclick = function() {
			var wasRight = myBlank.check();
			//if (wasRight) 						{this.disabled = true; this.value="correct";}
			//if (wasRight && myBlank.hasHint) 	{removeNode(buttonHelp);}
			if (!wasRight) 						{buttonCheck.value="try again!";}
			buttonCheck.blur();
			};
		insertAfterNode(blank, buttonCheck);
		}
	if (this.hasHint) {
		var buttonHelp = makeButton("?");
		this.buttonHelp = buttonHelp;		
		var myBlank = this;
		buttonHelp.onclick = function() {
			alert(myBlank.hintStr);
			};
		if(this.hasCheck) insertAfterNode(buttonCheck, buttonHelp)
			else insertAfterNode(blank, buttonHelp);
		}
	}

Blank.prototype.isEmpty = function() {
	if (!document.getElementById(this.id)) {return(false);}
	if (this.done) {return(false);}
	if (this.el.value =="") {return(true);}
	return(true);
}

//needs an h2 title
//if no ids, fill the blankArray BACKWARD
function prepareFillInTheBlanks(id, blankArray) {
	var theDiv = getById(id);
	takeOutReplaceable(id);
	var theButtons = document.createElement("div");
	//theButtons.style.align = "center";
	theButtons.appendChild(makeButton("Check", "butCheck"));
	theDiv.appendChild(makeCentered(theButtons));
	var keyText = makeTextNode("click '?' for hints.  Key: ", "small");
	var wrongText = makeTextNode("wrong answer ", "wrong");
	var rightText = makeTextNode(" right answer", "right");
	keyText.appendChild(wrongText);
	keyText.appendChild(rightText);
	theDiv.appendChild(makeCentered(keyText));
	

	theButtons.getElementsByTagName("input")[0].onclick = function() {
	//document.getElementById("butCheck").onclick = function() {
	 var allOK = true;
	 var anyBlank = false;
	 for (var i=0; i<blankArray.length; ++i) {
		if (blankArray[i].done==false)
			allOK = blankArray[i].check() && allOK; 
		if (blankArray[i].isEmpty()) {
				allOK=false;
				anyBlank = true;
				}
			}
	 if (allOK) {alert("great job!"); this.disabled=true; this.blur();}
	 else {alert("you have some wrong answers or blanks");	}	
	}
}

function prepareCheckBlank(id, ans, type, helpstr, prec, className) {
	if (!className) className = "right";
	if (!type) type = "float";
	if (helpstr="") var hasHint=false;
	var blank = findNode(id, "input");
	var objBlank = new Blank(id, ans,type, prec, true, hasHint, helpstr);
	var buttonCheck = document.createElement("input");
		buttonCheck.setAttribute("type", "button");
		buttonCheck.setAttribute("value", "!");
		buttonCheck.onclick = function() {
			var wasRight = objBlank.check();
			if (wasRight) {buttonCheck.disabled = true; }
			if (wasRight && buttonHelp) buttonHelp.disabled = true;
			if (!wasRight) alert("try again -- " + helpstr);
			};
		insertAfterNode(blank, buttonCheck);
	if (helpstr && helpstr!= "") {
		var buttonHelp = document.createElement("input");
		buttonHelp.setAttribute("type", "button");
		buttonHelp.setAttribute("value", "?");
		buttonHelp.onclick = function() {
			alert(helpstr);
			};
		insertAfterNode(buttonCheck, buttonHelp);
		}
	}

	
Blank.prototype.check = function() {
	var theBlank = this.el;
	var curr = theBlank.value;
	if ((this.type=="string" && this.ans == curr) ||
	 (this.type=="float" && Math.abs(this.ans - parseFloat(curr))<=this.tol) ||
	 (this.type=="int" && this.ans == parseInt(curr)) ) {
		replaceBlankByString(theBlank, this.ans, this.rightClassName);
		if (this.buttonCheck && this.buttonHelp) {
			this.buttonCheck.disabled = true; 
			this.buttonCheck.value="correct";
			removeNode(this.buttonHelp); }
		else if (this.buttonCheck) {
			this.buttonCheck.disabled = true; 
			this.buttonCheck.value="correct";}
		else if (this.buttonHelp) {
			this.buttonHelp.disabled = true;}
		this.done=true;
		return(true);
		}
	else if (curr=="") {
		return(false);
	}
	else {
		theBlank.className = this.wrongClassName;			
		if (this.buttonCheck) this.buttonCheck.value = "try again!";
		return(false);
		}
	}

function replaceBlankByString(theBlank, str, className) {
	replaceNode(theBlank, makeTextNode(str, className, "span"));}
	
//-------------prepareQHA------------------------------------------------
// hook= div, li = hints (use colons), h3 = question, h4 = answer
function prepareQHA(id) {
	takeOutReplaceable(id);
	var theDiv = document.getElementById(id);
	var theQu = theDiv.getElementsByTagName("h3")[0];
	var theHints = theDiv.getElementsByTagName("li");
	var theAns = theDiv.getElementsByTagName("h4")[0];
	//alert(theQu.innerHTML);
	//alert(theHints[0].innerHTML);
	//alert(theHints[1].innerHTML);
	//alert(theAns.innerHTML);
	//alert(id + " " + theHints.length +"hints");
	var numHints = theHints.length;
	for (var i=0; i<numHints; ++i) 
		makeButtonFromLi(theHints[0]);
	makeButtonFromAnswer(theAns);
	
}

function makeButtonFromLi(theLi) {
	//alert("make button");
	if (typeof(theLi)=="string") theLi = findNode(id, "li");
	if (theLi) {
		var theString = theLi.innerHTML.split(":");
		var buttonLi = document.createElement("input");
		buttonLi.setAttribute("type", "button");
		buttonLi.setAttribute("value", theString.shift());
		if (theString.length > 1) theString = theString.join(":");
		buttonLi.onclick = function() {
			makeWindow(theString, "one hint coming up...");
			};
		replaceNode(theLi, buttonLi);
		}
}

function makeButtonFromAnswer(theAns) {
	if (theAns) {
		var theString = theAns.innerHTML.split(":");
		var buttonAns = document.createElement("input");
		buttonAns.setAttribute("type", "button");
		buttonAns.setAttribute("value", theString.shift());
		if (theString.length > 1) theString = theString.join(":");
		//alert(theString);
		buttonAns.onclick = function() {
			makeWindow(theString, "and the answer is...");
			};
		replaceNode(theAns, buttonAns);
		}
}

//---------------------rollovers--------------------------------------------

function prepareRollover2(id, tosee) {
	var imgs = document.getElementById(id).getElementsByTagName("img");
	var outsrc = imgs[0].getAttribute("src");
	var oversrc = imgs[2].getAttribute("src");
	//alert(outsrc +" "+ oversrc);
	removeNode(imgs[2]);
	removeNode(imgs[1]);
	var msg = "place mouse on image";
	if (tosee) msg += " to see " + tosee;
	document.getElementById(id).getElementsByTagName("td")[0].firstChild.nodeValue = msg;
	imgs[0].onmouseover = function() {imgs[0].src = oversrc; }
	imgs[0].onmouseout = function() {imgs[0].src = outsrc; }
	}	
	
function prepareRollover1(id, tosee) {
	var theImg = document.getElementById(id).getElementsByTagName("img")[0];
	var outsrc = theImg.getAttribute("longdesc");
	var oversrc = theImg.getAttribute("src");
	theImg.src = outsrc;
	var msg = "place mouse on image";
	if (tosee) msg += " to see " + tosee;
	document.getElementById(id).getElementsByTagName("td")[0].firstChild.nodeValue = msg;
	theImg.onmouseover = function() {theImg.src = oversrc; }
	theImg.onmouseout = function() {theImg.src = outsrc; }

	}
	

//---------------------------------------Derivations
//hook = table with id	
function prepareDerivation(id) {
	takeOutReplaceable(id);
	var theRows = document.getElementById(id).getElementsByTagName("tr");
	var numRows = theRows.length;
	var hintArray = new Array(numRows);
	var explanArray = new Array(numRows);
	for (var i=1; i<numRows; ++i) 	 {
		var theCells = theRows[i].getElementsByTagName("td");
		hintArray[i] = theCells[1].innerHTML;
		explanArray[i] = theCells[2].innerHTML;
		var h = hintArray[i];
		var e = explanArray[i];
		theCells[1].innerHTML = "&nbsp;";
		theCells[2].innerHTML = "&nbsp;";
		var theButton = theRows[i].getElementsByTagName("input")[0];
		theButton.value = "print hint..."
		defineClick(theButton, theCells, hintArray, explanArray, i);
			
		}
	}

function defineClick(theButton, cells, hintArray, explanArray, i) {
	theButton.onclick = function() {
	if (theButton.value == "print hint...") {
		cells[1].innerHTML = hintArray[i];
		theButton.value = "print explanation..."; 
		}
	else if (theButton.value == "print explanation...") {
		cells[2].innerHTML = explanArray[i];
		theButton.value = "hide explanation...";
		}
	else if (theButton.value == "hide explanation...") {
		cells[2].innerHTML = "&nbsp;";
		theButton.value = "hide hint...";
		}
	else if (theButton.value == "hide hint...") {
		cells[1].innerHTML = "&nbsp";
		theButton.value = "print hint...";
		}
	}
}	
	
	
//------------------------Quiz-----------------------------------------------------------
function prepareQuiz(id, idAns) {
	var theQuList = document.getElementById(id).getElementsByTagName("table")[0];
	var numTables = document.getElementById(id).getElementsByTagName("table").length;
	if (numTables != 2) 
		alert("DANGER -- need 2 tables, have #" + document.getElementById(id).getElementsByTagName("table").length);
	var theQu = theQuList.getElementsByTagName("tr");
	var numQu = theQu.length-1;
	var theAns = readAnswers(idAns, numQu);
	takeOutReplaceable(id);
	var checkButton = makeButton("check");
	insertAfterNode(theQuList, checkButton);
	var theNames = getQuizNames(theQuList);
	checkButton.onclick = function() {
		takeOutReplaceable(id);
		var allGood = true;
		for (var i = 0; i<numQu; ++i) {
			//alert([theNames[i], ">>"+whichRadioAlt(theQuList, theNames[i])+"<<", ">>"+theAns[i]]+"<<");
			//alert(whichRadioAlt(theQuList, theNames[i]) == theAns[i]);
			if(whichRadioAlt(theQuList, theNames[i]) != theAns[i]) {
				theQu[i+1].getElementsByTagName("td")[0].appendChild(makeTextNode("X", "replaceable"));
				allGood = false;
				}
			}
		if (!allGood) alert("check the answers with x's"); 
			else alert("great job");
		var theNodes = document.getElementById(id).getElementsByTagName("span");
		for (var i = 0; i<theNodes.length; ++i) {
			if (theNodes[i].className == "hidefirst") theNodes[i].style.visibility = "visible";
			}
		this.disabled = "yes";
		 } //checkButton.onclick
}	

function getQuizNames(table) {
	var theNames = new Array();
	var theRows = table.getElementsByTagName("tr");
	var numQu = theRows.length-1;
	for (var i = 0; i < numQu; ++i) 
		theNames[i] = theRows[i+1].getElementsByTagName("input")[0].name;
	return(theNames);	
	}
	
function readAnswers(idAns, numQu) {
	var theAns = document.getElementById(idAns).childNodes[0];
	if (theAns.nodeType!=3) alert("DANGER -- the answers are not correctly formatted");
	var ansArray = theAns.nodeValue.split(", ");
	if (ansArray.length != numQu) alert("DANGER -- " + numQu + " questions, but " + ansArray.length + " answers");
	return(ansArray);	
	}

function getRadioGroup(container, name) {	
	var theInputs = container.getElementsByTagName("input");
	var theRadios = new Array();
	var r=0;
	for (var i = 0; i < theInputs.length; ++i) {
		if (theInputs[i].name == name && theInputs[i].type == "radio")
			theRadios[r++] = theInputs[i];
		}
	return(theRadios);
}

function whichRadioNum(container, name) {
	var theRadios = getRadioGroup(container, name);
	for (var i = 0; i < theRadios.length; ++i) {
		if (theRadios[i].checked == true) return(i);	
		}
	return(0);
	}	
	
function whichRadioLabel(container, name) {
	var theRadios = getRadioGroup(container, name);
	for (var i = 0; i < theRadios.length; ++i) {
		if (theRadios[i].checked == true) return(theRadios[i].nextSibling.nodeValue);
		}
	return("");
	}
	
function whichRadioAlt(container, name) {
	var theRadios = getRadioGroup(container, name);
	for (var i = 0; i < theRadios.length; ++i) {
		if (theRadios[i].checked == true) return(theRadios[i].getAttribute("alt"));
		}
	return("");
	}
	
function togglePic(elBut, idPic) {
	if (getById(idPic).style.visibility == "hidden" || getById(idPic).style.visibility == "") {
		getById(idPic).style.visibility = "visible";
		elBut.value = "Hide the graph...";
		}
	else {
		getById(idPic).style.visibility = "hidden";
		elBut.value = "See the graph...";
		}	
	}

function prepareTogglePic(idBut, idPic) {
	getById(idPic).style.visibility = "hidden";
	getById(idBut).onclick = function() {togglePic(this, idPic);}
}
	
