var see_correct;
var capitalization;
var usernamePopup;
var usertypePopup;
running=0;
var noAccent=true
reverse=0;
question=0;
var printedGrade;
solution=0;
attempted=0;
start=0;
stopHere=0;
reverseAttempts=0;
correct=0;
lastNumber=0;
number=0;
var up
var down;
var min;
var remember_min;
var sec;
clock=0;
stop=0;
reverse=0;
var checked;
var sent_already="no";
var conjugations=0;


function original() {
	self.location.reload();
}


function randomizeList(reset) {
		
	if (start==0) {
		correct=0;
		attempted=0;
		conjugations=0;
		sent_already="no";
		tsec = -1;
  		tmin = 0;
  		thour = 0;
		sec=0;
		setCookie();
		min=document.form.clockinput.value;
		stopwatch("start");
	}
	if (start != 0 && reset=="yes") {
		alert ("By pressing START in the middle of your practice, you reset your grade.");
		correct=0;
		attempted=0;
		sent_already="no";
		conjugations=0;
		sec=0;
		setCookie();
		min=document.form.clockinput.value;
		stopwatch("stop");
		stopwatch("start");
	}
	start++;
	responseLength = 0;
	document.form.response.value="";
	
	
	setValue();
}


function setCookie() {
//document["clockset"].src = "settime.php";
}

function setValue() {
if (definitions.length < 2) definitions = originalDefinitions.slice(0);
	if (answers.length < 2) answers = originalAnswers.slice(0);
	number=Math.floor(Math.random()*definitions.length);
	if (number==lastNumber) {
		while (number==lastNumber) number=Math.floor(Math.random()*definitions.length);
	}
	lastNumber=number;	
	if (reverse==0){
		document.form.word.value=definitions[number];
		solution=answers[number];
	}
	else{
		document.form.word.value=answers[number];
		solution=definitions[number];
	}
	document.form.response.focus();
}


function Reverse() {
	
	
		if (start==0) {
			alert("Press BEGIN to start the practice.");
			return
		}

		if (reverse==0) {
			reverse=1;
			document.form.word.value="";
			setValue();	
			return
			
		}
		else {
			reverse=0;
			document.form.word.value="";
			setValue();	
			return
		}
		
}



function checkAnswer() {
	responseLength = 0;
	if (start==0) {
		alert("Press BEGIN to start the practice.");
		return
	}
							//MATCH to see is person entered a response	
	var word=/\w+/;	
	var r=document.form.response.value;
	if (capitalization != "yes") r = r.toLowerCase();
	var entry=r.match(word);
	if (!entry) {
		alert("Please type an answer first.");
		document.form.response.focus();
		return
	}

	attempted++;
							//TRIM leading, trailing and double spaces
	while (r.charAt(0)==" ") {
		r=r.substring(1,r.length);
	}
	
	while (r.charAt(r.length-1)==" ") {
		r=r.substring(0,r.length-1);
	}

	for (i=0; i<=r.length; i++) {
		if (r.charAt(i)==" ") {
			while (r.charAt(i+1)==" "){
				r=r.substring(0,i+1)+r.substring(i+2,r.length);
			}
		}
	}
		
	var array_correct="no";
							
					//CHECK answer below, vs. string or array
	
	if (reverse != 1) {
		if (checked==true) {checked=false;return}

		
		var copySolutions=new Array();
		for (i=0;i<solution.length;i++) {
			var thisWord=solution[i];	
			if (capitalization != "yes") thisWord=thisWord.toLowerCase();
			if (r == thisWord) array_correct="yes";
			copySolutions[i]=thisWord;	
		}
		if (copySolutions.length>1) var popUpSolution=copySolutions.join(' OR ');
		else 	var popUpSolution=copySolutions[0];
	
				
		if (array_correct=="yes") {
			alert("CORRECT.");
			correct++;
			question++;
			answers.splice(number,1);
			definitions.splice(number,1);
			randomizeList();
			return
		}
		else {	
			
			if (capitalization != "yes") popUpSolution = popUpSolution.toUpperCase();
			if (see_correct == false) alert("Incorrect!\nPlease try again.");
			else alert("The answer is: "+popUpSolution);
			document.form.response.value="";
			document.form.response.focus();
		}
				
	}
	else {
		
		if (r==solution) {
			alert("CORRECT.");
			correct++;
			question++;
			if (reverse==1) {
				reverseAttempts++;
			}
			randomizeList();
			return;
		}
		else {
			alert("The answer is: " + solution.toUpperCase());
			document.form.response.value="";
			document.form.response.focus();
		}
		return
	}
		
}

function grade() {
	if (start==0) {
		alert("Press BEGIN to start the practice.");
		document.form.response.focus();
		return
		}
	else {

		if (attempted==0) {
			alert("You have 0 attempts, so you have no score.");
		}
		else {
	alert("You have correctly identified " + correct + " word(s) in " + attempted + " attempt(s).\nYOUR SCORE IS " + Math.round(correct/attempted*100) + " %.");
		}
	document.form.response.focus();
	}
}

	

	

