var nAttempts1=1;
var nAttempts2=1;
var nAttempts3=1;
var nAttempts4=1;

/**
* 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 {
		//a1.deactivate();
		var stuName=document.step0.studentName.value;
		var stuId=document.step0.studentId.value;
		stuName=stuName.replace(/<.*>/g,"");
		stuId=stuId.replace(/<.*>/g,"");		
		//document.getElementById("identification").style.display="block";
		//document.getElementById("intro").style.display="none";
		//setInnerHTMLById("identification_date",'Loaded at '+full_time());
		//setInnerHTMLById("identification_name",'Name: '+stuName+", Student Number: "+stuId);	
		
		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("identification").style.display="block";
		//document.getElementById("intro").style.display="none";
//		setInnerHTMLById("identification_date",'Loaded at '+full_time());
//		setInnerHTMLById("identification_name",'Name: '+stuName+", Student Number: "+stuId);
		a2.activate();
		logLogIn();
	}
}

/**
* Problem object
*/
function Problem() {
	this.icepackmass=format_sig(90+intRandom(0,7)*5,3);
	this.icepackT=format_sig(10+intRandom(0,3)*2,3);
	this.hotpackmass=format_sig(90+intRandom(0,7)*5,3);
	this.hotpackT=format_sig(40+intRandom(0,4)*3,3);
	this.hotorcold=intRandom(0,1);
	
}
/**
* Problem object
*/

	function check_cold() {
    var AttemptsLimit=3;
	var message="";
	
	//check if filled
	if(document.step1.massnh4no3.value=="" || document.step1.massh2o.value=="") {
		alert("Please enter your results before submitting.");
	} else {
		var calc_nh4no3=((((thisProblem.icepackmass)*(4.184)*(25-thisProblem.icepackT))/(26400))*80.043);
		var calc_water=(thisProblem.icepackmass-calc_nh4no3);
		var isCorrect=relativeError(parseFloat(document.step1.massnh4no3.value),calc_nh4no3,0.015);
		var errorType="UNKNOWN_ERROR";
		
		if(relativeError(calc_nh4no3,document.step1.massnh4no3.value,0.015)&&relativeError(calc_water,document.step1.massh2o.value,0.015)) {
			message="Your answer is CORRECT. Good Job.";
			errorType="CORRECT";
			a2.deactivate();
			document.getElementById("vlab").style.display="none";
			document.getElementById("done").style.display="block";
	
			} else if (nAttempts1>=AttemptsLimit) {
				message="Your answer is WRONG. ";
				message+="The required materials to create the above cold pack are: "+format_sig(calc_nh4no3,4)+" grams of ";
				message+="NH<sub><font size=-1>4<\/font><\/sub>NO<sub><font size=-1>3<\/font><\/sub> and "+format_sig(calc_water,4)+" ";
				message+="grams of water.  Verify this result in the virtual lab and then reload the problem to try again.<p>";
				a2.deactivate();
				a3.deactivate();
				
			} else {
				message+="Your result is wrong, but you have still "+(AttemptsLimit-nAttempts1)+" attempts left. ";
				message+="Check your answer and submit again.";
			}
			a2.setFeedback(message,(isCorrect)?1:nAttempts1);
						
			logFormCheck("step1",nAttempts1,document.step1.massnh4no3.value,format_sig(calc_nh4no3,3),message,isCorrect,errorType);
			nAttempts1++;
		}
	
	return false;
}

	function check_hot() {
    var AttemptsLimit=3;
	var message="";
	
	//check if filled
	if(document.step2.massmgso4.value=="" || document.step2.massh2o.value=="") {
		alert("Please enter your results before submitting.");
	} else {
		var calc_mgso4=((((thisProblem.hotpackmass)*(4.184)*(thisProblem.hotpackT-25))/(84900))*120.363);
		var calc_waterh=(thisProblem.hotpackmass-calc_mgso4);
		var isCorrect=relativeError(parseFloat(document.step2.massmgso4.value),calc_mgso4,0.015);
		var errorType="UNKNOWN_ERROR";
		
		if(relativeError(calc_mgso4,document.step2.massmgso4.value,0.010)&&relativeError(calc_waterh,document.step2.massh2o.value,0.010)) {
			message="Your answer is CORRECT. Good Job.";
			errorType="CORRECT";
			a3.deactivate();
			document.getElementById("vlab").style.display="none";
			document.getElementById("done").style.display="block";
			
			
			} else if (nAttempts2>=AttemptsLimit) {
				message="Your answer is WRONG. ";
				message+="The required materials to create the above cold pack are: "+format_sig(calc_mgso4,4)+" grams of ";
				message+="MgSO<sub><font size=-1>4<\/font><\/sub> and "+format_sig(calc_waterh,4)+" ";
				message+="grams of water.  Verify this result in the virtual lab and then reload the problem and try again.<p>";
				a3.deactivate();
				
			} else {
				message+="Your result is wrong, but you have still "+(AttemptsLimit-nAttempts2)+" attempts left. ";
				message+="Check your answer and submit again.";
			}
			a3.setFeedback(message,(isCorrect)?1:nAttempts2);
						
			logFormCheck("step2",nAttempts2,document.step2.massmgso4.value,format_sig(calc_mgso4,3),message,isCorrect,errorType);
			nAttempts2++;
		}
	
	return false;
}
