/**
 *	Created with MAX's HTML Beauty++ 2004
 *	empirical_check.js
 *	@author Amani Ahmed, Jordi Cuadros
 *	@date August 24 2004
*/

var minMoles = -1;		//negative number to indicate undefined

function calcMinMoles() {
	for(t=0;t<CmdElements.length;t++){
		var molesThisElement = round_sig(CmdElements[t].moles/CmdWeight,3);
		if(molesThisElement < minMoles || minMoles<0) minMoles = molesThisElement;
	}
}

/** 
 * Based on the index number and type given, this function changes the color of the appropriate
 * textbox to green or red. Green if the answer is correct and red if it is not. This is the check
 * function for the first two activities on the EmpiricalFormula.html page.  Currently asks for 
 * an exact answer to 3 significant digits on most questions; this can be changed to accept answers
 * in a range instead.
 * @param	s				integer defining index of the specie in the CmdElements array
 *			checkingThis	string defining the type of answer requested
 *
 *	DECIDE WHETHER YOU WANT THE ANSWER IN A RANGE OR EXACT TO A CERTAIN NUMBER OF SIG DIGS
 */
function colorChangeCheck(s, checkingThis){	
	var place = CmdElements[s];	
	var correct="";
	var user_data="";
	var changed_object=null;
	var active_form=null;
	var isWrong=true;
	var feedback_text="";
	var activity="1";

	if (minMoles==-1) calcMinMoles();
	
	// cancel feedbacks on the two first steps
	for (var i=0; i<document.forms.length;i++) {
		if (document.forms[i].name.substring(0,"activity1".length)=="activity1" ||
				document.forms[i].name.substring(0,"activity1".length)=="activity2") {
			setFeedback(i,"",1);
		}
	}	
	switch(checkingThis){
		case "grams":	
			correct = round_sig((gramsCompound * (place.moles * place.weight))/ CmdWeight, 3);
			eval("active_form=document.activity1"+s+".logicalForm;");
			eval("changed_object=document.activity1"+s+".grams"+place.specie+"InCmd;");		
			user_data=changed_object.value;
			if(user_data==correct) isWrong=false;
			else feedback_text="This ratio is intented to relate the mass of an element to the total mass of the compound. Which of your data corresponds to this relationship?";
			break;
		case "moles":
			correct = 1;
			eval("active_form=document.activity1"+s+".logicalForm;");
			eval("changed_object=document.activity1"+s+".moles"+place.specie+";");		
			user_data=changed_object.value;
			if(user_data==correct) isWrong=false;
			else feedback_text="How many moles are in a mass equivalent to the atomic weight of an element?";
			break;		
		case "mWeight":
			correct = place.weight;
			eval("active_form=document.activity1"+s+".logicalForm;");
			eval("changed_object=document.activity1"+s+".mWeight"+place.specie+";");		
			user_data=changed_object.value;
			if(user_data==correct) isWrong=false;
			else feedback_text="This ratio is intended to convert the mass of this element to the number of moles. What should you use for this purpose?";
			break;
		case "gramsOfCmd":
			correct = gramsCompound;
			eval("active_form=document.activity1"+s+".logicalForm;");
			eval("changed_object=document.activity1"+s+".gramsOfCmd;");		
			user_data=changed_object.value;
			if(user_data==correct) isWrong=false;
			else feedback_text="How many grams of compound do you have?";
			break;
		case "gramsOfCmd2":
			correct = gramsCompound;
			eval("active_form=document.activity1"+s+".logicalForm;");
			eval("changed_object=document.activity1"+s+".gramsOfCmd2;");		
			user_data=changed_object.value;
			if(user_data==correct) isWrong=false;
			else feedback_text="How many grams of compound do you have?";
			break;
		case "molesInCmd":
			correct = round_sig(place.moles/CmdWeight,3);
			eval("active_form=document.activity1"+s+".logicalForm;");
			eval("changed_object=document.activity1"+s+".moles"+place.specie+"InCmd;");		
			user_data=changed_object.value;
			if(relativeError(user_data,correct,0.01) && number_sig(user_data)>=3) isWrong=false;
			else
				if(absError(user_data,correct,5*Math.pow(10,Math.floor(Math.log(user_data)/Math.LN10)-number_sig(user_data))))
					feedback_text="Watch out for those significant figures! You should be using at least three here.";
			break;
		case "molesInCmd2":
			correct = round_sig(place.moles/CmdWeight,3);
			eval("active_form=document.activity2"+s+".logicalForm;");
			eval("changed_object=document.activity2"+s+".moles"+place.specie+"InCmd;");		
			user_data=changed_object.value;
			if(relativeError(user_data,correct,0.01)) isWrong=false;
			if(relativeError(user_data,correct,0.01) && number_sig(user_data)>=3) isWrong=false;
			else
				if(absError(user_data,correct,5*Math.pow(10,Math.floor(Math.log(user_data)/Math.LN10)-number_sig(user_data))))
					feedback_text="Watch out for those significant figures! You should be using at least three here.";
				else feedback_text="Remember you have to use the number of moles of this element in 100 grams of compound.";
			break;
		case "smallestMoles":
			correct = minMoles;
			eval("active_form=document.activity2"+s+".logicalForm;");
			eval("changed_object=document.activity2"+s+".smallestMoles;");		
			user_data=changed_object.value;
			if(relativeError(user_data,correct,0.01) && number_sig(user_data)>=3) isWrong=false;
			else
				if(absError(user_data,correct,5*Math.pow(10,Math.floor(Math.log(user_data)/Math.LN10)-number_sig(user_data))))
					feedback_text="Watch out for those significant figures! You should be using at least three here.";
				else feedback_text="Remember you have to use the smallest number of moles calculated above.";
			break;
		case "ratio": 
			correct = round_sig(place.moles/CmdWeight/minMoles,3);
			eval("active_form=document.activity2"+s+".logicalForm;");
			eval("changed_object=document.activity2"+s+"."+place.specie+"Ratio;");		
			user_data=changed_object.value;
			if(relativeError(user_data,correct,0.01)) isWrong=false;
			break;
	}
	activity=active_form.formName.substring(active_form.formName.length-2,active_form.formName.length-1)
	
	if (!(changed_object==null || changed_object.value=="")) {
		if (isWrong) {
			changed_object.style.color='red';
		} else {
			changed_object.style.color='green';
		}
		setFeedback(active_form.formIndex,feedback_text,3);
		if(window.logUserAction) logUserAction("EmpiricalFormula_change()", "txt", changeString(activity,place.specie,checkingThis,isWrong,user_data,correct,feedback_text));	
	}
}

