var nAttempt=1;
var ContextMessage='\n<class>\n\t<school>CMU</school>\n\t<period>09-106</period>\n'
		+'\t<description>Spring 2008</description>\n</class>\n'
		+'<dataset>\n\t<name>Online Vlab Homeworks</name>\n\t<leveltype="Assigned Homeworks">\n'
		+'\t\t<name>Stoichiometry Assignment</name>\n\t<problem>\n'
		+'\t\t<name>Determine stoichiometric ratio</name>\n\t\t<context>Stoichiometry </context>\n'
		+'\t</problem>\n\t</level>\n</dataset>\n';


/**
* 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";
		document.getElementById("prelab").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) {
		//1.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";
		//setInnerHTMLById("identification_date",'Loaded at '+full_time());
		//setInnerHTMLById("identification_name",stuName+", ID: "+stuId);
		
		logLogIn();
	}
}

/**
* Problem object
*/
function Problem() {
	this.conc4B=format_sig(intRandom(1,5)/10,2);
	this.vol4B=format_sig(intRandom(1,10)*5,2);
	this.conc2B=format_sig(intRandom(1,5)/10,2);
	do {
		this.vol2B=format_sig(intRandom(1,10)*5,2);
	} while (this.conc4B*this.vol4B*2==this.conc2B*this.vol2B)
}

/**
* Finish prelab and starts the lab
*/
function check_prelab() {
	//check if filled
	if(document.step1.predict.value=="") {
		alert("Please answer the question before proceding to the lab.");
	} else {
		a2.deactivate();
		document.getElementById("vlab").style.display="block";
		document.getElementById("prelabDone").style.display="block";
		document.getElementById("prelab").style.display="none";
		setInnerHTMLById("prelabDone_answer",document.step1.predict.value.replace(/\n/g,"<br>"));

		logFormCheck('prelab',1,document.step1.predict.value.replace(/\n/g," "),"","",true,"NOT_CHECKED");
	}
	return false;
}

/**
* Finish the lab
*/
function check_lab() {
	var message="";
	
	//check if filled
	if(document.step2.post.value=="" || document.step2.moles.value=="" || document.step2.reactant.selectedIndex==0) {
		alert("Please enter your results before submitting.");
	} else if(isNaN(document.step2.moles.value)) {
		// skip submit
		return false;
	} else {
		var submMoles=document.step2.moles.value;
		var submReactant=document.step2.reactant.selectedIndex;
		var correctReactant=(thisProblem.conc4B*thisProblem.vol4B*2>thisProblem.conc2B*thisProblem.vol2B)?1:2;
		var correctMoles=Math.abs(thisProblem.conc4B*thisProblem.vol4B*2-thisProblem.conc2B*thisProblem.vol2B)/2*correctReactant;
		var userAnswer="PROCEDURE: "+document.step2.post.value.replace(/\n/g," ")
				+". RESULT: "+submMoles+" nanomoles (10^-9 moles) of "+submReactant;
		
		if(relativeError(submMoles,correctMoles,0.01)&&number_sig(submMoles)==3&&submReactant==correctReactant) {
			logFormCheck('lab',nAttempt,userAnswer,format_sig(correctMoles,3)+" nanomoles (10^-9 moles) of "+
					document.step2.reactant.options[correctReactant].text,"Well done! You did a good job!",true,"CORRECT");
			problemCompleted();
		} else {
			var errorType="UNKNOWN_ERROR";
			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 {
			    if (nAttempt == 3) {
    				message="Your answer is not correct. The correct solution is "+format_sig(correctMoles,3)+" nanomoles (10^-9 moles) of ";
    				message+=document.step2.reactant.options[correctReactant].text;
    	    		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();
				}
			    else {
			        var n = 3 - nAttempt;
			        message = "Your answer is incorrect, but you have " + n + " attempt";
			        if (n > 1) message += "s";
			        message += " left.";
			    }
			}
			logFormCheck('lab',nAttempt,userAnswer,format_sig(correctMoles,3)+" nanomoles (10^-9 moles) of "+
					document.step2.reactant.options[correctReactant].text,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_answer1",document.step2.post.value.replace(/\n/g,"<br>"));
	setInnerHTMLById("done_answer2",document.step2.moles.value+' nanomoles (10^-9 moles) of '+
			document.step2.reactant.options[document.step2.reactant.selectedIndex].text +
			'.');
}



