﻿function voteaza(pollID) {
    if (pollID == 0) {
        alert("Votul dvs. a fost deja inregistrat pentru acest sondaj.");
    } else {
        var selected = false;
        var vote = 0;
        var votes = '';

        for (var i = 0; i < document.forms[0].elements.length; i++) {
            if (document.forms[0].elements[i].name == "rbPoll" + pollID + "_Answers" && document.forms[0].elements[i].checked) {
                vote = document.forms[0].elements[i].value;
                selected = true;
                break;
            }
        }
        
        var count = 0;
        for (var i = 0; i < document.forms[0].elements.length; i++) {
            if (document.forms[0].elements[i].name == "rbPoll" + pollID + "_Answers" && document.forms[0].elements[i].checked) {
                votes += ((votes!='') ? "," : "") + document.forms[0].elements[i].value;
                selected = true;
                count++;
            }
        }
        var error = "";
        var answers = document.getElementById("ctrlLeftMenu_ctrlPoll_hMaxVotes").value;
        if (!selected) {
            error +=  "Trebuie să alegeţi unul dintre răspunsuri şi apoi să daţi click pe \"Votează!\"\n";
        } 
        
        if(count != answers)
        {
            error +=  "Trebuie sa selectati "+ answers + " raspunsuri!\n";
        }
        
        
        if(error=='') {
            //Web.Controls.PollControl.RecordVote(vote, pollID, vote_callback);
            Web.Controls.PollControl.RecordVotes(votes, pollID, vote_callback);
        }
        else
        {
            alert(error);
        }
        
    }
}

function vote_callback() {
    document.location = "/sondaj.aspx?votat=1";
}