/**
 *	Created with MAX's HTML Beauty++ 2004
 *	pForm.js
 *	@author Amani Ahmed, Jordi Cuadros
 *	@date July 12 2004
 *	Requires fbElement.js, domHelper.js
*/

var formIndex = 0;

/**	
 * Constructs a fill in the blank question form
 * @param formName			string defining the name and ID of the Pform
 	   	  formTitle			string defining the title of the Pform
 		  formQuestion		string defining the question asked
		  formAnswer		string defining the check function to be used along with arguments to be used
		  formInput			array of answerElements and strings to be inserted into the Pform
		  					or a single answerElement
		  formHints			array of strings defining the hints to be given
		  formInstant		boolean defining if a instant feedback window is needed
		  formFeedback	   	boolean defining if a feedback window is needed
*/
function fbForm(formName, formTitle, formAnswer, formInput, formHints, formInstant, formFeedback){
	// attributes
	this.formName = removeTags(formName);
	this.formIndex = formIndex;
	this.formTitle = formTitle;
	this.formAnswer = formAnswer;

	if(formInput != null){
		if(formInput[0] != null)
			this.formInput = formInput;
		else{
			var modifiedFormInput = new Array(1);
			modifiedFormInput[0] = formInput;
		
			this.formInput = modifiedFormInput;
		}
	} else this.formInput = formInput;		

	this.formHints = formHints;
	this.formInstant = formInstant;
	this.formFeedback = formFeedback;

	// methods
	this.create = pformWrite;
	this.activate = activate;
	this.deactivate = deactivate;
	this.hide=hideForm;
	this.show=showForm;
	this.hideFeedback = hideFeedback;
	this.showFeedback = showFeedback;
	this.giveHint = eval("giveHint");
	this.setFeedback=setFeedback;
	this.setInstantFeedback=setInstantFeedback;
	
	formIndex++;
}


/**
 *	Write a pForm to the page
*/
function pformWrite(){
	inputArea = this.formInput;
	
	document.writeln("<div class='centered'><form class='questionForm' name='"+this.formName+"' id='"+this.formName+"' onSubmit='return "+this.formAnswer+";'>");
	document.writeln("");
	document.writeln("<table class='question'>");
	document.writeln("<tr>");	// write form title
	document.writeln("<td class='questionTD'><p><span class='qtitle'>"+this.formTitle);
	
	if (this.formTitle!="" && this.formTitle!=null) {
		document.writeln("&nbsp;");
	}
	
	document.writeln("<\/span>");
	if(inputArea != null){
		for(c = 0; c < inputArea.length; c++){
			// construct sentence with blanks
				if(inputArea[c].writeBlank) 
					inputArea[c].writeBlank();
				else
					document.writeln(inputArea[c]+"");	
		}
	}

	document.writeln("<\/td>");
	// insert check image and action
	if (this.formAnswer!=null) {
		document.writeln("<td class='buttonTD'><span name='checkimg_"+this.formIndex+"' id='checkimg_"+this.formIndex+"'>");
		document.writeln("<input type='image' border='0' src='../images/check_s.gif' value='Check'><\/span><\/td>");
	} else
		document.writeln("<td><\/td>");	
	
	if(this.formHints != null) { // insert hint image and action
		document.writeln("<td class='buttonTD'><span name='hintimg_"+this.formIndex+"' id='hintimg_"+this.formIndex+"'><a href='#' onMouseOver='window.status=\"Click on the image for a hint!\"; return true;' onMouseOut='window.status=\"\"; return true;' onClick='return document.forms["+this.formIndex+"].logicalForm.giveHint();'> ");
		document.writeln("<img border='0' src='../images/hint_s.gif' alt='Hint'><\/a><\/span>");
	} else
		document.writeln("<td>");
	
	document.writeln("<\/td><\/tr>");
	
	if(this.formInstant)	// insert instant feedback window
	 	document.writeln("<tr><td class='questionTD' colspan='3'><div class='qinstant' id='instant"+this.formIndex+"' name='instant"+this.formIndex+"'>You are about to submit:&nbsp;&nbsp;<\/div><\/td><\/tr>");		
	if(this.formFeedback)	// insert a feedback window
		document.writeln("<tr><td class='questionTD' colspan='3'><div class='feedback' id='feedback"+this.formIndex+"' name='feedback"+this.formIndex+"'><\/div><\/td><\/tr>");
	
	document.writeln("<\/table>");
	document.writeln("<\/form><\/div>");
	
	document.getElementById(this.formName).logicalForm = this;	
	maxID = 0;
}

