// JavaScript Document
NESSUNA  = "N";
VERO 	 = "V";
FALSO 	 = "F";
CORRETTA = "C";
ERRATA 	 = "E";

NUM_QUESITI_TOOLBAR = 10;
NUM_QUESITI_RIASSUNTO = 4;

var eSchedaMode = {
	DoubleMode:0,
	SingleMode:1
};

var eTipoListato = {
	TIPOLISTATO_NONVALIDO:0,
    TIPOLISTATO_SCONOSCIUTO:1,
	TIPOLISTATO_AB:2,
    TIPOLISTATO_SUP:3,
    TIPOLISTATO_EDU:4,
	TIPOLISTATO_ADR:5,
	TIPOLISTATO_NOQUIZ:6,
	TIPOLISTATO_PATENTINO:7,
	TIPOLISTATO_REV_AB:8,
	TIPOLISTATO_REV_SUP:9,
	TIPOLISTATO_APC1:10,
	TIPOLISTATO_APC2:11,
	TIPOLISTATO_CQC:12,
	TIPOLISTATO_NAUTICA:13,
	TIPOLISTATO_ADR_CONS:14,
	TIPOLISTATO_ABNEW:15
};

//millisecondi di pausa per il passaggio alla risposta successiva
var nextAswTimeout=500;

function Asw(idasw, txt, tra){
    this.idasw = idasw; //id della risposta
    this.txt = txt; //testo in italiano
    this.tra = tra; //traduzione in lingua
}

function Qst(idqst, txt, tra, fig){
    this.idqst = idqst; //id della domanda
    this.txt = txt; //testo in italiano
    this.tra = tra; //traduzione in lingua
    this.fig = new Image(); //id della figura

    if (fig)
        this.fig.src = fig;

    this.idAsws = []; //lista delle risposte
    this.SetAsw = function(idasw, txt, tra){
        this.idAsws.push(new Asw(idasw, txt, tra));
    };

    this.GetAsw = function(i){
        return this.idAsws[i];
    };
}

function Scheda(listato, lingua, bilingue,tipolist){

    this.listato    = listato;  //listato in uso
    this.lingua     = lingua;   //lingua
    this.bilingue   = bilingue; //bilingue in corso
    this.tipolist   = tipolist; //bilingue in corso

	this.aswCount = 1;
    this.qstCount = 1;

    this.finished=false;
	this.corrected=false;

	this.aswMin=0;

    this.idQsts = []; //lista delle domande

    this.SetQst = function(idqst, txt, tra, fig) {
        this.idQsts.push(new Qst(idqst, txt, tra, fig));
    };

    this.GetQst = function(i){
        return this.idQsts[i];
    };

    this.rtlMode = function(){
        return (this.lingua == 6) //arabo
    };
}

var scheda;

var risposte = new Array();
var commenti = new Array();
var fig_commenti = new Array();

var currentAsw = 0;
var currentQst = 0;
var currentIdx = 0;

var posizioneRiassunto = 0;

var colorToolbar 					= "#D1E6D7";
var colorSelezionato 				= "#FF0000";
var colorDeSelezionato 				= "#ADC5B5";
var colorRispostaRiassunto 			= "#0099CC";
var colorRispostaErrataRiassunto 	= "red";
var colorRispostaCorrettaRiassunto 	= "green";

var iMinuti = 0;
var iSecondi = 10;

//------------------------------------------------------------------
// Funzioni comuni
//------------------------------------------------------------------
function initTest() {

	delete scheda;
	scheda = new Scheda(0, 0, 0, 0);

  	//oggetti scheda sida
	$('#iRemarks').html('');
	$('#iRemarks').css({'visibility':'hidden'});

	$('#iFigRem').css({'visibility':'hidden'});
	$('#iFigRem').attr({'src': ''});
	$('#iCommLink').attr({'href': '' });

	//oggetti scheda ministeriale
	$('#tbFooter').css({'background':'#D1E6D7'});
	$('#footer').css({'background':'#D1E6D7'});

	$('#navAvanti').css({'visibility':'visible'});
	$('#navIndietro').css({'visibility':'visible'});

	$('#correggi').css({'visibility':'hidden'});
	$('#correzione').css({'visibility':'hidden'});
	$('#spRiassunto').css({'visibility':'hidden'});
	$('#suRiassunto').css({'visibility':'hidden'});
	$('#giuRiassunto').css({'visibility':'hidden'});
	$('#fine').css({'visibility':'hidden'});
	$('#modifica').css({'visibility':'hidden'});
	$('#conferma').css({'visibility':'hidden'});

	for (var i = 1; i <= scheda.aswCount; i++) {

    	numOpzione = 10 * i + 1;
		$('#opzione'+numOpzione+"Check").css({'visibility':'hidden'});

    	numOpzione = 10 * i + 2;
		$('#opzione'+numOpzione+"Check").css({'visibility':'hidden'});
	}

	$('#iNavigation').css({'height':'34px'});
	$('#iBodySumm').css({'visibility':'hidden'});

	currentAsw = 0;
	currentQst = 0;
	currentIdx = 0;
}

function playAudio(url2)
{
	$('#wrapper').html('<embed src="'+url2+'"  hidden=true autostart=true loop=false>');
}

//------------------------------------------------------------------
// Funzioni scheda ministeriale
//------------------------------------------------------------------
function creaToolbar(inizio){
    obj = window.document.getElementById('toolbar');
    appo = '<table class=\"tbToolbar\" cellspacing=0><tr>';
    for (var i = inizio; i < NUM_QUESITI_TOOLBAR + inizio; i++) {
        apice = "'";
        appo = appo + '<a href=\"javascript:bottoneClick(' + i + ')' + '\"><td id=\"bottoneToolbar' + (i) + '\">';
        appo = appo + '<span> &nbsp;' + '<a id=\"linkToolbar' + (i) + '\" href=\"javascript:bottoneClick(' + i + ')' + '\">' + (i + 1) + '&nbsp;' + '</a></span></td></a>';
    }
    appo = appo + '<td><a href=\"javascript:riassuntoClick()\"><img class=iOpzione src=\"./img/scheda/riassunto.jpg\"></a></td></tr></table>';
    obj.innerHTML = appo;
}

function countDown(){
    if ((iMinuti == 0) && (iSecondi == 0)) {
        riassuntoClick();
        confermaClick();
        return;
    }
    iSecondi = iSecondi - 1;
    if (iSecondi < 0) {
        iSecondi = 59;
        iMinuti = iMinuti - 1;
    }
    if (iSecondi > 9) {
        strSecondi = "" + iSecondi;
    }
    else {
        strSecondi = "0" + iSecondi;
    }
    if (iMinuti > 9) {
        strMinuti = "" + iMinuti;
    }
    else {
        strMinuti = "0" + iMinuti;
    }
    window.document.getElementById('tempoRimanente').innerHTML = strMinuti + ":" + strSecondi;
    setTimeout("countDown()", 1000);
}

