var nAttempt=1;
var nAttempts=1;
var AttemptsLimit = 3;
var ContextMessage1='<dataset><name>ChemCollective Autograded Problems</name><level type="Autograded Homework"><name>Thermochemistry</name>'
			+'<problem><name>CampingProblem</name><context>Heats of Reaction</context></problem>'
		+'</level></dataset><class><school>Anonymous Web User</school>'
		+'<description>2011</description></class>';
/**
* Enters name and date and starts the problem
*/
function check_intro() {
	//check if filled
	if(document.step0.studentName.value=="" || document.step0.studentId.value=="") {
		alert("Please enter your name and university ID to start the problem.");
	} else {
		a3.deactivate();
		a4.deactivate();
		var stuName=document.step0.studentName.value;
		var stuId=document.step0.studentId.value;
		stuName=stuName.replace(/<.*>/g,"");
		stuId=stuId.replace(/<.*>/g,"");		
		
		document.getElementById("description").style.display="block";
		document.getElementById("vlab").style.display="block";
		document.getElementById("vlabq1").style.display="block";
		document.getElementById("vlabq2").style.display="block";
		document.getElementById("vlabq3").style.display="block";
		setInnerHTMLById("identification_date",'Loaded at '+full_time());
		setInnerHTMLById("identification_name",stuName+", ID: "+stuId);

		logLogIn();
	}
	return false;
}

/**
* Enters name and date and starts the problem on PHP-enabled
*/
function check_data() {
	//check if filled
	if(isPHPworking) {
		a3.deactivate();
		a4.deactivate();
		var stuName=userName;
		var stuId=userID;
		stuName=stuName.replace(/<.*>/g,"");
		stuId=stuId.replace(/<.*>/g,"");		

		document.getElementById("description").style.display="block";
		document.getElementById("vlab").style.display="block";
		document.getElementById("vlabq1").style.display="block";
		document.getElementById("vlabq2").style.display="block";
		document.getElementById("vlabq3").style.display="block";
		setInnerHTMLById("identification_date",'Loaded at '+full_time());
		setInnerHTMLById("identification_name",stuName+", ID: "+stuId);
		
		logLogIn();
	}
}

/**
* Problem object
*/
function Problem() {
	this.dT=doubleRandom(45,70);
	this.dH=-this.dT*2*4.184;
	this.unkStr="u1,"+this.dH;
}

