var browser=navigator.appName;

if (browser=="Microsoft Internet Explorer"){window.onscroll = scrollEvent;}

function scrollEvent() {
var TopPos=document.documentElement.scrollTop+10;
document.getElementById('TestTimer').style.top=TopPos+"px";
}

//----------------------------------------------------------------------
var q_params;

var StatusColor = new Array();

StatusColor[true]="lightgreen";
StatusColor[false]="red";

var TestTimer = 0;

function checkTime(i){
	if (i<10)  {i="0" + i;}
	return i;
	}

function MakeClockTime(t){

t = parseInt(t);
	
var h = parseInt(t / 3600);
var m = parseInt((t - h*3600) / 60);
var s = t - h*3600 - m*60;

var ClockTime = checkTime(m) + ":" + checkTime(s);

if (t > 3599) {ClockTime = checkTime(h) + ":" + ClockTime;}

return ClockTime;

}


function startTestTimer() {

if (TestTimer != null) {


TestTimer = parseInt(TestTimer);

TestTimer--;

document.getElementById('TestTimer').innerHTML=MakeClockTime(TestTimer);

if (TestTimer<60) {document.getElementById('TestTimer').style.backgroundColor="red";}

if (TestTimer>0) {t=setTimeout('startTestTimer()',1000);} else {

alert("The time ran out ...");
	
//	alert("За съжаление времето за решаване на теста изтече.");

//	alert("\u0417\u0430 &#1089;&#1098;&#1078;&#1072;&#1083;&#1077;&#1085;&#1080;&#1077; &#1074;&#1088;&#1077;&#1084;&#1077;&#1090;&#1086; &#1079;&#1072; &#1088;&#1077;&#1096;&#1072;&#1074;&#1072;&#1085;&#1077; &#1085;&#1072; &#1090;&#1077;&#1089;&#1090;&#1072; &#1080;&#1079;&#1090;&#1077;&#1095;&#1077;.");

//	window.location.reload();
//	document.TestForm.submit();

check_test("CheckTest");
	
}
}
}


function hidetimer() {

TestTimer 	= null;
	 
if (document.getElementById) { // DOM3 = IE5, NS6 
document.getElementById('TestTimer').style.visibility = 'hidden'; 
} 
else { 
if (document.layers) { // Netscape 4 
document.TestTimer.visibility = 'hidden'; 
} 
else { // IE 4 
document.all.TestTimer.style.visibility = 'hidden'; 
} 
} 
} 

function showtimer() { 

if (document.getElementById) { // DOM3 = IE5, NS6 
document.getElementById('TestTimer').style.visibility = 'visible'; 
} 
else { 
if (document.layers) { // Netscape 4 
document.TestTimer.visibility = 'visible'; 
} 
else { // IE 4 
document.all.TestTimer.style.visibility = 'visible'; 
} 
}

TestTimer++;

startTestTimer();
 
}


function GetRemoteData(target)

{
if (window.XMLHttpRequest)
    {
    filecontent = new XMLHttpRequest();
    }
else
    {
    filecontent = new ActiveXObject("Microsoft.XMLHTTP");
    }

filecontent.open("GET",target,false);

filecontent.send(null);

return filecontent.responseText;

}

function PostRemoteData(target,params)

{
if (window.XMLHttpRequest)
    {
    filecontent = new XMLHttpRequest();
    }
else
    {
    filecontent = new ActiveXObject("Microsoft.XMLHTTP");
    }

filecontent.open("POST",target,false);
filecontent.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
filecontent.setRequestHeader("Content-length", params.length);
filecontent.setRequestHeader("Connection", "close");

filecontent.send(params);

return filecontent.responseText;

}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function CheckTestStatus() {

if (TestTimer > 0) {showtimer();}
	
var TestInProgres = readCookie("GeoBG_Test_In_Progres");

if (TestInProgres == 2) {
	
document.cookie = "GeoBG_Test_In_Progres=1";
	
} else {
	
if (TestInProgres == 1) {
	
document.cookie = "GeoBG_Test_In_Progres=0";

alert("BREAK !!!");

}	
}
}