function loadQstM(numQst){

    numQst = numQst - 1;

    if (numQst == (scheda.qstCount - 1))
        window.document.getElementById('imgAvanti').src = "./img/scheda/riassunto.jpg";
    else
        window.document.getElementById('imgAvanti').src = "./img/scheda/avanti.gif";

    numDomanda = numQst * scheda.aswCount;
    for (var i = 1; i <= scheda.aswCount; i++) {
        window.document.getElementById('testoDomanda' + i).innerHTML = scheda.GetQst(numQst).GetAsw(i - 1).txt;
        if (scheda.bilingue && scheda.GetQst(numQst).GetAsw(i - 1).tra.length)
			$('#testoDomandaTra'+i).html('(' + scheda.GetQst(numQst).GetAsw(i - 1).tra + ')');
        if (scheda.rtlMode())
            window.document.getElementById('testoDomandaTra' + i).style.direction = "rtl";

        window.document.getElementById('audioDomanda' + i + 'Ref').href = 'javascript:playAudio(\'/ecom.dll/audio?type=2&list=' + scheda.listato + '&lang=' + scheda.lingua + '&id=' + scheda.GetQst(numQst).GetAsw(i - 1).idasw + '\')';

        viewAnswersDouble((numQst * scheda.aswCount) + (i - 1));
    }

    window.document.getElementById('testoQuesito').innerHTML = scheda.GetQst(numQst).txt;
    window.document.getElementById('audioQuesitoRef').href = 'javascript:playAudio(\'/ecom.dll/audio?type=1&list=' + scheda.listato + '&lang=' + scheda.lingua + '&id=' + scheda.GetQst(numQst).idqst + '\')';

    if (scheda.bilingue && scheda.GetQst(numQst).tra.length)
		$('#testoQuesitoTra').html('(' + scheda.GetQst(numQst).tra + ')');
    if (scheda.rtlMode())
        window.document.getElementById('testoQuesitoTra').style.direction = "rtl";

    var imgPath = scheda.GetQst(numQst).fig.src;
    var imgText = imgPath.substring(imgPath.lastIndexOf("/") + 1);

    if (imgText != "") {
        window.document.getElementById('iQuesito').style.visibility = 'visible';
        /*window.document.getElementById('iFigQuesito').innerHTML = 'Figura n.' + imgText.substring(0, imgText.lastIndexOf("G"));*/
        window.document.getElementById('iQuesito').src = scheda.GetQst(numQst).fig.src;
        window.document.getElementById('iQuesitoLink').href = scheda.GetQst(numQst).fig.src;
        window.document.getElementById('numQuesito').innerHTML = parseInt(numQst) + 1;
    }
    else {
        window.document.getElementById('iQuesito').style.visibility = 'hidden';
        /*window.document.getElementById('iFigQuesito').innerHTML = "";*/
        window.document.getElementById('iQuesito').src = "";
        window.document.getElementById('iQuesitoLink').href = "";
    }
    window.document.getElementById('numQuesito').innerHTML = parseInt(numQst) + 1;
    window.document.getElementById('linkToolbar' + numQst).style.color = colorSelezionato;


    if (NUM_QUESITI_TOOLBAR < scheda.qstCount) {
		alert("NUM_QUESITI_TOOLBAR < scheda.qstCount");
        inizio = scheda.qstCount - NUM_QUESITI_TOOLBAR;
        if (inizio < numQst) {
            creaToolbar(inizio);
        }
        else {
            creaToolbar(numQst);
        }
    }

    //riaggiorno i controlli della prima maschera
    window.document.getElementById('tbFooter').style.background = "#D1E6D7";
    window.document.getElementById('footer').style.background = "#D1E6D7";

    window.document.getElementById('navAvanti').style.visibility = 'visible';
    window.document.getElementById('navIndietro').style.visibility = 'visible';

    //imposto di default la prima risposta
    currentAsw=1;
}

function indietroClick(){
    if (currentQst == 1) {
        return
    };
    $('#linkToolbar' + (currentQst - 1)).css({
        'color': colorDeSelezionato
    });
    currentQst = currentQst - 1;
    loadQstM(currentQst);
}

function avantiClick(){
    if (currentQst == scheda.qstCount) {
        riassuntoClick();
        return;
    }

    $('#linkToolbar' + (currentQst - 1)).css({
        'color': colorDeSelezionato
    });
    currentQst = currentQst + 1;
    loadQstM(currentQst);
}

function bottoneClick(numQuesito){
    window.document.getElementById('linkToolbar' + (currentQst - 1)).style.color = colorDeSelezionato;
    currentQst = numQuesito + 1;
    loadQstM(currentQst);
}

function riassuntoClick(){
    posizioneRiassunto = 1;
    window.document.getElementById('conferma').style.visibility = 'visible';
    window.document.getElementById('modifica').style.visibility = 'visible';
    componiRiassunto(1);
    window.document.getElementById('spRiassunto').style.visibility = 'visible';
    window.document.getElementById('suRiassunto').style.visibility = 'visible';
    window.document.getElementById('giuRiassunto').style.visibility = 'visible';
    //window.document.getElementById('guida').style.visibility='hidden';

    window.document.getElementById('tbFooter').style.background = "#FBC8D9";
    window.document.getElementById('footer').style.background = "#FBC8D9";

    window.document.getElementById('navAvanti').style.visibility = 'hidden';
    window.document.getElementById('navIndietro').style.visibility = 'hidden';
}

function modificaClick(numQuesito){
    window.document.getElementById('linkToolbar' + (currentQst - 1)).style.backgroundColor = "transparent";
    window.document.getElementById('linkToolbar' + (currentQst - 1)).style.color = colorDeSelezionato;

    window.document.getElementById('conferma').style.visibility = 'hidden';
    window.document.getElementById('modifica').style.visibility = 'hidden';
    window.document.getElementById('spRiassunto').style.visibility = 'hidden';
    window.document.getElementById('suRiassunto').style.visibility = 'hidden';
    window.document.getElementById('giuRiassunto').style.visibility = 'hidden';

    currentQst = numQuesito;
    loadQstM(currentQst);
}

function confermaClick(){
    if (confirm("Sei sicuro di voler confermare tutte le risposte date ? Con OK non avrai piu' la possibilita' di modificarle.")) {
        window.document.getElementById('conferma').style.visibility = 'hidden';
        window.document.getElementById('modifica').style.visibility = 'hidden';
        window.document.getElementById('correzione').style.visibility = 'visible';

	    iMinuti = 0;
	    iSecondi = 0;
    	window.document.getElementById('tempoRimanente').innerHTML = "00:00";
    }
}

function modificaRiassuntoClick(num){
    obj = window.document.getElementById('colImgRiassunto' + num);
    str = obj.innerHTML.substr(6);
    modificaClick(parseInt(str) - 1);
}

