var xmlHttp = false;
var page_element;
var rez_to_page;
var timer;

try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
    xmlHttp = false;
  }
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  xmlHttp = new XMLHttpRequest();
}

function startexam(p)
{	document.getElementById("main").innerHTML='<table width=100% height=100% border=0><tr><td valign=middle align=center><img src="img/wait_green.gif" width="32" height="32" alt="" border="0"></td></tr></table>';
	var url = "startexam.php?e="+p;
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = updatePageExamStart;
	xmlHttp.send(null);
}

function gotostart(p)
{	var r=confirm("Вы хотите начать экзамен?\nПротокол результатов будет создан после экзамена.\nДопускается один экзамен в день.");
	if (r==true)
	{		startexam(p);
	}
}

function updatePageExamStart() {
  if (xmlHttp.readyState == 4) {
	var response = xmlHttp.responseText;
	response=trim(response);
 	response = response.split(";");
 	if (response[0]==1)
 	{	    getcurquestion();
		timer = window.setTimeout("countdown()",999);
 	}
 	else
 	{ 	if (response[0]==2)
 	{    	document.getElementById("main").innerHTML='<h2>'+response[1]+'</h2>'; 	}
 	else
 	{ 	if (response[0]==3)
 	{    	//alert(response[1]);
    	getcurquestion();
    	timer = window.setTimeout("countdown()",999);
 	}
 	else
 	{
    	alert("Ошибка инициализации экзамена"+response);
 	}
 	}
 	}
 }
}

function countdown()
{
	var productElement = document.getElementById("sec");
	if (productElement != null)
	{
		var sec=document.all("sec").value;
		sec=sec-1;
		if (sec<0)
		{
			clearTimeout(timer);
            setzeroanswer();
		}
		else
		{
			document.getElementById("sec").value=sec;
			clearTimeout(timer);
			timer = window.setTimeout("countdown()",999);
		}
	}
	else
	{
		clearTimeout(timer);
		timer = window.setTimeout("countdown()",1999);
	}
}

function getcurquestion()
{	var url = "curquestion.php";
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = updatePagecurquestion;
	xmlHttp.send(null);
}

function setzeroanswer()
{	var ques=document.getElementById("qid").value;
	var url = "setanswer.php?answid=0"+"&quesid="+ques;
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = updatePagecuranswer;
	xmlHttp.send(null);
	document.getElementById("main").innerHTML='<table width=100% height=100% border=0><tr><td valign=middle align=center><img src="img/wait_green.gif" width="32" height="32" alt="" border="0"></td></tr><tr><td height=30% align=middle>Если страница долго не загружается, нажмите <a onClick="answernow()" href="#">сюда</a></td></tr></table>';
}

function answernow()
{	var answ=document.getElementById("aid").value;
	var ques=document.getElementById("qid").value;
	var url = "setanswer.php?answid="+answ+"&quesid="+ques;	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = updatePagecuranswer;
	xmlHttp.send(null);	document.getElementById("main").innerHTML='<table width=100% height=100% border=0><tr><td valign=middle align=center><img src="img/wait_green.gif" width="32" height="32" alt="" border="0"></td></tr><tr><td height=30% align=middle>Если страница долго не загружается, нажмите <a onClick="answernow()" href="#">сюда</a></td></tr></table>';
}

function updatePagecuranswer()
{
  if (xmlHttp.readyState == 4) {
	var response = xmlHttp.responseText;
	if (response)
	{		document.getElementById("main").innerHTML=response;
	}
	else
	{
		getcurquestion();
    	clearTimeout(timer);
    	timer = window.setTimeout("countdown()",999);
	}
  }
}

function setanswer(t)
{	document.getElementById("aid").value=t.value;}

function updatePagecurquestion()
{  if (xmlHttp.readyState == 4) {
	var response = xmlHttp.responseText;
	document.getElementById("main").innerHTML=response;
  }}

function trim (str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}

