//  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/


var msg = "";
var tempWindow;
var newTags;

// Check to see if answer is valid
function checkData(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();
	}
}
	
// Compare form input with correct answer and output response accordingly
function checkAnswer(form, whichQuestion) {
        var stringEntered = form.entry.value;
        if ((whichQuestion == 1 && stringEntered == "14") ||
            (whichQuestion == 1 && stringEntered == "+14") || 
            (whichQuestion == 2 && stringEntered == "-2") || 
            (whichQuestion == 3 && stringEntered == "122") || 
            (whichQuestion == 3 && stringEntered == "+122") || 
            (whichQuestion == 4 && stringEntered == "-3") ||
            (whichQuestion == 5 && stringEntered == "-30")) {
                form.result.value = "Correct!";
        } else {
            form.result.value = "Incorrect.  Please press CLEAR and try again.";
        }                     
        form.entry.focus();
        form.entry.select();
}

// Control message in the status bar
function setMsg(msg) {
	window.status = msg;
	return true;
}

// Clear a form
function clearForm(form) {
	form.entry.value="";
	form.entry.focus();
    form.entry.select();
}


// if second window is open, then close it
function closeSecondWindow() {
	if (tempWindow && !tempWindow.closed) {
		tempWindow.close();
	tempWindow = false;
	return tempWindow;
	}
}

// Determine which definition will be displayed in the second window
function whichDef(Def) {
	if (Def == "integers") {
		msg = "<STRONG>Integers</STRONG>" + " are whole numbers that describe opposites.";
	} else {
		if (Def == "fraction_bar") {
			msg = "A" + " <STRONG>fraction bar</STRONG>" + " separates the numerator and denominator of a fraction.  It indicates "; 
			msg += "that a division of the numerator by the denominator will be performed.  Another name for a fraction bar is a ";
			msg += "vinculum.";
		} 
	}

return msg;
}

// This function opens a small window that contains a Math definition.
function popUpWindow(definition) {
	passDef = definition;
	whichDef(passDef);
	// assemble HTML for new window
	var newTags = "<HTML><HEAD><TITLE>Definition Window</TITLE></HEAD>";
	newTags += "<BODY BGCOLOR='#FFFFFF'>";
	newTags += "<H2>Definition</H2>";
	newTags += msg + "<P>";
	newTags += "<form><input TYPE='button' value='close window' name='closeup' onClick='opener.closeSecondWindow()'></form>";
	newTags += "</BODY></HTML>";
		
	if (!tempWindow || tempWindow.closed) {
		tempWindow = window.open ("temp.html","tempWindow", "toolbar=no,width=500,height=180,directories=no,status=no,scrollbars=yes,resize=yes,menubar=no");

		if (!tempWindow.opener) {
			tempWindow.opener = window;
		}
	
		timerDelay(newTags);
	} else {
		timerDelay(newTags);
	}
	
}

// timer delay
function timerDelay(content) {
	getContent	= content;
	setTimeout("timerDelay",1000);
	
	
	// write HTML to second window
	tempWindow.document.write(getContent);
	tempWindow.document.close(); // end of writing to the Temp window
	tempWindow.focus();
	
}