function componiRiassunto(iniziaDa){

    for (var i = iniziaDa; i < NUM_QUESITI_RIASSUNTO + iniziaDa; i++) {
        quesito = i - iniziaDa;
        window.document.getElementById('colImgRiassunto' + quesito).innerHTML = "&nbsp;" + (i);
        window.document.getElementById('testoQuesitoRiassunto' + quesito).innerHTML = scheda.GetQst(i - 1).txt;
        window.document.getElementById('iRiassunto' + quesito).src = scheda.GetQst(i - 1).fig.src;

        if (scheda.GetQst(i - 1).fig.src != "")
            window.document.getElementById('iRiassunto' + quesito).style.width = '80px';
        else
            window.document.getElementById('iRiassunto' + quesito).style.width = '0px';

        for (var j = 0; j < scheda.aswCount; j++) {
            numDomanda = "" + quesito + (j + 1);
            iDomanda = ((i - 1) * scheda.aswCount) + j;
            window.document.getElementById('testoDomandaRiassunto' + numDomanda).innerHTML = scheda.GetQst(i - 1).GetAsw(j).txt;
            if (risposte[iDomanda] == NESSUNA || risposte[iDomanda] == (NESSUNA + ERRATA)) {
                window.document.getElementById('colRispostaRiassunto' + numDomanda + VERO).style.background = "white";
                window.document.getElementById('colRispostaRiassunto' + numDomanda + FALSO).style.background = "white";
                window.document.getElementById('colRispostaRiassunto' + numDomanda + VERO).innerHTML = "&nbsp;";
            }
            else {
                if (risposte[iDomanda] == VERO) {
                    window.document.getElementById('colRispostaRiassunto' + numDomanda + risposte[iDomanda]).style.background = colorRispostaRiassunto;
                    window.document.getElementById('colRispostaRiassunto' + numDomanda + FALSO).style.background = "white";
                }
                else
                    if (risposte[iDomanda] == FALSO) {
                        window.document.getElementById('colRispostaRiassunto' + numDomanda + risposte[iDomanda]).style.background = colorRispostaRiassunto;
                        window.document.getElementById('colRispostaRiassunto' + numDomanda + VERO).style.background = "white";
                    }
                    else
                        if (risposte[iDomanda] == (VERO + ERRATA)) {
                            window.document.getElementById('colRispostaRiassunto' + numDomanda + VERO).style.background = colorRispostaErrataRiassunto;
                            window.document.getElementById('colRispostaRiassunto' + numDomanda + VERO).style.color = "white";
                            //window.document.getElementById('colRispostaRiassunto'+numDomanda+VERO).innerHTML=commenti[iDomanda];
                            window.document.getElementById('colRispostaRiassunto' + numDomanda + FALSO).style.background = "white";
                            window.document.getElementById('colRispostaRiassunto' + numDomanda + FALSO).innerHTML = "&nbsp;";
                        }
                        else
                            if (risposte[iDomanda] == (FALSO + ERRATA)) {
                                window.document.getElementById('colRispostaRiassunto' + numDomanda + FALSO).style.background = colorRispostaErrataRiassunto;
                                //window.document.getElementById('colRispostaRiassunto'+numDomanda+FALSO).innerHTML=commenti[iDomanda];
                                window.document.getElementById('colRispostaRiassunto' + numDomanda + FALSO).style.color = "white";
                                window.document.getElementById('colRispostaRiassunto' + numDomanda + VERO).style.background = "white";
                                window.document.getElementById('colRispostaRiassunto' + numDomanda + VERO).innerHTML = "&nbsp;";
                            }
                            else
                                if (risposte[iDomanda] == (VERO + CORRETTA)) {
                                    window.document.getElementById('colRispostaRiassunto' + numDomanda + VERO).style.background = colorRispostaCorrettaRiassunto;
                                    window.document.getElementById('colRispostaRiassunto' + numDomanda + FALSO).style.background = "white";
                                    window.document.getElementById('colRispostaRiassunto' + numDomanda + FALSO).innerHTML = "&nbsp;";
                                }
                                else
                                    if (risposte[iDomanda] == (FALSO + CORRETTA)) {
                                        window.document.getElementById('colRispostaRiassunto' + numDomanda + FALSO).style.background = colorRispostaCorrettaRiassunto;
                                        window.document.getElementById('colRispostaRiassunto' + numDomanda + VERO).style.background = "white";
                                        window.document.getElementById('colRispostaRiassunto' + numDomanda + VERO).innerHTML = "&nbsp;";
                                    }
            }
        }
    }
}

function spostaRiassunto(quanto){
    //verfica indietro
    if ((posizioneRiassunto == 1) && (quanto < 0))
        return;

    if (((posizioneRiassunto + NUM_QUESITI_RIASSUNTO) == (scheda.qstCount + 1)) && (quanto > 0))
        return;

    posizioneRiassunto = posizioneRiassunto + quanto;
    componiRiassunto(posizioneRiassunto);
}

function creaTabellaRiassunto(){
    obj = window.document.getElementById('spRiassunto');
    riga = '<table class=\"tbRiassunto\" cellspacing=0; >\n';
    for (var i = 0; i < NUM_QUESITI_RIASSUNTO; i++) {
        riga = riga + '<tr class=\"rigaQuesitoRiassunto\"> <td class=\"colImgRiassunto\">';
        riga = riga + '&nbsp;<img class=\"iRiassunto\" id=\"iRiassunto' + i + '\"></td>';
        riga = riga + '<td class=\"colQuesitoRiassunto\"><font  id=\"testoQuesitoRiassunto' + i + '\" class=\"testoQuesitoRiassunto\">&nbsp;</font></td>';
        riga = riga + '<td class=\"colRispostaRiassuntoHead\">V</td><td class=\"colRispostaRiassuntoHead\">F</td><td class=\"colBottoniRiassunto\"></td></tr>\n';

        for (var j = 1; j <= scheda.aswCount; j++) {
            numDomanda = "" + i + j;
            iDomanda = (i * scheda.aswCount) + j;
            if (j == 1)
                riga = riga + '<tr class=\"rigaDomandaRiassunto\" ><td class=\"colImgBlank\"><a href=\"javascript:modificaRiassuntoClick(' + j + ');\"><font class=\"testoNumeroRiassunto\" id=\"colImgRiassunto' + i + '\">&nbsp;</font></a></td>';
            else
                riga = riga + '<tr class=\"rigaDomandaRiassunto\" ><td class=\"colImgBlank\">&nbsp;</td>';
            riga = riga + '<td class=\"colDomandaRiassunto\"><font id=\"testoDomandaRiassunto' + numDomanda + '\" class=\"testoDomandaRiassunto\">&nbsp;</font></td>';
            riga = riga + '<td id=\"colRispostaRiassunto' + numDomanda + VERO + '\" class=\"colRispostaRiassunto\">&nbsp;</td><td id=\"colRispostaRiassunto' + numDomanda + FALSO + '\" class=\"colRispostaRiassunto\">&nbsp;</td><td class=\"colBottoniRiassunto\"></td></tr>\n';
        }
    }
    riga = riga + '</table><table class=\"tbSpazioNullo\"><tr id=\"rowSeparatore\" class=\"rowSeparatore\"><td></td></tr></table>\n';

    obj.innerHTML = riga;
}

