﻿/**************************************************************
*
* CLASSE JAVASCRIPT ENQUETE
*
* CRIADO POR: CRISTIANO TAVELLA
* DATA: 24/06/2011
* OBS:
* EDITADO POR: CRISTIANO TAVELLA
* DATA: 24/06/2011
* OBS:
*
*************************************************************/

function responder() {
    if (typeof ($("input[name='enquete']:checked").val()) == "undefined") {
        alert("Selecione uma resposta antes de Enviar.")
        $("input[name='enquete']").focus();
        return;
    }

    var v_layout = this.layout;
    //perguntas
    v_layout = v_layout.replace(/\[pergunta\]/g, this.dados.pergunta);
    //respostas
    var v_resposta = "", v_resp1, v_resp2, v_resp3, v_resp4, v_resp5;

    eval("this.dados.votos_resposta" + $("input[name='enquete']:checked").val() + "++");
    eval("var v_value = this.dados.votos_resposta" + $("input[name='enquete']:checked").val() + ";");

    //grava resposta
    ajax({ action: "setEnquete", codigo_enq: this.dados.codigo_enq, resposta: "votos_resposta" + $("input[name='enquete']:checked").val(), value: v_value }, function (retorno) {  });

    var total = this.dados.votos_resposta1 + this.dados.votos_resposta2 + this.dados.votos_resposta3 + this.dados.votos_resposta4 + this.dados.votos_resposta5;

    this.dados.votos_resposta1 = Math.round(this.dados.votos_resposta1 * 100 / total);
    this.dados.votos_resposta2 = Math.round(this.dados.votos_resposta2 * 100 / total);
    this.dados.votos_resposta3 = Math.round(this.dados.votos_resposta3 * 100 / total);
    this.dados.votos_resposta4 = Math.round(this.dados.votos_resposta4 * 100 / total);
    this.dados.votos_resposta5 = Math.round(this.dados.votos_resposta5 * 100 / total);

    if (this.dados.resposta1 != "")
        v_resposta += "<div style='float:left; color:#999; font-weight:bold;'>" + this.dados.resposta1 + ":</div><div style='float:right; font-weight:bold;'>" + this.dados.votos_resposta1 + " %</div><br clear='all' />";
    if (this.dados.resposta2 != "")
        v_resposta += "<div style='float:left; color:#999; font-weight:bold;'>" + this.dados.resposta2 + ":</div><div style='float:right; font-weight:bold;'>" + this.dados.votos_resposta2 + " %</div><br clear='all' />";
    if (this.dados.resposta3 != "")
        v_resposta += "<div style='float:left; color:#999; font-weight:bold;'>" + this.dados.resposta3 + ":</div><div style='float:right; font-weight:bold;'>" + this.dados.votos_resposta3 + " %</div><br clear='all' />";
    if (this.dados.resposta4 != "")
        v_resposta += "<div style='float:left; color:#999; font-weight:bold;'>" + this.dados.resposta4 + ":</div><div style='float:right; font-weight:bold;'>" + this.dados.votos_resposta4 + " %</div><br clear='all' />";
    if (this.dados.resposta5 != "")
        v_resposta += "<div style='float:left; color:#999; font-weight:bold;'>" + this.dados.resposta5 + ":</div><div style='float:right; font-weight:bold;'>" + this.dados.votos_resposta5 + " %</div><br clear='all' />";

    v_resposta += "<br clear='all' /><br clear='all' /><div style='font-weight:bold; color:#F00; text-align:center;'>Obrigado pelo seu voto!</div>";

    v_layout = v_layout.replace(/\[respostas\]/g, v_resposta);

    
    $(this.btnResponder).hide();
    
}

function imprimir() {
    if (this.dados.codigo_enq == "") {
        $(this.destino).html("<div style=' width:120px; text-align:center; margin:40px auto;'><b>Nenhuma enquete cadastrada</b></div>");
        return;
    }

    var v_layout = this.layout;
    //perguntas
    v_layout = v_layout.replace(/\[pergunta\]/g, this.dados.pergunta);
    //respostas
    var v_resposta = "";
    if (this.dados.resposta1 != "")
        v_resposta += "<label><input type='radio' name='enquete' value='1' />" + this.dados.resposta1 + "</label>";
    if (this.dados.resposta2 != "")
        v_resposta += "<label><input type='radio' name='enquete' value='2' />" + this.dados.resposta2 + "</label>";
    if (this.dados.resposta3 != "")
        v_resposta += "<label><input type='radio' name='enquete' value='3' />" + this.dados.resposta3 + "</label>";
    if (this.dados.resposta4 != "")
        v_resposta += "<label><input type='radio' name='enquete' value='4' />" + this.dados.resposta4 + "</label>";
    if (this.dados.resposta5 != "")
        v_resposta += "<label><input type='radio' name='enquete' value='5' />" + this.dados.resposta5 + "</label>";

    v_layout = v_layout.replace(/\[respostas\]/g, v_resposta);
   
    $(this.destino).html(v_layout);

    var v_this = this;

    $(this.btnResponder).click(function () { v_this.responder(); });


}

function Enquete(parametros) {
    // Código fkempresa
    this.fkempresa = parametros.fkempresa;
    //Id de destino 
    this.destino = parametros.destino;
    //Layout
    this.layout = parametros.layout;
    //recebe dados
    this.dados = parametros.dados;
    //elemento que acionará a resposta
    this.btnResponder = parametros.btnResponder;

    //Métodos
    this.imprimir = imprimir;
    this.responder = responder;

    this.imprimir();    
}

