﻿var InstanceName = 'pb';
var dataurl;
var pb = new clsPollBuilder(InstanceName);
var jqueryInUsing = true;
var domain = getCurrentDomain();
if (domain == '') {
    dataurl = "http://ex.qianjia.com/psws/TopicAnswer.asmx";
} else {
    dataurl = "http://" + domain + "/PSProxy/PSProxy.aspx";
}




function clsStyleSet() {
    this.Sheet = 'Sheet';
    this.Summary = 'Summary';
    this.SummaryTopic = 'SummaryTopic';
    this.SummaryDesc = 'SummaryDesc';
    this.Question = 'Qst';
    this.QuestionTitle = 'QstTitle';
    this.QuestionDetail = 'QstDetail';
    this.OptionClass = 'Opt';
    this.OptionDesc = 'OptDesc';
    this.OptionTextBox = 'OptTXB';
    this.SubmitButton = 'button';
    this.FrameChart = 'FrameChart';
    this.FrameChartTitle = 'FrameChartTitle';
    this.Box = 'Box';
    this.BoxPage = 'BoxPage';
    this.BoxFrames = 'BoxFrames';
    this.BoxQuestion = 'BoxQst';
    this.BoxButtonContainer = 'BoxBTCotainer';
    this.BoxButtonNext = 'btNext';
    this.BoxButtonPrev = 'btPrev';
    this.BoxButtonSubmit = 'btSubmit';
    this.PercentBK = 'PercentBK';
    this.PercentBorder = 'PercentBorder';
    this.PercentInside = 'PercentInside';
    this.PercentNum = 'PercentNum';
};
 
 
 
 
 
