function obj2str(o) {
    var r = [];
    if (typeof o == "string") return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
    if (typeof o == "object") {
        if (!o.sort) {
            for (var i in o)
                r.push(i + ":" + obj2str(o[i]));
            if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
                r.push("toString:" + o.toString.toString());
            }
            r = "{" + r.join() + "}"
        } else {
            for (var i = 0; i < o.length; i++)
                r.push(obj2str(o[i]))
            r = "[" + r.join() + "]"
        }
        return r;
    }
    return o.toString();
}


var __sendToServer = function (url) {
    if (url == null) url = "/WebService/EditWPaper.asmx/";
    this.url = url;

    function getPaperFile() {
        var l = requestQueryString("paperid");

        return l;

    }
    this.save = function () { this.saveChang(); };
    //同时移动多个节点"/wcf/EditPaperService.svc/moveNode"
    var _changeNodesArr = [];
    this.changeNodes = function (movedNodesArr) {

        disabledSaveUI(false);
        $.each(movedNodesArr, function (i, n) {
            var exist = (jQuery.inArray(n, _changeNodesArr) != -1);

            if (!exist) _changeNodesArr.push(n);

        });
        if (_changeNodesArr.length < 5) return;

        this.saveChang();

    };
    function getChangeNodes() {
        var nodes = [];
        for (var i = 0; i < _changeNodesArr.length; i++) {
            var ids = _changeNodesArr[i];
            var obj = document.getElementById("test_" + ids.charAt(0) + "_" + ids);

            var paraArr = ids.split("_");
            var idd = paraArr[0];
            if (paraArr[1]) idd += "_" + paraArr[1]
            nodes[i] = { id: idd, type: ids.charAt(0), top: parseInt(obj.style.top), left: parseInt(obj.style.left), width: parseInt(obj.offsetWidth), height: parseInt(obj.offsetHeight) };

        };
        return nodes;
    };
    this.saveChang = function () {

        var nodes = getChangeNodes();
        if (_changeNodesArr.length == 0) { disabledSaveUI(true); return; }
        var url = this.url + "changeNodes";
        var json_data = { "paperFileName": getPaperFile(), nodes: nodes };
        $.ajax({
            type: "post",
            url: url,
            data: obj2str(json_data),
            processData: true,
            cache: false,
            async: true,
            contentType: "application/json",
            success: function (data) {
                _changeNodesArr = []; disabledSaveUI(true); checkLog(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
                // 只有一个会包含信息
                //alert(XMLHttpRequest.responseText);
            }
        });


    };
    //编辑控件高度
    this.changeSize = function (id, width, height) {

        disabledSaveUI(false);
        var url = this.url + "changeSize";
        var json_data = { "paperFileName": getPaperFile(), "id": id, "width": width, "height": height };
        $.ajax({
            type: "post",
            url: url,
            data: obj2str(json_data),
            processData: true,
            cache: false,
            async: true,
            contentType: "application/json",
            success: function (data) {
                disabledSaveUI(true); checkLog(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
                // 只有一个会包含信息
                //alert(XMLHttpRequest.responseText);
            }
        });

    };
    //添加checkbox
    this.appendRadio = function (rtl, left, top, count, score_1, score_2, score_3, score_4, score_5, score_6) {
        var url = this.url + "appendRadio";
        var json_data = { nodes: getChangeNodes(), "paperFileName": getPaperFile(), "rlt": rtl, "left": left, "top": top, "choiceCount": count, "score_1": score_1, "score_2": score_2, "score_3": score_3, "score_4": score_4, "score_5": score_5, "score_6": score_6 };
        $.ajax({
            type: "post",
            url: url,
            data: obj2str(json_data),
            processData: true,
            cache: false,
            async: false,
            contentType: "application/json",
            success: function (data) {
                _changeNodesArr = [];
                reloadPage(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
                // 只有一个会包含信息

            }
        });

    };
    this.appendDrop = function (rtl, left, top, count, score_1, score_2, score_3, score_4, score_5, score_6) {
        var url = this.url + "appendDrop";
        var json_data = { nodes: getChangeNodes(), paperFileName: getPaperFile(), rlt: rtl, left: left, top: top, choiceCount: count, score_1: score_1, score_2: score_2, score_3: score_3, score_4: score_4, score_5: score_5, score_6: score_6 };

        $.ajax({
            type: "post",
            url: url,
            data: JSON.stringify(json_data),
            processData: true,
            cache: false,
            async: false,
            contentType: "application/json",
            success: function (data) {
                _changeNodesArr = [];
                reloadPage(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
                // 只有一个会包含信息
                //alert(XMLHttpRequest.responseText);
            }
        });

    };
    this.appendMedia = function (url2, left, top) {
        var url = this.url + "appendMedia";
        var json_data = { nodes: getChangeNodes(), "paperFileName": getPaperFile(), "url": url2, "left": left, "top": top };
        $.ajax({
            type: "post",
            url: url,
            data: obj2str(json_data),
            processData: true,
            cache: false,
            async: false,
            contentType: "application/json",
            success: function (data) {
                _changeNodesArr = [];
                reloadPage(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
                // 只有一个会包含信息
                //alert(XMLHttpRequest.responseText);
            }
        });

    };
    //添加checkbox
    this.appendCheckboxs = function (rtl, left, top, count, keyStr, score) {
        var url = this.url + "appendCheckboxs";
        var json_data = { nodes: getChangeNodes(), "paperFileName": getPaperFile(), "rlt": rtl, "left": left, "top": top, "choiceCount": count, key: keyStr, "score": score };
        $.ajax({
            type: "post",
            url: url,
            data: obj2str(json_data),
            processData: true,
            cache: false,
            async: false,
            contentType: "application/json",
            success: function (data) {
                _changeNodesArr = [];
                reloadPage(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
                // 只有一个会包含信息
                //alert(textStatus + "'" + errorThrown);
            }
        });

    };
    //编辑checkbox的分值
    this.editCheckScore = function (groupName, value, score) {

        var url = this.url + "editCheckScore";
        var json_data = { "paperFileName": getPaperFile(), "groupName": groupName, "newKey": value, "score": score };
        $.ajax({
            type: "post",
            url: url,
            data: obj2str(json_data),
            processData: true,
            cache: false,
            async: false,
            contentType: "application/json",
            success: function (data) {
                reloadKey(data); showKey(groupName); $.modal.close();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
                // 只有一个会包含信息
                //alert(textStatus + "'" + errorThrown);
            }
        });

    };
    this.editMusicUrl = function (groupName, url0) {

        var url = this.url + "editMusicUrl";
        var json_data = { nodes: getChangeNodes(), "paperFileName": getPaperFile(), "groupName": groupName, url: url0 };
        $.ajax({
            type: "post",
            url: url,
            data: obj2str(json_data),
            processData: true,
            cache: false,
            async: false,
            contentType: "application/json",
            success: function (data) {
                _changeNodesArr = [];
                reloadPage(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
                // 只有一个会包含信息
                //alert(textStatus + "'" + errorThrown);
            }
        });

    };
    //编辑一组填空节点
    this.editInputScore = function (group, key, score) {

        var json_data = { "paperFileName": getPaperFile(), "group": group, "key": key, score: score };
        var url = this.url + "editInputScore";

        $.ajax({
            type: "post",
            url: url,
            data: JSON.stringify(json_data),
            processData: false,
            cache: false,
            async: false,
            contentType: "application/json",
            success: function (data) {
                reloadKey(data); showKey(group); $.modal.close();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
                // 只有一个会包含信息

                //alert(textStatus + "'" + XMLHttpRequest.responseText);
            }
        });



    };
    this.editRadioScore = function (groupName, key, score) {

        var json_data = { "paperFileName": getPaperFile(), "groupName": groupName, "keys": key, "scores": score };
        var url = this.url + "editRadioScore";

        $.ajax({
            type: "post",
            url: url,
            data: obj2str(json_data),
            processData: true,
            cache: false,
            async: false,
            contentType: "application/json",
            success: function (data) {
                reloadKey(data); showKey(groupName); $.modal.close();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
                // 只有一个会包含信息


            }
        });



    };
    this.editDropScore = function (groupName, key, score) {

        var json_data = { "paperFileName": getPaperFile(), "groupName": groupName, "keys": key, "scores": score };
        var url = this.url + "editDropScore";

        $.ajax({
            type: "post",
            url: url,
            data: obj2str(json_data),
            processData: true,
            cache: false,
            async: false,
            contentType: "application/json",
            success: function (data) {
                reloadKey(data); showKey(groupName); $.modal.close();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
                // 只有一个会包含信息


            }
        });



    };
    this.editTextareaScore = function (id, key4, score) {

        var json_data = { "paperFileName": getPaperFile(), "id": id, "score": score, "key": key4 };
        var url = this.url + "editTextareaScore";

        $.ajax({
            type: "post",
            url: url,
            data: JSON.stringify(json_data),
            processData: false,
            cache: false,
            async: false,
            contentType: "application/json",
            success: function (data) {
                reloadKey(data); showKey(id); $.modal.close();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
                // 只有一个会包含信息

            }
        });



    };
    this.appendTextarea = function (left, top, key, score) {

        var json_data = { nodes: getChangeNodes(), "paperFileName": getPaperFile(), "left": left, "top": top, "score": score, "key": key };
        var url = this.url + "appendTextarea";

        $.ajax({
            type: "post",
            url: url,
            data: JSON.stringify(json_data),
            processData: false,
            cache: false,
            async: false,
            contentType: "application/json",
            success: function (data) {
                _changeNodesArr = [];
                reloadPage(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
                // 只有一个会包含信息

            }
        });



    };
    this.delNodes = function (groupArr) {
        var json_data = { nodes: getChangeNodes(), "paperFileName": getPaperFile(), "group": groupArr };

        var url = this.url + "delNodes";
        $.ajax({
            type: "post",
            url: url,
            data: obj2str(json_data),
            processData: true,
            cache: false, async: false,
            contentType: "application/json",
            success: function (data) {

                _changeNodesArr = []; reloadPage(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
                // 只有一个会包含信息

            }
        });


    };

    //添加一组填空节点
    this.appendInputs = function (inputType, left, top, count, key, scorea) {

        var json_data = { nodes: getChangeNodes(), "paperFileName": getPaperFile(), "inputType": inputType, "count": count, "key": key, "left": left, "top": top, score: scorea };
        var url = this.url + "appendInputs";

        $.ajax({
            type: "post",
            url: url,
            data: JSON.stringify(json_data),
            processData: false,
            cache: false,
            async: false,
            contentType: "application/json",
            success: function (data) {
                _changeNodesArr = []; reloadPage(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
                // 只有一个会包含信息


            }
        });



    };
    //复制一组节点，限单选、多项g_v
    this.copyNodes = function (nodeId) {
        var url = this.url + "copyNodes";
        var json_data = { "paperFileName": getPaperFile(), "groupName": $("#" + nodeId).text() };
        $.ajax({
            type: "post",
            url: url,
            data: obj2str(json_data),
            processData: true,
            cache: false,
            async: true,
            contentType: "application/json",
            success: function (data) {
                reloadPage(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name
                // 只有一个会包含信息

            }
        });

    };
    //复制一节点，限单选、多项g_v
    this.copyNode = function (groupName, value) {
        var url = this.url + "copyNode";
        var json_data = { "paperFileName": getPaperFile(), "groupName": groupName, "value": value };
        $.ajax({
            type: "post",
            url: url,
            data: obj2str(json_data),
            processData: true,
            cache: false,
            async: true,
            contentType: "application/json",
            success: function (data) {
                reloadPage(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // 通常 textStatus 和 errorThrown 之中 + " Name:" +  JSON.parse(data).d.Name

            }
        });

    };

    //只检查是否已经登录
    function checkLog(data) {
        var d = data.d;
        if (!d.ok) {
            if (d.msgCode == 1) __sendToServer.prototype.logon(); else alert(d.msg)
        };


    };
    //刷新页面
    function reloadPage(data) {
        var d = data.d;
        if (d.ok) {
            __SetCookie("scrollTop", $(window).scrollTop());
            window.location.reload();
        }
        else {

            if (d.msgCode == 1) { $.modal.close(); __sendToServer.prototype.logon(); } else alert(d.msg)
        };



    };
    //刷新答案
    function reloadKey(data) {
        var d = data.d;
        if (d.ok) { loadPaperKey(false, null, displayKeys, false); }
        else {
            if (d.msgCode == 1) __sendToServer.prototype.logon(); else alert(d.msg)
        };



    };

};

__sendToServer.prototype.logon = function() {
    var src = "/secure/logon_modal.htm";
    $.modal('<iframe src="' + src + '"scrolling="no" height="295" width="645" style="border:0"></iframe>', {

	containerCss:{
		height:300,
		padding:0,
		width:650
	}
});





};
var send = new __sendToServer();