/*
	$Id: quizord.js,v 1.1 2006/12/20 16:17:40 joverg Exp joverg $

	Script to be used for word quizes, consisting of a number
	of text entry boxes with an associated button for each.
*/

// ShowSubResult
// =============
// This function shows a text in the label element for a question
// It is called with two parameters:
//	form: The form containing the label element
//	value: The formatted text to be shown
function ShowSubResult(form,value) {
	var inputTags = new Array();
	var x;

	inputTags = form.getElementsByTagName('LABEL');
	if ( inputTags.length != 0 ) {
		x = inputTags[0];
		x.innerHTML = "";
		x.innerHTML = value;
	}
}


// ShowCorrectAnswer
// =================
// This function shows the appropriate text,
// when a user provides the right answer for a question
// It is called with two parameters:
//	form: The form of the question
//	spanclass: The class of the show a correct answer
function ShowCorrectAnswer(form,spanclass) {
	ShowSubResult(form,'<span class="' + spanclass + '">Rigtigt!</span>');
}

// ShowWrongAnswer
// ===============
// This function shows the appropriate text,
// when a user provides the wrong answer for a question.
// If it is called with an non-empty string as the correct answer
// it shows this.
// It is called with three parameters:
//	form: The form of the question
//	rightvalue: The correct answer to the question
//	spanclass: The class of the element to contain a wrong answer
function ShowWrongAnswer(form,rightvalue,spanclass) {
	if (rightvalue != "") {
		ShowSubResult(form,'<a href="javascript:ShowSolution(' + "'" + rightvalue + "'" + ')"><span class="' + spanclass + '">Forkert</span></a>');
	} else {
		ShowSubResult(form,'<span class="' + spanclass + '">Forkert</span>');
	}
}

// ShowSolution
// ============
function ShowSolution(value) {
	alert('Det rigtige svar er "' + value + '"');
}

// NormalizeString
// ===============
// This function normalizes a string by removing leading and trailing spaces,
// normalizing consecutive spaces and converts it to lower case
// It is called with one paramter:
//	s: The string to be normalized
function NormalizeString(s) {
	// Strip leading spaces
	s = s.replace(/^\s*/,'');
	// Strip trailing spaces
	s = s.replace(/\s*$/,''); 
	// Remove more than one spaces
	s = s.replace(/\s{2,}/g,' ');
	// Make string lower case
	s = s.toLowerCase();

	return s;
}

// Validate
//
function Validate(form,okclass,badclass) {
	var inputTags = new Array();
	var i;
	var answer;
	var usertext = "";

	inputTags = form.getElementsByTagName("INPUT");
	for(i=0;i<inputTags.length;i++) {
		if (inputTags[i].getAttribute('type') == 'hidden') {
			answer = inputTags[i].value;
		}
		if (inputTags[i].getAttribute('type') == 'text') {
			usertext = inputTags[i].value;
		}
	}
	if (usertext == "") {
		alert("Du skal først skrive et svar");
	} else {
		if (NormalizeString(usertext) == NormalizeString(answer)) {
			ShowCorrectAnswer(form,okclass);
		} else {
			ShowWrongAnswer(form,answer,badclass);
		}
	}
}
