var nAttempt=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("intro").style.display="none";
		
	//	document.getElementById("identification").style.display="block";
		document.getElementById("description").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) {
		//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";
		//setInnerHTMLById("identification_date",'Loaded at '+full_time());
		//setInnerHTMLById("identification_name",stuName+", ID: "+stuId);
		
		logLogIn();
	}
}

/**
* Problem object
*/
function Problem() {
	this.molesAgNO3=doubleRandom(0.01,0.02);
	this.unknownString="u1,"+this.molesAgNO3;
}

/**
* Finish the lab
*/
function check_lab() {
	var message="";
	
	//check if filled
	if(document.step2.grams.value=="") {
		alert("Please enter your results before submitting.");
	} else if(isNaN(document.step2.grams.value)) {
		// skip submit
		return false;
	} else {
		var submGrams=document.step2.grams.value;
		var correctGrams=(thisProblem.molesAgNO3*169.8731);
		var userAnswer="RESULT: "+submGrams+" grams ";
		
		if(relativeError(submGrams,correctGrams,0.01)&&number_sig(submGrams)==3) {
			logFormCheck('lab',nAttempt,userAnswer,format_sig(correctGrams,3)+" grams of of AgNO<sub>3</sub>"+
					" &nbsp; Well done! You did a good job!",true,"CORRECT");
			problemCompleted();
		} else {
			var errorType="UNKNOWN_ERROR";
			if(relativeError(submGrams,correctGrams,0.01)&&number_sig(submGrams)!=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 (nAttempt < 3) {
			    n = 3 - nAttempt;
			    message = "Your answer is wrong, but you still have " + n + " attempt";
			    if (n > 1) { message += "s"; }
			    message += " left.";
			}
			else {
				message="Your answer is not correct. The correct solution is "+format_sig(correctGrams,3)+
				" grams of of AgNO<sub>3</sub> ";
	    		message+=". Use the Virtual Lab to make sense of this result and hit the RELOAD/REFRESH button ";
   	    		message+="to start over with a new solution. Keep going!";
				a3.deactivate();
			}
			logFormCheck('lab',nAttempt,userAnswer,format_sig(correctGrams,3)+" grams of AgNO3 "+
					message,false,errorType);
			a3.setFeedback(message,nAttempt);
			nAttempt++;		
		}
	}
	return false;
}

/*
* Finishes the problem
*/
function problemCompleted() {
	a3.deactivate();
	document.getElementById("vlab").style.display="none";
	document.getElementById("done").style.display="block";
	setInnerHTMLById("done_answer2",document.step2.grams.value+' grams of Silver Nitrate ');
}


