// JavaScript Document

//replaces button with text
//for snippet, use "Replace button with answer" or "Replace choices inline"
function withinAbs(answer, ideal, perc, absol) {
	if (Math.abs(answer-ideal)<Math.abs(perc*ideal) ||
		Math.abs(answer-ideal)<absol		) {return ideal;} else {return false;}
	}
function within(answer, ideal, perc) {
	if (Math.abs(answer-ideal)<Math.abs(perc*ideal) ) {return ideal;} else {return false;}
	}
	
function replaceButton(text, divID) {
	document.getElementById(divID).innerHTML=text;
	}

function moveIntoImage(img_name,img_src) {
	document[img_name].src=img_src;
	}

function moveOutOfImage(img_name,img_src) {
	document[img_name].src=img_src;
	}

gifArray=new Array(); gifArray[0]='housefly.gif';

function ranGif() {
   var rannum=Math.floor(Math.random()*gifArray.length);
   return gifArray[rannum];
}

function wrong(explan) {moduleWin(explan, 300, "Sorry, that\'s wrong..." );}
function wrong(explan, ht) {moduleWin(explan, ht, "Sorry, that\'s wrong..." );}

function right(explan) {moduleWin(explan, 300, "That\'s right..." );}
function right(explan, ht) {moduleWin(explan, ht, "That\'s right..." );}

function answer(explan) {moduleWin(explan, 300, "The answer is..." );}
function answer(explan, ht) {moduleWin(explan, ht, "The answer is..." );}

function hint(explan) {moduleWin(explan, 300, "Here\'s a hint..." );}
function hint(explan, ht) {moduleWin(explan, ht, "Here\'s a hint...");}

function moduleWin(explan, ht, head) {
   var newwin = window.open("", "", "width=300,height="+ht+",left=100,screenX=100,top=100,screenY=100");
   var str = "<html><head></head><body><table border='0' height='90%' width='100%'><caption align='bottom'>";
   str += "<input value='Close' type='button' onClick='window.close();'></caption>";
   str += "<tr><td><h1>"+head+"</h1></td>";
   str += "<td><img src="+ranGif()+"></td></tr>";
   str += "<tr><td align='top' colspan='2'>"+explan+"</td>";
   str += "</tr>";
   str += "</table></body></html>";
   newwin.document.write(str);
   newwin.document.close();
   }

///////////////////////// Used for derivations
function DerivTable(question, derivArr) {
	this.question=question;
	this.derivArr = derivArr;
	this.writeDeriv();
}

DerivTable.prototype.writeDeriv = function() { 
var str = '<form action="" method="get" name="deriv" class="deriv">' + 
  '<div align="center">' + 
    '<h2>'+this.question+'</h2>' + 
    '<span class="small">Clicking once on the button will give you a suggested course of action<br></span>&nbsp; &nbsp; ' + 
	'<span class="small">Clicking a second time will print details of that step</span> </div>' + 
  '<br> <div align="center">' + 
  '<table class="datatable">' + 
  '<tr ><td width="30%">Step</td><td>Details</td></tr>';
  document.write(str);
  for(var i=0; i<this.derivArr.length; ++i)
  	{this.derivArr[i].write();}
  var str1 = '</table></div><br>' + 
  '<div align="center"><input name="Input2" type="button" value="Clear" onClick="clearDeriv();"></div>' + 
'</form>';
document.write(str1);
}
function DerivStep(obj) {
	if(!obj) obj={};
	this.prompt = obj.prompt;
	this.detail = obj.detail;
	this.id = "_der_tf"+obj.i;
	this.fullid = "_der_full"+obj.i;
	this.buttonid = "_der_button"+obj.i;
	this.i = obj.i;
	}	
	
DerivStep.prototype.clear = function() {
	var button = document.getElementById(this.buttonid);
	var j = this.i+1;
	button.value="Step " + j;
	document.getElementById(this.id).innerHTML="";		
	}

DerivStep.prototype.write = function() {
	var j=this.i+1;
	var str = "<div id='"+this.fullid+"'><tr>";
	str += "<td><input id="+this.buttonid+" type='button' value='step "+j+"' onClick='derArr["+this.i+"].click();'></td>";
	str += "<td><div id='"+this.id+"'></div></td></tr></div>"
	document.write(str);
	}

DerivStep.prototype.click = function() {
	var button = document.getElementById(this.buttonid);
	if (button.value==this.prompt) {
		button.value="close";
		document.getElementById(this.id).innerHTML=this.detail;
	} else if (button.value=="close") {
		var j = this.i+1;
		button.value="Step " + j;
		document.getElementById(this.id).innerHTML="";		
	} else {
		button.value=this.prompt;
	}
}

