var activityType = "vocabulary";
var answersStr;
var selectionMethod = 1;
var responseStr;
var resultArr = new Array();
var o = new Array();
var see_correct;
var commands; //only here to make the print out work with verbs that needs this
var alltenses;//only here to make the print out work with verbs that needs this
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;
var punctuation = true;


function original() {
	self.location.reload();
}


function hideNotice(){
		ns4 = (document.layers) ? true:false 
		ie4 = (document.all) ? true:false 
		ng5 = (document.getElementById) ? true:false 

		if (ng5) document.getElementById("notice").style.visibility = "hidden" 
		else if (ns4) document.notice.visibility = "hide" 
		else if (ie4) notice.style.visibility ="hidden" 

}



function randomizeList(reset) {
	
	//hideNotice();
		
	if (start==0) {
		correct=0;
		attempted=0;
		conjugations=0;
		sent_already="no";
		resultArr = new Array();
		tsec = -1;
  		tmin = 0;
  		thour = 0;
		sec=0;
		setCookie();
		min=document.form.clockinput.value;
		Core.resetAnswer();
		document.form.elements["current_results"].value = "";
		stopwatch("start");
	}
	if (start != 0 && reset=="yes") {
		alert ("By pressing START in the middle of your practice, you reset your grade.");
		correct=0;
		resultArr = new Array();
		attempted=0;
		sent_already="no";
		conjugations=0;
		sec=0;
		setCookie();
		min=document.form.clockinput.value;
		stopwatch("stop");
		stopwatch("start");
		Core.resetAnswer();
		document.form.elements["current_results"].value = "";
	}
	start++;
	responseLength = 0;
	document.form.response.value="";
	$("#response").focus();
	//document.form.response.focus();
	
	
	setValue();
}


function setCookie() {
//document["clockset"].src = "settime.php";
}

shuffle = function(){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	
}	

function createO() {
	o = new Array();
	for (i = 0;i < answers.length; i++) o[o.length] = new Array(answers[i],definitions[i]);		
	shuffle();
}
function setValue() {
	if (selectionMethod == 1) {
		if (o.length < 1) createO();
		number = 0;
		solution = o[0][0];
		document.form.word.value = o[0][1];
		//$("#response").focus();
		document.form.response.focus();
	
	}
	
	if (selectionMethod == 0) {
		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];
		}
		//$("#response").focus();
		document.form.response.focus();
	}	
}


