﻿/*
JQuery scripts for voting module
*/

//retrive paramters
var radioID = document.getElementById("VoteJqScript").getAttribute("radioButtonID");
var sendButtonID = document.getElementById("VoteJqScript").getAttribute("VotingButtonID");
var QID = document.getElementById("VoteJqScript").getAttribute("QuestionID");

//add on click event to all radio buttons for answers , enable answer button if any one clicked
$(function() {// start
$("input[type=radio][id*='" + radioID + "']").attr('onclick', '');
$("input[type=radio][id*='" + radioID + "']").click(function() {
$("#"+sendButtonID).attr('disabled', '');
    });

    //add on click event to all radio buttons for answers , enable answer button if any one clicked
    $("#divResults").bind("ajaxStart", function() {
        $("#divAnswers").hide();
        // put here loading image
        $("#divResults").html('<center><img src="/_layouts/tatweer/images/loading.gif" alt="جاري التحميل" /></center>');
    });

// Get resutl button
    $("input[type=submit][id*='btnResult']").click(function() {  // start button get results click
        var guid = QID;
        $.ajax({ // start ajax
            url: '/_layouts/jVoting.aspx?op=GetResults&QuestionId=' + guid,
            asyn: true,
            cache: false,
            dataType: 'xml',
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.responseText);
            },
            success: function(xml) {
                var html = '<table>';
                $(xml).find('Answer').each(function() {
                    html += BuildTableRow(this);
                });
                html += "<tr><td style=\"width: 100%; font-family: Tahoma; font-size: 10pt;\">مجموع الأصوات&nbsp;&nbsp;:" + $(xml).find('TotalAnswers').text() + "</td></tr>";
                html += "<tr><td style=\"text-align:left\"><a href=\"#\" onclick=\"return false;\" id=\"hlShowAnswers\">تصويت</a></td></tr>";
                html += '</table>';
                $("#divResults").html(html);

                $("#hlShowAnswers").click(function() {
                    $("#divAnswers").show();
                    $("#divResults").html('');
                });

            }
        }); // end ajax
        return false;
    }); // end button get results click

// Vote Button
    $("input[type=submit][id*='ibtnVote']").click(function() {  // start button vote click
        $("input[type=radio][id*='rbtnAnswers']").each(function() { // start each loop
            if ($(this).attr('checked')) { // start if clause
                var guid = $(this).attr('value');

                $.ajax({ // start ajax
                    url: '/_layouts/jVoting.aspx?op=AnswerVote&Answerid=' + guid,
                    asyn: true,
                    cache: false,
                    dataType: 'xml',
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.responseText);
                    },
                    success: function(xml) {
                        var html = '<table>';
                        $(xml).find('Answer').each(function() {
                            html += BuildTableRow(this);
                        });


                        html += "<tr><td style=\"width: 100%; font-family: Tahoma; font-size: 10pt;\">مجموع الأصوات&nbsp;&nbsp;:" + $(xml).find('TotalAnswers').text() + "</td></tr>";
                        html += '</table>';
                        $("#divAnswers").hide();
                        $("#divResults").html(html);
                    }
                }); // end ajax

            } // end if clause
        }); // end each loop
        return false;
    }); // end button click
});// end

//Build the HTML table row for each answer
function BuildTableRow(answer) {
    var html = '<tr>\n';
    html += '<td  style="height: 30px">';
    html += "<span class='DevHelperVotingAnswers'>"+$(answer).attr("Text") +"</span> \n";
    html += " - <span style=\"font-size: 8pt; font-weight: bold; text-align: right\"> " + $(answer).attr("Rate") + " %</span>\n";
    html += '</td>\n';
    html += '</tr>\n';
    
    html += '<tr>\n';
    html += '<td>\n';
    html += '<table class="DevHelperVotingRate" cellpadding="0" cellspacing="0">\n';
    html += '<tr>\n';
    html += '<td style="height: 5px; width: 100%;">\n';
    html += '<span style="background-color:' + $(answer).attr('Color') + '; width:' + $(answer).attr('Rate') + '%;display: block; height: 5px;></span>"';
    html += '</td>\n';
    html += '</tr>\n';
    html += '</table>\n';
    html += '</td>\n';
    html += '</tr>\n';
    return html;
}