//  Copyright 2000-2010 Gisele Glosser.  All Rights Reserved.
//
//  Our interactive math lessons are shareware.  You may use our free demo lessons
//  on mathgoodies.com only.  You may NOT download nor save them.  You can purchase
//  a license to use all 135 of our lessons off line by visiting:
//  http://www.mathgoodies.com/cd/


// Get choice selected in form
function getChoice(whichSelected) {
	var index = 0;
	for (i = 0; i < whichSelected.length; i++) {
		if (whichSelected[i].checked) {
			return i;
		}
	}
	return 0;

}

// Compare form input with correct answer and output response accordingly
function checkChoice(form, whichQuestion) {

        var optionChosen = getChoice(form.entry);
        
        if ((whichQuestion == 5) && (form.entry[optionChosen].value == "Choice 2") || 
           	(whichQuestion == 6) && (form.entry[optionChosen].value == "Choice 1") ||
          	(whichQuestion == 7) && (form.entry[optionChosen].value == "Choice 3") ||
            (whichQuestion == 8) && (form.entry[optionChosen].value == "Choice 1") ||
            (whichQuestion == 9) && (form.entry[optionChosen].value == "Choice 4") ||
            (whichQuestion == 10) && (form.entry[optionChosen].value == "Choice 2")) {
                form.result.value = "Correct!";
        } else {
        	choice = optionChosen+1;
            form.result.value = "Choice " + choice + " is incorrect.  Please try again.";
       	}     
}

// Check to see if answer is valid
function checkInteger(form, whichQuestion) {
	var msg;
	var noErrors = true;
	var testValue = form.entry.value;
	if (testValue == "" || testValue == null) {
		msg = "Please enter an integer in the ANSWER BOX before clicking ENTER.";
		noErrors = false;
	} else {
		var tempStr = testValue.toString();
		for (var index = 0; index < tempStr.length; index++) {
		    var testOneChar = tempStr.charAt(index);
		    if ((index == 0 && testOneChar == "-") || (index == 0 && testOneChar == "+")) {
		        continue;
		    }
			if (testOneChar < "0" || testOneChar > "9") {
				msg = "Please enter an integer.";
				noErrors = false;
			}
		}		
	}
	if (noErrors) {
			checkAnswer(form, whichQuestion);
	} else {
			alert(msg);
			form.entry.focus();
			form.entry.select();
	}
}

function checkData(form, whichQuestion) {
	var msg;
	var noErrors = true;
	var testValue = form.entry.value;
	if (testValue == "" || testValue == null) {
		msg = "Please enter a number in the ANSWER BOX before clicking ENTER.";
		noErrors = false;
	} else {
		var tempVal = parseInt(testValue);
		var tempStr = tempVal.toString();
		if (testValue != tempStr || tempVal < 0) {
			msg = "Please enter a whole number.";
			noErrors = false;
		}		
	}
	if (noErrors) {
			checkAnswer(form, whichQuestion);
	} else {
			alert(msg);
			form.entry.focus();
			form.entry.select();
	}
}
	
// Compare form input with correct answer and output response accordingly
function checkAnswer(form, whichQuestion) {
        var stringEntered = form.entry.value;
        if ((whichQuestion == 1 && stringEntered == 975) ||  
            (whichQuestion == 2 && stringEntered == 392) || 
            (whichQuestion == 3 && stringEntered == 32) || 
            (whichQuestion == 4 && stringEntered == -2)) {
                form.result.value = "Correct!";
        } else {
            form.result.value = "Incorrect.  Please press CLEAR and try again.";
        }                     
        form.entry.focus();
        form.entry.select();
}


// Clear a form
function clearForm(form) {
	form.entry.value="";
	form.entry.focus();
    form.entry.select();
}
