nAttempts = 0;
AttemptsLimit = 2;

/**
* Enters name and date and starts the problem
*/
function check_intro() {
	//check if filled
	if(isPHPworking) {
		a1.deactivate();
		a3.deactivate();
		a4.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("vlab").style.display="block";
		document.getElementById("vlabq1").style.display="block";
		document.getElementById("vlabq2").style.display="block";
		document.getElementById("vlabq3").style.display="block";
		logLogIn();
	}
	return false;
}

/**
* Enters name and date and starts the problem on PHP-enabled
*/
function check_data() {
	//check if filled
	if(isPHPworking) {
		a2.show();
		a3.show();
		a4.show();
		a5.show();
		a6.show();
		a3.deactivate();
		a4.deactivate();
		a5.deactivate();
		a6.deactivate();
		document.getElementById("description").style.display="block";
		document.getElementById("prelab").style.display="block";
		document.getElementById("vlab").style.display="block";
		document.getElementById("vlabq1").style.display="block";		
		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.source.value=="") {
		alert("Please enter your results before submitting.");
		}
		else{
			if (document.step1.source.value == "a reaction") {
				message="Your answer is CORRECT. Good job. <p>";
				message+= "<br><b>Instructor Notes:</b> The source of the heat is the ";
				message+= "chemical reaction between X and Y.";
				a2.deactivate();
				errorType="CORRECT";
				nAttempts = 0;
				a3.show();
				a3.activate();
				
			} 
			else if (nAttempts>=AttemptsLimit) {
				message="Your answer is WRONG. <p>";
				message+= "<br><b>Instructor Notes:</b> The source of the heat is the ";
				message+= "chemical reaction between X and Y. Please try to make sense of this answer";
				message+= " and continue to the next question."
				a2.deactivate();
				nAttempts = 0;
				a3.show();
				a3.activate();
			}
			else {
				message= "Your result is wrong, but you have still have "+(AttemptsLimit-nAttempts)+" attempts left. ";
				message+= "Check your answer and submit again.";
			}
			a2.setFeedback(message,nAttempts);
			
			//logFormCheck("step2",nAttempts,document.step1.pH.value,format_sig(correct,3),message,isCorrect,errorType);
			nAttempts++;
			}
	return false;
	}


	/**
* Check question 2
*/
function check_labq2() {
	var message="";
	
	//check if filled
	if(document.step2.drain.value=="") {
		alert("Please enter your results before submitting.");
		}
		else{
			if (document.step2.drain.value == "water") {
				message="Your answer is CORRECT. Good job. <p>";
				message+= "<br><b>Instructor Notes:</b> The chemical reaction is occurring in solution, so ";
				message+= "most of the heat goes into the water. Some heat will also flow into the ";
				message+= "container and the air, but this is a small amount of heat so we will ";
				message+= "ignore it for this activity.</p>";
				a3.deactivate();
				errorType="CORRECT";
				nAttempts = 0;
				a4.show();
				a4.activate();
				
			} 
			else if (nAttempts>=AttemptsLimit) {
				message="Your answer is WRONG. <p>";
				message+= "<br><b>Instructor Notes:</b> The chemical reaction is occurring in solution, so ";
				message+= "most of the heat goes into the water. Some heat will also flow into the ";
				message+= "container and the air, but this is a small amount of heat so we will ";
				message+= "ignore it for this activity. </p><p> Please try to make sense of this answer ";
				message+= " and continue to the next question.</p>";
				a3.deactivate();
				nAttempts = 0;
				a4.show();
				a4.activate();
			}
			else {
				message= "Your result is wrong, but you have still have "+(AttemptsLimit-nAttempts)+" attempts left. ";
				message+= "Check your answer and submit again.";
			}
			a3.setFeedback(message,nAttempts);
			
			//logFormCheck("step2",nAttempts,document.step1.pH.value,format_sig(correct,3),message,isCorrect,errorType);
			nAttempts++;
			}
	return false;
	}


	
		/**
* Check question 3
*/
function check_labq3() {
	var message="";
	
	//check if filled
	if(document.step3.sourceeq.value=="") {
		alert("Please enter your results before submitting.");
		}
		else{
			if (document.step3.sourceeq.value == "q = n delta-H") {
				message="Your answer is CORRECT. Good job. <p>";
				message+= "<br><b>Instructor Notes:</b> The equation q = m Cv delta-T applies when a ";
				message+= "system loses or gains heat without undergoing a chemical reaction or ";
				message+= "change of state (boiling, melting). In this case, a chemical reaction is ";
				message+= "taking place, so we use q = n delta-H to describe the heat produced or ";
				message+= "consumed by this. In this equation, n is the number of moles ";
				message+= "undergoing the reaction or change of state and delta-H is the heat of ";
				message+= "reaction (also known as the enthalpy of reaction). ";
				a4.deactivate();
				errorType="CORRECT";
				nAttempts = 0;
				a5.show();
				a5.activate();
				
			} 
			else if (nAttempts>=AttemptsLimit) {
				message="Your answer is WRONG. <p>";
				message+= "<br><b>Instructor Notes:</b> The equation q = m Cv delta-T applies when a ";
				message+= "system loses or gains heat without undergoing a chemical reaction or ";
				message+= "change of state (boiling, melting). In this case, a chemical reaction is ";
				message+= "taking place, so we use q = n delta-H to describe the heat produced or ";
				message+= "consumed by this. In this equation, n is the number of moles ";
				message+= "undergoing the reaction or change of state and delta-H is the heat of ";
				message+= "reaction (also known as the enthalpy of reaction).</p><p>";
				message+= "Please try to make sense of this answer and continue to the next question.</p>";
				a4.deactivate();
				nAttempts = 0;
				a5.show();
				a5.activate();
			}
			else {
				message= "Your result is wrong, but you have still have "+(AttemptsLimit-nAttempts)+" attempts left. ";
				message+= "Check your answer and submit again.";
			}
			a4.setFeedback(message,nAttempts);
			
			//logForCheck("step2",nAttempts,document.step1.pH.value,format_sig(correct,3),message,isCorrect,errorType);
			nAttempts++;
			}
	return false;
	}
	
	
	/**
* Check question 4
*/
function check_labq4() {
	var message="";
	
	//check if filled
	if(document.step4.draineq.value=="") {
		alert("Please enter your results before submitting.");
		}
		else{
			if (document.step4.draineq.value == "q = m Cv delta-T") {
				message="Your answer is CORRECT. Good job. <p>";
				message+= "<br><b>Instructor Notes:</b> The water absorbs the heat without undergoing a ";
				message+= "chemical reaction or change of state, so the heat is described by the ";
				message+= "equation q = m Cv delta-T. <p> Use the virtual lab to determine the ";
				message+= "enthalpy of reaction for the above reaction";
				a5.deactivate();
				errorType="CORRECT";
				nAttempts = 0;
				a6.show();
				a6.activate();
				
			} 
			else if (nAttempts>=AttemptsLimit) {
				message="Your answer is WRONG. <p>";
				message+= "<br><b>Instructor Notes:</b> The water absorbs the heat without undergoing a ";
				message+= "chemical reaction or change of state, so the heat is described by the ";
				message+= "equation q = m Cv delta-T. </p><p>";
				message+= "Please try to make sense of this answer and then use the virtual lab to determine the ";
				message+= "enthalpy of reaction for the above reaction";
				a5.deactivate();
				nAttempts = 0;
				a6.show();
				a6.activate();
			}
			else {
				message= "Your result is wrong, but you have still have "+(AttemptsLimit-nAttempts)+" attempts left. ";
				message+= "Check your answer and submit again.";
			}
			a5.setFeedback(message,nAttempts);
			
			//logFormCheck("step2",nAttempts,document.step1.pH.value,format_sig(correct,3),message,isCorrect,errorType);
			nAttempts++;
			}
	return false;
	}
		
	
function check_labq5() {
	var message="";
	
	//check if filled
	if(document.step5.enthalpy.value=="") {
		alert("Please enter your results before submitting.");
	} else if(isNaN(document.step5.enthalpy.value)) {
		// skip submit
		return false;
	} else {
		var sEnthalpy=document.step5.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) {
			message="Your answer is CORRECT. Good job. <p>";
		//	logFormCheck('labq5',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 has not the correct sign. ";
				message+="Please check it and submit again.";
				errorType="SIGN_ERROR";
			} else if (nAttempts>=AttemptsLimit) {
				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!";
				a6.deactivate();
				nAttempts = 0;
			}
			else {
				message= "Your result is wrong, but you have still have "+(AttemptsLimit-nAttempts)+" attempts left. ";
				message+= "Check your answer and submit again.";
			}
		//	logFormCheck('labq5',nAttempts,userAnswer,cAnswer,message,false,errorType);
			a6.setFeedback(message,nAttempts);
			nAttempts++;		
		}
	}
	return false;
}

/*
* Finishes the q1
*/
function q1Completed() {
	a6.deactivate();
	document.getElementById("vlabq1done").style.display="block";
	setInnerHTMLById("done_answer_q1",document.step5.enthalpy.value+" kJ/mol.");
}


