// Globals
var thebgColor = "DDFFDD";
var X = 0;
var x = 0;
var y = 0;
var h = 0;
var xh = 0;
var hx = 0;
var t = 0;
var th = 0;
var ht = 0;
var a = 0;
var b = 0;
var guesses = 0;
var quoteMark = unescape( '%22' );
var singlequoteMark = unescape( '%27' );

// *** Error Handler ******
function myErrorTrap(message,url,linenumber) {
this.parent.bottom.window.location = "wrong.html";
return (true);
} // end of on error
// ********************

function writeHeading(theTitle) {
var theText = '<center><table width = 90%><tr><td nowrap class = myButtons width = 80 align = center>' + theTitle + '</td><td nowrap><img src = ' + quoteMark + 'elts/buttonbg.gif' + quoteMark + ' width = 100% height = 4></td></tr></table></center>'
document.writeln(theText);
}

function drawLine() {
var theText = '<center><table width = 90%><tr><td><img src = ' + quoteMark + 'elts/buttonbg.gif' + quoteMark + ' width = 100% height = 4></td></tr></table></center>'
document.writeln(theText);
}


function sesame(url,hsize,vsize){ 
// Default size is 550 x 400
        var tb="toolbar=0,directories=0,status=0,menubar=0"
        tb+=",scrollbars=1,resizable=1,"
    var tbend="width="+hsize+",height="+vsize;
    if(tbend.indexOf("<undefined>")!=-1){tbend="width=550,height=400"}
        tb+=tbend;
        Win_1 = window.open(url,"win1",tb);
	Win_1.focus();
    }


// Bottom Window handlers

var theScriptString = '<STYLE TYPE=' + quoteMark + 'text/css' + quoteMark + '>BODY, LAYER, P, DIV, TABLE, TR, TD, TH {font-size: 12px; font-family: times; color: 000000;margin-top: 0pt}.MATH {font-family: Times; font-size: 12px; font-style: italic; }td.lb {font-size: 2px;border-left: 1px black solid;border-bottom: 1px black solid;border-top: 1px black solid;}.rb {font-size: 2px;border-right: 1px black solid;border-bottom: 1px black solid;border-top: 1px black solid;}</STYLE>';

function writeBottomWrong(theMessage) {
var theString = "";
if (theMessage == "") theString = '<HTML>' + theScriptString + '<BODY bgcolor = "' + thebgColor + '" text ="#050005"><img src = "../../calcTutElts/greenborder.gif" width = 8 height = 100% align = right hspace = 0><center><img src = "../../calcTutElts/wrong.gif">';
else theString = '<HTML>' + theScriptString + '<BODY bgcolor = "' + thebgColor + '" text ="#050005"><img src = "../../calcTutElts/greenborder.gif" width = 8 height = 100% align = right hspace = 0><center><table><tr><td align = center><img src = "../../calcTutElts/smallx.gif"><br><table><tr><td>'
theString += theMessage ;
theString += '</td></tr></table></center>';
this.parent.bottom.document.write(theString);
this.parent.bottom.document.close();
} // writeBottomWrong

function writeBottomCorrect() {
var theString = '<HTML>' + theScriptString + '<BODY bgcolor = "' + thebgColor + '" text ="#050005"><img src = "../../calcTutElts/greenborder.gif" width = 8 height = 100% align = right hspace = 0><center><img src = "../../calcTutElts/check.gif"></center>'
this.parent.bottom.document.write(theString);
this.parent.bottom.document.close();
} // writeBottomCorrect

