// Copyright INFORMATIC Ltd. (www.orfo.ru), Drozdov S.
var img1;
var img2;
var img3;
var img4;
var spc = 0;
var strInit1 = "<table cellpadding=0 cellspacing=0 style='border-collapse: collapse' filter: alpha(opacity=85); opacity: 0.85; onmouseover='show_opac(pop, 0)' onmouseout='show_opac(pop, 1)'><tr><td style='border-left: 1px solid #1B434A; border-top: 1px solid #1B434A; padding-left:2px; padding-right:2px' bgcolor=#A5BFDA></td><td style='border-right: 1px solid #1B434A; border-top: 1px solid #1B434A' bgcolor=#A5BFDA><img style='float: right' src=../Images/cls.gif onmouseover='Kill()'></td></tr><tr><td  colspan=2 style='white-space:nowrap; background-color: #FFFFE2; border-left: 1px solid #1B434A; border-right: 1px solid #1B434A; border-bottom: 1px solid #1B434A; padding: 6px'>";
var strInit2 = "<span style='cursor: pointer' onclick=\"CheckSpell(";
var strInit3 = "<span style='cursor: pointer' onclick=\"Insert(";
var strInit4 = ")\" onmouseover=\"style.color='#0000FF';style.background='#CCD8D6'\" onmouseout=\"style.color='#000000';style.background='#FFFFE2'\">";
var strInit5 = "</td></tr></table>";

if((typeof HTMLElement != 'undefined') && (HTMLElement.prototype.__defineGetter__ != 'undefined'))
{
	HTMLElement.prototype.__defineGetter__("innerText", function ()
	{
		var r = this.ownerDocument.createRange();
		r.selectNodeContents(this);
		return r.toString();
	}); 
}
//_________________________________________________________
function DisplayAll (subName, show1, hide1, n, im)
{
SelImages(im);
if (eval(show1).style.display == "none")
{
	eval(show1).style.display = "";
	eval(hide1).style.display = "none";
	for (i = 1; i <= n; i++)
	{	
		eval(subName + i).style.display = "none";
		eval("img" + subName + i).src = img1;  
	}
}
else
{
	eval(show1).style.display = "none";
	eval(hide1).style.display = "";
	for (i = 1; i <= n; i++)
	{
		eval(subName + i).style.display = "";
		eval("img" + subName + i).src = img2;
	}
}
}
//_________________________________________________________
function subDisplay (subName, i)
{
SelImages(i);
if (eval(subName).style.display == "none")
  {
	eval(subName).style.display = "";
	eval("img"+subName).src = img2;
  }
else  {
	eval(subName).style.display = "none";
	eval("img"+subName).src = img1;  
      }
}
//_________________________________________________________
function MouseOver (subName, i)
{
SelImages(i);
if (eval(subName).style.display == "none")
	eval("img"+subName).src = img3;
else
	eval("img"+subName).src = img4;
}
//_________________________________________________________
function MouseOut (subName, i)
{
SelImages(i);
if (eval(subName).style.display == "none")
	eval("img"+subName).src = img1;
else
	eval("img"+subName).src = img2;
}
//_________________________________________________________
function ShowSection (sec)
{
	sec.style.display = "";
}
//_________________________________________________________
function HideSection (sec)
{
	sec.style.display = "none";
}
//_________________________________________________________
function SelImages (i)
{	// choose images
switch (i)
{
	case 1:
		img1="../Images/Bplus.gif";
		img2="../Images/Bminus.gif";
		img3="../Images/Bplus3d.gif";
		img4="../Images/Bminus3d.gif";
		break;
	case 2:
		img1="../Images/FldCls.gif";
		img2="../Images/FldOpn.gif";
		img3="../Images/FldClsR.gif";
		img4="../Images/FldOpnR.gif";
		break;
	default:
		break;
}
}

// *************************************************************** //
// ********************* FUNCTIONS FOR TESTS ********************* //
// *************************************************************** //