function clsPollBuilder(instanceName) {
    this.name = instanceName;
    this.topic = null;
    this.StyleSet = new clsStyleSet();
    this.panelTp = null; //存放问卷
    this.panelSum = null;
    this.panelBT = null;
    this.clsAnswer = '{"ID":0,"SheetID":0,"TopicID":0,"QuestionID":0,"OptionID":0,"Answer":null,"Points":0}';
    this.clsSheet = '{"ID":0,"TopicID":0,"QuoteID":0,"UserID":0,"User":null,"IP":null,"Reivew":null,"Answers":null}';

    this.showResultOnVoting = true;
    
    //box properties
    this.isShowInBox = false;
    this.boxFrames = null;//frame id array
    this.boxCurrentFrame = null;
    this.boxCurrentIndex = 0;
    this.boxShowSummary = false;
    


    this.Sheet = null;
    eval('this.Sheet = ' + this.clsSheet);
    
    
    
    
    this.GetType = function(type) {
        switch (type) {
            case (0): return '下拉';
            case (1): return '单选';
            case (2): return '多选';
            case (3): return '按钮';
            case (4): return '问答';
            default:
                return '单选';

        }
    };
    this.GetTypeValue = function(type) {
        switch (type) {
            case ('下拉'): return 0;
            case ('单选'): return 1;
            case ('多选'): return 2;
            
            case ('按钮'): return 3;
            case ('问答'): return 4;
            default:
                return 4;

        }
    };
    this.GetOrderTypeValue = function(type) {
        var ttype = type.substr(0, 2);
        switch (ttype) {
            case ('列铺'): return 0;
            case ('横铺'): return 1;
            default:
                return 0;

        }
    };



    //----------------------------- 显示 元素-------------
    //显示简介
    this.ShowSummary = function(parentElement) {
        if (parentElement == null) return;
        this.panelSum = parentElement;
        var div = document.createElement('div');
        with (div) {
            id = 'divTopicTitle';
            className = this.StyleSet.Summary;

            var divTopic = document.createElement('span');
            divTopic.className = this.StyleSet.SummaryTopic;
            divTopic.innerHTML = this.topic.Topic;

            var divDesc = document.createElement('div');
            divDesc.className = this.StyleSet.SummaryDesc;
            divDesc.innerHTML = this.topic.Desc;

            appendChild(divTopic);
            appendChild(divDesc);
        }
        parentElement.appendChild(div);
    };
    //显示专题
    this.ShowTopic = function(parentElement) {
        this.panelTp = parentElement;
        parentElement.appendChild(this.buildTopic(this.topic));
    };
    this.ShowButtonSubmit = function(parentElement) {
        if (parentElement == null) return;
        this.panelBT = parentElement;
        var ipt = document.createElement('<input onclick="SubmitPoll(this)" />');
        ipt.type = 'button';
        ipt.className = this.StyleSet.SubmitButton;
        ipt.value = '提交';
        parentElement.appendChild(ipt);
        parentElement.appendChild(this.BuildChartFrame());
    };
    this.ShowBox = function(parentElement) {
        if (parentElement == null) return;
        parentElement.appendChild(this.BuildBox());

        with (document.getElementById('pollFrames')) {
            parentNode.style.height = parentNode.parentNode.offsetHeight;
            style.height = parentNode.offsetHeight -
            parentNode.childNodes[0].offsetHeight -
            parentNode.childNodes[2].offsetHeight - 4;
        }
        this.BoxSetCurrentFrame(this.boxFrames[0]);

    };






    //--------------创建元素

    this.createQuestion = function() { var qst = null; eval("qst=" + this.clsQuestion); return qst; }
    this.createOption = function() { var opt = null; eval("opt=" + this.clsOption); return opt; }

    this.buildTopic = function(topic) {
        if (topic == null) topic = this.topic;
        var div = document.createElement('div');
        with (div) {
            id = 'divQuestions';
            className = this.StyleSet.Sheet;
        }
        if (topic != null && topic != null && topic.Questions != null)
            for (var i = 0; i < topic.Questions.length; i++) {
            div.appendChild(this.buildQuestion(topic.Questions[i]));
        }
        return div;
    };

    //创建Box用的专题描述
    this.buildBoxSummary = function() {
        var div = document.createElement('div');
        with (div) {
            id = 'qst_sum';
            className = this.StyleSet.BoxQuestion;
        }

        var divTitle = document.createElement('div');
        with (divTitle) {
            innerHTML = this.topic.Topic;
            className = this.StyleSet.QuestionTitle;
        }

        div.appendChild(divTitle);

        var divqst = document.createElement("div");
        with (divqst) {
            className = this.StyleSet.QuestionDetail;
            innerHTML = this.topic.Desc;
        }

        div.appendChild(divqst);
        return div;
    };

    //创建问题
    this.buildQuestion = function(qst) {
        var div = document.createElement('div');
        with (div) {
            id = 'qst_' + qst.ID;
            if (this.isShowInBox)
                className = this.StyleSet.BoxQuestion;
            else
                className = this.StyleSet.Question;
        }

        var divTitle = document.createElement('div');
        with (divTitle) {
            innerHTML = qst.NO + '：' + qst.Title + "(" + this.GetType(qst.Type) + ")";
            className = this.StyleSet.QuestionTitle;
        }

        div.appendChild(divTitle);

        var divqst = document.createElement("div");
        with (divqst) {
            className = this.StyleSet.QuestionDetail;
            appendChild(this.buildOptions(qst));
        }

        div.appendChild(divqst);
        return div;
    };

    
    
    //创建选项
    this.buildOptions = function(qst) {
        var table = document.createElement('table');
        with (table) {
            //style.border = "1px solid black";
            border = 0;
            cellSpacing = 0;
            cellPadding = 0;
            className = this.StyleSet.OptionClass;
        }
        if (qst.Type == 4) {
            var tr = table.insertRow();
            var opt = qst.Options[0];
            this.BuildTextArea(qst, opt, tr);
        }
        else if (qst.Type == 0) {
            for (var i = 0; i < qst.Options.length; i++) {
                var opt = qst.Options[i];
                var tr = table.insertRow();
                this.BuildDropDownList(qst, opt, tr);
            }
        }
        else {
            if (qst.OrderType == '列铺') {
                //获取总票数
                var total = this.topic.VoteCount;
                for (var i = 0; i < qst.Options.length; i++) {
                    var opt = qst.Options[i];
                    if (opt.Value == NaN) opt.Value = 0;
                    //total += Number(opt.Value);
                }



                var j = 1;
                for (var i = 0; i < qst.Options.length; i++) {
                    var opt = qst.Options[i];
                    var tr = table.insertRow();
                    this.BuidlInput(qst, opt, tr);
                    if (this.showResultOnVoting) {
                        var td2 = tr.insertCell();
                        td2.className = this.StyleSet.OptionDesc;
                        td2.style.width = "35%";

                        var divPercent = document.createElement('div');
                        divPercent.className = this.StyleSet.PercentBK;

                        var divPData = document.createElement('div');
                        var divPInside = document.createElement('div');
                        var divPBorder = document.createElement('div');
                        var divPNum = document.createElement('div');

                        divPData.className = 'Percent' + j;
                        j++;
                        if (j > 5) j = 1;
                        divPBorder.className = this.StyleSet.PercentBorder;
                        divPInside.className = this.StyleSet.PercentInside;
                        divPNum.className = this.StyleSet.PercentNum;

                        if (total == 0)
                            divPNum.innerHTML = '0%';
                        else
                            divPNum.innerHTML = Math.round(Number(opt.Value) * 10000 / total) / 100 + '%';
                        divPData.style.width = divPNum.innerHTML;


                        divPBorder.appendChild(divPInside);
                        divPData.appendChild(divPBorder);
                        divPercent.appendChild(divPData);
                        divPercent.appendChild(divPNum);


                        td2.appendChild(divPercent);


                    }
                }
            } else {
                var cols = qst.OrderType.substr(2);
                var tr = null;
                for (var i = 0; i < qst.Options.length; i++) {
                    var opt = qst.Options[i];
                    if (i % cols == 0 || i == 0) {
                        tr = table.insertRow();
                    }
                    this.BuidlInput(qst, opt, tr);
                }
            }
        }
        return table;
    };


    this.BuidlInput = function(qst, opt, tr) {
        var td = tr.insertCell();
        td.id = 'opt_' + qst.ID + '_' + opt.ID;
        td.className = this.StyleSet.OptionDesc;
        var ipt = document.createElement('<input name="on_' + qst.NO + '" />');
        var lbl = document.createElement('label');

        ipt.id = qst.ID + "_" + opt.ID;
        if (this.topic.Type == '投票调查')
            lbl.innerHTML = opt.Desc;
        else
            lbl.innerHTML = opt.Desc + '(' + opt.Value + '分)';

        lbl.htmlFor = ipt.id;
        ipt.value = opt.Value;
        switch (qst.Type) {
            case (0): break;
            case (1):
                ipt.type = 'radio';
                break;
            case (2):
                ipt.type = 'checkbox';
                break;
            case (3):
                ipt.type = 'button';
                break;
            default:
        }
        td.appendChild(ipt);
        td.appendChild(lbl);



    };
    this.BuildTextArea = function(qst,opt, tr) {
        var td = tr.insertCell();
        td.id = 'opt_' + qst.ID + '_'+opt.ID;
        var ipt = document.createElement('<textarea name="on_' + qst.ID + '" />');
        ipt.id = qst.ID + "_"+opt.ID;
        ipt.rows = 6;
        ipt.className = this.StyleSet.OptionTextBox;
        td.appendChild(ipt);
    };
    this.BuildDropDownList = function(qst, opt, tr) {
        var td = tr.insertCell();
        td.id = 'opt_' + qst.ID + '_' + opt.ID;

        var lbl = document.createElement('label');
        lbl.style.margin = '0px 0px 0px 16px';
        lbl.innerHTML = opt.Desc;

        td.className = this.StyleSet.OptionDesc;
        var ov = opt.Value.split(':');

        var ipt = document.createElement('select');
        ipt.style.margin = '0px 0px 0px 10px';
        ipt.id = qst.ID + "_"+opt.ID;
        for (var i = Number(ov[0]); i <= Number(ov[1]); i++) {
            var option = document.createElement('option');
            option.value = i;
            option.innerHTML = i;
            if (i == ov[0]) option.selected = 'selected';
            ipt.appendChild(option);
        }
        td.appendChild(lbl);
        td.appendChild(ipt);
        var ed = new BuildEditor(td);
        td.onmouseover = ed.Show.bind(ed);
        td.onmouseleave = ed.Hide.bind(ed);
    };
    this.BuildChartFrame = function() {
        var div = document.createElement('div');
        with (div) {
            className = this.StyleSet.FrameChart;
        }
        var divTitle = document.createElement('div');
        with (divTitle) {
            className = this.StyleSet.FrameChartTitle;
        }
        div.appendChild(divTitle);
//        var frameChart = document.createElement('iframe');
//        with (frameChart) {
//            id = "frameChart";
//            name = id;
//            style.width = "100%";
//            style.height = "100%";
//            frameBorder = 0;
//        }
//        div.appendChild(frameChart);
        return div;
    };


    //------------------------------------------------------- Build Box
    this.BuildBox = function() {
        var divBox = document.createElement('div');
        with (divBox) {
            id = "pollBox";
            className = this.StyleSet.Box;
        }

        //build page
        var divPage = document.createElement('div');
        with (divPage) {
            id = "pollPage";
            className = this.StyleSet.BoxPage;
        }
        //build frame
        var divFrames = document.createElement('div');
        with (divFrames) {
            id = "pollFrames";
            className = this.StyleSet.BoxFrames;
        }

        this.boxFrames = [];

        if (this.boxShowSummary) {
            var qs = this.buildBoxSummary();
            this.boxFrames.push(qs.id);
            divFrames.appendChild(qs);
            var ps = document.createElement('div');
            with (ps) {
                innerHTML = '概述';
                id = 'p_' + qs.id;
                onmouseover = function() {
                    if (this.className == 'PageActived') return;
                    this.className = 'PageHover';
                    this.style.cursor = 'pointer';
                }
                onmouseleave = function() {
                    if (this.className == 'PageActived') return;
                    this.className = '';
                }
                onclick = function() {
                    var id = this.id.substr(2);
                    eval(instanceName + '.BoxSetCurrentFrame("' + id + '")');
                }
            }
            divPage.appendChild(ps);
        }



        for (var i = 0; i < this.topic.Questions.length; i++) {
            var q = this.buildQuestion(this.topic.Questions[i]);
            q.style.display = 'none';
            //q.style.margin = '0px';
            q.style.width = '';
            divFrames.appendChild(q);
            this.boxFrames.push(q.id);

            var ttt = this.topic.Questions[i].Title;
            if (ttt.length > 4) ttt = ttt.substr(0, 4) + "...";
            var p = document.createElement('div');
            with (p) {
                title = this.topic.Questions[i].Title;
                innerHTML = i + 1 + "." + ttt;
                id = 'p_' + q.id;
                onmouseover = function() {
                    if (this.className == 'PageActived') return;
                    this.className = 'PageHover';
                    this.style.cursor = 'pointer';
                }
                onmouseleave = function() {
                    if (this.className == 'PageActived') return;
                    this.className = '';
                }
                onclick = function() {
                    var id = this.id.substr(2);
                    eval(instanceName + '.BoxSetCurrentFrame("' + id + '")');
                }
            }
            divPage.appendChild(p);
        }


        var divButtons = document.createElement('div');
        divButtons.className = this.StyleSet.BoxButtonContainer;
        //divButtons.className = 'BoxButtonContainer';

        var btSubmit = document.createElement('input');
        with (btSubmit) {
            id = 'btSubmit';
            type = 'button';
            value = '提交';
            className = this.StyleSet.BoxButtonSubmit;
            onclick = function() {
                SubmitPoll(this);
            }
        }

        var btNext = document.createElement('input');
        with (btNext) {
            id = 'btNext';
            type = 'button';
            value = '下一题';
            className = this.StyleSet.BoxButtonNext;
            onclick = function() {
                eval(instanceName + '.BoxNextFrame()');
            }
        }

        var btPrev = document.createElement('input');
        with (btPrev) {
            id = 'btPrev';
            type = 'button';
            value = '上一题';
            className = this.StyleSet.BoxButtonPrev;
            onclick = function() {
                eval(instanceName + '.BoxPrevFrame()');
            }
        }
        divButtons.appendChild(btSubmit);
        divButtons.appendChild(btPrev);
        divButtons.appendChild(btNext);

        if (this.boxShowSummary || this.topic.Questions.length > 1) { divPage.style.display = 'block'; }


        divBox.appendChild(divPage);
        divBox.appendChild(divFrames);
        divBox.appendChild(divButtons);
        //build button

        return divBox;
    };

    this.BoxSetCurrentFrame = function(id) {
        if (this.boxCurrentFrame != null) {
            document.getElementById(this.boxCurrentFrame).style.display = 'none';
            document.getElementById('p_' + this.boxCurrentFrame).className = '';
        }
        this.boxCurrentFrame = id;
        document.getElementById(id).style.display = 'block';
        document.getElementById('p_' + id).className = 'PageActived';
        for (var i = 0; i < this.boxFrames.length; i++) {
            if (this.boxCurrentFrame == this.boxFrames[i]) {
                this.boxCurrentIndex = i;
                break;
            }
        }
        if (this.boxCurrentIndex == 0) {
            if (this.topic.Questions.length == 1) {
                document.getElementById('btSubmit').style.display = 'block';
                document.getElementById('btNext').style.display = 'none';
            }
            else {
                document.getElementById('btSubmit').style.display = 'none';
                with (document.getElementById('btNext')) {
                    if (this.boxShowSummary) {
                        value = '开始吧';
                    } else {
                        value = '下一题';
                    }
                    style.display = 'block';
                }
            }
            document.getElementById('btPrev').style.display = 'none';

        } else if (this.boxCurrentIndex == this.boxFrames.length - 1) {
            document.getElementById('btSubmit').style.display = 'block';
            document.getElementById('btNext').style.display = 'none';
            document.getElementById('btPrev').style.display = 'block';
        } else {
            with (document.getElementById('btNext')) {
                style.display = 'block';
                value = '下一题';
            }
            document.getElementById('btPrev').style.display = 'block';
            document.getElementById('btSubmit').style.display = 'none';
        }

    };

    this.BoxNextFrame = function() {
        if (this.boxFrames == null || this.boxFrames.length == 0) return;
        if (this.boxCurrentFrame == null) {
            this.BoxSetCurrentFrame(this.boxFrames[0]);
            this.boxCurrentIndex = 0;
        } else {
        if (this.boxCurrentIndex <= this.boxFrames.length - 2) {
                this.BoxSetCurrentFrame(this.boxFrames[this.boxCurrentIndex + 1]);
            }
        }
    };


    this.BoxPrevFrame = function() {
        if (this.boxFrames == null || this.boxFrames.length == 0) return;
        if (this.boxCurrentFrame == null) {
            this.BoxSetCurrentFrame(this.boxFrames[0]);
            this.boxCurrentIndex = 0;
        } else {
            if (this.boxCurrentIndex > 0) {
                this.BoxSetCurrentFrame(this.boxFrames[this.boxCurrentIndex - 1]);
            }
        }
    };

    this.clearAll = function() {
        var qstPanel = document.getElementById('divQuestions');
        if (qstPanel == null) return;
        qstPanel.innerHTML = "";

    };
    this.getCurrent = function(qstNO,optNO) {
        //var t = tdID.split('_');
        var qst = this.topic.Questions[Number(qstNO) - 1];
        var opt = qst.Options[Number(optNO) - 1];
        currentOpt = opt;
        currentQst = qst;
        return true;
    };










    //--------------------- collect answers

    this.CollectAnswers = function() {
        if (this.panelTp == null) return false;
        //sender.disabled = true;
        this.Sheet.Answers = new Array();
        var panelQst;
        if (this.isShowInBox)
            panelQst = this.panelTp.childNodes[0].childNodes[1];
        else
            panelQst = this.panelTp.childNodes[0];


        for (var i = 0; i < panelQst.childNodes.length; i++) {
            var qst = panelQst.childNodes[i];
            var j = qst.childNodes[0].innerHTML.lastIndexOf('(');
            var type = qst.childNodes[0].innerHTML.substr(j);
            switch (type) {
                case ("(问答)"):
                    if (!this.CollectTxb(qst)) {
                        return false;
                    }
                    break;

                case ("(单选)"):
                    if (!this.CollectRadio(qst)) {
                        return false;
                    }
                    break;
                case ("(多选)"):
                    if (!this.CollectCheck(qst)) {
                        return false;
                    }
                    break;
                case ("(下拉)"):
                    if (!this.CollectDropDown(qst)) {
                        return false;
                    }
            }

        }
        return true;
    };

    this.CollectTxb = function(qst) {
        var t = qst.childNodes[1].childNodes[0];
        var a = null;
        var td = t.rows[0].cells[0];
        if (td.childNodes[0].value == '') {
            alert("请填写：" + qst.childNodes[0].innerHTML);
            if (this.isShowInBox) this.BoxSetCurrentFrame(qst.id);
            return false;
        }
        var ov = td.id.split('_');
        eval('a = ' + this.clsAnswer);
        a.Answer = td.childNodes[0].value;
        a.TopicID = this.topic.ID;
        a.QuestionID = Number(ov[1]);
        a.OptionID = Number(ov[2]);

        this.Sheet.Answers.push(a);
        return true;
    };

    this.CollectRadio = function(qst) {
        var t = qst.childNodes[1].childNodes[0];
        var al = new Array();
        for (var i = 0; i < t.rows.length; i++) {
            for (var j = 0; j < t.rows[i].cells.length; j++) {
                var td = t.rows[i].cells[j];
                if (td.childNodes[0].checked) {
                    var ov = td.id.split('_');
                    var a = null;
                    eval('a = ' + this.clsAnswer);
                    a.Answer = td.childNodes[1].innerHTML;
                    a.TopicID = this.topic.ID;
                    a.QuestionID = Number(ov[1]);
                    a.OptionID = Number(ov[2]);
                    al.push(a);
                    i = 999;
                    j = 999;
                    break;
                }

            }
        }

        if (al.length == 0) {
            alert("请选择：" + qst.childNodes[0].innerHTML);
            if (this.isShowInBox) this.BoxSetCurrentFrame(qst.id);
            return false;
        }

        for (var i = 0; i < al.length; i++) {
            this.Sheet.Answers.push(al[i]);
            delete al[i];
        }
        al = null;

        return true;
    };

    this.CollectCheck = function(qst) {
        var t = qst.childNodes[1].childNodes[0];
        var al = new Array();
        for (var i = 0; i < t.rows.length; i++) {
            for (var j = 0; j < t.rows[i].cells.length; j++) {
                var td = t.rows[i].cells[j];
                if (td.childNodes[0].checked) {
                    var ov = td.id.split('_');
                    var a = null;
                    eval('a = ' + this.clsAnswer);
                    a.Answer = td.childNodes[1].innerHTML;
                    a.TopicID = this.topic.ID;
                    a.QuestionID = Number(ov[1]);
                    a.OptionID = Number(ov[2]);
                    al.push(a);
                }

            }
        }

        if (al.length == 0) {
            alert("请至少选择一项：" + qst.childNodes[0].innerHTML);
            if (this.isShowInBox) this.BoxSetCurrentFrame(qst.id);
            return false;
        }

        for (var i = 0; i < al.length; i++) {
            this.Sheet.Answers.push(al[i]);
            delete al[i];
        }
        al = null;
        return true;
    };

    this.CollectDropDown = function(qst) {
        var t = qst.childNodes[1].childNodes[0];
        var al = new Array();
        for (var i = 0; i < t.rows.length; i++) {
            for (var j = 0; j < t.rows[i].cells.length; j++) {
                var td = t.rows[i].cells[j];
                var ov = td.id.split('_');
                var a = null;
                eval('a = ' + this.clsAnswer);
                with (td.childNodes[1]) {
                    a.Answer = options[selectedIndex].text;
                }
                a.TopicID = this.topic.ID;
                a.QuestionID = Number(ov[1]);
                a.OptionID = Number(ov[2]);
                al.push(a);

            }
        }
        for (var i = 0; i < al.length; i++) {
            this.Sheet.Answers.push(al[i]);
            delete al[i];
        }
        al = null;
        return true;
    };
}