function correctTestMin() {

	scheda.corrected = true;
    iErrate = 0;
    iCorrette = 0;

    $.getJSON("/ecom.dll/schedacorr", { risposte: risposte }, function(json){

		//flag termine test
		scheda.finished=true;

        for (i = 0; i < (scheda.qstCount*scheda.aswCount); ++i) {
            if (json.risposte[i] == CORRETTA) {
                risposte[i] = risposte[i] + CORRETTA;
                iCorrette++;
            }
            else {
                risposte[i] = risposte[i] + ERRATA;
                iErrate++;
            }

            commenti[i] = json.commenti[i];
        }

        doc = window.document;

        doc.getElementById('correggi').style.visibility = 'visible';
        doc.getElementById('fine').style.visibility = 'visible';

        doc.getElementById('risposteEsatte').innerHTML = "&nbsp;" + iCorrette;
        doc.getElementById('risposteErrate').innerHTML = "&nbsp;" + iErrate;

        if (iCorrette >= scheda.aswMin)
			doc.getElementById('esito').innerHTML = "&nbsp; IDONEO";
        else
			doc.getElementById('esito').innerHTML = "&nbsp; NON IDONEO";

        componiRiassunto(1);
    });
}

function loadTestM(tipoScheda, numSchede, argoCount, bitMaskArgo){

    initTest();

    $.getJSON("/ecom.dll/scheda", {
        tipo: tipoScheda,
        num: numSchede,
        argoCount: argoCount,
        mask: bitMaskArgo
    }, function(json){
        $('#numeroScheda').html(json.numeroScheda);
        $('#nomeCandidato').html(json.nomeCandidato);

        scheda.listato = json.listato;
		scheda.tipolist = json.tipolistato;
        scheda.lingua = json.lingua;
        scheda.bilingue = json.bilingue;

		scheda.aswMode  = parseInt(json.mq_modo); //0=VEROFALSO==DoubleMode - 1=QUIZ123=SingleMode
		scheda.qstCount = parseInt(json.mq_quiz);
		scheda.aswCount = parseInt(json.mq_questionquiz);
		scheda.aswMin 	= parseInt(json.mq_minimo);

		for (i = 0; i < scheda.qstCount; ++i) {
            scheda.SetQst(json.quesitiid[i], json.quesiti[i], json.quesitiTra[i], json.imgQuesiti[i]);
        }

        for (i = 0; i < (scheda.aswCount * scheda.qstCount); ++i) {
            var rem = i % scheda.aswCount;
            var qst = (i - rem) / scheda.aswCount;
            scheda.GetQst(qst).SetAsw(json.domandeid[i], json.domande[i], json.domandeTra[i]);
        }

        for (var i = 1; i <= scheda.aswCount; i++) {

            numOpzione = 10 * i + 1;
            objImg = window.document.getElementById('iOpzione' + numOpzione);
            objImg.src = "./img/scheda/V_it.gif";

            objImg = window.document.getElementById('opzione' + numOpzione + 'Check');
            objImg.style.visibility = 'hidden';

            numOpzione = 10 * i + 2;
            objImg = window.document.getElementById('iOpzione' + numOpzione);
            objImg.src = "./img/scheda/F_it.gif";

            objImg = window.document.getElementById('opzione' + numOpzione + 'Check');
            objImg.style.visibility = 'hidden';

            $('#opzione'+i+'1').bind('click',{index:(i+'1')},function(e) {
			     optionClick(e.data.index);
			     selAnswerMin();

			});

			$('#opzione'+i+'2').bind('click',{index:(i+'2')},function(e) {
			     optionClick(e.data.index);
			     selAnswerMin();
			});
        }

        //reset delle risposte date
		resetRisposte(scheda.aswCount * scheda.qstCount);

        iMinuti = 0;
        iSecondi = 10;

        thumbnailviewer.init();
        creaToolbar(0);

        //assegno la prima domanda
        currentQst = 1;
        loadQstM(currentQst);

        creaTabellaRiassunto();

		$(document).unbind('keypress');
        $(document).keypress(function(e)
			{
				switch(e.which)
				{
				case 118:
					optionClick(currentAsw+'1');
				break;
				case 102:
					optionClick(currentAsw+'2');
				break;
				}
				selAnswerMin();
			});

		//countDown();
    });
}

function selAnswerMin() {
    /* avanti automatico sulla ministeriale=>commentato sembra "scomodo"
    if ((currentAsw + 1)>scheda.aswCount)
        setTimeout(avantiClick,nextAswTimeout);
    else
        currentAsw = (currentAsw + 1);
    */
    if (!((currentAsw + 1)>scheda.aswCount))
        currentAsw = (currentAsw + 1);
}



//------------------------------------------------------------------
// Funzioni scheda SIDA
//------------------------------------------------------------------
// JavaScript Document
function IntDiv(quotient) {
	return ( quotient >= 0 ) ? Math.floor(quotient) : Math.ceil(quotient);
}

//costruzione di un elemento risposta
function buildAswElement(idx,txt) {
	var ret;
	ret="<div id=\"iAsw"+idx+"\" class=\"isAsw\">"+
        "<div id=\"iAsw"+idx+"Btn\" class=\"isAswBtn\">"+
		"<div>";

	switch(scheda.aswMode) {
		case eSchedaMode.SingleMode:
			ret = ret + "<span><img id=\"opzione"+idx+"Check\" class=\"opzioneCheck\" src= \"img/scheda/X.gif\"></span>"+
						"<span><img class=\"aswCheck\" id=\"opzione"+idx+"\" src=\"./img/scheda/"+idx+"_it.gif\"/></span>";
		break;
		case eSchedaMode.DoubleMode:
			ret = ret + "<span><img id=\"opzione"+idx+"1Check\" class=\"opzioneCheck\" src= \"img/scheda/X.gif\"></span>"+
					    "<span><img id=\"opzione"+idx+"2Check\" class=\"opzioneCheck\" src= \"img/scheda/X.gif\"></span>"+
						"<span><img class=\"aswCheck\" id=\"opzione"+idx+"1\" src=\"./img/scheda/V_it.gif\"/></span>"+
						"<span><img class=\"aswCheck\" id=\"opzione"+idx+"2\" src=\"./img/scheda/F_it.gif\"/></span>";
		break;
	}

	ret=ret+"</div>"+
            "<div><a id=\"iAswAudioRef"+idx+"\"><img id=\"iAswAudio\" src= \"./img/scheda/audio2.png\"/></a></div></div>"+
            "<div class=\"iAswNumTxt\" id=\"iAswNumTxt"+idx+"\">"+
                "<div class=\"iAswNum\" id=\"iAswNum"+idx+"\">"+idx+"</div>"+
                "<div class=\"iAswTxt\" id=\"iAswTxt"+idx+"\">"+
					"<div class=\"iAswTxtIta\" id=\"iAswTxtIta"+idx+"\"></div>"+
					"<div class=\"iAswTxtTra\" id=\"iAswTxtTra"+idx+"\"></div>"+
				"</div>"+
            "</div>"+
         "</div>";

	return ret;
}