function get_questions(q_type, num, obj, params){
	
if (num>0) {
	
var result = GetRemoteData("ajax/get_questions.php?type="+q_type+"&num="+num+"&params="+q_params);
} else {result = "0|0||0";}

var data = result.split("|");	

q_type = q_type - 1;

QPoints[q_type] 	= data [0];
QTime[q_type] 		= data [1];
QList[q_type] 		= data [2];
QNum[q_type] 		= data [3];

var TimeTotal 		= parseInt(QTime[0]) 	+ parseInt(QTime[1]) 	+ parseInt(QTime[2]);
var PointsTotal 	= parseInt(QPoints[0]) 	+ parseInt(QPoints[1]) 	+ parseInt(QPoints[2]);
var QuestionsTotal 	= parseInt(QNum[0]) 	+ parseInt(QNum[1]) 	+ parseInt(QNum[2]);

var QuestionList = "";
var Coma = "";

if (QList[0].length>0) {QuestionList = QList[0];Coma = ","}
if (QList[1].length>0) {QuestionList = QuestionList + Coma + QList[1];Coma = ","}
if (QList[2].length>0) {QuestionList = QuestionList + Coma + QList[2];}

var TimeClock = MakeClockTime(TimeTotal);

var Temp = PointsTotal + " &#1090;&#1086;&#1095;&#1082;&#1080;, ";

if (PointsTotal < document.CreateTestForm.MinTestPoints.value) {Temp = "<font color='red'>" + Temp + "</font>"}

Temp += QuestionsTotal + "&nbsp;&#1074;&#1098;&#1087;&#1088;&#1086;&#1089;&#1072;";
 
document.getElementById("Q_Info").innerHTML = TimeClock + "<br><br>" + Temp;

document.CreateTestForm.TimeTotal.value 	= TimeTotal;
document.CreateTestForm.PointsTotal.value 	= PointsTotal;
document.CreateTestForm.QuestionList.value 	= QuestionList;
document.CreateTestForm.QuestionsTotal.value= QuestionsTotal;

document.CreateTestForm.StartTestButton.disabled = !(TimeTotal > 0 && PointsTotal > 0);

}


