﻿function init_Survey(_moduleid, _iseditable) {

    var i;
    var html = "";
    var summary = "";
    var url;

    $('#lnksubmit').click(function() {
        submit_survey();
    });

    $.ajax({
        type: "POST",
        url: "/desktopmodules/sitebuilder/components/WebService.asmx/FetchSurvey_Settings",
        // pass parameters to webmethod
        data: "{ModuleID: " + _moduleid + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            // if data is returned, iterate through response
            if (null != msg) {
                if (msg.PrivateResults = false && Math.ceil(new Date(eval('new' + msg.ClosingDate.replace(/\//g, ' ')).toString('MM/dd/yyyy h:mm tt')) - new Date()) < 1) {
                    $('#lnkresults').show();
                    $('#lnkresults').attr("href", dnn.getVar('publicurl') + '?skin=' + _SiteSkin + '&moduleid=' + _moduleid + '&mode=survey_results');
                    $('#lnkaddnew').hide();
                }
                else {
                    if (_iseditable != 'True') {
                        $('#lnkresults').hide();
                    }
                    else {
                        $('#lnkresults').attr("href", dnn.getVar('publicurl') + '?skin=' + _SiteSkin + '&moduleid=' + _moduleid + '&mode=survey_results');
                    }
                }
            }

            url = (_iseditable == 'True') ? "/desktopmodules/sitebuilder/components/WebService.asmx/FetchSurvey_All" : "/desktopmodules/sitebuilder/components/WebService.asmx/FetchSurvey_Current";
            $.ajax({
                type: "POST",
                url: url,
                // pass parameters to webmethod
                data: "{ModuleID: " + _moduleid + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg2) {
                    html += "<ol id='survey_questions'>"
                    for (i = 0; i < msg2.length; i++) {
                        if (_iseditable == 'True') { html += '<li><a class="lnkedit shadowbox" rel=' + msg2[i].SurveyId + ' href="' + _privateurl + '?skin=' + _SiteSkin + '&mode=survey&SurveyID=' + msg2[i].SurveyId + '"><img visible="false" class="imgedit" src="/desktopmodules/sitebuilder/images/edit.gif" alt="edit" /></a>' } else { html += '<li>' }
                        html += msg2[i].Question + "<ul id='survey_" + msg2[i].SurveyId + "' rel='" + msg2[i].OptionType + "'></ul></li>"
                    }
                    html += "</ol>";

                    $('#modulebody').append(html);
                    SetEditControls(_iseditable, _moduleid, 'survey');

                    $.ajax({
                        type: "POST",
                        url: "/desktopmodules/sitebuilder/components/WebService.asmx/FetchSurveyOptions_GetByModule",
                        // pass parameters to webmethod
                        data: "{ModuleID: " + _moduleid + "}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function(msg3) {
                            for (i = 0; i < msg3.length; i++) {
                                if ($('#survey_' + msg3[i].SurveyId).attr("rel") == "C") {
                                    $('ul#survey_' + msg3[i].SurveyId).append('<li><input rel="' + msg3[i].SurveyOptionId + '" type="checkbox">' + msg3[i].OptionName + '</input></li>');
                                }
                                else {
                                    $('ul#survey_' + msg3[i].SurveyId).append('<li><input rel="' + msg3[i].SurveyOptionId + '" name="' + msg3[i].SurveyId + '" type="radio">' + msg3[i].OptionName + '</input></li>');
                                }
                            }
                        }
                    });
                }
            });
        }
    });
}

function submit_survey() {
    $('#survey_questions input:checked').each(function() {
        $.ajax({
            type: "POST",
            url: "/desktopmodules/sitebuilder/components/WebService.asmx/SubmitSurveyResult",
            // pass parameters to webmethod
            data: "{SurveyOptionID: " + $(this).attr("rel") + ", UserID: " + _UserID + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
            }
        });

        $('#lnksubmit').hide();
        $('#lblSucess').show();
    });
}