function selAnswerNext(){

	if (!checkAnswers()) {

		if (scheda.aswMode == eSchedaMode.DoubleMode) {
			//deselezione compontente precedente
			$('#' + "iNavBtn" + currentIdx).css({'border': '1px solid #000000'});
			$('#' + 'iAswNum' + currentAsw).css({'background': 'url(../img/scheda/bg_asw_num.jpg)'});
			$('#' + 'iAswTxt' + currentAsw).css({'background': 'url(../img/scheda/bg_asw_txt.jpg)'});
		}

		currentIdx = currentIdx + 1;

		switch(scheda.aswMode) {
			case eSchedaMode.SingleMode:
				if (currentIdx > ((scheda.qstCount) - 1))
					currentIdx = 0;
			break;
			case eSchedaMode.DoubleMode:
				if (currentIdx > ((scheda.aswCount * scheda.qstCount) - 1))
					currentIdx = 0;
			break;
		}

		selAnswer(currentIdx);
	}
	else {
		if (scheda.corrected==false) {
			if (confirm("Test finito, correggo tutte le risposte date?")) {
				correctTest();
			}
		}
		else {
			showSummary();
		}
	}
}

function selAnswerBack(){

	if (!checkAnswers()) {

		//deselezione compontente precedente
		$('#' + "iNavBtn" + currentIdx).css({'border': '1px solid #000000'});
		$('#' + 'iAswNum' + currentAsw).css({'background': 'url(../img/scheda/bg_asw_num.jpg)'});
		$('#' + 'iAswTxt' + currentAsw).css({'background': 'url(../img/scheda/bg_asw_txt.jpg)'});

		currentIdx = currentIdx - 1;

		switch(scheda.aswMode) {
			case eSchedaMode.SingleMode:
				if (currentIdx < 0)
					currentIdx = ((scheda.qstCount) - 1);
			break;
			case eSchedaMode.DoubleMode:
				if (currentIdx < 0)
					currentIdx = ((scheda.aswCount * scheda.qstCount) - 1);
			break;
		}

		selAnswer(currentIdx);
	}
	else {
		if (scheda.corrected==false) {
			if (confirm("Test finito, correggo tutte le risposte date?")) {
				correctTest();
			}
		}
		else {
			showSummary();
		}
	}
}

function showSummary() {
	$('#iNavigation').css({'height':'0px'});

	$('#iBodySumm').width($('#iBody').width());
	$('#iBodySumm').height($('#iBody').height());
	/*
	$('#iBodySumm').offset($('#iBody').offset());
	*/
    $('#iBodySumm').css({'top':'0px'});
	$('#iBodySumm').css({'visibility':'visible'});
}

function showRemarks(idx) {

	if (commenti[idx]) {
		$('#iRemarks').html(commenti[idx]);
		$('#iRemarks').css({'visibility':'visible'});
	}
	else {
		$('#iRemarks').html('');
		$('#iRemarks').css({'visibility':'hidden'});
	}

	var imgPath = fig_commenti[idx];
  	var imgText = "";
  	if ((typeof imgPath!="undefined") && (imgPath!=""))
  		imgText = imgPath.substring(imgPath.lastIndexOf("/")+1);

  	if (imgText!="") {
		$('#iFigRem').css({'visibility':'visible'});
		$('#iFigRem').attr({'src': fig_commenti[idx]});
		$('#iCommLink').attr({'href': fig_commenti[idx] });
		$('#iRemImgFig').html('Figura n.'+imgText.substring(0,imgText.lastIndexOf("G")));
  	}
  	else {
		$('#iFigRem').css({'visibility':'hidden'});
		$('#iFigRem').attr({'src': ''});
		$('#iCommLink').attr({'href': '' });
		$('#iRemImgFig').html('');
  	}
}
function selAnswer(idx) {

	var qst,asw;

	//logica selezione domanda
	switch(scheda.aswMode) {
		case eSchedaMode.SingleMode:
			qst=idx+1;
			asw=1;

			//deselezione/selezione navigazione
			$('#iNavBtn'+(currentQst-1)).css({'border':'1px solid #000000'});
			$('#iNavBtn'+idx).css({'border':'1px solid #ffff00'});
		break;
		case eSchedaMode.DoubleMode:
			qst = (IntDiv(idx/scheda.aswCount)+1);
			asw = (idx%scheda.aswCount)+1;

			//deselezione/selezione navigazione
			$('#iNavBtn'+currentIdx).css({'border':'1px solid #000000'});
			$('#iNavBtn'+idx).css({'border':'1px solid #ffff00'});
		break;
	}

	if (!scheda.finished && scheda.aswMode == eSchedaMode.DoubleMode) {
		$('#iAswNum' + currentAsw).css({'background': 'url(../img/scheda/bg_asw_num.jpg)'});
		$('#iAswTxt' + currentAsw).css({'background': 'url(../img/scheda/bg_asw_txt.jpg)'});

		$('#iAswNum' + asw).css({'background': 'url(../img/scheda/bg_asw_num_sel.jpg)'});
		$('#iAswTxt' + asw).css({'background': 'url(../img/scheda/bg_asw_txt_sel.jpg)'});
	}
	else {
		showRemarks(idx);
	}

	if (currentAsw!=asw)
		currentAsw=asw;

	if (currentQst != qst)
		loadQst(qst);

	if(currentIdx!=idx)	{
		currentIdx=idx;
	}
}

//selezione della risposta relativa sempre alla stessa domanda (non dalla navigazione)
//idx 0-indexed (0,1,2)
function selAnswerLiteDouble(idx) {
	return;

	var newIdx = ((currentQst-1)*scheda.aswCount)+idx;

	if (idx != (currentIdx % scheda.aswCount)) {

		//deselezione/selezione navigazione
		$('#iNavBtn' + currentIdx).css({'border': '1px solid #000000'});
		$('#iNavBtn' + newIdx).css({'border': '1px solid #ffff00'});

		if (!scheda.finished) {
			$('#iAswNum' + currentAsw).css({'background': 'url(../img/scheda/bg_asw_num.jpg)'});
			$('#iAswTxt' + currentAsw).css({'background': 'url(../img/scheda/bg_asw_txt.jpg)'});


			//deselzione/selezione sfondo risposta
			$('#iAswNum' + (idx + 1)).css({'background': 'url(../img/scheda/bg_asw_num_sel.jpg)'});
			$('#iAswTxt' + (idx + 1)).css({'background': 'url(../img/scheda/bg_asw_txt_sel.jpg)'});
		}

		if (currentAsw != (idx + 1))
			currentAsw = (idx + 1);
		if (currentIdx != newIdx)
			currentIdx = newIdx;
	}
	else {
		if (scheda.finished)
			showRemarks(idx);
	}

	$('#iNavBtn'+newIdx).css({'background':'#ffd42b'});
}

