/**
 * Get correct exercise solution 'div' within element indicated by parameter id.
 *
 * @param id String id of problem
 * @param whichSolution Number determines which of the potentially multiple solutions is the correct one
 * @return nsIDOMElement 'div' of solution
 */
function getExerciseSolution(id,whichSolution) {
    descendantDivs = document.getElementById(id).getElementsByTagName("div");
    solutionDivs = new Array();
    for (i = 0, j = 0; i < descendantDivs.length; i++) {
	if (descendantDivs[i].className == 'solution') {
	    solutionDivs[j]=descendantDivs[i];
	    j++;
	}
    }
    return solutionDivs[whichSolution - 1];
}

/**
 * Get correct solution button 'div' within element indicated by parameter id.
 *
 * @param id String id of problem
 * @param whichSolution Number determines which of the potentially multiple solutions is the correct one
 * @return nsIDOMElement 'div' of solution button
 */
function getSolutionButton(id,whichSolution) {
    descendantDivs = document.getElementById(id).getElementsByTagName("div");
    buttonDivs = new Array();
    for (i = 0, j = 0; i < descendantDivs.length; i++) {
	if (descendantDivs[i].className == 'button') {
	    buttonDivs[j]=descendantDivs[i];
	    j++;
	}
    }
    return buttonDivs[(whichSolution - 1) * 2]; // there are 2 "buttons" for each solution. take the first of each pair.
}

/**
 * Show the solution of problem indicated by id.
 *
 * @param id String id of problem
 * @param whichSolution Number determines which of the potentially multiple solutions is the correct one
 */
function showSolution(id,whichSolution) {
    getExerciseSolution(id,whichSolution).style.display="block";
    getSolutionButton(id,whichSolution).style.display="none";
}

/**
 * Hide the solution of problem indicated by id.
 *
 * @param id String id of problem
 * @param whichSolution Number determines which of the potentially multiple solutions is the correct one
 */
function hideSolution(id,whichSolution) {
    getExerciseSolution(id,whichSolution).style.display="none";
    getSolutionButton(id,whichSolution).style.display="block";
}