/**
 * Check function for the THIRD activity on the Empirical Formula problem
 */
var nAttempts = 1;
function act3Check(){
	var message="";
	var user_answer = new Array(CmdElements.length);
	var user_answer_string="";
	var isAttemptWrong=true;
		
	for(i=0;i<user_answer.length; i++){
		user_answer[i] = eval("document.activity3."+CmdElements[i].specie+"_sub.value");
		if(user_answer[i] == ""){
			user_answer[i] = 1;
		}
		user_answer_string+=""+CmdElements[i].specie+user_answer[i];	
	}		

	var cnt = 0;
	for(i=0;i<user_answer.length;i++){
		if(user_answer[i] == CmdElements[i].moles)
			cnt++
	}
	
	if(cnt == user_answer.length){
		message+="Great Job!";
		isAttemptWrong=false;
		deactivateAll();
		nAttempts = 0;
	}else if(nAttempts < 3){
		if(cnt == user_answer.length-1)
			message+="You're pretty close! But one of the subscripts is incorrect.";
		else
	 	 	message+="Try again."; 	
	}else if (nAttempts==3) {
    	message+="This was your third attempt and your answer wasn't correct. ";
    	message+="The correct empirical formula is ";
		for(y=0;y<CmdElements.length;y++){
			message+=CmdElements[y].specie;
			if(CmdElements[y].moles != 1) message+=CmdElements[y].moles;
		}
		message+=".";
		deactivateAll();
 	} else if (nAttempts > 3) {
		message="You have already spent your three allowed attempts for this question. CLICK REFRESH/RELOAD to start over with a new problem.";
		deactivateAll();
	}
	setFeedback(document.forms.length-1, message,nAttempts);			
	if(window.logUserAction) logUserAction("EmpiricalFormula_Step3_check()", "txt", checkString(3,isAttemptWrong,nAttempts,user_answer_string,message));	
	nAttempts++;
	return false;
}

/**
*	Deactivates all the forms in the document
*/
function deactivateAll() {
	for(var i=0;i<document.forms.length;i++) {
		document.forms[i].logicalForm.deactivate();
	}
}

/**	
 * Constructs a string that reports the statistics of the user's latest attempt of the 
 * problem including the attempt number, user-given answers and feedback
 *
 * @return toTrace		a string containing information about the user's last attempt
*/
function checkString(fIndex,isWrong,nAtt,usAns,message) {
    var toTrace=isWrong?"Wrong":"Correct";
    	toTrace += " checking of form "+fIndex;
		toTrace += "(attempt "+nAtt+ "). ";	
		toTrace += "The delivered answer is "+usAns+". ";
		toTrace += "The system answers: "+message;
	return toTrace;
}

/**	
 * Constructs a string that reports the statistics of the user's latest change in a cell of the
 * problem
 *
 * @return toTrace		a string containing information about the user's last attempt
*/
function changeString(step,element,magnitude_checked,isWrong,usAns,expected,message) {
    var toTrace=isWrong?"Wrong":"Correct";
    	toTrace += " change in step "+step+": "+magnitude_checked+" for "+element+". ";
		toTrace += "The delivered answer is "+usAns+" while the expected is "+expected+". ";
		if(message!="") toTrace += "The system complaints: "+message;
	return toTrace;
}