function selAnswerLiteSingle(idx) {
	if (currentAsw != (idx + 1))
		currentAsw = (idx + 1);

	$('#iNavBtn'+(currentQst-1)).css({'background':'#ffd42b'});
}


function restyleNavBar(countBtns) {

	for(i=0;i<countBtns;i++) {
		var risp=risposte[i];

		if (risp.charAt(0)==NESSUNA) {
			$('#iNavBtn' + i).css({'background': '#d4d4d4'});
			$('#iNavBtn' + i).css({'border': '1px solid #000000'});
		}

		if (risp.charAt(1)==CORRETTA)
			$('#iNavBtn'+i).css({'background':'#00ff00'});

		if (risp.charAt(1)==ERRATA)
			$('#iNavBtn'+i).css({'background':'#ff0000'});
	}
}

function makeNavBar(countBtns) {

	var offMain = $('#iNavigation').offset();

	//impostati di default, width e height non sono ancora valorizzati
	var w = 996; //$('#iNavigation').width();
	var h = 34;  //$('#iNavigation').height();

	var pmarg_y = 0.6;

    var perc  = 20;
    var perc2 = 20;

    var sp11,sp12;
    var sp21,sp22;
	var singleCellW,singleBtn;

    //primo calcolo
    singleCellW = IntDiv(w/scheda.qstCount);
	sp12 = IntDiv((w-(singleCellW*scheda.qstCount))/2);
	sp11 = IntDiv((singleCellW*perc/200));

	singleCellW = singleCellW - (sp11*2);

	switch(scheda.aswMode) {
		case eSchedaMode.SingleMode:
			//tutto azzero vale solo il primo calcolo
			singleBtn = singleCellW;
			sp22 = 0;
			sp21 = 0;
		break;
		case eSchedaMode.DoubleMode:
			singleBtn = IntDiv(singleCellW/scheda.aswCount);
			sp22 = IntDiv((singleCellW-(singleBtn*scheda.aswCount))/2);
			sp21 = IntDiv((singleBtn*perc2/200));

			singleBtn = singleBtn - (sp21*2);
		break;
	}

	var off_x=sp12 - (sp11+sp22+sp21);

	for(i=0;i<countBtns;i++) {
		var idBtn = "iNavBtn"+i;

		var t ="<div id=\""+idBtn+"\" class=\"iNavBtn\" ></div>";

		var mar_y = IntDiv(h*pmarg_y);
		var off_y = IntDiv(mar_y/2);
		var heightBtn = h-mar_y;

		$('#iNavigation').append(t);

		//dimensionamento
		$('#'+idBtn).height(heightBtn);
		$('#'+idBtn).width(singleBtn);

		if ((i%scheda.aswCount)==0 || (scheda.aswMode==eSchedaMode.SingleMode))
            off_x=off_x+((sp11+sp22+sp21)*2);
        else
            off_x=off_x+sp21+sp21;

		//posizionamento
		$('#'+idBtn).offset({ top: offMain.top+off_y, left: offMain.left+off_x });

		off_x = off_x + singleBtn;

		$('#'+idBtn).bind('click',{index:i},function(e) {
			selAnswer(e.data.index);
		});
	}
}

function loadQst(numQst) {

	currentQst = numQst;

	var idx=numQst-1;

	$('#iQstNum').text(numQst);
	$('#iQstTxtIta').text(scheda.GetQst(idx).txt);
	$('#iQstTxtTra').text(scheda.GetQst(idx).tra);
	$('#iQstAudioRef').attr({
		'href': 'javascript:playAudio(\'/ecom.dll/audio?type=1&list=' + scheda.listato + '&lang=' + scheda.lingua + '&id=' + scheda.GetQst(idx).idqst + '\')'
	});

	if (scheda.rtlMode())
	 	$('#iQstTxtTra').css({'direction':'rtl','text-align':'right'});

	for (var i = 1; i <= scheda.aswCount; i++) {
		$('#iAswTxtIta' + i).text(scheda.GetQst(idx).GetAsw(i - 1).txt);
		$('#iAswTxtTra' + i).text(scheda.GetQst(idx).GetAsw(i - 1).tra);
		$('#iAswAudioRef'+i).attr({
			'href': 'javascript:playAudio(\'/ecom.dll/audio?type=2&list=' + scheda.listato + '&lang=' + scheda.lingua + '&id=' + scheda.GetQst(idx).GetAsw(i - 1).idasw + '\')'
		});

		//per le schede a modalità singola tutte le risposte sono selezionate
		if (scheda.aswMode == eSchedaMode.SingleMode) {
			$('#iAswNum' + (i)).css({'background': 'url(../img/scheda/bg_asw_num_sel.jpg)'});
			$('#iAswTxt' + (i)).css({'background': 'url(../img/scheda/bg_asw_txt_sel.jpg)'	});
		}

		if (scheda.rtlMode())
			$('#iAswTxtTra' + i).css({'direction':'rtl','text-align':'right'});

		if (scheda.finished) {

			switch(scheda.aswMode) {
				case eSchedaMode.SingleMode:
					var risp = risposte[currentQst - 1];

					if (risp.charAt(1)==CORRETTA && (parseInt(risp.charAt(0))==i)) {
						$('#iAswNum' + i).css({'background': 'url(../img/scheda/bg_asw_num_ok.jpg)'});
						$('#iAswTxt' + i).css({'background': 'url(../img/scheda/bg_asw_txt_ok.jpg)'});
					}

					if (risp.charAt(1)==ERRATA) {
						if (parseInt(risp.charAt(0))==i) {
							$('#iAswNum' + i).css({'background': 'url(../img/scheda/bg_asw_num_ko.jpg)'});
							$('#iAswTxt' + i).css({'background': 'url(../img/scheda/bg_asw_txt_ko.jpg)'});
						}
						if (parseInt(risp.charAt(2))==i) {
							$('#iAswNum' + i).css({'background': 'url(../img/scheda/bg_asw_num_ok.jpg)'});
							$('#iAswTxt' + i).css({'background': 'url(../img/scheda/bg_asw_txt_ok.jpg)'});
						}
					}
				break;
				case eSchedaMode.DoubleMode:
					//calcolo dell'indice relativo alla risposta data
					var rispid = ((currentQst - 1) * scheda.aswCount) + (parseInt(i,10)-1);

					if (risposte[rispid] == (VERO + CORRETTA) || risposte[rispid] == (FALSO + CORRETTA) || risposte[rispid] == ('1' + CORRETTA) || risposte[rispid] == ('2' + CORRETTA) || risposte[rispid] == ('3' + CORRETTA)) {
						$('#iAswNum' + i).css({'background': 'url(../img/scheda/bg_asw_num_ok.jpg)'});
						$('#iAswTxt' + i).css({'background': 'url(../img/scheda/bg_asw_txt_ok.jpg)'});
					}

					if (risposte[rispid] == (VERO + ERRATA) || risposte[rispid] == (FALSO + ERRATA) || risposte[rispid] == (NESSUNA + ERRATA) || risposte[rispid] == ('1' + ERRATA) || risposte[rispid] == ('2' + ERRATA) || risposte[rispid] == ('3' + ERRATA)) {
						$('#iAswNum' + i).css({'background': 'url(../img/scheda/bg_asw_num_ko.jpg)'});
						$('#iAswTxt' + i).css({'background': 'url(../img/scheda/bg_asw_txt_ko.jpg)'});
					}
				break;
			}
		}

		if (scheda.aswMode==eSchedaMode.DoubleMode)
			viewAnswersDouble((idx*scheda.aswCount)+(i-1));
	}

	if (scheda.aswMode==eSchedaMode.SingleMode)
		viewAnswersSingle(idx);

	var imgPath = scheda.GetQst(idx).fig.src;
  	var imgText = imgPath.substring(imgPath.lastIndexOf("/")+1);

  	if (imgText!="") {
		$('#iQuesito').css({'visibility':'visible'});
		$('#iQuesito').attr({'src': scheda.GetQst(idx).fig.src});
		$('#iBodyImgFig').html('Figura n.'+imgText.substring(0,imgText.lastIndexOf("G")));
		$('#iQuesitoLink').attr({'href': scheda.GetQst(idx).fig.src });
  	}
  	else {
		$('#iQuesito').css({'visibility':'hidden'});
		$('#iQuesito').attr({'src': ''});
		$('#iBodyImgFig').html('');
		$('#iQuesitoLink').attr({'href': '' });
  	}
}