function writeBottomSmile(theMessage,x) {
// x = 1 gives one smile (with a space or text as the message)
// x = 2 gives another
// x = 3 gives a third
var theString = "";
if ((theMessage == "")&& (pageLanguage == "en")) theString = '<HTML>' + theScriptString + '<BODY bgcolor = "' + thebgColor + '" text ="#050005"><img src = "../../calcTutElts/greenborder.gif" width = 8 height = 100% align = right hspace = 0><center><img src = "../../calcTutElts/bigsmiley.gif">';
else if ((theMessage == "")&& (pageLanguage == "es")) theString = '<HTML>' + theScriptString + '<BODY bgcolor = "' + thebgColor + '" text ="#050005"><img src = "../../calcTutElts/greenborder.gif" width = 8 height = 100% align = right hspace = 0><center><img src = "../../calcTutElts/bigsmileyes.gif">';
else if (x == 2) theString = '<HTML>' + theScriptString + '<BODY bgcolor = "' + thebgColor + '" text ="#050005"><img src = "../../calcTutElts/greenborder.gif" width = 8 height = 100% align = right hspace = 0><center><img src = "../../calcTutElts/smiley2.gif"><br><table><tr><td>';
else if (x == 3) theString = '<HTML>' + theScriptString + '<BODY bgcolor = "' + thebgColor + '" text ="#050005"><img src = "../../calcTutElts/greenborder.gif" width = 8 height = 100% align = right hspace = 0><center><img src = "../../calcTutElts/smiley3.gif"><br><table><tr><td>';
else if (x == 4) theString = '<HTML>' + theScriptString + '<BODY bgcolor = "' + thebgColor + '" text ="#050005"><img src = "../../calcTutElts/greenborder.gif" width = 8 height = 100% align = right hspace = 0><center><img src = "../../calcTutElts/smileyMex.gif"><br><table><tr><td>';
else theString = '<HTML>' + theScriptString + '<BODY bgcolor = "' + thebgColor + '" text ="#050005"><img src = "../../calcTutElts/greenborder.gif" width = 8 height = 100% align = right hspace = 0><center><img src = "../../calcTutElts/smiley.gif"><br><table><tr><td>'
theString += theMessage ;
theString += '</td></tr></table></center>';
this.parent.bottom.document.write(theString);
this.parent.bottom.document.close();
} // writeBottomWrong

function writeBottomMmmh(theMessage) {
var theString = '<HTML>' + theScriptString + '<BODY bgcolor = "' + thebgColor + '" text ="#050005"><img src = "../../calcTutElts/greenborder.gif" width = 8 height = 100% align = right hspace = 0><center><table><tr><td align = center><img src = "../../calcTutElts/mmh.gif"><br>'
theString += theMessage ;
theString += '</td></tr></table></center>';
this.parent.bottom.document.write(theString);
this.parent.bottom.document.close();
} // writeBottomMessage

function writeBottomMessage(theMessage) {
var theString = '<HTML>' + theScriptString + '<BODY bgcolor = "' + thebgColor + '" text ="#050005"><img src = "../../calcTutElts/greenborder.gif" width = 8 height = 100% align = right hspace = 0><center><table><tr><td>'
theString += theMessage ;
theString += '</td></tr></table></center>';
this.parent.bottom.document.write(theString);
this.parent.bottom.document.close();
} // writeBottomMessage

function writeBottomYell(theMessage) {
var theString = '<HTML>' + theScriptString + '<BODY bgcolor = "' + thebgColor + '" text ="#050005"><img src = "../../calcTutElts/greenborder.gif" width = 8 height = 100% align = right hspace = 0><center><table><tr><td><center><img src = "../../calcTutElts/yell.gif"></center>'
theString += theMessage ;
theString += '</td></tr></table></center>';
this.parent.bottom.document.write(theString);
this.parent.bottom.document.close();
} // writeBottomMessage

function writeBottomYell2(theMessage) {
var theString = '<HTML>' + theScriptString + '<BODY bgcolor = "' + thebgColor + '" text ="#050005"><img src = "../../calcTutElts/greenborder.gif" width = 8 height = 100% align = right hspace = 0><center><table><tr><td align = center><img src = "../../calcTutElts/yell2.gif"><br>'
theString += theMessage ;
theString += '</td></tr></table></center>';
this.parent.bottom.document.write(theString);
this.parent.bottom.document.close();
} // writeBottomMessage


// ********* Answer checking routines *********
// the following two routines are can be used in both tutorials and 
// review exercises to do answer checking and "peeking"
// handles both strings and numerical values.

// These routines use one or two other string utilities in this file
// example of html code that uses this:
// 
// <form name = "Form1">
//  <input type=text size=35 value="" name="Q1a">
// <input type="button" value = "Check" onClick = 
// "if (checkAnswer('Form1','Q1a',3,3.1419)) winopen('correct.html','bottom'); 
// else winopen('wrong.html','bottom') ">
// <input type="button" value = "Peek" onClick="togglePeek('Form1','Q1a', '3.1419')">
//