function clearDeriv() {clearDeriv1(0, derArr.length);}
function clearDeriv1(start, stop) {
	for (var i=start; i<stop; ++i) 
		{derArr[i].clear();}
	}

///////////////////////// Used for fill-in blank form
function Blank(obj) {
	if(!obj) obj={};
	this.ans = obj.ans;
	this.hint = obj.hint || "no hint, sorry";
	this.size = obj.size || 5;
	this.id = obj.id;
	this.i = obj.i;
	}	
Blank.prototype.givehint = function() {hint(this.hint, 300);}
	
Blank.prototype.check = function() {
	if (document.getElementById(this.id).value=="") {
		alert("you skipped a space");
		return(1);
	} else if (this.ans == document.getElementById(this.id).value) {
		document.getElementById(this.id).style.color = "#9999FF";
		document.getElementById(this.id).style.fontWeight = "bold";
		return(0);
	} else {
		document.getElementById(this.id).style.color = "#EE0000";
		document.getElementById(this.id).style.fontWeight = "bold";
		return(1);
	}
}
	
Blank.prototype.clear = function() {
	document.getElementById(this.id).value = "";
	}

Blank.prototype.write = function() {
	var clickstr = "blankArr["+this.i+"].givehint();"
	var changeStr = "style.color='#000000'";
	var str = "<input id=" +this.id+ " type='text' size='"+this.size+"' onFocus="+changeStr+"> &nbsp;";
	str += "<input  type='button' value='?' onClick=" + clickstr + ">&nbsp;"; 
	document.write(str);
	}
	
function checkBlanks() {checkBlanksPartial(0, blankArr.length);}
function checkBlanksPartial(start, stop) {
	var err=0;
	for (var i=start; i<stop+1; ++i) 
		{err += blankArr[i].check();}
	if (err==0) alert("great job");
	}
function clearBlanks() {clearBlanksPartial(0, blankArr.length);}
function clearBlanksPartial(start, stop) {
	for (var i=start; i<stop+1; ++i) 
		{blankArr[i].clear();}
	}
	
// Example:
// simplePreload( '01.gif', '02.gif' ); 
function simplePreload()
{ 
  var args = simplePreload.arguments;
  document.imageArray = new Array(args.length);
  for(var i=0; i<args.length; i++)
  {
    document.imageArray[i] = new Image;
    document.imageArray[i].src = args[i];
  }
}

function Image(name, w, h, alt) {
	this.name = name;
	this.w = w;
	this.h = h;
	this.alt = alt;
	}

function StartStopMovie(istill, imovie, cellname) {
	var strclick1 = "document[\'"+cellname+"\'].src=\'"+imovie.name+"\';";
	var strclick2 = "document[\'"+cellname+"\'].src=\'"+istill.name+"\';";
	var str = '<div align="center">' +
	 '<img src=\"'+istill.name+'\" alt='+istill.alt+' width='+istill.w+' height='+istill.h+' name=\"'+cellname+'\"><br> ' + 
         '<input type="button" value="Start" onClick='+strclick1+'>' +
         '<input type="button" value="Reset" onClick='+strclick2+'> ' +  
     '</div>';
	 alert(str);
	 document.write(str);
	 }
	 
//For slideshows
		function ImageSet(arr, name) {
			this.arr = arr;
			this.curr = 0;
			this.length = arr.length;
			this.btn1txt = "<<";
			this.btn2txt = "<";
			this.btn3txt = ">";
			this.btn4txt = ">>";
			this.name = name;
			this.btn1name = this.name+'bb';
			this.btn2name = this.name+'b';
			this.btn3name = this.name+'f';
			this.btn4name = this.name+'ff';
		}
		ImageSet.prototype.changeButtonText = function(t1, t2, t3, t4) {
			this.btn1txt = t1;
			this.btn2txt = t2;
			this.btn3txt = t3;
			this.btn4txt = t4;
		}
		
		ImageSet.prototype.changeCurr = function(num) {
			this.curr += num;
			if(this.curr>this.length) this.curr=this.length-1;
			if(this.curr<0) this.curr=0;
			}
			
		ImageSet.prototype.stepThrough = function(button, idname) {
			if(button.name==this.btn1name) {this.curr=0; }
			if(button.name==this.btn2name) {this.changeCurr(-1);}
			if(button.name==this.btn3name) {this.changeCurr(+1);}
			if(button.name==this.btn4name) {this.curr=this.length-1; }
			document.getElementById(idname).src = this.arr[this.curr].name;
			}

		ImageSet.prototype.writeSlideShow = function(idname) {
		var str = 	'<div align="center">' +
			'<img src='+this.arr[this.curr].name+' w='+this.arr[this.curr].w+' h='+this.arr[this.curr].h+' id='+idname+'><br>	<br>' + 
			'<input name='+this.btn1name+' value='+this.btn1txt+' type="button" onClick="'+this.name+'.stepThrough(this, \''+idname+'\');" > ' + 
			'<input name='+this.btn2name+' value='+this.btn2txt+' type="button" onClick="'+this.name+'.stepThrough(this, \''+idname+'\');" > ' + 
			'<input name='+this.btn3name+' value='+this.btn3txt+' type="button" onClick="'+this.name+'.stepThrough(this, \''+idname+'\');" > ' + 
			'<input name='+this.btn4name+' value='+this.btn4txt+' type="button" onClick="'+this.name+'.stepThrough(this, \''+idname+'\');" >' +
			'</div>';
		document.write(str);
		}	
		
		//////////////////for TFOther Quiz
		function TFOther(userChoices, correctAnswer, question, hint, i) {
    this.question = question;
    this.correctAnswer = correctAnswer-1;
    this.userAnswer = null;
	this.hint = hint;
	this.i = i;
	this.idhint = "idhint"+i;
    this.userChoices = userChoices;
}

