//  Copyright 2009 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 124 of our lessons off line by visiting:
//  http://www.mathgoodies.com/cd/


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function checkData(form, whichQuestion) {
	var msg;
	var noErrors = true;
	var testValue = form.entry.value;
	var testChar = "";
	var testZero = "";
	var i = 0;
	var temp = "false";
	if (testValue == "" || testValue == null) {
		msg = "Please enter a number in the ANSWER BOX before clicking ENTER.";
		noErrors = false;
	} else {
		testZero = testValue.toString();
		for (i = 0; i < testZero.length; i++) {
			if (isNaN(testZero))  {
				msg = "Please enter a number.";
				noErrors = false;
				break;
			}
	
		}		
	}
	
	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 == "2.89") || 
            (whichQuestion == 2 && stringEntered == "1.95")) { 
                form.result.value = "Correct!";
        } else {
            form.result.value = "Incorrect.  Please press CLEAR and try again.";
        }                     
        form.entry.focus();
        form.entry.select();
}

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 == 3) && (form.entry[optionChosen].value == "choice3") || 
           	(whichQuestion == 4) && (form.entry[optionChosen].value == "choice2") ||
            (whichQuestion == 5) && (form.entry[optionChosen].value == "choice4")) {
                form.result.value = "Correct!";
        } else {
        	choice = optionChosen+1;
            form.result.value = "Choice " + choice + " is incorrect.  Please try again.";
       	}     
}

var msg = "";
var tempWindow;
var newTags;



// 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 == "decimal_digits") {
		msg = "All digits to the right of the decimal point, that name the fractional part of the number, are " + "<strong>decimal digits</strong>.";
	} else {
		 	if (Def == "equivalent_fraction") {
				msg = "<STRONG>Equivalent fractions</STRONG> are different fractions that represent the same number. ";
				msg += "For example, the three fractions below are equivalent since each one represents five ";
				msg += "tenths (.5).<BR>";
				msg += "<IMG SRC='images/equivalent_fractions.gif' ALT='[IMAGE]' WIDTH='55' HEIGHT='19' BORDER='0'>";
					} 
			}
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
	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=220,directories=no,status=no,scrollbars=yes,resize=yes,menubar=no");

		if (!tempWindow.opener) {
			tempWindow.opener = window;
		}
	
		timerDelay(newTags);
	} else {
		timerDelay(newTags);
	}
	
}

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();
	
}