//numOpzione es: 11 o 12 o 21...
function optionClick(numOpzione){

	numOpzione= "" + numOpzione;

	if (!scheda.finished)
		storeAnswer(numOpzione.substr(0,1), numOpzione.substr(1,1));

	vis = $('#opzione' + numOpzione + 'Check').css('visibility');

	if ((vis=='')||(vis=='hidden')) {

		$('#opzione' + numOpzione + 'Check').css({'visibility': 'visible'});

		switch(scheda.aswMode) {
			case eSchedaMode.SingleMode:
				if (numOpzione!=currentAsw && !scheda.finished)
					$('#opzione' + currentAsw + 'Check').css({'visibility': 'hidden'});
			break;
			case eSchedaMode.DoubleMode:
				numOpzione=parseInt(numOpzione,10);
				resto = numOpzione%2;
				if (resto==1)
					numOpzione=numOpzione+1;

				if (resto==0)
					numOpzione=numOpzione-1;

				$('#opzione' + numOpzione + 'Check').css({'visibility': 'hidden'});
			break;
		}
	}

	if (!scheda.corrected)
		$('#iNavBtn'+currentIdx).css({'background':'#ffd42b'});

	//solo per la scheda ministeriale c'e' questa evidenziazione del controllo
    if (tipo_scheda==1 && allAnswers()) {
		$('#bottoneToolbar'+(currentQst-1)).css({'background':colorToolbar});
		$('#linkToolbar'+(currentQst-1)).css({'color':'white'});
	}
}

function allAnswers(){
  var j = parseInt((currentQst-1)*scheda.aswCount);
  for (var i=0; i<scheda.aswCount;i++){
    if (risposte[j+i]==NESSUNA){
      return(false);
    }
  }
  return(true);
}

//memorizzazione della risposta data nell'array delle risposte
function storeAnswer(numDom, vero) {

	switch(scheda.aswMode) {
			case eSchedaMode.SingleMode:
				risposte[currentQst-1]= numDom;
			break;
			case eSchedaMode.DoubleMode:
				//calcolo dell'indice relativo alla risposta data
				idxAsw = ((currentQst-1) * scheda.aswCount) + (parseInt(numDom,10)-1);
				risposte[idxAsw]= (vero=='1') ? VERO : FALSO;
			break;
		}
}

//verifica se sono state date tutte le risposte
function checkAnswers() {
	for(i=0; i<(scheda.aswCount * scheda.qstCount); i++)
		if (risposte[i]==NESSUNA)
			return false;

	return true;
}

function viewAnswersSingle(idx){

	var risp=risposte[idx];

	for(i=1;i<=scheda.aswCount;i++)
		$('#opzione' + i + 'Check').css({'visibility': 'hidden'});

	if (risp.charAt(0) == '1' || risp.charAt(2) == '1')
		optionClick('1');

	if (risp.charAt(0) == '2' || risp.charAt(2) == '2')
		optionClick('2');

	if (risp.charAt(0) == '3' || risp.charAt(2) == '3')
		optionClick('3');



}
//visualizzazione delle risposte date in funzione dei valore contenuti nell'
//array delle risposte
function viewAnswersDouble(idx){
	var asw = (idx%scheda.aswCount)+1;

  	if (risposte[idx]==NESSUNA){
		$('#opzione'+asw+'1Check').css({'visibility':'hidden'});
		$('#opzione'+asw+'2Check').css({'visibility':'hidden'});
	}

	var risp=risposte[idx];

  	if (risp.charAt(0) == 'V') {
  		optionClick(asw+'1');
  	}

  	if (risp.charAt(0) == 'F') {
  		optionClick(asw+'2');
  	}
}

//correzione del test completato
function correctTest() {

	scheda.corrected = true;
	iErrate=0;
  	iCorrette=0;

  	$.getJSON("/ecom.dll/schedacorr", { risposte: risposte }, function(json) {

		//flag termine test
		scheda.finished=true;

		for (i=0; i<risposte.length; ++i) {
		  if (json.risposte[i]==CORRETTA) {
		    risposte[i] = risposte[i] + json.risposte[i];
		    iCorrette++;
		  }
		  else {
		    risposte[i] = risposte[i] + json.risposte[i];
		    iErrate++;
		  }
		  commenti[i] = json.commenti[i];
		  fig_commenti[i] = json.fig_commenti[i];
		}

		//formattazione esito
		$('#iSummCounter1').text('Risposte corrette:'+iCorrette);
		$('#iSummCounter2').text('Risposte errate:'+iErrate);

		if (iCorrette < scheda.aswMin) {
			$('#iSummResult2').text('NON SUFFICIENTE');
			$('#iSummResult3').text('è necessario seguire le lezioni a scuola, leggere bene i commenti agli errori e continuare ad esercitarsi');
		}
		else {
			$('#iSummResult2').text('SUFFICIENTE');
			$('#iSummResult3').text('congratulazioni, è necessario continuare ad esercitarsi con costanza per completare il programma di studio');
		}

		switch(scheda.aswMode) {
			case eSchedaMode.SingleMode:
				navBtnCount = scheda.qstCount;
			break;
			case eSchedaMode.DoubleMode:
				navBtnCount = scheda.aswCount*scheda.qstCount;
			break;
		}

		selAnswer(0);

		restyleNavBar(navBtnCount);

		alert("Clicca i pulsanti rossi sulla barra per la visione degli errori.");
	});
}

function unloadTest() {
	currentAsw = 0;
	currentQst = 0;
	currentIdx = 0;
}

function resetRisposte(count) {
	for (var i = 0; i < count; i++)
		risposte[i] = NESSUNA;
}