function ShowInBox(id, targetId,showSummary) {
    //make a box
    //make pagebutton
    //make data
    //make next previous submit
    if (typeof ($) == 'undefined') {
        alert('未引用JQuery');
        return;
    }
    pb.panelTp = document.getElementById(targetId);
    pb.isShowInBox = true;
    pb.boxShowSummary = showSummary;
    GetTopic(id);
    
    
    
}





function ShowPoll(id,bodyid, headid, buttonid) {
    if (typeof ($) == 'undefined') {
        alert('未引用JQuery');
        return;
    }
    pb.panelSum = document.getElementById(headid);
    pb.panelTp = document.getElementById(bodyid);
    pb.panelBT = document.getElementById(buttonid);
    GetTopic(id);
}


function GetTopic(id) {
    var topic = null;
    $.ajax({
        type: "POST",
        contentType: (domain == '' ? "application/json" : "application/x-www-form-urlencoded"),
        url: dataurl + (domain == '' ? '/GetTopicReuslt' : ''),
        data: (domain == '' ? "{id:" + id + "}" : { id: id }),
        dataType: 'json',
        success: function(result) {
            pb.topic = result.d;
            if (pb.isShowInBox) {
                if (pb.panelTp != null) pb.ShowBox(pb.panelTp);
            }
            else {
                if (pb.panelSum != null) pb.ShowSummary(pb.panelSum);
                if (pb.panelTp != null) pb.ShowTopic(pb.panelTp);
                if (pb.panelBT != null) pb.ShowButtonSubmit(pb.panelBT);
            }
        }
    });
}


