var nAttempt=1;

/**
* Enters name and date and starts the problem
*/
function check_intro() {
	//check if filled
		if(isPHPworking) {
		a1.deactivate();
		var stuName=".";
		var stuId=".";	
		document.getElementById("intro").style.display="none";
		
		document.getElementById("identification").style.display="block";
		document.getElementById("description").style.display="block";
		document.getElementById("prelab").style.display="block";

		a2.activate();
		a3.deactivate();
		
		logLogIn();
	}
	return false;
}

/**
* Enters name and date and starts the problem on PHP-enabled
*/
function check_data() {
	//check if filled
	if(isPHPworking) {
		a1.deactivate();
		var stuName=userName;
		var stuId=userID;
		stuName=stuName.replace(/<.*>/g,"");
		stuId=stuId.replace(/<.*>/g,"");		
		document.getElementById("intro").style.display="none";

		document.getElementById("identification").style.display="block";
		document.getElementById("description").style.display="block";
		document.getElementById("prelab").style.display="block";
		a2.activate();
		a3.deactivate();
		
		logLogIn();
	}
}

/**
* Problem object
*/
function Problem() {
	this.coffeeT=format_sig(80+intRandom(0,3)*5,3);
	this.milkT=format_sig(1+intRandom(0,3)*2,3);
	this.desiredT=format_sig(60+intRandom(0,3)*5,3);
	this.desiredV=format_sig(200+intRandom(0,1)*50,3);
}

/**
* Finish prelab and starts the lab
*/
function check_prelab() {
	//check if filled
	if(document.step1.solutionProcedure.value=="") {
		alert("Please answer the question before proceding to the lab.");
	} else {
		a2.deactivate();
		a3.activate();
		document.getElementById("prelabDone").style.display="block";
		document.getElementById("vlab").style.display="block";
		document.getElementById("vlabq2").style.display="block";
		document.getElementById("prelab").style.display="none";
		setInnerHTMLById("prelabDone_answer",document.step1.solutionProcedure.value.replace(/\n/g," <br>"));
		
		logFormCheck('prelab',1,document.step1.solutionProcedure.value.replace(/\n/g," "),"","",true,"NOT_CHECKED");
	}
	return false;
}

/**
* Check question 1
*/
function check_lab2() {
	var message="";
	
	//check if filled
	if(document.step2.volume.value=="" || document.step2.procedure.value=="") {
		alert("Please enter your results before submitting.");
	} else {
		var sVol=document.step2.volume.value;
		var cVol=thisProblem.desiredV*(thisProblem.desiredT-thisProblem.coffeeT)/
				(thisProblem.milkT-thisProblem.desiredT);
		var userAnswer="PROCEDURE: "+document.step2.procedure.value.replace(/\n/g," ")
				+". RESULT: "+sVol+"ml";
		var cAnswer=cVol+"ml";
		
		if(relativeError(sVol,cVol,0.01)&&number_sig(sVol)==3) {
			logFormCheck('lab',nAttempt,userAnswer,cAnswer,"Well done! You did a good job!",true,"CORRECT");
			q2Completed();
		} else {
			var errorType="UNKNOWN_ERROR";
			if (nAttempt == 3) {
			/*if(relativeError(submMoles,correctMoles,0.01)&&number_sig(submMoles)!=3&&submReactant==correctReactant) {
				message="Your result is not expressed with the correct number of significant figures. ";
				message+="Please check it and submit again.";
				errorType="SIGNIFICANT_FIGURES_ERROR";
			} else {*/
				message="Your answer is not correct. The correct solution is "+format_sig(cVol,3)+"ml";
	    		message+=". Use the Virtual Lab to make sense for this result and hit the RELOAD/REFRESH button ";
   	    		message+="to start over with a new problem. Keep going!";
				a3.deactivate();
			//}
	        }
	        else {
	            message = "Your answer is not correct, but you have "+ (3 - nAttempt) + " attempts left.";
	        }
			logFormCheck('lab',nAttempt,userAnswer,cAnswer,false,errorType);
			a3.setFeedback(message,nAttempt);
			nAttempt++;		
		}
	}
	return false;
}

/*
* Finishes the problem
*/
function q2Completed() {
	a3.deactivate();
	document.getElementById("vlabq2").style.display="none";
	document.getElementById("vlab").style.display="none";
	document.getElementById("vlabdone").style.display="block";
	document.getElementById("done").style.display="block";
	setInnerHTMLById("done_answer1_q2",document.step2.procedure.value.replace(/\n/g,"<br>"));
	setInnerHTMLById("done_answer2_q2",document.step2.volume.value+"ml.");
	setInnerHTMLById("solvedProblemCode",getProblemCode(thisProblem,userName,userID,1));
	document.nextproblem.problemcode.value=getProblemCode(thisProblem,userName,userID,1);
}