function Reverse() {
	
	
		if (start==0) {
			alert("Press START 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 verifyPunctuation() {
	var str = "You cannot change the PUNCTUATION SETTING while you have a score. Refresh or re-start the activity to make the change.";
	if (attempted == 0) {
		if (document.form.punc.checked == true) punctuation = false;
		else punctuation = true;
	}
	else {
		if (punctuation) document.form.punc.checked = false;
		else document.form.punc.checked = true;
		alert(str);
	}
	return;
}


function verifyAnswer() {
	responseLength = 0;
	if (start==0) {
		alert("Press START 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++;
	
							//remove punctuation if called for from response:
	//alert(punctuation)
	if (!punctuation) r = r.replace(/[\.\,\?!żĄ]/g, "");
							
	
							//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);
			
		}
	}
	//jquery trim
	r=$.trim(r);
	var array_correct="no";
							
					//CHECK answer below, vs. string or array

	if (capitalization != "yes") var copyR = r.toLowerCase();
	else var copyR = r;
	if (reverse != 1) {		//ask English, answer in target language:
	
		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();
			//alert("correct answer: '"+thisWord+"'\nresponse: '"+copyR+"'");
			if (!punctuation) thisWord = thisWord.replace(/[\.\,\?!żĄ]/g, "");
			thisWord=$.trim(thisWord);
			//alert("correct answer: '"+thisWord+"'\nresponse: '"+copyR+"'");
			if (copyR == thisWord) {
				array_correct="yes";
				//for storing results:	
			}
			copySolutions[i]=thisWord;	
		}
		if (copySolutions.length>1) var popUpSolution=copySolutions.join(' OR ');
		else 	var popUpSolution=copySolutions[0];
		var printPopSolution = popUpSolution;
		//alert(capitalization+"word:"+printPopSolution+"="+copyR);
		if (array_correct=="yes") {
			//alert("CORRECT.");
			Core.setAnswerCorrect();
			correct++;
			question++;
			if (selectionMethod == 1) o.splice(number,1);
			if (selectionMethod == 0) {
				answers.splice(number,1);
				definitions.splice(number,1);
			}
			randomizeList();
		}
		else {	
			//if (capitalization != "yes") popUpSolution = popUpSolution.toUpperCase();
			if (see_correct == false) {
				//alert("Incorrect!\nPlease try again.");
				Core.setAnswerWrong("Incorrect!\nPlease try again.");
			}
			else {
				//alert("The answer is: "+popUpSolution);
				Core.setAnswerWrong("<span>The answer is</span>: "+popUpSolution);
			}
			
			document.form.response.value="";
			document.form.response.focus();
		}
		var thisstr = r+"***"+printPopSolution;
				
	}
	else {
		
		if (r==solution) {
			//alert("CORRECT.");
			Core.setAnswerCorrect();
			correct++;
			question++;
			if (reverse==1) reverseAttempts++;
			randomizeList();
			
		}
		else {
			//alert("The answer is: " + solution.toUpperCase());
			
			//document.form.response.value="";
			Core.setAnswerWrong("<span>The answer is</span>: " + solution.toUpperCase());
			document.form.response.focus();
		}
		
	}
	
	resultArr.push(thisstr);
	var sc = Math.round(correct/attempted*100) + "%";
	//document.form.elements["current_results"].value = "Your current grade is "+sc+" ("+correct+"/"+attempted+")";
	document.form.elements["current_results"].value = sc+" ("+correct+"/"+attempted+")";
	return;
}

function grade() {
	if (start==0) {
		alert("Press START 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 (demo) {
		alert("To record or print your grade, you need to be logged in. Please return to the home page to log in.");
		return
	}
	if (attempted==0) {
		alert("You have 0 attempts, so you have no grade.");
		document.form.response.focus();
		return
	}
	
	//store string of responses for results print out:
	responseStr = resultArr.join("||");	
	//make string of answers for results print out:
	var copyAnswers = new Array();
	for (i = 0;i< originalAnswers.length; i++) {
		var thisstr = originalAnswers[i];
		if (thisstr.length>1) var thisstr=thisstr.join(' OR ');
		copyAnswers.push(thisstr);
	}
	answersStr = copyAnswers.join("||");
	
	printedGrade=Math.round(correct/attempted*100) + " % : "+correct+"/"+attempted;
	tt=document.sw.timer.value;
	if (how == "send_current") {
		var confirmsend = confirm("Are you sure you want to record your grade?");
		if (!confirmsend) 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 

		}
		
		document.forms['h'].elements['score'].value = printedGrade;
		document.forms['h'].elements['time'].value = tt;
		document.forms['h'].elements['exercise'].value = title_teacher;
		document.forms['h'].elements['responses'].value = responseStr;
		document.forms['h'].elements['answers'].value = answersStr;
		document.forms['h'].elements['cheat'].value = conjugations;
		document.forms['h'].elements['grade_function'].value = how;
		if (how != "print_current") sent_already = "yes";
		document.forms['h'].submit();
	}
	else {
		var url="popup.php?type="+sid+"&do="+how;
		var sent = window.open(url,'sent','menubar=yes,scrollbars=yes,toolbar=yes,height=600,width=700,resizable=yes');
		return
	}
}



//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) {
		alert("Your teacher has requested that you be timed, so you cannot edit the CLOCK feature.");
		document.form.approval.checked = false;
		return;
	}
	running=0;
}

function countdown(){
	if (document.form.approval.checked == false && 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) {
	return;
}

function microsoftReturn() {
	return;
}