function check_test(mode){
	
if (mode == "TestAgainButton")	{
	
document.TestAgain.submit();

} else {
	
var TimeRemain = TestTimer;	

hidetimer();

/*
for (var i=0; i<document.TestForm.length; i++) {
	
document.TestForm.item(i).disabled = true;
	
}
*/

for (var i=0; i<document.forms['TestForm'].length; i++) {
	
document.forms['TestForm'].elements[i].disabled = true;
	
}


var TestPoints 	   = 0;
var QuestionsRight = 0;
var QuestionsTotal = 0;
var QuestionsPass  = 0;
var QList  = "";

var QNum = document.TestForm.QuestionsCounter.value;

var QuestionsID = QuestionList.split(",");
//---------------------------------------------------------------------------

for (var i=1; i<=QNum; i++){

var QType = document.getElementById("QType_"+i).value;

var QPoints = parseInt(document.getElementById("QPoints_"+i).value);

switch (QType){

	case 'R1':

var Status = document.getElementById("AV_"+i+"_1").checked;

document.getElementById("AR_"+i+"_1").style.background = StatusColor[Status];

QuestionsTotal ++;

if (Status) {TestPoints = TestPoints + QPoints; QuestionsRight ++; QuestionsPass++;}

	break;

	case 'T1':
	case 'S1':
	
var QCount = document.getElementById("QCount_"+i).value;

var QA = 0;
var QR = 0;

var Result = new Array();

for (var j=1; j<=QCount; j++){
	
var Status = false;

var RightAnswer = "";

var UserAnswer = document.getElementById("AV_"+i+"_"+j).value;

var QAnswers = document.getElementById("AN_"+i+"_"+j).value.split("|");

for (var k=0; k<QAnswers.length; k++){

if (QAnswers[k] == UserAnswer && !Result[k]) {
	
	Status = true; 

	if (QAnswers.length > 1) {Result[k] = true;}

}

if (QType == 'S1') {
	
	var AnswerList = document.getElementById("AnswerList_"+i).value.split("|");
	
	RightAnswer = AnswerList[j-1] + ", ";
	
	
	} else {
		
	RightAnswer = RightAnswer + QAnswers[k] + ", ";	
	
	}
}	


document.getElementById("AV_"+i+"_"+j).style.background = StatusColor[Status];

QuestionsTotal ++;
QA++;

if (Status) {TestPoints = TestPoints + QPoints; QuestionsRight ++; QR++;}

document.getElementById("AV_"+i+"_"+j).title = RightAnswer.slice(0,-2);

}

if (QR/QA>=0.5) {QuestionsPass++;}
	
	break;
}

if (Status) {QList += QuestionsID[i-1]+","}


}

//---------------------------------------------------------------------------

var Ocenka = parseInt((2 + TestPoints * (4/PointsTotal))*100);

var KlassID = document.TestForm.KlassID.value;
var TestScore = 0; 

if (Ocenka < 300) {Ocenka = 200;} else {TestScore = KlassID * TestPoints;}

var PointsRatio = parseInt(TestPoints/PointsTotal*100);
var QuestionsRatio = parseInt(QuestionsRight/QuestionsTotal*100);

var TestOcenka = Ocenka/100;

if (parseInt(Ocenka/100) == Ocenka/100) {TestOcenka = TestOcenka + ".0"}
if (parseInt(Ocenka/10)  == Ocenka/10)  {TestOcenka = TestOcenka + "0"}

var QuestionsRatio = parseInt(QuestionsRight/QuestionsTotal*100);
var PassRatio = parseInt(QuestionsPass/Questions*100);

var Message = "<table id='stattable'>";

Message += "<tr><td id='statrow'>&#1041;&#1088;&#1086;&#1081; &#1085;&#1072; &#1074;&#1098;&#1087;&#1088;&#1086;&#1089;&#1080;&#1090;&#1077; &#1074; &#1090;&#1077;&#1089;&#1090;&#1072; : </td><td>"  + QuestionsPass + " / " + Questions + " (" + PassRatio + "%)</td></tr>";

Message += "<tr><td id='statrow'>&#1041;&#1088;&#1086;&#1081; &#1085;&#1072; &#1074;&#1072;&#1088;&#1080;&#1072;&#1085;&#1090;&#1080;&#1090;&#1077; &#1079;&#1072; &#1086;&#1090;&#1075;&#1086;&#1074;&#1086;&#1088; : </td><td>" + QuestionsRight + " / " + QuestionsTotal + " (" + QuestionsRatio + "%)</td></tr>";

Message += "<tr><td id='statrow'>&#1041;&#1088;&#1086;&#1081; &#1085;&#1072; &#1090;&#1086;&#1095;&#1082;&#1080;&#1090;&#1077; : </td><td>" + TestPoints + " / " + PointsTotal + " (" + PointsRatio + "%)</td></tr>";

var TimeElapsed = TestLength - TimeRemain;
var TimeRatio 	= parseInt(TimeElapsed/TestLength*100);
	
Message += "<tr><td id='statrow'>&#1042;&#1088;&#1077;&#1084;&#1077; &#1079;&#1072; &#1088;&#1077;&#1096;&#1072;&#1074;&#1072;&#1085;&#1077; : </td><td>" + MakeClockTime(TimeElapsed) + " / " + MakeClockTime(TestLength) + " (" + TimeRatio + "%)</td></tr>";


Message += "<tr><td id='statrow'>&#1054;&#1094;&#1077;&#1085;&#1082;&#1072; : </td><td>"  + TestOcenka + "</td></tr>";

Message += "<tr><td id='statrow'>&#1057;&#1087;&#1077;&#1095;&#1077;&#1083;&#1077;&#1085;&#1080; &#1090;&#1086;&#1095;&#1082;&#1080; : </td><td>"  + TestScore + "</td></tr>";

Message += "<tr><td id='statrow'>&#1054;&#1089;&#1090;&#1072;&#1085;&#1072;&#1083;&#1086; &#1074;&#1088;&#1077;&#1084;&#1077; : </td><td>"  + TimeRemain + "</td></tr>";
Message += "</table>";

var SaveStatus = PostRemoteData("ajax/save_result.php","points=" + TestScore + "&user=" + UserID + "&time=" + TestBegin + "&ocenka=" + Ocenka + "&key=" + LogID + "&list=" + QuestionList + "&klas=" + KlassID + "&test=" + document.TestForm.LessonID.value);

if (SaveStatus == null) {Message += "???";} else {

var TestStat = SaveStatus.split("|");

}

document.getElementById("button_action").innerHTML = Message+"<br><input type='button' name='TestAgainButton' onclick='check_test(this.name)' value='&#1053;&#1072;&#1087;&#1088;&#1072;&#1074;&#1080; &#1085;&#1086;&#1074; &#1090;&#1077;&#1089;&#1090;' />";

document.TestForm.TestAgainButton.disabled = false;

document.cookie = "GeoBG_Test_In_Progres=0";

}

}

var QList 	= new Array("","","");
var QPoints = new Array(0,0,0);
var QTime 	= new Array(0,0,0);
var QNum 	= new Array(0,0,0);


document.cookie = "GeoBG_Test_Cookie=1";