function SubmitPoll(sender) {
    if (pb.CollectAnswers() == false) return;
    sender.disabled = true;
    pb.Sheet.TopicID = pb.topic.ID;  //$get('hfTopicID').value;
    pb.Sheet.QuoteID = pb.topic.TargetID; //$get('hfQuoteID').value;
    var aaa = new JSON(pb.Sheet);
    $.ajax({
        type: "POST",
        contentType: (domain == '' ? "application/json" : "application/x-www-form-urlencoded"),
        url: dataurl + (domain == '' ? '/SubmitAnswer' : ''),
        data: (domain == '' ? "{sheet:" + aaa.toString() + "}" : { sheet: aaa.toString() }),
        dataType: 'json',
        success: function(result) {
            if (result.d != 0) {
                var msg = "";
                switch (result.d) {
                    case 1:
                    case 2: msg = "你已经参与过"; break;
                    case 3: msg = "次数超过限制"; break;
                    case 4: msg = "本专题已关闭"; break;
                    case 5: msg = "本专题已过期"; break;
                    case 6: msg = "你必须登录"; break;
                    case 7: msg = "操作失败！"; break;
                }
                alert(msg);
            }
            else {
                //var frame = document.getElementById('frameChart');
                //frame.src = "http://ex.qianjia.com/TC_Statistics.aspx?id=" + pb.topic.ID;
                //frame.parentNode.style.display = "block";
                window.open("http://ex.qianjia.com/TC_Statistics.aspx?id=" + pb.topic.ID);
                alert("感谢你的参与");
            }
        }
    });
}