function TFOtherQuiz(arr, heading, wid, name) {
	this.quArr = arr;
	this.heading = heading;
	this.wid = wid;
	this.name = name;
}

TFOther.prototype.isCorrect = function() {
    if (this.correctAnswer == this.userAnswer) {
		document.getElementById(this.idhint).className="hidden";
        return true;
    }
    else {
		document.getElementById(this.idhint).className="red";
        return false;
    }
}


TFOtherQuiz.prototype.correctQuiz = function() {
    var correct = 0;
    for (var i = 0; i < this.quArr.length; i++) {
        if (this.quArr[i].isCorrect()) {correct++;}
    }

    if (correct < this.quArr.length) {wrong("check the answers with red text...", 200);}
    	else {right("Well done, you aced it.", 200); }

}

TFOther.prototype.showForm = function(n, wid, name) {
	var str = '<form><tr><td width="'+wid+'%" vertical-align="top">';
    for (var i = 0; i < this.userChoices.length; i++) {
        str += '<input type="radio" name="q' + n + 
                       '" onClick = "'+name+'.quArr[' + n + '].userAnswer = ' + 
                       i + '">';
        str += this.userChoices[i] + '&nbsp;';
    }
    str += '</td><td vertical-align="top">'+this.question ;
    str += "<span class='hidden' id='"+this.idhint+"'> -- " + this.hint + ". </span><br><br></td></tr>" ;
    str += '</form>';
	document.write(str);
}
 
TFOtherQuiz.prototype.write = function() {
	var str = '<div class="problem"><h2>'+this.heading+'</h2><p><table border="1">'; document.write(str);
	for (var i = 0; i < this.quArr.length; i++) {
    		this.quArr[i].showForm(i, this.wid, this.name);
			}
	str = '</table><div align="center"><input type="button" value="Correct Quiz" onClick="'+this.name+'.correctQuiz();"></div></div>'; 
	document.write(str);
}	

//////////////////use for 2 hints and answer

function Question(correctAnswer, userChoices, id) {
    //this.question = question;
    this.correctAnswer = correctAnswer-1;
    this.userAnswer = null;
	this.userChoices = new Array();
	this.hints = new Array();
	for (var i=0; i<userChoices.length; ++i) {
		this.userChoices[i]=userChoices[i][0];
		this.hints[i]=' -- ' + userChoices[i][1];
		}
	this.id='idq'+id;
	this.name = id;
    }


Question.prototype.isCorrect = function() {
	//alert(this.correctAnswer +' ' + this.userAnswer);
    if (this.correctAnswer == this.userAnswer) {
		right("great job", 200);
		document.getElementById(this.id+'hint'+this.userAnswer).innerHTML = this.hints[this.userAnswer];
		document.getElementById(this.id+'hint'+this.userAnswer).className = "purple";
		return true;
		}
    else {
		wrong("check the text in red and try again!", 200);
		document.getElementById(this.id+'hint'+this.userAnswer).innerHTML = this.hints[this.userAnswer];
		return false;
		}
}

Question.prototype.write = function() {
    var str = '<blockquote><form>';
    for (var i = 0; i < this.userChoices.length; i++) {
        str += '<input type="radio" name="'+this.id+'" onClick = "'+this.name+'.userAnswer = ' + i + '">';
        str += this.userChoices[i];
		str += '<span class="red" id="'+this.id+'hint'+i+'"> </span><br>';
    }
    str += '</form></blockquote>';
	document.write(str);
}

     
function hintButton1(myhint, ht) {     
	document.write('<input name="button" type="button" onClick="hint(\''+myhint+'\','+ht+');" value="Give me a hint">');
	}
function hintButton2(myhint, ht) {     
	document.write('<input name="button" type="button" onClick="hint(\''+myhint+'\','+ht+');" value="I need another hint">');
	}