function pt(ev2, txt, vnt, qnt, snt)
// popup for tests
{
	spc = 0;
	PopupSyntSent(txt, vnt, qnt, snt);
	show_bar(ev2);
}
//_________________________________________________________
function pp(ev2, txt, vnt, qnt, snt)
// popup for tests
{
	spc = 0;
	PopupSyntPunct(txt, vnt, qnt, snt);
	show_bar(ev2);
}
//_________________________________________________________
function ps(ev2, txt, vnt, qnt, snt)
// popup for tests
{
	spc = 1;
	PopupSyntPunct(txt, vnt, qnt, snt);
	show_bar(ev2);
}
//_________________________________________________________
function Init()
{
	for (i = 0; i <= max; i++)
	{
		test[i] = "00";
		SrcText[i] = "";
	}
}
//_________________________________________________________
function Insert(ctr, txt, snt, spc)
// for tests with sentences
{
var tmp = '';
tmp = txt;
if (spc == 1)
	tmp = tmp + " ";
ctr.innerHTML = tmp;

sum = 0;
for (i = 0; i <= max; i++)
	if (test[i] == "10")
		sum++;
document.all('res').innerHTML = '&#1048;&#1089;&#1087;&#1088;&#1072;&#1074;&#1083;&#1077;&#1085;&#1086; &#1087;&#1088;&#1077;&#1076;&#1083;&#1086;&#1078;&#1077;&#1085;&#1080;&#1081;: '+sum;
document.all('rht').innerHTML = '';
document.all('wrg').innerHTML = '&nbsp;';
eval("sent" + snt).style.color = '#7900BB';
eval("sent" + snt).style.textDecoration = 'none';
Kill();
}
//_________________________________________________________
function CheckSpell(ctr, txt, ver)
// for tests with words
{
n = ctr.id.substr(3);
n = n - 0;
ctr.innerHTML = txt;
ctr.style.color = '#7900BB';
ctr.style.backgroundImage = 'none';
if (ver == 1)
{
	test[n] = "11";
}
else if (ver == 0)
{
	test[n] = "10";
}
sum = 0;
for (i = 0; i <= max; i++)
	if (test[i] == "10" || test[i] == "11")
		sum++;
document.all('res').innerHTML = '&#1055;&#1086;&#1083;&#1091;&#1095;&#1077;&#1085;&#1086; &#1086;&#1090;&#1074;&#1077;&#1090;&#1086;&#1074;: ' + sum; 
document.all('rht').innerHTML = '';
document.all('wrg').innerHTML = '&nbsp;';
Kill();
}
//_________________________________________________________
function Result()
// for tests with words
{
sum = 0;
rt = 0;
wr = 0;
for (i = 0; i <= max; i++)
{
	if (test[i] == "10" || test[i] == "11")
		sum++;
	if (test[i] == "11")
	{
		rt++;
		eval("ctr"+i).style.color = '#006600';
		eval("ctr"+i).style.backgroundImage = 'none';
	}
	if (test[i] == "10")
	{
		wr++;
		eval("ctr" + i).style.color = '#800000';
		eval("ctr" + i).style.backgroundImage = "url('../Images/Un.gif')";
		eval("ctr" + i).style.backgroundPosition = 'bottom';
		eval("ctr" + i).style.backgroundRepeat = 'repeat-x';
	}
}
if (sum != 0)
{
	proc = Math.ceil(rt / sum * 100);
	if (sum == max + 1)
	{
		if (proc >= 96 && proc <= 100)
			document.all('mark').innerHTML = "<img src='../Images/m5.gif'>";
		else if (proc >= 91 && proc <= 95)
			document.all('mark').innerHTML = "<img src='../Images/m5-.gif'>";
		else if (proc >= 88 && proc <= 90)
			document.all('mark').innerHTML = "<img src='../Images/m4+.gif'>";
		else if (proc >= 78 && proc <= 87)
			document.all('mark').innerHTML = "<img src='../Images/m4.gif'>";
		else if (proc >= 75 && proc <= 77)
			document.all('mark').innerHTML = "<img src='../Images/m4-.gif'>";
		else if (proc >= 50 && proc <= 74)
			document.all('mark').innerHTML = "<img src='../Images/m3.gif'>";
		else if (proc == 48 || proc == 49)
			document.all('mark').innerHTML = "<img src='../Images/m3-.gif'>";
		else if (proc == 47)
			document.all('mark').innerHTML = "<img src='../Images/m3=.gif'>";
		else if (proc <= 46 && proc != 0)
			document.all('mark').innerHTML = "<img src='../Images/m2.gif'>";
		else if (proc == 0)
			document.all('mark').innerHTML = "<img src='../Images/m1.gif'>";
		document.all('rht').innerHTML = '&#1055;&#1088;&#1072;&#1074;&#1080;&#1083;&#1100;&#1085;&#1099;&#1093; &#1086;&#1090;&#1074;&#1077;&#1090;&#1086;&#1074;: ' + rt;
	}
	else
		document.all('rht').innerHTML = '&#1055;&#1088;&#1072;&#1074;&#1080;&#1083;&#1100;&#1085;&#1099;&#1093; &#1086;&#1090;&#1074;&#1077;&#1090;&#1086;&#1074;: ' + rt + ' (' + proc + '%)';
	
	document.all('rht').style.color = '#006600';
	document.all('res').innerHTML = '';
	document.all('wrg').innerHTML = '&#1053;&#1077;&#1087;&#1088;&#1072;&#1074;&#1080;&#1083;&#1100;&#1085;&#1099;&#1093; &#1086;&#1090;&#1074;&#1077;&#1090;&#1086;&#1074;: ' + wr;
	document.all('wrg').style.color='#800000';
}
}
//_________________________________________________________
function PopupSyntDash(ctr, vnt, qnt, ver, len)
// ctr - имя <span>
// vnt - список вариантов
// qnt - кол-во вариантов
// ver - № правильного варианта
// len - длина заменяемой последовательности
{
var ch;
var tmp = '';

tmp = tmp + ctr.innerHTML;
wdh = tmp.length * 9.1 - 10;
str = strInit1;

n = ctr.id.substr(3);
n = n - 0;
if (test[n] == "00" || test[n] == "01")
	SrcText[n] = tmp;
PopupText = "<span class='nw'>";
ch = vnt.split(",");
for (i = 0; i <= qnt; i++)
{
	newtxt = ch[i] + SrcText[n].substr(len);
	PopupText = PopupText + strInit2 + ctr.id;
	PopupText = PopupText + ", '" + newtxt + "', ";
	if (i == ver)
		PopupText = PopupText + "1";
	else
		PopupText = PopupText + "0";
	PopupText = PopupText + strInit4;
	PopupText = PopupText + newtxt + "</span>";
	if (i < qnt)
		PopupText = PopupText + "<br>";
}
PopupText = PopupText + "</span>";
str = str + PopupText;
str = str + strInit5;
document.all["pop"].innerHTML = str;
}
//_________________________________________________________
function PopupSyntCap(ctr, vnt, qnt, ver, pos)
// ctr - имя <span>
// vnt - список вариантов
// qnt - кол-во вариантов
// ver - № правильного варианта
// pos - позиция вставки
{
var ch;
var tmp = '';

tmp = tmp + ctr.innerHTML;
wdh = tmp.length * 9.1 - 10;
str = strInit1;

n = ctr.id.substr(3);
n = n - 0;
if (test[n] == "00" || test[n] == "01")
	SrcText[n] = tmp;
srh =/_/;
srh = SrcText[n].substr(pos, 3);
PopupText = "<span class='nw'>";
ch = vnt.split(",");
for (i = 0; i <= qnt; i++)
{
	rpt = "<b>"+ ch[i] +"</b>";
	newtxt = SrcText[n].replace(srh, rpt);
	PopupText = PopupText + strInit2 + ctr.id;
	PopupText = PopupText + ", '" + newtxt + "', ";
	if (i == ver)
		PopupText = PopupText + "1";
	else
		PopupText = PopupText + "0";
	PopupText = PopupText + strInit4;
	PopupText = PopupText + newtxt + "</span>";
	if (i < qnt)
		PopupText = PopupText + "<br>";
}
PopupText = PopupText + "</span>";
str = str + PopupText;
str = str + strInit5;
document.all["pop"].innerHTML = str;
}
//_________________________________________________________
function PopupSyntWord(ctr, vnt, qnt, ver, pos, len)
// ctr - имя <span>
// vnt - список вариантов
// qnt - кол-во вариантов
// ver - № правильного варианта
// pos - позиция вставки
// len - только для слов с 2-мя пропусками - длина заменяемой последовательности
{
var ch;
var tmp = '';

tmp = tmp + ctr.innerHTML;
wdh = tmp.length * 9.1 - 10;
str = strInit1;

n = ctr.id.substr(3);
n = n - 0;
if (test[n] == "00" || test[n] == "01")
	SrcText[n] = tmp;
if (len == null)
	len = 1;
srh =/_/;
if (len > 1)
{
	srh = SrcText[n].substr(pos,len);
}
PopupText = "<span class='nw'>";
ch = vnt.split(",");
for (i = 0; i <= qnt; i++)
{
	rpt = "<b>"+ ch[i] +"</b>";
	newtxt = SrcText[n].replace(srh, rpt);
	PopupText = PopupText + strInit2 + ctr.id;
	PopupText = PopupText + ", '" + newtxt + "', ";
	if (i == ver)
		PopupText = PopupText + "1";
	else
		PopupText = PopupText + "0";
	PopupText = PopupText + strInit4;
	PopupText = PopupText + newtxt + "</span>";
	if (i < qnt)
		PopupText = PopupText + "<br>";
}
PopupText = PopupText + "</span>";
str = str + PopupText;
str = str + strInit5;
document.all["pop"].innerHTML = str;
}
//_________________________________________________________
function PopupSyntSent(ctr, vnt, qnt, snt)
// for tests with sentences