function getCurrentDomain() {
    var s, siteUrl;
    s = document.location + "";
    siteUrl = s.substring(7, s.indexOf('/', 7));
    return siteUrl;
}


var JSON = function(sJSON) {
    this.objType = (typeof sJSON);
    this.self = [];
    (function(s, o) { for (var i in o) { o.hasOwnProperty(i) && (s[i] = o[i], s.self[i] = o[i]) }; })(this, (this.objType == 'string') ? eval('0,' + sJSON) : sJSON);
}
JSON.prototype = {
    toString: function() {
        return this.getString();
    },
    valueOf: function() {
        return this.getString();
    },
    getString: function() {
        var sA = [];
        (function(o) {
            var oo = null;
            sA.push('{');
            for (var i in o) {
                if (o.hasOwnProperty(i) && i != 'prototype') {
                    oo = o[i];
                    if (oo instanceof Array) {
                        sA.push('"' + i + '":[');
                        for (var b in oo) {
                            if (oo.hasOwnProperty(b) && b != 'prototype') {
                                sA.push(oo[b] + ',');
                                if (typeof oo[b] == 'object') arguments.callee(oo[b]);
                            }
                        }
                        sA.push('],');
                        continue;
                    } else {
                        switch (typeof oo) {
                            case ("string"):
                                if (oo.substr(0, 3) == '/Da')
                                    sA.push('"' + i + '":"' + oo.replace(/\//g, "\\/") + '",');
                                else
                                    sA.push('"' + i + '":"' + oo + '",');
                                break;
                            default:
                                sA.push('"' + i + '":' + oo + ',');
                        }

                    }
                    if (typeof oo == 'object' && oo != null) arguments.callee(oo); //递归传递
                }
            }
            sA.push('},');
        })(this.self);
        return sA.slice(0).join('').replace(/\[object object\],/ig, '').replace(/,\}/g, '}').replace(/,\]/g, ']').slice(0, -1);
    },
    push: function(sName, sValue) {
        this.self[sName] = sValue;
        this[sName] = sValue;
    }
}

