function gradeToTeacher(how,sid) {
	
	
	if (attempted==0) {
		alert("You have 0 attempts, so you have no grade.");
		document.form.response.focus();
		return
		}
	if (sent_already=="yes" && how=="send_current") {

		var message="You cannot send the same grade twice. You can still print your grade, however, if you feel the 'send' feature did not work properly.\n\nTo send another grade, press START again to begin another practice.";
		alert(message);
		return 

	}
	printedGrade=Math.round(correct/attempted*100) + " % : "+correct+"/"+attempted;
	tt=document.sw.timer.value;


	var phpsession=document.form.phpsession.value;
	usernamePopup=document.form.username.value;
	usertypePopup=document.form.usertype.value;
	var url="popup.php?sid="+phpsession+"&type="+sid+"&do="+how;
	
	var sent=window.open(url,'sent','menubar=yes,scrollbars=no,toolbar=yes,height=500,width=600,resizable=yes');
	
	

	if (how != "print_current") {
		sent_already="yes";
	}

}



//COPY FROM HERE AND PATCH ONTO WEB

var tsec = -1;
var tmin = 0;
var thour = 0;

function stopwatch(text) {

	tsec++;
	if (tsec == 60) {
		tsec = 0;
   		tmin = tmin + 1; }
  	else {
   		tmin = tmin; }
  	if (tmin == 60) {
   		tmin = 0;
   		thour += 1; }

	if (tsec<=9) { tsec = "0" + tsec; }
   		document.sw.timer.value = ((tmin<=9) ? "0" + tmin : tmin) + " : " + tsec;

 	if (text == "stop") {

		resetIt();
		return
	}
	SD=window.setTimeout("stopwatch();", 1000);
}

function resetIt() {
  	tsec = -1;
  	tmin = 0;
  	thour = 0;

  	window.clearTimeout(SD);
}



function Minutes(data) {
	for (var i=0; i<data.length; i++)
	if (data.substring(i,i+1)==":") break;
	return (data.substring(0,i));}

function Seconds(data) {
	for (var i=0; i<data.length; i++)
	if (data.substring(i,i+1)==":") break;
	return (data.substring(i+1,data.length));}

function setvariable() {

	if (running !=0) return
	
	min=1*Minutes(document.form.clockinput.value);
	sec=0+Seconds(document.form.clockinput.value);
	document.form.time.value=Display(min,sec);
	clock++;
	running++;
	correct=0;
	attempted=0;
	countdown();
	}

function stopClock() {
	if (running!=0) {
	stop=1;
	countdown();
	}
	else return

}

function originalClock() {
	if (!selectClock) {
		document.form.approval[0].checked = true;
		document.form.approval[1].checked = false;
		alert("Your teacher has requested that you be timed, so you cannot edit the CLOCK feature.");
	}
	running=0;
}

function countdown(){
	if (document.form.approval[0].checked && clock!=0) {
		sec--;
		if (sec==-1) {sec=59; min--}
		document.form.time.value=Display(min,sec);
		if ((min==0)&&(sec==0)||stop==1) {
			alert("TIME'S UP!!\nYou correctly identified " + correct + " word(s) in " + attempted + " attempt(s).\nYou checked the VOCABULARY CHART button "+conjugations+" time(s).\n\nYOUR SCORE IS " + Math.round(correct/attempted*100) + " %.");
			clock=0;
			stopwatch("stop");
			stop=0;
			running=0;
			start=0;
			document.form.time.value="";
		}
		else down=setTimeout("countdown()",1000);

	}
	else return
	

}

function Display (min,sec) {
	var disp;
	if (min<=9) disp=" 0" 
	else disp=" ";
 	disp+=min+":";
	if (sec<=9) disp+="0"+sec;
	else disp+=sec;
	return(disp);
}



 function captureclicks() {
	return
	bName=navigator.appName;
	if (bName=="Netscape") {
	window.captureEvents(Event.KEYPRESS)

	}
}
var responseLength = 0;
function getCurrentLength() {
	var realLength = document.form.response.value.length;
	if (realLength > responseLength+1) return false
	else return true
	
}
function netscapeReturn(caughtEvent) {
	bName=navigator.appName;
	//var result = getCurrentLength();
	//if (!result) document.form.response.value = "";		
}

function microsoftReturn() {
	bName=navigator.appName;
	if (bName=="Microsoft Internet Explorer") {
		//var result = getCurrentLength();
		//if (!result) document.form.response.value = "";
		key=event.keyCode;
		if (key==13) {
		checkAnswer();
		event.returnValue=false
		}
		return
	}
	
}
document.onkeydown=netscapeReturn;

