﻿
$(document).ready(function () {

});


// Post the User Poll Result to the Database
function PostPollAnswer(PollName, Answer, PollTitle, PollName)
{
    // update the value attrbute on the Answer input - depending on the button that was pressed
    if (Answer == true) {
        $("#Answer").attr("value", 'true');
    }
    else {
        $("#Answer").attr("value", 'false');
    }

    // serialize the form to get the values
    var formData = $("#PollSubmitForm").serialize();

    // call the method using Ajax
    $.ajax({
        dataType: "json",
        type: "POST",
        data: formData,
        url: "/Poll/PollEntry/",
        beforeSend: function () {
            $('#Preloader').removeClass("hidden");
            $("#PollContainer").remove();
            $("#MotorpointPoll").prepend("<legend id='PollLegend' class='bold hidden'>" + PollTitle + "</legend>");
            $("#PollLegend").removeClass("hidden");
        },
        complete: function () {
            $('#Preloader').hide();
        },
        success: function (data) {

            // generate a random query string based on the time stamp - this ensures the image is not cached
            var imageName = PollName + ".png?r=" + Math.round(new Date().getTime() / 1000).toString();

            // add the graph image, remove the style attribute.
            $("#MasterPollContainer").prepend("<img src='/Graphs/" + imageName + "' />");
            $('#MasterPollContainer').removeAttr("style");
        },
        error: function (data) {
            //alert("An Error has occured " + PollName + " - " + Answer);
        },
        cache: false
    });
}