/**
* Check question 1
*/
function check_labq1() {
	var message="";
	
	//check if filled
	if(document.step1.enthalpy.value=="") {
		alert("Please enter your results before submitting.");
	} else if(isNaN(document.step1.enthalpy.value)) {
		// skip submit
		return false;
	} else {
		var sEnthalpy=document.step1.enthalpy.value;
		var cEnthalpy=thisProblem.dH;
		var userAnswer=sEnthalpy+" kJ/mol";
		var cAnswer=format_sig(cEnthalpy,3)+" kJ/mol";

		if(relativeError(sEnthalpy,cEnthalpy,0.01)&&number_sig(sEnthalpy)==3) {
			logFormCheck('labq1',nAttempt,userAnswer,cAnswer,"Well done! You did a good job!",true,"CORRECT");
			q1Completed();
		} else {
			var errorType="UNKNOWN_ERROR";
			if(relativeError(sEnthalpy,cEnthalpy,0.01)&&number_sig(sEnthalpy)!=3) {
				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 if(relativeError(sEnthalpy,-cEnthalpy,0.01)&&number_sig(sEnthalpy)==3) {
				message="Your result does not have the correct sign.  Remember that exothermic reactions have a negative enthalpy.  ";
				message+="Please check your answer and submit again.";
				errorType="SIGN_ERROR";
			} else {
				message="Your answer is not correct. The correct solution is "+cAnswer;
				message+=". Use the Virtual Lab to make sense of this result and hit the RELOAD/REFRESH button ";
   	    		message+="to start over with a new problem. Keep going!";
				a2.deactivate();
			}
			logFormCheck('labq1',nAttempt,userAnswer,cAnswer,message,false,errorType);
			a2.setFeedback(message,nAttempt);
		}
	}
	return false;
}

/**
* Check question 2
*/
function check_labq2() {
	var message="";

	//check if filled
	if(document.step2.mix1.value=="" || document.step2.mix2.value=="" || 
			document.step2.mix3.value=="" || document.step2.mix4.value=="" ||
			document.step2.mix5.value=="" || document.step2.explain.value=="") {
		alert("Please enter your results before submitting.");
	} else if(isNaN(document.step2.mix1.value) || isNaN(document.step2.mix2.value) || 
			isNaN(document.step2.mix3.value) || isNaN(document.step2.mix4.value) || 
			isNaN(document.step2.mix5.value)) {
		return false;
	} else {
		var userAnswer=document.step2.mix1.value+"&#176;C, "+document.step2.mix2.value+"&#176;C, "+
				document.step2.mix3.value+"&#176;C, "+document.step2.mix4.value+"&#176;C, "+
				document.step2.mix5.value+"&#176;C. Explanation: "+document.step2.explain.value.replace(/\n/g," ");
		var cAnswer=format_sig(thisProblem.dT/10,3)+"&#176;C, "+format_sig(thisProblem.dT/10,3)
				+"&#176;C, "+format_sig(thisProblem.dT/10,3)+"&#176;C, "+
				format_sig(thisProblem.dT/5,3)+"&#176;C, "+format_sig(thisProblem.dT*.3,3)+"&#176;C.";
		var donetext="100ml of 0.1 M Solution X with 100ml of 0.1 M Solution Y: &#916;T = "+
				document.step2.mix1.value+"&#176;C <br>50ml of 0.1 M Solution X with 50ml of 0.1 M Solution Y: &#916;T =  "+
				document.step2.mix2.value+"&#176;C <br>100ml of 0.1 M Solution X with 100ml of 0.2 M Solution Y: &#916;T =  "+
				document.step2.mix3.value+"&#176;C <br>100ml of 0.2 M Solution X with 100ml of 0.2 M Solution Y: &#916;T =  "+
				document.step2.mix4.value+"&#176;C <br>100ml of 0.3 M Solution X with 100ml of 0.3 M Solution Y: &#916;T =  "+
				document.step2.mix5.value+"&#176;C";
		
		var areCorrect=false;
		
		if(relativeError(document.step2.mix1.value,thisProblem.dT/10,0.01) && relativeError(document.step2.mix2.value,thisProblem.dT/10,0.01) && 
		   relativeError(document.step2.mix3.value,thisProblem.dT/10,0.01) && relativeError(document.step2.mix4.value,thisProblem.dT/5,0.01) && 
		   relativeError(document.step2.mix5.value,thisProblem.dT*.3,0.1))
					areCorrect=true;
		
//		var sData=new Array();
//		for(i=0;i<4;i++) {
//			sData[i]=eval('document.step2.mix'+(i+1)+'.value');
//			if(!relativeError(sData[i],thisProblem.dT/10,0.01) || number_sig(sData[i])!=3)
//					areCorrect==false;
//			if(!relativeError(sData[i],thisProblem.dT/10,0.01))
//					areInError=false;
//		}
//		sData[4]=document.step2.mix5.value;
//		if(!relativeError(sData[4],thisProblem.dT/5,0.01) || number_sig(sData[i])!=3)
//				areCorrect==false;
//		if(!relativeError(sData[4],thisProblem.dT/5,0.01))
//				areInError=false;
	
		if(areCorrect) {
			a3.deactivate();
			a4.activate();
			logFormCheck('labq2',nAttempt,userAnswer,cAnswer,"Let's go for the last one!",true,"CORRECT");
			document.getElementById("vlabq2").style.display="none";
			document.getElementById("vlabq2done").style.display="block";
			setInnerHTMLById("done_answer1_q2",donetext);
			setInnerHTMLById("done_answer2_q2",document.step2.explain.value.replace(/\n/g,"<br>"));
		} else {
			var errorType="UNKNOWN_ERROR";
			 if (nAttempt>=AttemptsLimit) {
				message="Your answer is not correct. The correct temperature values are "+cAnswer;
				message+=" Use the Virtual Lab to make sense of this result and hit the RELOAD/REFRESH button ";
   	    		message+="to start over with a new problem. Keep going!";
				a3.deactivate();
				nAttempt = 1;
				}
				else {
					message= "At least one of your temperature values is wrong, but you have still have "+(AttemptsLimit-nAttempt)+" attempts left. ";
					message+= "Check your answer and submit again.";
				}
			logFormCheck('labq2',nAttempt,userAnswer,cAnswer,message,false,errorType);
			a3.setFeedback(message,nAttempt);
			nAttempt++;		
		}
	}
	return false;
}

/**
* Check question 3
*/
function check_labq3() {
	var message="";

	//check if filled
	if(document.step3.MX.value=="" || document.step3.MY.value=="") {
		alert("Please enter your results before submitting.");
	} else if(isNaN(document.step3.MX.value) || isNaN(document.step3.MY.value)) {
		return false;
	} else {
		var userAnswer=document.step3.MX.value+"M Solution X and "+ document.step3.MY.value+
				"M Solution Y";
		var correct=-8.368*25/thisProblem.dH;
		var cAnswer=format_sig(correct,3)+"M Solution X and "+format_sig(correct,3)+
				"M Solution Y";

		if((relativeError(document.step3.MX.value,correct,0.01) && number_sig(document.step3.MX.value)==3)
				&& (relativeError(document.step3.MY.value,correct,0.01) && number_sig(document.step3.MY.value)==3)) {
			a3.deactivate();
			logFormCheck('labq3',nAttempts,userAnswer,cAnswer,"Great! You are done!",true,"CORRECT");
			document.getElementById("vlabq3").style.display="none";
			document.getElementById("vlab").style.display="none";
			document.getElementById("done").style.display="block";
			setInnerHTMLById("done_answer1",userAnswer);
		} else {
			var errorType="UNKNOWN_ERROR";
			if((relativeError(document.step3.MX.value,correct,0.01) && number_sig(document.step3.MX.value)!=3)
				|| (relativeError(document.step3.MY.value,correct,0.01) && number_sig(document.step3.MY.value)!=3)) {
				message="Your results are not expressed with the correct number of significant figures. ";
				message+="Please check them and submit again.";
				errorType="SIGNIFICANT_FIGURES_ERROR";
			} else if (nAttempts>=AttemptsLimit) {
				message="Your answer is not correct. The correct solutions are "+cAnswer;
				message+=". Use the Virtual Lab to make sense of this result and hit the RELOAD/REFRESH button ";
   	    		message+="to start over with a new problem. Keep going!";
				a4.deactivate();
				nAttempts = 1;
			}
			else {
				message= "Your result is wrong, but you have still have "+(AttemptsLimit-nAttempts)+" attempts left. ";
				message+= "Check your answer and submit again.";
			}
			logFormCheck('labq3',nAttempts,userAnswer,cAnswer,message,false,errorType);
			a4.setFeedback(message,nAttempts);
			nAttempts++;		
		}
	}
	return false;
}


/*
* Finishes the q1
*/
function q1Completed() {
	a2.deactivate();
	a3.activate()
	document.getElementById("vlabq1").style.display="none";
	document.getElementById("vlabq1done").style.display="block";
	setInnerHTMLById("done_answer_q1",document.step1.enthalpy.value+" kJ/mol.");
}


