var blocked = false; 
var corecte = 0;
var gresite = 0;

function initCh() {
	$("rezultOK").style.display = "none";
	$("rezultNot").style.display = "none";
	blocked = false;
}

function delayRaspuns() {
	if(blocked) return false;	
	blocked = true;
	xajax_getNextQuestion(chID, 0);
}

function charPressed(ind) {
	if(blocked) return false;
		
	raspunsuriArr[ind] = true;
 	$("v_"+ind).style.backgroundColor = "#FFFF00";
}

function delRaspuns() {	
	if(blocked) return false;
		
	for(var ind in raspunsuriArr) {
		$("v_"+ind).style.backgroundColor = "#FFFFFF";
	}
	raspunsuriArr = new Array();
}

function continua() {
	var finish = false;
	if(ramase <= 0) {
		finish = true;	
	}
	
	if(!finish) {
		xajax_getNextQuestion(chID, 1);
	}
	else {		
		var   tDate = new Date();
		if(!tStart) tStart   = new Date();
		var   tDiff = tDate.getTime() - tStart.getTime();
		
		document.location.href="chestionar.php?finish=1&id="+chID+"&k="+corecte+"&g="+gresite + "&t="+tDiff;
	}
}

function checkRaspuns() {
	if(blocked) return false;
	
	var ok = true;
	for(var ind in raspunsuriArr) {
		if(!rightVals[ind]) {
			ok = false;
		}
	}
	
	for(var ind in rightVals) {
		if(!raspunsuriArr[ind]) {
			ok = false;
		}
	}
	
	if(ok) {
		corecte ++;
	}
	else {
		gresite ++;
	}
	
	ramase --;
	var finish = false;
	if(ramase <= 0) {
		finish = true;
		ramase = 0;
	}
	
	$("q_ramase").value = ramase;
	$("q_ok").value = corecte;
	$("q_wrong").value = gresite;
	
	$("rezultOK").style.display = (ok ? "" : "none");
	$("rezultNot").style.display = (ok ? "none" : "");	
	
	blocked = true;
	
	if($("veziraspuns").checked) {		
		for(var ind in raspunsuriArr) {
			if(!rightVals[ind]) {
				$("v_"+ind).style.backgroundColor = "#FFDDD0";
			}
			else {
				$("v_"+ind).style.backgroundColor = "#66FF99";
			}
		}

		for(var ind in rightVals) {			
			$("v_"+ind).style.backgroundColor = "#66FF99";			
		}
		
		$("btn_rasp").innerHTML = '<table border="0" cellpadding="10" cellspacing="0" align="center"><tr><td><input type="button" class="btn" onclick="continua();" value="Continua" /></td></tr></table>';
		return;
	}
	
	if(!finish) {
		setTimeout("xajax_getNextQuestion("+chID+", 1)", 1500);
	}
	else {		
		var   tDate = new Date();
		if(!tStart) tStart   = new Date();
		var   tDiff = tDate.getTime() - tStart.getTime();
		
		setTimeout("document.location.href='chestionar.php?finish=1&id="+chID+"&k="+corecte+"&g="+gresite + "&t="+tDiff+"'", 1000);
	}
}

function timerFinish() {
	blocked = true;
	alert('Timpul a expirat');	
	finishTest();
}

function finishTest() {
	var   tDate = new Date();
	if(!tStart) tStart   = new Date();
	var   tDiff = tDate.getTime() - tStart.getTime();
		
	document.location.href="chestionar.php?finish=1&id="+chID+"&k="+corecte+"&g="+gresite + "&t="+tDiff;
}

var timerID = 0;
var tStart  = null;
var totalMinutes = 0;

function UpdateTimer() {
   if(timerID) {
      clearTimeout(timerID);
      clockID  = 0;
   }

   if(!tStart) tStart   = new Date();

   var   tDate = new Date();
   var   tDiff = tStart.getTime() + totalMinutes * 60 * 1000 - tDate.getTime();
   if(tDiff <= 0) {   		
  		timerFinish();
  		return;
   }

   tDate.setTime(tDiff);

   $("timp_ramas").value = "" 
                                   + tDate.getMinutes() + ":" 
                                   + tDate.getSeconds();
   
   timerID = setTimeout("UpdateTimer()", 1000);
}

function Start(tip) {
   tStart   = new Date();

   totalMinutes = tip=='a'?30:20;
   $("timp_ramas").value = totalMinutes+":00";


   timerID  = setTimeout("UpdateTimer()", 1000);
}

function Stop() {
   if(timerID) {
      clearTimeout(timerID);
      timerID  = 0;
   }

   tStart = null;
}

function Reset() {
   tStart = null;
   $("timp_ramas").value = "00:00";
}