// ctr - id <span>
// vnt - список вариантов
// qnt - кол-во вариантов
// snt - id предложения
{
var ch;
var tmp = '';
var len1 = 0;
var len2 = 0;

tmp = tmp + ctr.innerHTML;
len1 = tmp.length;
str = strInit1;

n = ctr.id.substr(3);
n = n - 0;
PopupText = "<span class='t4n'>";
ch = vnt.split("|");
for (i = 0; i <= qnt; i++)
{
	newtxt = ch[i];
	len2 = ch[i].length;
	if (len2 > len1)
		len1 = len2;
	PopupText = PopupText + strInit3 + ctr.id;
	PopupText = PopupText + ", '" + newtxt + "', ";
	PopupText = PopupText + snt + ", ";
	PopupText = PopupText + spc;
	PopupText = PopupText + strInit4;
	PopupText = PopupText + newtxt + "</span>";
	if (i < qnt)
		PopupText = PopupText + "<br>";
}
PopupText = PopupText + "</span>";
str = str + PopupText;
str = str + strInit5;
document.all["pop"].innerHTML = str;
wdh = len1 * 9.1 - 10;
test[snt] = "10";
}
//_________________________________________________________
function PopupSyntPunct(ctr, vnt, qnt, snt)
// for tests with sentences

