var graphic_blank = "blank.gif";
var graphic_yes   = "yes.gif";
var graphic_no    = "no.gif";

window.onerror = stopError;

function stopError() { return true; }

function unFrame() {
    if (self != top)
    top.location.href = self.location.href;
}


function donothing(){}

function resetgraphics(ref) {
    k = 0;
    i = 0;
    for (i = 0; i < ref.elements.length; i++) {
        if (ref.elements[i].type == 'radio') {
            i += eval("temp = ref." + ref.elements[i].name + ".length")-1;
            k++;
        }
    }
    for (i = 1; i <= k; i++)	{
          document.getElementById("img"+i).src = graphic_blank;
    }
}

function grade(ref) {

    var numberRight = 0;
    var blank = 0;

    // array of radio buttons:  radioArray[i:question#][j:answer#]
    radioArray = new Array();
    i = 0;
    for (j = 0; j < ref.elements.length; j++) {
        if (ref.elements[j].type == 'radio') {
            eval("radioArray[i] = ref." + ref.elements[j].name);
            j += radioArray[i].length-1;
            i++;
        }
    }
    for (i = 0; i < radioArray.length; i++)	{
        count = 0;
        correct = 0;
        for (j = 0; j < radioArray[i].length; j++) {
            if (!radioArray[i][j].checked) { count++; }
            else if (radioArray[i][j].value=="R") {correct=1};
        }
        document.getElementById("img"+eval(i+1)).src = 
            (correct)? graphic_yes : graphic_no;
        if (correct) numberRight++;
        if (count == radioArray[i].length) blank++;
    }
    var possible=radioArray.length;
    var perc = 100*numberRight/possible;
    perc -= (perc%1);

    if (numberRight<possible) { 
	  var message = "You answered "+numberRight+" out of "+possible+" questions correctly. ("+perc+"%)\n" +
	 		        "Incorrect answers are marked with a red X.";
      if (blank > 0) message+="\nYou left "+blank+" questions blank.";
    } else var message = "You answered all "+possible+" questions correctly!\n";
    alert (message);

    return false;

}