function exitTest() {

	if (!scheda.corrected) {
		if (confirm("Il test non è ancora finito, correggo comunque la scheda?"))
			correctTest();
		return;
	}
	if (!scheda.finished) {
		if (confirm("Il test non è ancora finito, esco comunque?")) {
			unloadTest();
			show_schede(1);
		}
	}
	else {
		if (confirm("Hai terminato la lettura? Esco?")) {
			unloadTest();
			show_schede(1);
		}
	}
}

function loadTest(tipoScheda,numSchede,argoCount,bitMaskArgo) {

	initTest();

	$.getJSON("/ecom.dll/scheda",{ tipo:tipoScheda, num:numSchede, argoCount:argoCount, mask:bitMaskArgo }, function(json) {

		$('#usType').html(json.numeroScheda);
		$('#usCand').html(json.nomeCandidato);

		scheda.listato  = json.listato;
		scheda.tipolist = json.tipolistato;
		scheda.lingua   = json.lingua;
		scheda.bilingue = json.bilingue;

		scheda.aswMode  = parseInt(json.mq_modo); //0=VEROFALSO==DoubleMode - 1=QUIZ123=SingleMode
		scheda.qstCount = parseInt(json.mq_quiz);
		scheda.aswCount = parseInt(json.mq_questionquiz);
		scheda.aswMin 	= parseInt(json.mq_minimo);

		for (i=0; i<scheda.qstCount; ++i) {
			scheda.SetQst(json.quesitiid[i],json.quesiti[i],json.quesitiTra[i],json.imgQuesiti[i]);
		}

		for (i=0; i<(scheda.aswCount * scheda.qstCount); ++i) {
			var rem = i%scheda.aswCount;
			var qst = (i-rem)/scheda.aswCount;
			scheda.GetQst(qst).SetAsw(json.domandeid[i],json.domande[i],json.domandeTra[i]);
		}

		$('#AswGrouper').empty();
		for (var i=1; i<=scheda.aswCount;i++) {
			$('#AswGrouper').append(buildAswElement(i,i));

			switch(scheda.aswMode) {
				case eSchedaMode.SingleMode:
					$('#opzione'+i+'Check').height($('#opzione'+i).height());
					$('#opzione'+i+'Check').css({'visibility': 'hidden'});

					$('#opzione'+i).bind('click',{index:(i)},function(e) {
						optionClick(e.data.index);
						setTimeout(selAnswerNext,nextAswTimeout);
					});
				break;
				case eSchedaMode.DoubleMode:

					//restyling scheda AB 2011
					if (scheda.tipolist==eTipoListato.TIPOLISTATO_ABNEW) {

						//restyle proporzioni del corpo scheda
						$('#iBodyImg').css({'width':'330px'});
						$('#iBodyQst').css({'margin':'0 0 0 330px'});

						//restyle immagine principale
						$('#iMainImg').css({
											'left':'75px',
											'top':'70px'
										   });


						//restyle immagine/area commenti
						$('#iRemarks').css({'height':'100px'});

						$('#iRemImg').css({
										   'left':'115px',
										   'bottom':'35px',
										   'height':'100px',
										   'width':'100px'
										  });

						$('#iFigRem').css({
										   'width':'100px',
										   'height':'100px'
										  });

						//restyle "X"
						$('img.aswCheck').css({'height':'55px'});

						//restyle area domanda
						$('#iQst').css({'height':'50px'});

						$('div.isAsw').css({'height':'210px'});
						$('div.iAswTxt').css({'height':'210px'});
						$('div.iAswNum').css({'height':'210px'});

						$('div.iAswTxtIta').css({'margin-right':'120px'});
						$('div.iAswTxtIta').css({'font-size':'20pt'});

						$('#iQstNum').css({
										   'font-size':'30pt',
										   'right':'40px'
										  });


						$('.iFigQuesito').css({'text-align':'center'});
						$('div.isAswBtn').css({'top':'145px'});

						$('.iAswNum').html('');

						$('#iQstAudio').remove();
						$('#iQstTxtIta').remove();
						$('#iQstTxtTra').remove();
						$('#iAswAudio').remove();
					}

					$('#opzione'+i+'1Check').height($('#opzione'+i+'1').height());
					$('#opzione'+i+'1Check').width($('#opzione'+i+'1').width());

					offAsw = $('#opzione'+i+'1').offset();
					$('#opzione'+i+'1Check').offset({ top: offAsw.top, left: offAsw.left });

					$('#opzione'+i+'2Check').width($('#opzione'+i+'2').width());
					$('#opzione'+i+'2Check').height($('#opzione'+i+'2').height());

					offAsw = $('#opzione'+i+'2').offset();
					$('#opzione'+i+'2Check').offset({ top: offAsw.top, left: offAsw.left });

					$('#opzione'+i+'1Check').css({'visibility': 'hidden'});
					$('#opzione'+i+'2Check').css({'visibility': 'hidden'});

					$('#opzione'+i+'1').bind('click',{index:(i+'1')},function(e) {
						optionClick(e.data.index);
						if ((currentAsw + 1)>scheda.aswCount)
        					setTimeout(selAnswerNext,nextAswTimeout);
    					else
							selAnswerNext();
					});

					$('#opzione'+i+'2').bind('click',{index:(i+'2')},function(e) {
						optionClick(e.data.index);
						if ((currentAsw + 1)>scheda.aswCount)
        					setTimeout(selAnswerNext,nextAswTimeout);
    					else
							selAnswerNext();
					});
				break;
			}
		}

		var navBtnCount = 0;

		switch(scheda.aswMode) {
			case eSchedaMode.SingleMode:
				navBtnCount = scheda.qstCount;
			break;
			case eSchedaMode.DoubleMode:
				navBtnCount = scheda.aswCount*scheda.qstCount;
			break;
		}

		resetRisposte(navBtnCount);

		makeNavBar(navBtnCount);
		restyleNavBar(navBtnCount);

		selAnswer(0); //0-index
		//motore di zoom dei segnali
    	thumbnailviewer.init();

		scheda.finished=false;

		$(document).unbind('keypress');
		$(document).keypress(function(e) {
			switch(e.which)
			{
			case 118:
				optionClick(currentAsw+'1');
				if ((currentAsw + 1)>scheda.aswCount)
    				setTimeout(selAnswerNext,nextAswTimeout);
				else
					selAnswerNext();
			break;
			case 102:
				optionClick(currentAsw+'2');
				if ((currentAsw + 1)>scheda.aswCount)
    				setTimeout(selAnswerNext,nextAswTimeout);
				else
					selAnswerNext();
			break;
			case 49:
				optionClick(1);
				setTimeout(selAnswerNext,nextAswTimeout);
				break;
			case 50:
				optionClick(2);
				setTimeout(selAnswerNext,nextAswTimeout);
				break;
			case 51:
				optionClick(3);
				setTimeout(selAnswerNext,nextAswTimeout);
			break;
			}
		});
    });
}