// ctr - id <span>
// vnt - список вариантов
// qnt - кол-во вариантов
// snt - id предложения
{
var ch;
var tmp = '';
var len1 = 0;
var len2 = 0;

tmp = tmp + ctr.innerHTML;
str = strInit1;

n = ctr.id.substr(3);
n = n - 0;
PopupText = "<span class='t4n'>";
ch = vnt.split("|");
for (i = 0; i <= qnt; i++)
{
	newtxt = ch[i];
	coretxt = newtxt.substr(1);
	if (coretxt == " " || coretxt == "")
		{
		coretxt = "<span onmouseover=\"style.background='#CCD8D6'\" onmouseout=\"style.background='#FFFFE2'\"><span class=tbi4r>&lt;</span><span class=ti4>&#1087;&#1091;&#1089;&#1090;&#1086;</span><span class=tbi4r>&gt;</span></span>";
		len2 = 7;
		}
	else
		{
		coretxt = "<span onmouseover=\"style.background='#CCD8D6'\" onmouseout=\"style.background='#FFFFE2'\"><span class=tbi4r>&lt; </span><span class=tb4>" + coretxt + "</span><span class=tbi4r> &gt;</span></span>";
		len2 = newtxt.length;
		}
	if (len2 > len1)
		len1 = len2;
	PopupText = PopupText + strInit3 + ctr.id;
	PopupText = PopupText + ", '" + newtxt + "', ";
	PopupText = PopupText + snt + ", ";
	PopupText = PopupText + spc;
	PopupText = PopupText + strInit4;
	PopupText = PopupText + coretxt + "</span>";
	if (i < qnt)
		PopupText = PopupText + "<br>";
}
PopupText = PopupText + "</span>";
str = str + PopupText;
str = str + strInit5;
document.all["pop"].innerHTML = str;
wdh = len1 * 9.1 - 10;
test[snt] = "10";
}
//_________________________________________________________
function ResultS()
// for tests with sentences
{
if (sum > 0)
{
	var rgt = [];
	rt = 0;
	wr = 0;
	for (i = 0; i <= max; i++)
		if (nVaria[i] > 1)
		{
			flg = 0;
			// 0 - unchecked
			// 1 - false
			// 2 - true
			tst = eval("sent" + i).innerText;
			for (j = 1; j <= nVaria[i]; j++)
			{
				rgt[j] = eval("corr" + i + "_" + j).innerText;
				if (test[i] == "10")
				{
					flg = 1;
					if (tst == rgt[j] && test[i] == "10")
					{
						flg = 2;
						break;
					}
				}
			}
			if (flg == 2)
			{
				rt++;
				eval("sent" + i).style.color = '#006600';
				eval("sent" + i).style.textDecoration = 'none';
			}
			if (flg == 1)
			{
				wr++;
				eval("sent" + i).style.color = '#800000';
				eval("sent" + i).style.textDecoration = 'underline';
			}
		}
		else
		{
			tst = eval("sent" + i).innerText;
			rgt[1] = eval("corr" + i).innerText;
			if (tst == rgt[1] && test[i] == "10")
			{
				rt++;
				eval("sent" + i).style.color = '#006600';
				eval("sent" + i).style.textDecoration = 'none';
			}
			if (tst != rgt[1] && test[i] == "10")
			{
				wr++;
				eval("sent" + i).style.color = '#800000';
				eval("sent" + i).style.textDecoration = 'underline';
			}
		}
	proc = Math.ceil(rt / (rt + wr) * 100);
	if (sum == max + 1)
	{
		if (proc >= 96 && proc <= 100)
			document.all('mark').innerHTML = "<img src='../Images/m5.gif'>";
		else if (proc >= 91 && proc <= 95)
			document.all('mark').innerHTML = "<img src='../Images/m5-.gif'>";
		else if (proc >= 88 && proc <= 90)
			document.all('mark').innerHTML = "<img src='../Images/m4+.gif'>";
		else if (proc >= 78 && proc <= 87)
			document.all('mark').innerHTML = "<img src='../Images/m4.gif'>";
		else if (proc >= 75 && proc <= 77)
			document.all('mark').innerHTML = "<img src='../Images/m4-.gif'>";
		else if (proc >= 50 && proc <= 74)
			document.all('mark').innerHTML = "<img src='../Images/m3.gif'>";
		else if (proc == 48 || proc == 49)
			document.all('mark').innerHTML = "<img src='../Images/m3-.gif'>";
		else if (proc == 47)
			document.all('mark').innerHTML = "<img src='../Images/m3=.gif'>";
		else if (proc <= 46 && proc != 0)
			document.all('mark').innerHTML = "<img src='../Images/m2.gif'>";
		else if (proc == 0)
			document.all('mark').innerHTML = "<img src='../Images/m1.gif'>";
		document.all('rht').innerHTML = '&#1055;&#1088;&#1072;&#1074;&#1080;&#1083;&#1100;&#1085;&#1099;&#1093; &#1087;&#1088;&#1077;&#1076;&#1083;&#1086;&#1078;&#1077;&#1085;&#1080;&#1081;: '+rt;
	}
	else
		document.all('rht').innerHTML = '&#1055;&#1088;&#1072;&#1074;&#1080;&#1083;&#1100;&#1085;&#1099;&#1093; &#1087;&#1088;&#1077;&#1076;&#1083;&#1086;&#1078;&#1077;&#1085;&#1080;&#1081;: '+rt+' ('+proc+'%)';
	
	document.all('rht').style.color='#006600';
	document.all('res').innerHTML = '';
	document.all('wrg').innerHTML = '&#1055;&#1088;&#1077;&#1076;&#1083;&#1086;&#1078;&#1077;&#1085;&#1080;&#1081; &#1089; &#1086;&#1096;&#1080;&#1073;&#1082;&#1072;&#1084;&#1080;: '+wr;
	document.all('wrg').style.color='#800000';
}
}
//_________________________________________________________
function ToggleSent(sent, n)
// for tests with sentences
{
for (i=1; i<=n; i++)
{
	if (VisText[sent] == i)
	{
		VisText[sent] = i+1;
		if (VisText[sent] > n)
			VisText[sent] = 1;
		for (j=1; j<=n; j++)
		{
			if (j == VisText[sent])
				eval("corr"+sent+"_"+j).style.display = "";
			else
				eval("corr"+sent+"_"+j).style.display = "none";
		}
		break;
	}
}
}
//_________________________________________________________
function Check()
{
document.all('Verify').style.display='none';
document.all('Correct').style.display='';
document.getElementById("pop").style.visibility="hidden";
document.all('res').innerHTML = '';
document.all('rht').innerHTML = '';
document.all('wrg').innerHTML = '';
document.all('mark').innerHTML = '';
Init();
}