function checkinn(e)
{	var path="http://cls.npmaap.ru/img/";
	var inn=document.getElementById("inn").value;
	var secret=document.getElementById("secret").value;
	var hsecret=document.getElementById("secr").value;
	if ((inn.length==10) || (inn.length==12))
	{
			var url = "searchinn.php?i="+inn+"&c="+secret+"&h="+hsecret;
			xmlHttp.open("GET", url, true);
			xmlHttp.onreadystatechange = updatePagecheckinn;
			xmlHttp.send(null);
	}
	else
	{		if (inn!="") document.getElementById("indicator1").outerHTML='<img name="indicator1" id="indicator1" src="'+path+'waitsmall.gif" width="16" height="16" alt="" border="0">';	}
	if (secret.length==5)
	{		var url = "searchinn.php?i="+inn+"&c="+secret+"&h="+hsecret;
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = updatePagecheckinn;
		xmlHttp.send(null);	}
	else
	{		if (secret!="")
		{			document.getElementById("indicator2").outerHTML='<img name="indicator2" id="indicator2" src="'+path+'waitsmall.gif" width="16" height="16" alt="" border="0">';
			if (inn!="") document.getElementById("indicator1").outerHTML='<img name="indicator1" id="indicator1" src="'+path+'waitsmall.gif" width="16" height="16" alt="" border="0">';
		}	}
}
function checkfio()
{
	var fname=document.getElementById("fname").value;
	var lname=document.getElementById("lname").value;
	var sname=document.getElementById("sname").value;
	var inn=document.getElementById("inn").value;
	var secret=document.getElementById("secret").value;
	var hsecret=document.getElementById("secr").value;
	var serianomer=document.getElementById("serianomer").value;
	if ((fname!="") && (lname!="") && (sname!="") && (serianomer!=""))
	{
			var url = "searchfio.php?f="+fname+"&l="+lname+"&s="+sname+"&i="+inn+"&c="+secret+"&h="+hsecret+"&sn="+serianomer;
			xmlHttp.open("GET", url, true);
			xmlHttp.onreadystatechange = updatePagecheckfio;
			xmlHttp.send(null);
	}
}
function updatePagecheckfio() {
  if (xmlHttp.readyState == 4) {
    response="";
	var response = xmlHttp.responseText;
	//alert(response);
 	response = response.split(";");
 	if (response[0]==1)
 	{ 		document.getElementById("fname").style.borderColor="lightgreen";
 		document.all("fname").disabled=true;
 		document.getElementById("lname").style.borderColor="lightgreen";
 		document.all("lname").disabled=true;
 		document.getElementById("sname").style.borderColor="lightgreen"; 		document.all("sname").disabled=true;
 		document.getElementById("serianomer").style.borderColor="lightgreen";
 		document.all("serianomer").disabled=true;
 	}
 	else
 	{		//document.getElementById("typedocument").outerHTML=response[1];
 		document.getElementById("fname").style.borderColor="red"; 		document.getElementById("lname").style.borderColor="red";
 		document.getElementById("sname").style.borderColor="red";
 		document.getElementById("serianomer").style.borderColor="red";
 	}
  }
}
function checkandsend()
{	var fname=document.getElementById("fname").value;
	var lname=document.getElementById("lname").value;
	var sname=document.getElementById("sname").value;
	var inn=document.getElementById("inn").value;
	var secret=document.getElementById("secret").value;
	var hsecret=document.getElementById("secr").value;
	//var typedocument=document.getElementById("typedocument").value;
	var serianomer=document.getElementById("serianomer").value;
	//var dateofdocument=document.getElementById("dateofdocument").value;
	if ((fname!="") && (lname!="") && (sname!="") && (inn!="") && (secret!="") && (hsecret!="") && (serianomer!=""))
	{
 		document.all("sname1").value=document.all("sname").value;
 		document.all("fname1").value=document.all("fname").value;
 		document.all("lname1").value=document.all("lname").value;
		document.all("inn1").value=document.all("inn").value;
		document.all("secret1").value=document.all("secret").value;
		//document.all("typedocument1").value=document.all("typedocument").value;
		document.all("serianomer1").value=document.all("serianomer").value;
		//document.all("dateofdocument1").value=document.all("dateofdocument").value;
		document.all("saveandenter").value=1;
		document.getElementById("frm1").submit();
	}
	else
	{		alert("Введены не все данные. Вход невозможен");	}}
function updatePagecheckinn() {
  if (xmlHttp.readyState == 4) {
	response="";
	var response = xmlHttp.responseText;
	//alert(response);
 	response = response.split(";");
	path="http://cls.npmaap.ru/img/";
 	if (response[0]==0)
 	{   		document.getElementById("indicator1").outerHTML='<img name="indicator1" id="indicator1" src="'+path+'warn.gif" width="16" height="16" alt="'+response[1]+'" border="0">';   		document.getElementById("indicator2").outerHTML='<img name="indicator2" id="indicator2" src="'+path+'warn.gif" width="16" height="16" alt="'+response[1]+'" border="0">';
   		document.getElementById("sbmt").style.visibility="hidden";
 	}
 	if (response[0]==1)
 	{
   		document.getElementById("indicator1").outerHTML='<img name="indicator1" id="indicator1" src="'+path+'done.png" width="16" height="16" alt="'+response[1]+'" border="0">';
   		document.getElementById("indicator2").outerHTML='<img name="indicator2" id="indicator2" src="'+path+'done.png" width="16" height="16" alt="'+response[1]+'" border="0">';
   		document.getElementById("sbmt").style.visibility="visible";
		document.all("inn").disabled=true;
		document.all("secret").disabled=true;
 	}
 	if (response[0]==2)
 	{
   		document.getElementById("indicator1").outerHTML='<img name="indicator1" id="indicator1" src="'+path+'warn.gif" width="16" height="16" alt="'+response[1]+'" border="0">';
   		document.getElementById("sbmt").style.visibility="hidden";
 	}
 	if (response[0]==3)
 	{
   		document.getElementById("indicator2").outerHTML='<img name="indicator2" id="indicator2" src="'+path+'warn.gif" width="16" height="16" alt="'+response[1]+'" border="0">';
   		document.getElementById("sbmt").style.visibility="hidden";
 	}
 	if (response[0]==4)
 	{
   		document.getElementById("indicator2").outerHTML='<img name="indicator2" id="indicator2" src="'+path+'done.png" width="16" height="16" alt="'+response[1]+'" border="0">';
   		document.getElementById("sbmt").style.visibility="hidden";
 	}
  }
}
