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>Create a stock solution</name>\n\t\t<context>Dilution</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) {
		//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";
		document.getElementById("prelab").style.display="block";
		//setInnerHTMLById("identification_date",'Loaded at '+full_time());
		//setInnerHTMLById("identification_name",stuName+", ID: "+stuId);
		
		logLogIn();
	}
}

/**
* Problem object
*/
function Problem() {
	var stockSolutionIndex=intRandom(0,6);
	switch (stockSolutionIndex){
	case 0:
		this.compound="HCl";
		this.stockConc=11.6;
		break;
	case 1:
		this.compound="NaOH";
		this.stockConc=19.0;
		break;
	case 2:
		this.compound="H<sub>2<\/sub>SO<sub>4<\/sub>";
		this.stockConc=17.8;
		break;
	case 3:
		this.compound="HNO<sub>3<\/sub>";
		this.stockConc=15.4;
		break;
	case 4:
		this.compound="HClO<sub>4<\/sub>";
		this.stockConc=15;
		break;
	case 5:
		this.compound="H<sub>3<\/sub>PO<sub>4<\/sub>";
		this.stockConc=14.6;
		break;
	case 6:
		this.compound="NH<sub>3<\/sub>";
		this.stockConc=14.8;
		break;
	}
	var possibleVolumes=new Array(100,250,500,1000);
	this.volume=possibleVolumes[intRandom(0,3)];
	this.conc=format_sig(intRandom(1,6)*.5,3);
}

/**
* Finish prelab and starts the lab
*/
function check_prelab() {
	//check if filled
	if(document.step1.solutionProcedure.value=="") {
		alert("Please answer the question before proceding to the lab.");
	} else {
		a2.deactivate();
		document.getElementById("prelabDone").style.display="block";
		document.getElementById("vlab").style.display="block";
		document.getElementById("prelab").style.display="none";
		setInnerHTMLById("prelabDone_answer",document.step1.solutionProcedure.value.replace(/\n/g," <br>"));
		
		logFormCheck('prelab',1,document.step1.solutionProcedure.value.replace(/\n/g," "),"","",true,"NOT_CHECKED");
	}
	return false;
}

/**
* Finish the lab
*/
function check_lab() {
	var message="";
	
	//check if filled
	if(document.step2.solutionProcedure2.value=="" || document.step2.milliliters.value=="") {
		alert("Please enter your results before submitting.");
	} else if(isNaN(document.step2.milliliters.value)) {
		// skip submit
		return false;
	} else {
		var submMl=document.step2.milliliters.value;
		var correct=parseFloat(thisProblem.conc)/thisProblem.stockConc*thisProblem.volume;
		var userAnswer="PROCEDURE: "+document.step2.solutionProcedure2.value.replace(/\n/g," ")
				+". RESULT: "+submMl+"ml";

		if(relativeError(submMl,correct,0.01)&&number_sig(submMl)==3) {
			logFormCheck('lab',nAttempt,userAnswer,format_sig(correct,3)+"ml","Well done! You did a good job!",true,"CORRECT");
			problemCompleted();
		} else {
			var errorType="UNKNOWN_ERROR";
			if(relativeError(submMl,correct,0.01)&& number_sig(submMl)!=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) {
				    message="Your answer is not correct. The correct solution is "+format_sig(correct,3)+" ml. ";
   	    		    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(correct,3)+"ml",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.solutionProcedure2.value.replace(/\n/g,"<br>"));
	setInnerHTMLById("done_answer2",document.step2.milliliters.value+'ml. ');
}



