function trim(inputString) 
{
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;

	// replace '«' and '»' with ' ' ?
   while (retValue.indexOf("«") != -1) {
      retValue = retValue.substring(0, retValue.indexOf("«")) + " " + retValue.substring(retValue.indexOf("«")+1, retValue.length);
   }
   while (retValue.indexOf("»") != -1) {
      retValue = retValue.substring(0, retValue.indexOf("»")) + " " + retValue.substring(retValue.indexOf("»")+1, retValue.length);
   }

   var ch = retValue.substring(0, 1);
   while (ch == " ") {
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while ((ch == " ")||(ch == "?")||(ch == "!")||(ch == ".")) {
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) {
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
   }
   
   var Bis = " ,^, ^ !^! ^ ?^? ^ -^- ^ .^. ".split("^");
   var Bir = ",^,^!^!^?^?^-^-^.^.".split("^");
   
   for (var i=0;i<Bis.length;i++)
   {
		while (retValue.indexOf(Bis[i]) != -1) {
			retValue = retValue.substring(0, retValue.indexOf(Bis[i])) + Bir[i] + retValue.substring(retValue.indexOf(Bis[i])+Bis[i].length, retValue.length);
		}
   }
   return retValue;
}

function trim2(inputString)
{
	var retValue = inputString;
	retValue = trim(retValue);
	if (retValue.length>0) retValue = retValue.substring(0,1).toUpperCase() + retValue.substring(1,retValue.length);
	return retValue;
}

function rand ( n )
{
  return ( Math.floor ( Math.random ( ) * n ) );
}

function enHex(aDigit)
{
    return("0123456789ABCDEF".substring(aDigit, aDigit+1));
}

function toHex(n)
{
    return (enHex((0xf00000 & n) >> 20) +enHex((0x0f0000 & n) >> 16) +enHex((0x00f000 & n) >> 12) +
            enHex((0x000f00 & n) >>  8) +enHex((0x0000f0 & n) >>  4) +enHex((0x00000f & n) >>  0));
}

function getAbsolutePositionX(obj,parent) {
  var c=0;
  while (obj.id != parent)
  {
    var d = (obj.offsetLeft + (obj.style.borderLeftWidth ? parseInt(obj.style.borderLeftWidth) : 0));
    c += d;
    obj = obj.offsetParent;
    if (!obj) break;
    //while (obj.tagName == "TR" || obj.tagName == "FORM") obj = obj.parentNode;
  }
  return c;
}

function getAbsolutePositionY(obj,parent) {
  var c=0;
  while (obj.id != parent)
  {
    var d = (obj.offsetTop + (obj.style.borderTopWidth ? parseInt(obj.style.borderTopWidth) : 0));
    c += d;
    obj = obj.offsetParent;
    if (!obj) break;
  }
  return c;
}

function getX(obj) { return getAbsolutePositionX(obj,"CanvasQ"); }
function getY(obj) { return getAbsolutePositionY(obj,"CanvasQ"); }

function getAX(obj) { return getAbsolutePositionX(obj,"."); }
function getAY(obj) { return getAbsolutePositionY(obj,"."); }

var Questions = new Array();
var QStatus = new Array(); // 0 - none, 1 - skipped, 2 - answered
var QCount = 0;
var QPos = 0;
var QAns = 0;

function AddQuestion(q)
{
	Questions[QCount] = q;
	QStatus[QCount] = 0;
	QCount++;
}

function Q_ABC(question,answers,correct,id)
{
	this.Q = question;
	this.A = answers.split("^");
	this.C = correct - 1;
	this.S = -1;
	
	this.Id = (id!=null?id:"");
	
	this.Show = function ()
		{
			var text = "";
			
			text += '<div style="margin-left:20px; text-indent:-20px">'+this.Q+"</div>";

			text += "<form name='Q"+this.Id+"_AnsForm' onSubmit='return false;' style='margin:10px 0px 0px 50px;'>";

			for (var i=0;i<this.A.length;i++)
			{
				text += "<div style='text-indent:-22px;' ><input type=radio name='Q"+this.Id+"_AnsRG' value='"+(i)+"'>&nbsp;"+this.A[i]+"</input></div>";
			}
			
			text += "</form>";
			
			
			return text;
		}
	this.Mistakes = function ()
		{
			var text = "";
			
			text += this.Q+' <span class="aIncorrect">[ ';
			
			if (this.S>=0) text += this.A[this.S];
			
			text += ' ]</span> <span class="aCorrect">( ';

			text += this.A[this.C];

			text += ' )</span><br>';
			
			return text;
		}
	this.Answered = function ()
		{
			if (this.A.length>1)
			{
				for (var i=0;i<this.A.length;i++)
				{
					if (document["Q"+this.Id+"_AnsForm"]["Q"+this.Id+"_AnsRG"][i].checked) 
					{
						this.S = i;
						return 1;
					}
				}
			} else {
				if (document["Q"+this.Id+"_AnsForm"]["Q"+this.Id+"_AnsRG"].checked) 
				{
					this.S = 0;
					return 1;
				}
			}
			return 0;
		}
	this.Mark = function ()
		{
			return (this.S==this.C?1:0);
		}
	this.Reset = function ()
		{
			this.S = -1;
		}
}

function Q_MultiABC(mainquestion,questions)
{
	this.MQ = mainquestion;
	this.Qs = new Array();
	
	var t1s = questions.split("&");
	for (var i=0;i<t1s.length;i++)
	{
		var t2s = t1s[i].split("~");
		var q = unescape("%"+(i+49).toString(16))+") "+t2s[0];
		var a = t2s[1];
		var c = t2s[2];
		this.Qs[i] = new Q_ABC(q,a,c,i);
	}
	
	this.Show = function ()
		{
			var text = "";
			
			text += this.MQ+"<br><br>";

			text += '<table cellspacing="0" cellpadding="2"><tr valign="top"><td>';
			var i;
			for (i=0;i<Math.ceil(this.Qs.length/2);i++)
			{
				text += this.Qs[i].Show();
				if (i+1<Math.ceil(this.Qs.length/2)) text += "<br>";
			}
			text += '</td><td width="25px">&nbsp;</td><td bgcolor="lightgrey"></td><td width="25px">&nbsp;</td><td>';
			for (var j=i;j<this.Qs.length;j++)
			{
				text += this.Qs[j].Show();
				if (j+1<this.Qs.length) text += "<br>";
			}
			text += '</td></tr></table>';			
			
			return text;
		}
	this.Mistakes = function ()
		{
			var text = "";
			
			text += this.MQ+"<br><br>";

			for (var i=0;i<this.Qs.length;i++)
			{
				if (this.Qs[i].Mark()<1) text += this.Qs[i].Mistakes();
			}
			
			return text;
		}
	this.Answered = function ()
		{
			var done = 1;
			for (var i=0;i<this.Qs.length;i++)
			{
				if (!this.Qs[i].Answered()) done = 0;
			}
			return done;
		}
	this.Mark = function ()
		{
			var S = 0;
			for (var i=0;i<this.Qs.length;i++)
			{
				S += this.Qs[i].Mark();
			}
			return S/this.Qs.length;			
		}
	this.Reset = function ()
		{
			for (var i=0;i<this.Qs.length;i++)
			{
				this.Qs[i].Reset();
			}
		}
}

function Q_Fill(question,blanks)
{
	this.Q = question;
	this.Fs = new Array();
	
	var t1s = blanks.split("&");
	for (var i=0;i<t1s.length;i++)
	{
		var t2s = t1s[i].split("~");
		this.Fs[i] = new Object();
		if (t2s[0]=='1') 
		{
			this.Fs[i].type = 1;
			this.Fs[i].text = t2s[1];
		} else if (t2s[0]=='2') {
			this.Fs[i].type = 2;
			this.Fs[i].anss = t2s[1].split("^");
			if (t2s.length>3) this.Fs[i].trim = t2s[3]; else this.Fs[i].trim = " ";
			var ms = 3;
			if (t2s.length>2) if (t2s[2].length>0) ms = eval(t2s[2]);
			for (var j=0;j<this.Fs[i].anss.length;j++) if (ms<this.Fs[i].anss[j].length+2) ms = this.Fs[i].anss[j].length+2;
			this.Fs[i].size = ms;
		} else if (t2s[0]=='3') {
			this.Fs[i].type = 3;
		} else if (t2s[0]=='4') {
			this.Fs[i].type = 4;
		}
	}
	
	this.Show = function ()
		{
			var text = "";
			
			text += this.Q+"<br><br>";

			text += "<form name='Q_AnsForm' onSubmit='return false;' style='margin:0px 0px 0px 0px;'>";
			text += '<table cellspacing="0" cellpadding="4"><tr valign="top"><td>';
			
			for (var i=0;i<this.Fs.length;i++)
			{
				if (this.Fs[i].type==1)
				{
					text += this.Fs[i].text;
				} else if (this.Fs[i].type==2) {
					text += "<input type=text id='ID_"+(i)+"' name='Q_AnsE"+i+"'"+(this.Fs[i].size>0?"size='"+this.Fs[i].size+"'":"")+">";
				} else if (this.Fs[i].type==3) {
					text += "</td><td>";
				} else if (this.Fs[i].type==4) {
					text += "</td></tr><tr valign='top'><td>";
				}
			}
			
			text += "</td></tr></table></form>";
			
			return text;
		}
	this.Mistakes = function ()
		{
			var text = "";
			
			text += this.Q+"<br><br>";

			text += '<table cellspacing="0" cellpadding="4"><tr valign="top"><td>';
			
			for (var i=0;i<this.Fs.length;i++)
			{
				if (this.Fs[i].type==1)
				{
					text += this.Fs[i].text;
				} else if (this.Fs[i].type==2) {
					var cor = 0;
					var ans = eval("trim"+this.Fs[i].trim+"(this.Fs[i].asel)");
				
					for (var j=0;j<this.Fs[i].anss.length;j++)
					{
						if (ans == eval("trim"+this.Fs[i].trim+"(this.Fs[i].anss[j])"))
						{
							cor = 1;
							break;
						}
					}
					
					if (cor == 1)
					{
						text += ' <span class="aCorrect">' + this.Fs[i].asel + '</span> ';
					} else {
						text += ' <span class="aIncorrect">[ ' + this.Fs[i].asel + ' ]</span> ';
						
						text += ' <span class="aCorrect">( ';
						
						for (var j=0;j<this.Fs[i].anss.length;j++)
						{
							if (j>0) text += " / ";
							 text += this.Fs[i].anss[j];
						}
						
						text += ' )</span> ';
					}
				} else if (this.Fs[i].type==3) {
					text += "</td><td>";
				} else if (this.Fs[i].type==4) {
					text += "</td></tr><tr valign='top'><td>";
				}
			}
			
			text += "</td></tr></table>";
			
			return text;
		}
	this.Answered = function ()
		{
			var done = 1;
			for (var i=0;i<this.Fs.length;i++)
			{
				if (this.Fs[i].type!=2) continue;
				
				this.Fs[i].asel = document["Q_AnsForm"]["Q_AnsE"+i].value;
				/*if (document["Q_AnsForm"]["Q_AnsE"+i].value.length<=0)
				{
					document["Q_AnsForm"]["Q_AnsE"+i].focus();
					done = 0;
				} else {
				}*/
			}
			return done;
		}
	this.Mark = function ()
		{
			var S = 0;
			var N = 0;
			for (var i=0;i<this.Fs.length;i++)
			{
				if (this.Fs[i].type!=2) continue;

				N++;
				var ans = eval("trim"+this.Fs[i].trim+"(this.Fs[i].asel)");
				
				for (var j=0;j<this.Fs[i].anss.length;j++)
				{
					if (ans == eval("trim"+this.Fs[i].trim+"(this.Fs[i].anss[j])"))
					{
						S++;
						break;
					}
				}
			}
			return S/N;			
		}
	this.Reset = function ()
		{
			for (var i=0;i<this.Fs.length;i++)
			{
				if (this.Fs[i].type==2) this.Fs[i].asel = null;
			}
		}
	
}

function qMatch_StopConnecting()
{
	if (Questions[QPos].Connecting)
	{
		var id = "qM_"+Questions[QPos].ConnCol+"_"+Questions[QPos].ConnRow;
		document.getElementById(id).style.backgroundColor = "white";
		Questions[QPos].Canvas[Questions[QPos].ConnCol][Questions[QPos].ConnRow].clear();
		Questions[QPos].Connecting = 0;
		Questions[QPos].Ans[Questions[QPos].ConnCol][Questions[QPos].ConnRow] = -1;
	}
}

function qMatch_MOver(obj)
{
	var aid = obj.id.split("_");
	if (aid[0]!="qM") return;
	if (Questions[QPos].Connecting)
	{
		if (aid[1]==(Questions[QPos].ConnCol+1)) 
		{ 
			obj.style.backgroundColor = "#FAEBD7";
			
			var ob1 = document.getElementById("qI_"+Questions[QPos].ConnCol+"_"+Questions[QPos].ConnRow);
			var Canvas = Questions[QPos].Canvas[Questions[QPos].ConnCol][Questions[QPos].ConnRow];

			Canvas.clear();
			Canvas.setStroke(-1);
			Canvas.drawLine(getX(ob1)+ob1.offsetWidth, getY(ob1)+(ob1.offsetHeight)/2, getX(obj)-2, getY(obj)+(obj.offsetHeight)/2);
			Canvas.paint();
			
			return;
		}
		else if ((aid[1]==Questions[QPos].ConnCol)&&(aid[2]==Questions[QPos].ConnRow)) return;
	}
	if (aid[1]>=Questions[QPos].Cols-1) return;
	if (Questions[QPos].Ans[aid[1]][aid[2]]>=0) return;
	
	obj.style.backgroundColor = "lightyellow";
}

function qMatch_MOut(obj)
{
	if (Questions[QPos].Connecting)
	{
		var aid = obj.id.split("_");
		if (aid[1]==Questions[QPos].ConnCol) 
			if (aid[2]==Questions[QPos].ConnRow) 
				return;
		Questions[QPos].Canvas[Questions[QPos].ConnCol][Questions[QPos].ConnRow].clear();
	}
	var aid = obj.id.split("_");
	if (aid[0]!="qM") return;
	if (aid[1]<Questions[QPos].Cols-1)
	{
		if (Questions[QPos].Ans[aid[1]][aid[2]]>=0) {obj.style.backgroundColor = "#E0FFFF"; return;}
	}
	else if (aid[1]>0)
	{
		for (var i=0;i<Questions[QPos].Rows;i++)
		{
			if (Questions[QPos].Ans[eval(aid[1])-1][i] == aid[2]) {obj.style.backgroundColor = "#E0FFFF";return;}
		}
	}
	obj.style.backgroundColor = "white";
}

function qMatch_MDown(obj)
{
	Questions[QPos].MDown = 1;
	qMatch_StopConnecting();
}

function qMatch_MMove(obj)
{
	if (Questions[QPos].MDown) 
	{
		if (!Questions[QPos].Connecting)
		{
			var aid = obj.id.split("_");
			if (aid[0]!="qM") return;
			if (aid[1]>=Questions[QPos].Cols-1) return;
			
			Questions[QPos].Connecting = 1;
			Questions[QPos].ConnCol = eval(aid[1]);
			Questions[QPos].ConnRow = eval(aid[2]);
			obj.style.backgroundColor = "#7FFFD4";
			Questions[QPos].Canvas[Questions[QPos].ConnCol][Questions[QPos].ConnRow].clear();
			if (Questions[QPos].Ans[Questions[QPos].ConnCol][Questions[QPos].ConnRow]>=0)
			{
				var id = "qM_"+(Questions[QPos].ConnCol+1)+'_'+Questions[QPos].Ans[Questions[QPos].ConnCol][Questions[QPos].ConnRow];
				document.getElementById(id).style.backgroundColor = "white";
			}
			var id = "qI_"+Questions[QPos].ConnCol+'_'+Questions[QPos].ConnRow;
			document.getElementById(id).value = "";
			Questions[QPos].Ans[Questions[QPos].ConnCol][Questions[QPos].ConnRow] = -1;
		}
	}
}

function qMatch_MUp(obj)
{
	Questions[QPos].MDown = 0;
	if (Questions[QPos].Connecting)
	{
		var aid = obj.id.split("_");
		if (aid[0]=="qM")
		{
			if (aid[1]==(Questions[QPos].ConnCol+1)) 
			{ 
				// check if another (correct) choice with the same text exists
				var Cor = Questions[QPos].Cor[Questions[QPos].ConnCol][Questions[QPos].ConnRow];
				var ca = 0;
				var cai = -1;
				for (var i=0;i<Cor.length;i++)
				{
					if (Cor[i]==aid[2]) { ca = 1; break; }
					if (Questions[QPos].Col[Questions[QPos].ConnCol+1][Cor[i]]==Questions[QPos].Col[Questions[QPos].ConnCol+1][aid[2]])
					{ cai = Cor[i]; break; }
				}
				if ((!ca)&&(cai>=0))
				{
					aid[2] = cai;
					obj = document.getElementById(aid.join("_"));
				}
				
				obj.style.backgroundColor = "#FAEBD7";
				
				var ob1 = document.getElementById("qM_"+Questions[QPos].ConnCol+"_"+Questions[QPos].ConnRow);
				ob1.style.backgroundColor = "#E0FFFF";
				obj.style.backgroundColor = "#E0FFFF";
				
				var ob1 = document.getElementById("qI_"+Questions[QPos].ConnCol+"_"+Questions[QPos].ConnRow);
				var Canvas = Questions[QPos].Canvas[Questions[QPos].ConnCol][Questions[QPos].ConnRow];

				ob1.value = 1+eval(aid[2]);

				Canvas.clear();
				Canvas.setStroke(2);
				Canvas.drawLine(getX(ob1)+ob1.offsetWidth, getY(ob1)+(ob1.offsetHeight)/2, getX(obj)-2, getY(obj)+(obj.offsetHeight)/2);
				Canvas.paint();

				Questions[QPos].Ans[Questions[QPos].ConnCol][Questions[QPos].ConnRow] = -1;
				for (var i=0;i<Questions[QPos].Rows;i++)
				{
					if (Questions[QPos].Ans[Questions[QPos].ConnCol][i] == aid[2])
					{
						Questions[QPos].Canvas[Questions[QPos].ConnCol][i].clear();
						var id = "qM_"+(Questions[QPos].ConnCol)+'_'+i;
						document.getElementById(id).style.backgroundColor = "white";
						var id = "qI_"+(Questions[QPos].ConnCol)+'_'+i;
						document.getElementById(id).value = "";
						Questions[QPos].Ans[Questions[QPos].ConnCol][i] = -1;
					}
				}
				Questions[QPos].Ans[Questions[QPos].ConnCol][Questions[QPos].ConnRow] = eval(aid[2]);
				
				Questions[QPos].Connecting = 0;
				return;
			}
		}
	}
	qMatch_StopConnecting();
}

function qMatch_MTOut(event,obj)
{
	if (!Questions[QPos].MDown) return;
	var tx = getAX(obj);
	var ty = getAY(obj);
	var tw = obj.offsetWidth;
	var th = obj.offsetHeight;

	var mx = event.clientX + (document.documentElement.scrollLeft>=0?document.documentElement.scrollLeft:window.pageXOffset);
	var my = event.clientY + (document.documentElement.scrollTop>=0?document.documentElement.scrollTop:window.pageYOffset);

	if ((mx<=tx+5)||(my<=ty+5)||(mx>=tx+tw-5)||(my>=ty+th-5))
	{
		Questions[QPos].MDown = 0;
		qMatch_StopConnecting();
	}
}

function qMatch_KE(obj)
{
	var aid = obj.id.split("_");
	aid[1] = eval(aid[1]);

	if (aid[0]=="qI")
	{
		var v = -1;
		if (obj.value!="") v = eval(obj.value)-1;
		
		if (Questions[QPos].Ans[aid[1]][aid[2]]>=0)
		{
			var id = "qM_"+(aid[1]+1)+'_'+Questions[QPos].Ans[aid[1]][aid[2]];
			document.getElementById(id).style.backgroundColor = "white";
		}

		if ((v<0)||(v>=Questions[QPos].Rows))
		{
			obj.value = "";
			Questions[QPos].Canvas[aid[1]][aid[2]].clear();
			var id = "qM_"+(aid[1])+'_'+aid[2];
			document.getElementById(id).style.backgroundColor = "white";
			Questions[QPos].Ans[aid[1]][aid[2]] = -1;
			return;
		}

		var lobj = obj;
		obj = document.getElementById("qM_"+(eval(aid[1])+1)+"_"+(v));
		
		// check if another (correct) choice with the same text exists
		var Cor = Questions[QPos].Cor[aid[1]][aid[2]];
		var ca = 0;
		var cai = -1;
		for (var i=0;i<Cor.length;i++)
		{
			if (Cor[i]==v) { ca = 1; break; }
			if (Questions[QPos].Col[aid[1]+1][Cor[i]]==Questions[QPos].Col[aid[1]+1][v])
			{ cai = Cor[i]; break; }
		}
		if ((!ca)&&(cai>=0))
		{
			v = cai;
			lobj.value = v+1;
			obj = document.getElementById("qM_"+(eval(aid[1])+1)+"_"+(v));
		}
		
		obj.style.backgroundColor = "#FAEBD7";
		
		var ob1 = document.getElementById("qM_"+aid[1]+"_"+aid[2]);
		ob1.style.backgroundColor = "#E0FFFF";
		obj.style.backgroundColor = "#E0FFFF";
		
		var ob1 = document.getElementById("qI_"+aid[1]+"_"+aid[2]);
		var Canvas = Questions[QPos].Canvas[aid[1]][aid[2]];

		Canvas.clear();
		Canvas.setStroke(2);
		Canvas.drawLine(getX(ob1)+ob1.offsetWidth, getY(ob1)+(ob1.offsetHeight)/2, getX(obj)-2, getY(obj)+(obj.offsetHeight)/2);
		Canvas.paint();

		Questions[QPos].Ans[aid[1]][aid[2]] = -1;
		for (var i=0;i<Questions[QPos].Rows;i++)
		{
			if (Questions[QPos].Ans[aid[1]][i] == v)
			{
				Questions[QPos].Canvas[aid[1]][i].clear();
				var id = "qM_"+(aid[1])+'_'+i;
				document.getElementById(id).style.backgroundColor = "white";
				var id = "qI_"+(aid[1])+'_'+i;
				document.getElementById(id).value = "";
				Questions[QPos].Ans[aid[1]][i] = -1;
			}
		}
		Questions[QPos].Ans[aid[1]][aid[2]] = eval(v);
		
		Questions[QPos].Connecting = 0;
		return;
	}
}

function qMatch_KP(event,obj)
{
	event.cancelBubble = true;
	if (event.keyCode==13) { qMatch_KE(obj); return false; }
	if (event.keyCode<48) return false;
	if (event.keyCode>57) return false;
	return true;
}

var oc = [249,252,255];
function randomColor(prevc)
{
	var c;
	var bg = [249,252,255];
	var c1,c2,c3;
	var d;
	do {
		c = rand(16777216);
		c1 = (0xff0000 & c) >> 16;
		c2 = (0x00ff00 & c) >> 8;
		c3 = (0x0000ff & c);
		d = Math.pow(Math.abs(bg[0]-c1),2)+Math.pow(Math.abs(bg[1]-c2),2)+Math.pow(Math.abs(bg[2]-c3),2);
		var d2 = Math.pow(Math.abs(oc[0]-c1),2)+Math.pow(Math.abs(oc[1]-c2),2)+Math.pow(Math.abs(oc[2]-c3),2);
		if (d2<d) d = d2;
	} while (d<20000);
	oc = [c1,c2,c3];
	return "#"+toHex(c);
}

function Q_Match(question,columns,numbers)
{
	this.Q = question;
	this.Col = new Array();
	this.Cor = new Array();
	this.Ans = new Array();
	this.Canvas = new Array();
	this.Rows = 0;
	this.Cols = 0;
	this.MDown = 0;
	this.Connecting = 0;
	this.ConnCol = 0;
	this.ConnRow = 0;
	this.ConnSel = 0;
	
	var t1s = columns.split("&");
	var k=0;
	for (var i=0;i<t1s.length;i+=2)
	{
		var t2s = t1s[i].split("~");
		this.Col[k] = new Array;
		for (var j=0;j<t2s.length;j++)
		{
			this.Col[k][j] = t2s[j];
			if (this.Rows<j+1) this.Rows = j+1;
		}
		this.Cols++; k++;
	}
	
	k=0;
	for (var i=1;i<t1s.length;i+=2)
	{
		var t2s = t1s[i].split("~");
		this.Cor[k] = new Array;
		this.Ans[k] = new Array;
		for (var j=0;j<t2s.length;j++)
		{
			this.Cor[k][j] = new Array();
			var t3s = t2s[j].split("|");
			for (var l=0;l<t3s.length;l++)
			{
				this.Cor[k][j][l] = eval(t3s[l])-1;
			}			
			this.Ans[k][j] = -1;
		}
		k++;
	}
	
	this.Show = function ()
		{
			var text = "";
			
			text += this.Q+'<br>';

			text += "<form name='Q_AnsForm' onSubmit='return false;' style='margin:0px 0px 0px 0px;'>";
			text += '<table id="qMTable" onmouseout="qMatch_MTOut((window.event?window.event:event),this);" onmouseup="qMatch_MUp(this);" cellspacing="5" cellpadding="2">';
			
			for (var i=0;i<this.Rows;i++)
			{
				text += '<tr valign="top">';
				if (numbers) text += '<td class="qMatch" width="1px">'+(i+1)+'</td>';
				for (var j=0;j<this.Cols;j++)
				{
					var m = (j+1<this.Cols);
					text += '<td id="qM_'+(j)+'_'+(i)+'" class="qMatch" onmouseup="qMatch_MUp(this);" onmousemove="qMatch_MMove(this);" onmouseout="qMatch_MOut(this);" onmouseover="qMatch_MOver(this);" onmousedown="qMatch_MDown(this); return false;" onselectstart="return false;">'+this.Col[j][i];
					text += '</td>';
					if (m) text += '<td width="1px"><input onkeypress="return qMatch_KP((window.event?window.event:event),this);" onblur="qMatch_KE(this);" type=text size=2 id="qI_'+(j)+'_'+(i)+'" name="qN_'+(j)+'_'+(i)+'"></td><td width="80px">&nbsp;</td>'
				}
				text += '</tr>';
			}
			
			text += '</table></form>';
			
			for (var i=0;i<this.Rows;i++)
			{
				for (var j=0;j<this.Cols-1;j++)
				{
					text += '<div id="qMC_'+(j)+'_'+(i)+'" style="position:absolute; top:0px; left:0px;"></div>'
				}
			}
			
			return text;
		}
	this.ShowPost = function ()
		{
			for (var j=0;j<this.Cols-1;j++)
			{
				this.Canvas[j] = new Array();
				for (var i=0;i<this.Rows;i++)
				{
					this.Canvas[j][i] = new jsGraphics("qMC_"+(j)+'_'+(i));
					this.Canvas[j][i].setColor(randomColor());
				}
			}
		}
	this.Mistakes = function ()
		{
			var text = "";
			
			text += this.Q+"<br><br>";

			text += '<table cellspacing="1" cellpadding="2"><tr valign="top">';
			
			for (var j=0;j<this.Cols-1;j++)
			{
				text+='<td>';
				for (var i=0;i<this.Rows;i++)
				{
					var m = 1;
					for (var k=0;k<this.Cor[j][i].length;k++)
					{
						if (this.Ans[j][i]==this.Cor[j][i][k]) { m=0; break; }
					}

					if (m)
					{
						text += this.Col[j][i];
						text += ' <span class="aIncorrect">[ ';
						if (this.Ans[j][i]>=0) text += this.Col[j+1][this.Ans[j][i]];
						text += ' ]</span> <span class="aCorrect">( ';
						text += this.Col[j+1][this.Cor[j][i][0]];
						text += ' )</span><br>';
					}
				}
				text+='</td>'
			}
			
			text += '</tr></table>';
			
			return text;
		}
	this.Answered = function ()
		{
			return 1;
		}
	this.Mark = function ()
		{
			var S=0;
			var N=0;
			for (var j=0;j<this.Cols-1;j++)
			{
				for (var i=0;i<this.Rows;i++)
				{
					N++;
					for (var k=0;k<this.Cor[j][i].length;k++)
					{
						if (this.Ans[j][i]==this.Cor[j][i][k]) { S++; break; }
					}
				}
			}
			return S/N;
		}
	this.Reset = function ()
		{
			for (var j=0;j<this.Cols-1;j++)
			{
				for (var i=0;i<this.Rows;i++)
				{
					this.Ans[j][i]=-1;
				}
			}
		}
	
}

function ShowQuestion()
{
	if (QPos>=0) 
	{
		document.getElementById("QuestionBody").innerHTML = "Question "+(QPos+1)+": "+Questions[QPos].Show();
		if (Questions[QPos].ShowPost) Questions[QPos].ShowPost();
	}

	var text = "";

	text += '<table width="1px" cellspacing="0" cellpadding="0"><tr>';
	for (var i = 0; i < QCount; i++)
	{
		text += '<td><table width="1px" cellspacing="0" cellpadding="3" border="1px"><tr>';
		text += '<td bgcolor="'+(QStatus[i]==2?(Questions[i].Mark()==1?"lightgreen":(Questions[i].Mark()>0?"#FFFF99":"#ffb080")):i==QPos?"lightgrey":"")+'">';
		text += (i==QPos?"<b>":"") + (i+1) + (i==QPos?"</b>":"") +'</td></tr></table></td>';
	}
	text += '</tr></table>';
	
	document.getElementById("QuestionStatus").innerHTML = text;
	
	cTranslator.Hook();
}

function PrintResults()
{
	var text = "";
	
	var S = 0;
	for (var i = 0; i < QCount; i++) S += Questions[i].Mark();
	var SP = Math.floor(S/QCount*100);
	
	text += "Score: " + SP + " % ( " + Math.floor(S*100)/100 + " / " + QCount + " ) <br>";
	
	document.getElementById("QuestionBody").innerHTML = text;
	
	NavButtonEnable("Restart");
	if (SP<100) NavButtonEnable("Review_Mistakes");
}

function NavRestart()
{
	for (var i = 0; i < QCount; i++) 
	{
		QStatus[i] = 0;
		Questions[i].Reset();
	}
	
	NavButtonEnable("Next");
	NavButtonEnable("Skip");
	NavButtonEnable("Back");
	NavButtonDisable("Restart");
	NavButtonDisable("Review_Mistakes");
	document.getElementById("ReviewParent").style.display = 'none';
	
	QPos = 0;
	QAns = 0;
	ShowQuestion();
}

function NavReview_Mistakes()
{
	document.getElementById("ReviewParent").style.display = '';

	var text = "<br>";
	
	var nf = 0;
	for (var i = 0; i < QCount; i++) 
	{
		if (Questions[i].Mark()<1) 
		{
			if (nf==1) text += "<hr>";
			text += "Question "+(i+1)+": " + Questions[i].Mistakes();
			nf = 1;
		}
	}
	text += "<br>";
	
	document.getElementById("ReviewBody").innerHTML = text;
}

function NavNext()
{
	if (!Questions[QPos].Answered())
	{
		//return;
	}

	QStatus[QPos] = 2;

	QAns++;
	
	if (QAns>=QCount) 
	{
		// Test Finished
		
		QPos=-1;
		ShowQuestion();	

		NavButtonDisable("Next");
		
		PrintResults();
		
		return;
	}
	
	if (QAns==QCount-1)
	{
		// Disable Skip/Back
		
		NavButtonDisable("Skip");
		NavButtonDisable("Back");
	}

	QPos++;
	if (QPos>=QCount) QPos=0;

	while (QStatus[QPos]>1)
	{
		QPos++; 
		if (QPos>=QCount) QPos=0;
	}

	ShowQuestion();	
}

function NavSkip()
{
	QStatus[QPos] = 1;

	QPos++;
	if (QPos>=QCount) QPos=0;

	while (QStatus[QPos]>1)
	{
		QPos++; 
		if (QPos>=QCount) QPos=0;
	}

	ShowQuestion();	
}

function NavBack()
{
	QStatus[QPos] = 1;

	QPos--;
	if (QPos<0) QPos=QCount-1;

	while (QStatus[QPos]>1)
	{
		QPos--; 
		if (QPos<0) QPos=QCount-1;
	}

	ShowQuestion();	
}

function NavButtonDisable(button)
{
	document.getElementById("NavBtn"+button).innerHTML = "";
	document.getElementById("NavBtn"+button).style.cursor = "";
}

function NavButtonEnable(button)
{
	document.getElementById("NavBtn"+button).innerHTML = button.split("_").join("&nbsp;");
	document.getElementById("NavBtn"+button).style.cursor = "hand";
}

function NavButton(button)
{
	return '<td id="NavBtn'+button+'" align="center" style="cursor:pointer" \
		onMouseOver="if (this.innerHTML.length>0) this.bgColor=\'#dddddd\'" \
		onMouseOut="this.bgColor=\'\'" \
		onselectstart="return false;" \
		onmousedown="return false;" \
		onClick="if (this.innerHTML.length>0) Nav'+button+'(); return false;" \
		>'+button.split("_").join("&nbsp;")+'</td>'
}

function StartTest(testTitle)
{
	var test = "";
	
	test += '<table width="670px" align="left" style="position:absolute;top:10px;left:0px;" bgcolor="#f9fcff" border="3" cellspacing="0" cellpadding="0">';
	
	// info panel
	test += '<tr height="1px"><td>';
		
		test += '<table width="100%" cellspacing="0" cellpadding="10">';
		test += '<tr><td>' + testTitle + '</td></tr><tr><td>';
		test += '<table width="1px" cellspacing="5" cellpadding="0"><tr><td>'
		test += 'Questions:&nbsp;</td><td id="QuestionStatus">';

		test += '</td>'+ "" +'</tr></table></td><td width="1px" align="right"><b>Cyrillization</b>:</td><td width="1px" align="right"><div class="langLink" title="Êëàâèàòóðà"></div></td></tr>'; // "" = NavButton("&nbsp;Give&nbsp;Up&nbsp;") 
		test += '</table>';

	test += '</td></tr>';
	
	// question panel
	
	test += '<tr valign="top" height="257px"><td><div id="CanvasQ" style="position:relative"><table cellspacing="0" cellpadding="5"><tr><td id="QuestionBody">';
	
		test += Questions[0].Show(1);
	
	test += '</td></tr></table></div></td></tr>';
	
	// navigation panel

	test += '<tr height="1px"><td><table width="1px" cellspacing="2" cellpadding="5"><tr>';
	
	test += NavButton("Next") + NavButton("Skip") + NavButton("Back") + NavButton("Restart") + NavButton("Review_Mistakes");
	
	test += '</tr></table></td></tr><tr height="1px" id="ReviewParent" style="display: none;"><td id="ReviewBody"></td></tr>';
	

	test += '</table>';
	
	document.body.innerHTML = test;
	
	NavButtonDisable("Restart");
	NavButtonDisable("Review_Mistakes");
	
	cTranslator.Start();

	ShowQuestion();

	//document.write("<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>");

	//for (i=0;i<100;i++)
	//{
		//var obj = document.getElementById("ID_"+i); if (!obj) continue;
		//document.write(obj.offsetHeight+"<BR>");
		
		//Canvas.drawLine(getX(obj), getY(obj)+(obj.offsetHeight)/2, 200, 150);
	//}
	//Canvas.paint();

	
	
}

document.write('<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">');
document.write('<link href="cyr.css" rel="stylesheet" type="text/css">');

document.write('<script type="text/javascript" src="mobrowser.js"></script>');
document.write('<script type="text/javascript" src="motranslator.js"></script>');
document.write('<script type="text/javascript" src="wz_jsgraphics.js"></script>');

