// riddle.js
/******************************************************************
        Makes use of global variables specified within the
        corresponding HTML document:

	r[] - array of objects derived from class "riddle"

******************************************************************/

var basepath = "http://www.langenscheidt.de/lehrwerke_onlineprojekte/optimal_B1/";
var DOM = (document.getElementById('DOM-Test')) ? true : false;
if (!DOM) alert ("Warnung: Ihre Browser unterstuetzt kein DOM. Es kann zu Beeintraechtigungen bei der Loesung bestimmter Aufgabentypen kommen.\n");

mouse_in = false;

// class "riddle"

function riddle(str,vn) {

	// <attributes>

	this.h       = str.length;	// height
	this.w       = str[0].length;	// width
	this.content = new Array();	// content
	this.varname = vn;		// script variable
	this.key     = -1;		// key column

	// </attibutes>

	// initialize content as a two-dimensional array ("fields")
	for (var i = 0; i < this.h; i++)
		this.content[i] = str[i].split("");

	// <methods>

	// return value of field
	this.get_field = function(x,y) {

		if (this.content[x][y] == "_") return -1;
		else if (this.content[x][y] == " ") return 0;
		else return this.content[x][y];
	}

	// set value of field
	this.set_field = function(x,y,v) {

		this.content[x][y] = v;
	}

	// return formated string of a field's coordinates ("id")
	this.get_fieldid = function(i,j) {

		var x = String(i);
		var y = String(j);

		if (i < 10) x = "0" + x;
		if (j < 10) y = "0" + y;

		return x + y;
	}

	// retrieve index of column that contains the solution ("key")
	this.get_key = function() {

		var i = 1, j = 0;

		while (this.content[0][j] != '_') j++;

		while (this.content[i] && this.content[0][j] != ' ') {
			if (this.content[i][j] != ' ') i++;
			else { i = 1; j++; }
		}

		return (i == this.h) ? j : -1;
	}

	// </methods>
}

// function that joins all riddles' fields into one string (passed to the Perl script)

function JoinItems() {

	// NOTE: the Array() 'r' used in this function is supposed to be a global

	var ptr = (DOM) ? document.getElementById("rdlstr") : document.exercises.rdlstr;
	ptr.value = "";

	// outer loop for multiple riddles
	for (var k = 0; k < r.length; k++) {

		// determine variable name and insert leading descriptor
		var rdl = eval(r[k]);
		var str = rdl.varname + "=";

		// inner loops to address each individual field
		for (var i = 0; i < rdl.h; i++) {
	
			for (var j = 0; j < rdl.w; j++) {

				var v = rdl.get_field(i,j);

				// we don't need non-existing fields
				if (v < 0) str += "_";
				else if (v) str += v;
			}
			// seperate lines
			str += ":";
		}

		// pop last ':' and add a tailing linebreak to seperate multiple riddles
		str.length--;
		str += "\n";

		// write all that into the hidden input field
		ptr.value += str;
	}
}

// print riddle

function PrintRiddle(rdl,flag) {

	// NOTE: var 'flag' determines the design
	//       flag = 0 adds line-numbers
	//       flag = 1 adds a red stripe

	rdl.key = (flag) ? rdl.get_key() : (-1);

	document.write("\t\t\t\t\t\t<TABLE BORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\" ALIGN=\"left\">\n");

	for (var i = -1; i <= rdl.h; i++) {

		document.write("\t\t\t\t\t\t\t<TR>\n");

		for (var j = (-1 + flag); j < rdl.w; j++) {

			// the red stripe
			var stripe = (j == rdl.key) ? " BGCOLOR=\"#ff3333\"" : "";

			document.write("\t\t\t\t\t\t\t\t<TD WIDTH=\"18\" ALIGN=\"right\" VALIGN=\"middle\"");
	
			if (i >= 0 && i < rdl.h) {

				if (j < 0) {
	
					document.write(" CLASS=\"NormalTXT\">\n");
					document.write("\t\t\t\t\t\t\t\t\t&nbsp;" + (i + 1) + "\n");
				}
				else {
	
					// input fields
					if (rdl.get_field(i,j)) {

						var field = rdl.get_field(i,j);
						var fieldid = rdl.varname + rdl.get_fieldid(i,j);

						document.write(stripe + ">");
						document.write("<INPUT TYPE=\"text\" ID=\"" + fieldid + "\" SIZE=\"2\" MAXLENGTH=\"1\" ONKEYUP=\"UpdateField(" + rdl.varname + "," + i + "," + j + ")\"");
						// field is either a character or an integer (latter one incase of 'cell-fillers' and write protected fields)
						if (field != -1) document.write(" VALUE=\"" + field + "\" READONLY");
					}

					 document.write(">\n");
				}
			}

			else {

				if (j >= 0) document.write(stripe);
				document.write(">\n");

				// header and footer	
				var imgheight = (i < 0) ? 20 : 38;
				if (flag) document.write("\t\t\t\t\t\t\t\t\t<IMG SRC=\"" + basepath + "img/trans.gif\" WIDTH=\"20\" HEIGHT=\"" + imgheight + "\" BORDER=\"0\">\n");
			}

			document.write("\t\t\t\t\t\t\t\t</TD>\n");
		}

		document.write("\t\t\t\t\t\t\t</TR>\n");
	}

	document.write("\t\t\t\t\t\t</TABLE>\n");
}

// update value of field

function UpdateField(rdl,i,j) {

	var id = rdl.get_fieldid(i,j);

	var ptr = (DOM) ? document.getElementById(rdl.varname + id) : eval("document.exercises." + rdl.varname + id);

	if (ptr) {

		if (ptr.value) {

			v = ptr.value;
			v = v.toUpperCase();
			v = v.charCodeAt(0);

			if (v >= 65 && v <= 90)
				rdl.set_field(i,j,ptr.value);
		}
		else rdl.set_field(i,j,"_");
	}

	// Netscape doesn't support "UNSUBMIT" within the <FORM> tag, so we have to use a work-around
	JoinItems();
}