﻿
var i = 0;
var currQuestionIndex = -1;
function Intreb(id, intrebare) {
    // initialize the member variables for this instance
    this.id = id;
    this.intrebare = intrebare;
    this.optiune1 = "";
    this.optiune2 = "";
    this.optiune3 = "";
    this.optiune4 = "";
    this.optiune5 = "";
    this.raspuns = -10;
    this.importanta = -10;
}
var intrebari = new Array();
function fakeFunction(arg, context) {
    document.getElementById('divDetalii').innerHTML = arg;
}
function toggleLayer(whichLayer, value) {
    var elem, vis;
    if (document.getElementById) // this is the way the standards work
        elem = document.getElementById(whichLayer);
    else if (document.all) // this is the way old msie versions work
        elem = document.all[whichLayer];
    else if (document.layers) // this is the way nn4 works
        elem = document.layers[whichLayer];
    vis = elem.style;
    // if the style.display value is blank we try to figure it out here

    vis.display = value ? 'block' : 'none';
}
function answer(id) {
    for (var i = 1; i <= 5; i++)
        document.getElementById("lnkQuestion" + i).className = 'unselected';
    var elt = document.getElementById("lnkQuestion" + id);
    var cn = elt.className;
    elt.className = cn == 'selected' ? 'unselected' : 'selected';
    intrebari[currQuestionIndex].raspuns = id - 3;
    nextQuestion();
}
function answerImp(id) {
    for (var i = 1; i <= 3; i++)
        document.getElementById("lnkImp" + i).className = 'unselected';
    var elt = document.getElementById("lnkImp" + id);
    var cn = elt.className;
    elt.className = cn == 'selected' ? 'unselected' : 'selected';
    intrebari[currQuestionIndex].importanta = id;
    nextQuestion();
}
function validateNext() {

    if (currQuestionIndex == -1) {
        if(intrebari)
            document.getElementById("lblCount").innerHTML = intrebari.length;
        currQuestionIndex++;
        return true;
    }
    if (currQuestionIndex >= intrebari.length - 1) {
        //var message = "Nu mai exista intrebari";
        saveAnswers();
        return false;
    }
    if (intrebari[currQuestionIndex].raspuns != -10 && intrebari[currQuestionIndex].importanta != -10) {
        showError(false, '');
        currQuestionIndex++;
        return true;
    }

    var message = '';

    if (intrebari[currQuestionIndex].raspuns == -10) {
        message = "Selectati un raspuns pentru a merge la urmatoarea intrebare";
    } else
        message = "Selectati gradul de importanta pentru a merge la urmatoarea intrebare";
    showError(true, message);
    return false;
}
function saveAnswers() {
    var b = true;
    var neraspuns=new Array();
    for (var i = 0; i < intrebari.length; i++) {
        if (intrebari[i].raspuns == -10 || intrebari[i].importanta == -10) {
            b = false;
            if (i != intrebari.length - 1)
                neraspuns.push(i);
        }
    }
    if (neraspuns.length > 0) {
        var message = 'Nu ati raspuns la intrebarile: ';
        for (var j = 0; j < neraspuns.length - 1; j++)
            message += (neraspuns[j] + 1) + ', ';
        if (neraspuns.length > 0)
            message += (neraspuns[j] + 1) + '.';
        showError(true, message);
    }
    var response = "";
    if (b) {
        for (var j = 0; j < intrebari.length; j++) {
            response = response + intrebari[j].id + "," + intrebari[j].raspuns + "," + intrebari[j].importanta + " ";
        }
        if (Get_Cookie("Raspunsuri")) {
            Delete_Cookie("Raspunsuri");
        }
        Set_Cookie("Raspunsuri", response, 1);
        window.location = document.getElementById("hfRedirectUrl").value;
    }
}
function showError(value, message) {
    document.getElementById("spEroare").className = value ? "eroareTextVizibil" : "eroareText";
    document.getElementById("imgEroare").className = value ? "eroareImagineVizibil" : "eroareImagine";
    document.getElementById("spEroare").innerHTML = message;
}
function nextQuestion() {

    if (validateNext()) {
        for (var i = 1; i <= 5; i++)
            document.getElementById("lnkQuestion" + i).className = 'unselected';
        for (var i = 1; i <= 3; i++)
            document.getElementById("lnkImp" + i).className = 'unselected';
        displayOrHidePrevBtn();
        setRepeater();
        setOptiuni();
        var intr = intrebari[currQuestionIndex].intrebare;
        var sp = document.getElementById('spIntrebare');
        sp.innerHTML = intr;
        if (intrebari[currQuestionIndex].raspuns != -10) {
            if (document.getElementById("lnkQuestion") + (intrebari[currQuestionIndex].raspuns + 3)) {
                document.getElementById("lnkQuestion" + (intrebari[currQuestionIndex].raspuns + 3)).className = "selected";
            }
            if (document.getElementById("lnkImp") + (intrebari[currQuestionIndex].importanta)) {
                document.getElementById("lnkImp" + (intrebari[currQuestionIndex].importanta)).className = "selected";
            }
        }
        //if (currQuestionIndex < intrebari.length - 1)
        //    return false;
    }

}

function prevQuestion() {
    showError(false);
    currQuestionIndex--;
    for (var i = 1; i <= 5; i++)
        document.getElementById("lnkQuestion" + i).className = 'unselected';
    for (var i = 1; i <= 3; i++)
        document.getElementById("lnkImp" + i).className = 'unselected';
    if (intrebari[currQuestionIndex].raspuns != -10) {
        if (document.getElementById("lnkQuestion") + (intrebari[currQuestionIndex].raspuns + 3)) {
            document.getElementById("lnkQuestion" + (intrebari[currQuestionIndex].raspuns + 3)).className = "selected";
        }
        if (document.getElementById("lnkImp") + (intrebari[currQuestionIndex].importanta)) {
            document.getElementById("lnkImp" + (intrebari[currQuestionIndex].importanta)).className = "selected";
        }
    }
    displayOrHidePrevBtn();
    setRepeater();
    setOptiuni();
    var intr = intrebari[currQuestionIndex].intrebare;
    var sp = document.getElementById('spIntrebare');
    sp.innerHTML = intr;
}
function displayOrHidePrevBtn() {
    if (currQuestionIndex < 1) {
        toggleLayer('lnkPrecedenta', false);
    } else {
        toggleLayer('lnkPrecedenta', true);
    }
}
function setOptiuni() {
    document.getElementById('lnkQuestion1').innerHTML = intrebari[currQuestionIndex].optiune1;
    document.getElementById('lnkQuestion2').innerHTML = intrebari[currQuestionIndex].optiune2;
    document.getElementById('lnkQuestion3').innerHTML = intrebari[currQuestionIndex].optiune3;
    document.getElementById('lnkQuestion4').innerHTML = intrebari[currQuestionIndex].optiune4;
    document.getElementById('lnkQuestion5').innerHTML = intrebari[currQuestionIndex].optiune5;
    var name = document.getElementById('hfCurrQueId').value;
    document.getElementById(name).value = currQuestionIndex;
}
function setRepeater() {
    document.getElementById("lblNoQuestion").innerHTML = (currQuestionIndex + 1) + "";
    for (var i = 0; i < intrebari.length; i++) {
        var elt = document.getElementById("liElement" + i);
        if (i == currQuestionIndex)
            elt.className = "current";
        else
            if (intrebari[i].raspuns == -10 && intrebari[i].raspuns == -10)
            elt.className = "todo";
        else
            elt.className = "done";
    }
}
