doRandomize = 0;
showQuestions = 10;

form_fio = "";
form_pol = "";
form_vozrast = "";

start_time = 0;

qAnswers = new Array();
qOrder = new Array();

function FillZero(num)
{
	if(num < 10)
		return "0" + num;

	return num;
}

function PostResults()
{
	var	date = new Date(),
		h = FillZero(date.getHours()),
		m = FillZero(date.getMinutes()),
		s = FillZero(date.getSeconds()),
		d = FillZero(date.getDate()),
		mn = FillZero(date.getMonth() + 1),
		y = date.getYear(),
		i,
		protocol,
		qAnswersAux = new Array();

	protocol = "<" + "?xml version=\"1.0\" encoding=\"windows-1251\"?>\n";
	protocol += "<ht>\n";
	protocol += "<obolochka>MAINTEST 3</obolochka>\n";
	protocol += "<Titleeng>" + ver + "</Titleeng>\n";
	protocol += "<versiya>vd</versiya>\n";
	protocol += "<tele>0</tele>\n";
	protocol += "<protokol>\n";
	protocol += "<psevdonim>\n";
	protocol += "<lname>" + form_fio + "</lname>\n";
	protocol += "<fname></fname>\n";
	protocol += "<mname></mname>\n";
	protocol += "<pol>" + form_pol + "</pol>\n";
	protocol += "<birth></birth>\n";
	protocol += "<vozrast>" + form_vozrast + "</vozrast>\n";
	protocol += "<date>" + d + "." + mn + "." + y + "</date>\n";
	protocol += "<time>" + h + ":" + m + ":" + s + "</time>\n";
	protocol += "<testtime>" + (date.getTime() - start_time.getTime()) + "</testtime>\n";
	protocol += "<pr1>" + ver + "</pr1>\n";
	protocol += "<pr2></pr2>\n";
	protocol += "</psevdonim>\n";
	protocol += "<ans>";

	for(i = 0; i < data.length; i++)
		qAnswersAux[qOrder[i]] = qAnswers[i];

	for(i = 0; i < data.length; i++)
		protocol += qAnswersAux[i] + " ";

	protocol += "</ans>\n";
	protocol += "<ans1>";

	for(i = 0; i < data.length; i++)
		protocol += qAnswersAux[i] + " ";

	protocol += "</ans1>\n";
	protocol += "<times>";

	for(i = 0; i < data.length; i++)
		protocol += "1 ";

	protocol += "</times>\n";
	protocol += "<view>";

	for(i = 0; i < data.length; i++)
		protocol += "1 ";

	protocol += "</view>\n";
	protocol += "<random>";

	for(i = 0; i < data.length; i++)
		protocol += (i + 1) + " ";

	protocol += "</random>\n";
	protocol += "<random_ans>";

	for(i = 0; i < data.length; i++)
		protocol += "%";

	protocol += "</random_ans>\n";
	protocol += "</protokol>\n";
	protocol += "</ht>\n";

	document.HTForm.results.value = protocol;
	document.HTForm.action = "http://www.ht.ru/profile/process.php";
	document.HTForm.submit();
}

function GetRegInfo()
{
	var	j,
		x;

	if(typeof(document.HTForm.form_fio) != "undefined")
		form_fio = document.HTForm.form_fio.value;

	if(typeof(document.HTForm.form_pol) != "undefined")
	{
		fp = document.HTForm.form_pol;
		x = '?';

		for(j = 0; j < fp.length; j++)
		{
			if(fp[j].checked)
			{
				x = fp[j].value;
				break;
			}
		}

		form_pol = x;
	}

	if(typeof(document.HTForm.form_vozrast) != "undefined")
		form_vozrast = document.HTForm.form_vozrast.value;

	start_time = new Date();
}

forceSubmit = 0;