function checkAnswer(formName,fieldName,Option,correctAnswer) {
// returns true if the answer is correct, else returns false
// Option1 compares the string (spaces stripped case insensitive) with the desired string
// Option 2 compares a modified string: changes X to x, Y to y, Z to z, 
// and removes * before comparing
// Option 3 evaluates the string 
// Option 4 checks to see if the string is blank & returns true if blank
// Option 5 here is for 5 decimal places accuraqcy

var theCommand = ""; 
var theString = "";
var a1 = eval("theString = stripSpaces(document."+ formName + "." + fieldName + ".value"+")" );
if (theString != "") {
if (Option == 1)
	{
	var act1 = eval("theString = stripSpaces(document."+ formName + "." + fieldName + ".value"+")" );
	if (theString.toLowerCase() == correctAnswer.toLowerCase()) return (true);
	else return (false);
	} // Option 1
else if (Option == 2)
	{
	var act1 = eval("theString = stripSpaces(document."+ formName + "." + fieldName + ".value"+")" );
	theString = replaceChar (theString,"X","x");
	theString = replaceChar (theString,"Y","y");
	theString = replaceChar (theString,"Z","z");
	theString = replaceChar (theString,"A","a");
	theString = replaceChar (theString,"B","b");
	theString = replaceChar (theString,"C","c");
	theString = stripChar(theString,"*");
	if (theString == correctAnswer) return (true);
	else return (false);
	} // Option 2
else if (Option == 3)
	{
	var act1 = eval("theString = document."+ formName + "." + fieldName + ".value" );
	// check to see if the string looks is ok for eval
	if (looksLikeANumber(theString)) 
		{
		var x = eval(theString);
		var err = Math.abs(x - correctAnswer);
		if (err < 0.00005) return (true);
		else return (false);
		}
	else return (false);
	return(false);
	} // Option 3

	else if (Option == 5)
	{
	var act1 = eval("theString = document."+ formName + "." + fieldName + ".value" );
	// check to see if the string looks is ok for eval
	if (looksLikeANumber(theString)) 
		{
		var x = eval(theString);
		var err = Math.abs(x - correctAnswer);
		if (err < 0.000005) return (true);
		else return (false);
		}
	else return (false);
	return(false);
	} // Option 5

} // endif non-empty
else if (Option == 4) return(true); // in case of empty string
}


function checkAnswerandValue(formName,fieldName,correctAnswer,minLength, maxLength, maxTolerance, assig1,assig2,assig3, assig4) {
// use this to check for simplified expressions
// evaluates the input string in the specified form 
// & returns 1 if the answer is correct (agrees with correctAnswer)
// to within 0.00005 and is the right length otherwise set "maxerror" = whatever
// as one of the assignments
// returns 2 if the answer is right but string too long (unsimplified)
// returns 3 if answer is right but string too short 
// returns 0 if the answer is blank 
// else returns 6;
// it evalutes the input string at sthe specified values 
// Each assigi has the form "x=2" or "h=2.3" 
// minLength & maxLength are the permissable ranges of an input string
// maxTolerance is the largest possible error
// NOTE: requires GLOBAL variables x, h, or whatever is assigned

var theString = "";
var maxerror = maxTolerance; 	
var a1 = eval("theString = stripSpaces(document."+ formName + "." + fieldName + ".value"+")" );
theString = theString.toLowerCase();
if (assig1 != "") var b1 = eval(assig1);  // set the values equal to numbers
if (assig2 != "") var b2 = eval(assig2);
if (assig3 != "") var b3 = eval(assig3);
if (assig4 != "") var b4 = eval(assig4);

if (theString == "") return(0);

else {
	theString = stripChar (theString, "$");		// for dollar signs
	theString = stripChar (theString, ",");		// for longer numbers
	theString = theString.toLowerCase();
	var val = myEval (theString);
	var correctVal = myEval(correctAnswer);
	var err = Math.abs(val - correctVal);
	if (err < maxerror)
		{
		if (theString.length > maxLength) return(2);
		else if  (theString.length < minLength) return(3);
		else return(1);
		}
	else return (6);
	} // endif non-empty
} // end checkAnswerandValue



function togglePeek(formName,fieldName,expression) {
// displays the given expression in formName.fieldname if not already visible
// if already visible, it hides it

var theCommand = "";
var theString = "if (document." + formName + "." + fieldName + ".value != '" + expression + "') document." + formName + "." + fieldName + ".value = '" + expression + "';  else document." + formName + "." + fieldName + ".value = '';"
// alert(theString)
var act1 = eval(theString);
return(true);
}

// ************** end of answer handling routines *************