/**
 * Provides a hint for a given question by using an array of string values
 * @param 	hints	an array of strings which define the hints to be given
*/
var hintIndex = 0;
var hintLastFormIndex=0;
function giveHint(){
	var hints=this.formHints;
	if(hintLastFormIndex!=this.formIndex) {
		hintIndex=0;
		hintLastFormIndex=this.formIndex;
	}
	if(hints[hintIndex] == null)
		hintIndex = 0;

	alert(hints[hintIndex]);
	if(window.logUserAction) logUserAction("giveHint()", "txt", "Hint "+hintIndex+" of form "+ this.formIndex+ " delivered: "+hints[hintIndex]);
	hintIndex++;
	return false;
}

/**
 *	Hides a given form from view
 *
 *	@param		the formName of the pForm to be hidden
*/
function hide() {
	for (var i=0;i<arguments.length;i++) {
		var obj;
		obj=document.getElementById(arguments[i]);
		if (obj!=null) {
			obj.style.height='0';
			obj.style.display='none';
		}
	}
}

/**
* Method to call the hide function
*/
function hideForm() {
	hide(this.formName);
}

/**
 *	Shows a previously hidden pForm
 *
 *	@param		the formName of the pForm to be shown
*/
function show() {
	for (var i=0;i<arguments.length;i++) {
		var obj;
		obj=document.getElementById(arguments[i]);
		if (obj!=null) {
			obj.style.height='auto';
			obj.style.display='block';
		}
	}
}

/**
* Method to call the hide function
*/
function showForm() {
	show(this.formName);
}

/**
 * Disables the answer blanks of a given pForm
*/

function deactivate() {
	for (var i=0;i<document.forms[this.formName].elements.length;i++){
		document.forms[this.formName].elements[i].disabled=true;
	}	

	var cbutton = document.getElementById('checkimg_'+this.formIndex);
	if (cbutton!=null) cbutton.style.display='none';
	if(this.formHints != null){
		var hbutton = document.getElementById('hintimg_'+this.formIndex);
		hbutton.style.display='none';
	}
}

/**
 *	Enables previously disabled answer blanks of a given pForm
 */
function activate() {
	for (var i=0;i<document.forms[this.formName].elements.length;i++){
		document.forms[this.formName].elements[i].disabled=false;
	}	
	
	var cbutton = document.getElementById('checkimg_'+this.formIndex);
	if (cbutton!=null) cbutton.style.display='block';
	if(this.formHints != null){
		var hbutton = document.getElementById('hintimg_'+this.formIndex);
		hbutton.style.display='block';
	}
}

/**
 * Hides the feedback area of a given pForm
 */
function hideFeedback() {
	var fdbk=document.getElementById("feedback"+this.formIndex);
	fdbk.style.display = 'none';	
}

/**
 * Shows a previously hidden feedback area of a given pForm
*/
function showFeedback() {
	var fdbk=document.getElementById("feedback"+this.formIndex);
	fdbk.style.display = 'block';	
}

/**
 * 
 * Method that sets a string value for the contents of a feedback area of a this pForm
 *
 * @param	formIndex 	integer defining the index of a created pForm
 *			message		string defining the text to be entered in the feedback area		
 */
function setFeedback(message,attempt_no){
	setFeedbackFunction(this.formIndex,message,attempt_no);
}

/**
 * Sets a string value for the contents of a feedback area of a given pForm
 *
 * @param	formIndex 	integer defining the index of a created pForm
 *			message		string defining the text to be entered in the feedback area		
 *			attempt_no	integer defining the current attempt number
 */
function setFeedbackFunction(formIndex, message, attempt_no){
	setInnerHTMLById("feedback"+formIndex,"");
	setInnerHTMLById("feedback"+formIndex,message);
	if(attempt_no == 0) attempt_no = 1;
	document.getElementById("feedback"+formIndex).className = "feedback"+attempt_no;
}

/**
 * 
 * Method that sets a string value for the contents of an instant feedback area of a given pForm
 *
 * @param	message		string defining the text to be entered in the instant feedback area		
 */
function setInstantFeedback(message) {
	setInstantFeedbackFunction(this.formIndex,message);
}


/**
 * Sets a string value for the contents of an instant feedback area of a given pForm
 *
 * @param	formIndex 	integer defining the index of a created pForm
 *			message		string defining the text to be entered in the instant feedback area		
 */
function setInstantFeedbackFunction(formIndex, message){
	var InstantNode=document.getElementById("instant"+formIndex);
	setInnerHTMLById("instant"+formIndex,"");
	setInnerHTMLById("instant"+formIndex,"You are about to submit:&nbsp;&nbsp;"+message);
}

/**
* Reset all forms in the web page
*/
function resetAllForms() {
	for(var i=0;i<document.forms.length;i++) {
		document.forms[i].reset();
		if(document.forms[i].logicalForm) document.forms[i].logicalForm.activate();
	}
}