function ShowQuestions(from)
{
	var	html = "",
		text,
		first,
		to,
		i,
		j,
		n,
		q,
		type,
		force,
		delimeters = new Array('<<', '<', '|', '>', '>>');

	if(from != 0)
	{
		if(showQuestions == 0)
			first = 0;
		else
			first = from - showQuestions;

		for(i = first; i < from; i++)
		{
			x = eval("document.HTForm.HTQuestion" + (i + 1));

			if(typeof(x) != "undefined")
			{
				n = 0;

				for(j = 0; j < x.length; j++)
				{
					if(x[j].checked)
						n = j + 1;
				}

				if(n != 0)
					qAnswers[i] = n;
			}
		}

		if(	from == data.length ||
			forceSubmit == 1)
		{
			PostResults();
			return;
		}
	}

	forceSubmit = 0;

	if(showQuestions == 0)
		to = data.length;
	else
	{
		to = from + showQuestions;

		if(to > data.length)
			to = data.length;
	}

	html += "<div class=\"HTHeader\">" + name + "</div><br>";
	html += "<form name=\"HTForm\" method=\"post\" action=\"javascript:ShowQuestions(" + to + ")\">";

	for(i = from; i < to; i++)
	{
		n = qOrder[i];

		q = data[n][0];
		type = data[n][data[n].length - 1];

		html += "<div class=\"HTQuestion\">" + (i + 1) + ") " + q + "</div>";

		if(q.charAt(0) == '@')
		{
			q = q.substr(1);

			text = "Передать результат";
			html += "<input name=\"results\" type=\"hidden\" value=\"\">";
			html += "<br><input class=\"HTButton\" type=\"submit\" onclick=\"javascript:forceSubmit=1;\" value=\"" + text + "\"><br>";
		}
		else if(type == "RADIO")
		{
			for(j = 1; j < data[n].length - 1; j++)
				html += "<div class=\"HTAnswer\"><input name=\"HTQuestion" + (i + 1) + "\" type=\"radio\" value=\"" + j + "\">&nbsp;" + data[n][j] + "</div>";
		}
		else if(type == "SELECT")
		{
			html += "<center>" +
			"<table width=\"80%\" border=\"0\" cellpadding=\"10\" cellspacing=\"0\">" +
			"<tr>" +
			"	<td width=\"50%\" align=\"right\" valign=\"top\"><div class=\"HTAnswer\">" + data[n][1] + "</div></td>" +
			"	<td width=\"50%\" align=\"left\" valign=\"top\"><div class=\"HTAnswer\">" + data[n][2] + "</div></td>" +
			"</tr>" +
			"<tr>" +
			"	<td colspan=\"2\" align=\"center\" valign=\"top\">" +
			"		<table border=\"0\" cellpadding=\"5\" cellspacing=\"0\">" +
			"		<tr>";

			for(j = 0; j < 5; j++)
				html += "<td align=\"center\" valign=\"middle\"><div class=\"HTAnswer\">" + delimeters[j] + "</div></td>";

			html +=
			"		</tr>"+
			"		<tr>";

			for(j = 1; j < 6; j++)
				html += "<td align=\"center\" valign=\"middle\"><input name=\"HTQuestion" + (i + 1) + "\" type=\"radio\" value=\"" + j + "\"></td>";
				
			html +=
			"		</tr>" +
			"		</table>" +
			"</td>" +
			"</tr>" +
			"</table>" +
			"</center>";
		}

		html += "<br>";
	}

	if(to == data.length)
	{
		text = "Передать результат";
		html += "<input name=\"results\" type=\"hidden\" value=\"\">";
	}
	else
		text = "Продолжить тестирование";

	html += "<center><input class=\"HTButton\" type=\"submit\" value=\"" + text + "\"></center>";
	html += "</form>";

	HTTest.innerHTML = html;
	document.body.scrollTop = 0;
}

function RegForm()
{
	var html;

	var link='<a href="#" onclick="ShowContestMessage(); return false;">конкурсе</a>';

	html = "<center>" +
	"<table width=\"70%\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\">" +
	"<tr>" +
	"	<td width=\"50%\" align=\"right\" valign=\"middle\"><span class=\"HTReg\">Имя:</span></td>" +
	"	<td width=\"50%\" align=\"left\" valign=\"top\"><input class=\"HTInput\" type=\"text\" name=\"form_fio\"></td>" +
	"</tr>" +
	"<tr>" +
	"<td align=\"right\" valign=\"middle\"><span class=\"HTReg\">Возраст:</span></td>" +
	"<td align=\"left\" valign=\"top\"><input class=\"HTInput\" type=\"text\" name=\"form_vozrast\"></td>" +
	"</tr>" +
	"<tr>" +
	"<td align=\"right\" valign=\"middle\"><span class=\"HTReg\">Пол:</span></td>" +
	"<td align=\"left\" valign=\"top\"><span class=\"HTReg\"><input class=\"HTInput\" type=\"radio\" name=\"form_pol\" value=\"m\"> мужской<br><input type=\"radio\" name=\"form_pol\" value=\"f\"> женский</span></td>" +
	"</tr>" +
	"</table><br>" +
	"</center>";

	return html;
}

function ShowTest()
{
	var	i,
		n,
		t;

	for(i = 0; i < data.length; i++)
	{
		qAnswers[i] = 0;
		qOrder[i] = i;
	}

	if(doRandomize)
	{
		for(i = 0; i < data.length; i++)
		{
			n = Math.round(Math.random() * (data.length - 1));

			t = qOrder[i];
			qOrder[i] = qOrder[n];
			qOrder[n] = t;
		}
	}

	document.write("<div id=\"HTTest\"></div>");

	ShowInstruction();
}

function StartTest()
{
	GetRegInfo();
	ShowQuestions(0);
}


function ShowContestMessage()
{
  var url="contest.htm";
  var win=OpenWindowModal("contest",600,600,url);
}


function OpenWindow(winname,width,height,url)
{
  var x=(screen.availWidth-width)/2; 
  var y=(screen.availHeight-height)/2;
  var mode = "width=" +width+ ",height=" +height+ ",left=" +x+ ",top=" +y+ ",directories=no,location=no,resizable=yes,scrollbars=yes,titlebar=yes,toolbar=no,status=yes,menubar=no";
  var win=window.open(url,winname,mode); 
  win.focus();
}

function OpenWindowModal(winname,width,height,url)
{
  var param=0;
  var x=(screen.availWidth-width)/2; 
  var y=(screen.availHeight-height)/2;
  var mode="dialogHeight:"+height+"px; dialogWidth:"+width+"px;help:no; resizable:yes; status:no;";
  window.showModalDialog(url,param,mode);
}

