﻿var Survey = {
    Vote: function(oObj) {
        oParent = $(oObj).parents('.poll-content:first');
        var sSurveyId = $(oParent).attr("id");
        $(oParent).find('input:checked').each(function(index) {

        var sVoteId = $(this).attr("id");

            $.ajax({
                type: "POST",
                cache: false,
                url: "getXML.aspx",
                dataType: "xml",
                data: "obj=Survey&sFunc=SendSurveyResult&SurveyId=" + sSurveyId + "&VoteId=" + sVoteId,
                success: function(xml) {
                    var isSucceed = $T(XPath(xml, "/root/Succeed")[0]) == "true";
                    if (isSucceed) {
                        alert("תודה על תשובתך!");
                        iNewScore = $T(XPath(xml, "/root/votes")[0]);
                        $(".poll-results[id='" + sSurveyId + "']").find("li[id='" + sVoteId + "'] span").attr("name", iNewScore);
                        Survey.Refresh();
                        $(oObj).prev().click();
                    } else {
                        alert("תקלה כללית. אנא נסה שנית מאוחר יותר.");
                    }

                }
            });

        });
    },
    Refresh: function() {
        //calcultes all results on page
        $(".poll-results").each(function(index) {
            //each survay
            var iTotal = 0;
            $(this).find(".lstQuestions ul:first").each(function() {
                $(this).children("li").each(function(li_index) {
                    //each answer in survay
                    iTotal += $(this).find(".poll-question").attr("name") * 1;
                });


                $(this).children("li").each(function(li_index) {
                    //each answer in survay
                    var iScore = $(this).find(".poll-question").attr("name") * 1;
                    var fPercent = iTotal != 0 ? ((100 * iScore / iTotal).toFixed(2)) : 0;
                    $(this).find(".poll-line").css("width", fPercent + "%");
                    $(this).find(".percent").text(fPercent + "%");
                });
            });
            $(this).find(".poll-infoandacts .votes .bold").text(iTotal);
        });
    }
}
