var secsRemaining = 1800;

function lanseazaChestionar() {
	with(document.selChestionar) {
		if (document.getElementById("chTipRandom").checked)
			chestTip.value = "random";
		else if (document.getElementById("chTipCateg").checked) {
			chestTip.value = "categ";
			categsVal.value = categ.value;
		}
		else if (document.getElementById("chTipCategs").checked) {
			oneChecked = false;
			allChecked = true;
			intrebari = 0;
			categsVal.value = "";
			for (i = 0; i < categs.length; i++)
				if (categs[i].checked) {
					oneChecked = true;
					intrebari += parseInt(categs[i].id);
					categsVal.value += categs[i].value + "+";
				}
				else
					allChecked = false;
			if (allChecked)
				chestTip.value = "random";
			if (!oneChecked) {
				alert("Trebuie selectata minimum o categorie de intrebari !");
				return false;
			}
			minQs = nrQcats[0].checked ? 15 : 26;
			if (intrebari < minQs) {
				alert("Numarul total de intrebari din categoriile selectate este insuficient pentru a genera un chestionar !\n\nAlegeti categoriile astfel incat numarul de intrebari sa insumeze minimum " + minQs + ".");
				return false;
			}
			
			chestTip.value = "categs";
			categsVal.value = categsVal.value.substr(0, categsVal.value.length - 1);
		}		
		submit();
	}
}

function sendComment(nrIntrebare) {
	if (nrIntrebare != '') {
		document.formComment.question.value = nrIntrebare;
		document.formComment.submit();
	}
}

function checkIntrebari(index) {
	intrebari = 0;
	if (index != null) document.selChestionar.categs[index].checked = !document.selChestionar.categs[index].checked;
	for (i = 0; i < document.selChestionar.categs.length; i++)
		if (document.selChestionar.categs[i].checked) 					
			intrebari += parseInt(document.selChestionar.categs[i].id);
	if (index != null) document.selChestionar.categs[index].checked = !document.selChestionar.categs[index].checked;
	
	col = "#ff0000";
	minQs = document.selChestionar.nrQcats[0].checked ? 15 : 26;
	if (intrebari >= minQs) col = "#21A121";
	
	document.getElementById("totalIntrebari").innerHTML = 'Total intrebari posibile: <span style="color:' + col + '; font-weight:bold">' + intrebari + '</span>'
	return false;
}

function runTimer() {
	if (secsRemaining == 0) rezolvaChestionar();
	else {
		secsRemaining--;
		secs = secsRemaining % 60;
		mins = (secsRemaining - secs) / 60;
		if (secs < 10) secs = '0' + secs;
		if (mins < 10) mins = '0' + mins;
		document.getElementById("divTimer").innerHTML = "Timp ramas:<br>" + mins + ":" + secs;
		scurs = document.chestionar.elapsed.value;
		scurs++;
		document.chestionar.elapsed.value = scurs;
		
		setTimeout ("runTimer()", 1000);
	}
}

function rezolvaChestionar() {
	qs = document.getElementById("nrQs").value;
	for (i = 0; i < qs; i++) {
		currform = "q" + i;
		with (document.getElementById(currform)) {
			var toAppend = new String("");
			toAppend += nrq.value + "-";
			for (j = 0; j < 3; j++)
				if (raspuns[j].checked) toAppend += "1";
				else toAppend += "0";
		}
		document.chestionar.rezultate.value += toAppend + "-=-";
	}
	document.chestionar.rezultate.value = document.chestionar.rezultate.value.substr(0, document.chestionar.rezultate.value.length - 3);
	
	document.chestionar.submit();
}

function intrebareaUrmatoare() {
	var rasp = new String("");
	with (document.currentQuestion) {
		for (i = 0; i < 3; i++)
			if (raspuns[i].checked)
				rasp += '1';
			else
				rasp += '0';
		document.nextQuestion.raspuns.value = rasp;
		document.nextQuestion.submit();
	}
}