var nEnable = "";
var tEnable = "";
var easyName, valueLenth,osd1Max;
var get_timexml_doc, osdxml_doc;
var xhrState, g_bIsIE;
var namePosX, namePosY, timePosX, timePosY;
var tnumber = 0,
    osdCount = 0;
var keepdoit = {
        width: "",
        height: "",
        base64: "",
        length: 0
    },
    keepdoit2 = {
        width: "",
        height: "",
        base64: "",
        length: 0
    };
var addrile = null;
var currentchannel = 0, deviceCap = {};
$(document).ready(function () {
    if (window.performance.navigation.type == 1 && navigator.userAgent.indexOf("Firefox") !== -1) {
        self.playervideo.destroy();
        self.playervideo = undefined; // 判断是否刷新页面原来的刷新逻辑在火狐中有问题，强制给它清除.刷新后触发
        config.refreshplayvideo = false;
        config.deviceloseSte = true
    }
    ScalePitch("box-border");
    if (!$('.select2').hasClass('select2-offscreen')) {
        $(".select2").select2({
            minimumResultsForSearch: -1,
            width: 220,
        });
    }
    for (var i = 1; i <= config.deviceInfo.VideoInputChannels; i++) {
        $("#NVR_ChnOSDChannelMask").append('<option class="option" value="' + i + '">' + i + '</option>'); //Channel列表
    }
    // 默认第一个通道
    $("#NVR_ChnOSDChannelMask").val("1").trigger('change.select2')
    config.chnelNum=0;
    // 默认第一个通道是否支持osd2
    deviceCap = config.isSupportDeviceCapV2 ? config.deviceCapV2[currentchannel] : config.deviceCapV1[currentchannel];
    if (deviceCap != {}) {
        if (deviceCap.osd2support != "true") {
            $(".osd2display").hide();
            osdCount = 1;
        } else {
            maxOsd2();
            $(".osd2display").show();
            osdCount = 2;
        }
    } else {
        $(".osd2display").hide();
        osdCount = 1;
    }
    maxOsd1();
    if (config.devicelose) {
        addEventListener("resize", function(event) {});
    }

    $(window).resize(function() {
        if (sessionStorage.getItem("xystyle") == "p6s" || sessionStorage.getItem("xystyle") == "p2p") {
            if (window.location.hash.indexOf("#nvr/pages/image/osd.html")!==-1 && sessionStorage.getItem('dpiRadio') != null) {
                ScalePitch("box-border");
                preWindowfollowing(true);
            }
        } else {
            if (sessionStorage.getItem("playstyle") == "EasyVideo" && window.location.hash == "#nvr/pages/image/osd.html") {
                ScalePitch("box-border");
                preWindowfollowing(true);
            }
        }
    });
    // 监听滚动条滚动改变easyvideo位置
    $(".osd-gun").scroll(function() {
        if (sessionStorage.getItem("xystyle") == "p6s" || sessionStorage.getItem("xystyle") == "p2p") {
            if (sessionStorage.getItem("playstyle") == "EasyVideo" && window.location.hash.indexOf("#nvr/pages/image/osd.html")!==-1 && sessionStorage.getItem('dpiRadio') != null) {
                var pt = GetAbsoluteLocationEx($("#axdiv")[0]);
                if (pt.Top > 80) {
                    preHiddenDisplaywindow(5);
                    preWindowfollowing(true);
                } else {
                    preHiddenDisplaywindow(4);
                }
            }
        } else {
            if (sessionStorage.getItem("playstyle") == "EasyVideo" && window.location.hash == "#nvr/pages/image/osd.html") {
                var pt = GetAbsoluteLocationEx($("#axdiv")[0]);
                if (pt.Top > 127) {
                    preHiddenDisplaywindow(5);
                    preWindowfollowing(true);
                } else {
                    preHiddenDisplaywindow(4);
                }
            }
        }
    });
    g_bIsIE = !(/(msie\s|trident.*rv:)([\w.]+)/.exec(navigator.userAgent.toLowerCase()) == null);
    if (g_bIsIE&&!config.forceEV) {
        $("#playCanvas").hide();
        $("#axdiv").show();
        $("#axdiv").empty();
        // insertDrags();
        getTimeinfo();
        getVideoPlugin("ActiveX", 1, "", true, 2, "", 0);
        ChangeLayout(1);
        SetActiveXMode(1);
        // setTimeout(function() {
        CloseAllChannels();
        PreviewSpecificChannelInFirstWindow(0, 4);
        // getTimeselect();
        // getOSD();
        CHOSDGetSel(1)
        // }, 1500);
    } else {
        if (sessionStorage.getItem("xystyle") == "p6s" || sessionStorage.getItem("xystyle") == "p2p") {
            config.refreshplayvideo = true;
            $("#playCanvas").hide();
            $("#display").hide();
            $("#axdiv").show();
            if (self.playervideo) {
                NVReasyldspecialvideos(true, "");
                getVideoPlugin(sessionStorage.getItem("playstyle"), 1, "", true, 2, "",true,0);
                // getOSD();
            } else {
                refershPlayer("osd");
            }
            // if (self.playervideo) {
            //     // NVReasyldspecialvideos(true, "");
            // }
            // getVideoPlugin(sessionStorage.getItem("playstyle"), 1, "", true, 2, "", 0);
            // refershPlayer("osd");
            getTimeselect();
        } else {
            if (sessionStorage.getItem("playstyle") == "EasyVideo") {
                $("#playCanvas").hide();
                $("#axdiv").show();
                $("#display").hide();
            }
            if (config.refreshplayvideo && window.performance.navigation.type != 1) {
                NVReasyldspecialvideos(true, '');
                getVideoPlugin(sessionStorage.getItem("playstyle"), 1, "", true, 2, "",true,0);
                if (config.devicelose == false || sessionStorage.getItem("playstyle") != "EasyVideo") {
                    CHOSDGetSel(1);
                }
                getTimeselect();
            } else {
                if (sessionStorage.getItem("playstyle") == "EasyVideo") {
                    refershPlayer("osd", "", "", 2)
                    getTimeselect()
                }
            }

        }
    }
    // 显示隐藏字号
    $("#osddt").change(function() {
        if ($(this).is(':checked')) {
            $(".showdot").show();
        } else {
            $(".showdot").hide();
        }
    });
    // 点击保存
    $("#iamgeSave").click(function() {
        // setDatetype();
        CHOSDSaveSel($("#NVR_ChnOSDChannelMask").val());
    });
    // 通道号改变，再开流
    $("#NVR_ChnOSDChannelMask").change(function() {
        var chid = $("#NVR_ChnOSDChannelMask").val() * 1;
        currentchannel = chid - 1; // 当前通道号
        if (!config.isSupportDeviceCapV2&&config.deviceCapV1[currentchannel] == null) {
            getDeviceCap(chid);
        }
        deviceCap = config.isSupportDeviceCapV2 ? config.deviceCapV2[currentchannel] : config.deviceCapV1[currentchannel];
        if (deviceCap != {}) {
            if (deviceCap.osd2support != "true") {
                $(".osd2display").hide();
                osdCount = 1;
            } else {
                maxOsd2();
                $(".osd2display").show();
                osdCount = 2;
            }
        } else {
            $(".osd2display").hide();
            osdCount = 1;
        }
        if (sessionStorage.getItem("playstyle") == "EasyVideo") {
            NVReasyldspecialvideos(true, tnumber)
            getVideoPlugin(sessionStorage.getItem("playstyle"), 1, "", true, 2, "",true,chid - 1)
            tnumber = chid - 1;
            config.chnelNum = tnumber;
            // CHOSDGetSel(chid)
        } else {
            StopVideo(0);
            PreviewSpecificChannelInFirstWindow(chid - 1, 4);
            CHOSDGetSel(chid);
            config.chnelNum = chid;
        }
        maxOsd1()
    });
});

function maxOsd1(){
    $("#osd1List").empty();
    deviceCap.osd2support== "true" ? osd1Max=deviceCap.osd1line : osd1Max=deviceCap.osdline;
    for (var h = 0; h < osd1Max; h++) {
        if(h===0){
            $("#osd1List").append('<input type="text" class="form-control osdinput" id="osdna" name="nName" onkeyup="WidthCheck(this,30)" />');
        }else{
            $("#osd1List").append('<input type="text" class="form-control osdinput" id="osdna' + h + '" name="nName" onkeyup="WidthCheck(this,30)"  style="margin-top: 15PX;" />');
        }
    }
}

function maxOsd2(){
    $("#osd2List").empty();
    for (var h = 0; h < deviceCap.osd2line; h++) {
        if(h===0){
            $("#osd2List").append('<input type="text" class="form-control osdinput" id="osd2Text" name="tName" />');
        }else{
            $("#osd2List").append('<input type="text" class="form-control osdinput" id="osd2Text' + h + '" name="tName" style="margin-top: 15PX;" />');
        }
    }
}
// 获取各个通道下的osd配置
function CHOSDGetSel(ch) {
    var cyURL = window.config.baseUrl + "/Pictures/" + ch + "/MultiOSDV2";
    $.ajax({
        type: "GET",
        url: cyURL,
        processData: false,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function (xmlDoc, xhr, textStatus) {
            if (deviceCap.osddoit == "true") { //表示此时当前通道支持点阵设置
                if (g_bIsIE || sessionStorage.getItem("playstyle") != "EasyVideo") {
                    $(".isdiao").hide();
                    $(".showdot").hide();
                    $(".osdlist").hide();
                    $(".pad").hide();
                    $(".savebtn").hide();
                } else {
                    $(".isdiao").show();
                    $(".showdot").show();
                    if ($(xmlDoc).find('OSDKind').eq(0).text() == 'PictureOSD') {
                        $("#osddt").prop('checked', true)
                    } else {
                        $("#osddt").prop('checked', false)
                    }
                    $("#fontsize").val($(xmlDoc).find('OSDBmp').eq(0).find('FontSize').text()).trigger("change")
                }
            } else {
                $(".isdiao").hide();
                $(".showdot").hide();
                $(".osdlist").show();
                $(".pad").show();
                $(".savebtn").show();
                $("#osddt").prop('checked', false)
            }
            var osdName = $(xmlDoc).find('OSD').eq(0).find('Text').eq(0).text();
            var arr = osdName.split("^");
            var text = arr.join("<br>");
            if (jQuery.inArray("[RES]", arr) > -1 || jQuery.inArray("[BPS]", arr) > -1 || jQuery.inArray("[RES] [BPS]", arr) > -1) {
                var type = arr.pop();

            }
            switch (type) {
                case "[RES]":
                    $('#additional').val("2").trigger('change.select2')
                    break;
                case "[BPS]":
                    $('#additional').val("3").trigger('change.select2')
                    break;
                case "[RES] [BPS]":
                    $('#additional').val("4").trigger('change.select2')
                    break;
                default:
                    $('#additional').val("1").trigger('change.select2')
            }
            $("#osdna").val("")
            for (var i = 1; i < osd1Max; i++) {
                $("#osdna" + i).val("")
            }
            $("#osdna").val(arr[0]);
            if (arr[arr.length - 1].indexOf('[RES]') != -1 || arr[arr.length - 1].indexOf('[BPS]') != -1) {
                for (var i = 1; i < arr.length - 1; i++) {
                    $("#osdna" + i).val(arr[i])
                }
                addrile = arr[arr.length - 1];
            } else {
                for (var i = 1; i < arr.length; i++) {
                    $("#osdna" + i).val(arr[i])
                }
                addrile = "";
            }
            $("#osdw").prop('checked', JSON.parse($(xmlDoc).find('OSD').eq(0).find('Enable').eq(0).text()));
            namePosX = $(xmlDoc).find('OSD').eq(0).find('x').eq(0).text();
            namePosY = $(xmlDoc).find('OSD').eq(0).find('y').eq(0).text();
            $("#osdt").prop('checked', JSON.parse($(xmlDoc).find('DisplayTime').eq(0).find('Enable').eq(0).text()));
            timePosX = $(xmlDoc).find('DisplayTime').eq(0).find('PosX').eq(0).text();
            timePosY = $(xmlDoc).find('DisplayTime').eq(0).find('PosY').eq(0).text();
            // 当前通道是否支持osd2
            if ((deviceCap.osd2support == "true"&&!g_bIsIE)||config.forceEV) {
                $(".osd2display").show();
                osdCount = 2;
                //osd2 文本坐标
                var osd2Name = $(xmlDoc).find("OSD").eq(1).find("Text").text();
                var osd2Texts, osd2text;
                // 检查有无^ 有就去掉第一个^
                var index = osd2Name.indexOf("^");
                if (index == -1) { //无 直接显示，，，，用户没有输入换行符且没超过30个字符
                    osd2Texts = osd2Name;
                    osd2text = osd2Name;
                    $("#osd2Text").val(osd2Texts);
                } else { //有换行符
                    var add = osd2Name.split("^");
                    $("#osdlisdtow").empty();
                    $("#osd2Text").val(add[0]);
                    $("#osd2Text1").val(add[1]);
                    osd2Texts = osd2Name;
                }
                osd2Enable = $(xmlDoc).find("OSD").eq(1).find('Enable').eq(0).text();
                if (osd2Enable) {
                    $("#osd2Enable").prop('checked', JSON.parse(osd2Enable));
                }
                window3_left = $(xmlDoc).find('OSD').eq(1).find('x').eq(0).text();
                window3_top = $(xmlDoc).find('OSD').eq(1).find('y').eq(0).text();
            } else {
                $(".osd2display").hide();
            }
            if (sessionStorage.getItem('playstyle') == "EasyVideo") {
                // 获取osd1文本坐标数组
                var textArr = [],
                    windowLeftArr = [],
                    windowTopArr = [];
                if (osdName.indexOf("[")!==-1) {
                    var newText = osdName.split("^").slice(0, -1).join("^");
                } else {
                    var newText = osdName;
                }
                textArr.push(newText);
                windowLeftArr.push(parseInt(namePosX));
                windowTopArr.push(parseInt(namePosY));
                //osd2
                if (deviceCap.osd2support == "true"&&osd2Texts!=null) {
                    textArr.push(osd2Texts);
                    windowLeftArr.push(parseInt(window3_left));
                    windowTopArr.push(parseInt(window3_top));
                }
                //初始化
                osdinit(textArr, windowLeftArr, windowTopArr, timePosX, timePosY, 1, osdCount);
                // 时间的显示隐藏
                var osdtChecked = $("#osdt").is(':checked');
                osdinit(textArr, windowLeftArr, windowTopArr, timePosX, timePosY, osdtChecked ? 7 : 8, osdCount);
                // osd1显示隐藏
                var osdwChecked = $("#osdw").is(':checked');
                osdinit(textArr, windowLeftArr, windowTopArr, namePosX, namePosY, osdwChecked ? 5 : 6, osdCount, 0);
                // osd2显示隐藏
                if (deviceCap.osd2support == "true") {
                    var osd2EnableChecked = $("#osd2Enable").is(':checked'); //5: 显示 OSD 文本6: 隐藏 OSD 文本
                    osdinit(textArr, windowLeftArr, windowTopArr, window3_left, window3_top, osd2EnableChecked ? 5 : 6, osdCount, 1);
                }
            } else {
                var xml = "<?xml version='1.0' encoding='utf-8'?><DisplayOsdControl>" +
                    "<ShowText>1</ShowText>" +
                    "<TimeFormat>1</TimeFormat>" +
                    "<TextPosX>" + namePosX + "</TextPosX>" +
                    "<TextPosY>" + namePosY + "</TextPosY>" +
                    "<TimePosX>" + timePosX + "</TimePosX>" +
                    "<TimePosY>" + timePosY + "</TimePosY>" +
                    "<OsdText>" + osdName + "</OsdText>" +
                    "</DisplayOsdControl>";
                DisplayOsdControl(1, ch - 1, xml);
            }
        }
    });
}
// 组文本数据
function alltheText() {
    var str = "";
    str += $("#osdna").val();
    for (var o = 1; o < osd1Max; o++) {
        if ($("#osdna" + o).val() != "") {
            str = str + '^' + $("#osdna" + o).val()
        }
    }
    if ($("#additional").val() != "1") {
        switch ($("#additional").val()) {
            case "2":
                addrile += "[RES]"
                break;
            case "3":
                addrile += "[BPS]"
                break;
            case "4":
                addrile += "[RES] [BPS]"
                break;
        };
    } else {
        addrile = "";
    }
    if (addrile) {
        str = str + '^' + addrile
    }
    return str
}
// 组文本数据
function gettheText() {
    var str = "";
    str += $("#osdna").val();
    for (var o = 1; o < osd1Max; o++) {
        if ($("#osdna" + o).val() != "") {
            str = str + '^' + $("#osdna" + o).val()
        }
    }
    return str
}
//组文本数据
function alltheText2() {
    var str = "";
    if ($("#osd2Text1").val() != ""&&$("#osd2Text1").val()!= undefined) {
        str = $("#osd2Text").val() + '^' + $("#osd2Text1").val();
    } else {
        str = $("#osd2Text").val();
    }
    return str
}
// 下发各个通道下的osd配置
function CHOSDSaveSel(ch) {
    if (g_bIsIE && !config.forceEV) {
        if (deviceCap.osdTag == "true") { //是否支持多行osd
            var nRet = GetDisplayOsdPosition(ch - 1);
            var cyXml= '<?xml version="1.0" encoding="UTF-8" ?>' +
                '<MultiLineOSD Version="1.0">' +
                '<DisplayTime Version="1.0"><Enable>' + $("#osdt").is(':checked') + '</Enable><PosX>' + nRet.getItem(2) + '</PosX><PosY>' + nRet.getItem(3) + '</PosY></DisplayTime>' +
                '<OSD Version="1.0"><ID>1</ID><Enable>' + $("#osdw").is(':checked') + '</Enable><Text>' + alltheText() + '</Text><x>' + nRet.getItem(0) + '</x><y>' + nRet.getItem(1) + '</y></OSD>' +
                '<OSD Version="1.0"><ID>2</ID><Enable>false</Enable><Text/><x>0</x><y>0</y></OSD>' +
                '<OSD Version="1.0"><ID>3</ID><Enable>false</Enable><Text/><x>0</x><y>0</y></OSD>' +
                '<OSD Version="1.0"><ID>4</ID><Enable>false</Enable><Text/><x>0</x><y>0</y></OSD></MultiLineOSD >'

            var cyURL = window.config.baseUrl + "/Pictures/" + ch + "/MultiOSDV2";
            $.ajax({
                type: "PUT",
                url: cyURL,
                processData: false,
                data: cyXml,
                beforeSend: function(xhr) {
                    xhr.setRequestHeader("If-Modified-Since", "0");
                },
                complete: function(xhr) {
                    SaveState(xhr);
                    CHOSDGetSel(ch);
                }
            });
        } else {
            var nRet = GetDisplayOsdPosition(ch - 1);
            var cyXml = "<?xml version='1.0' encoding='UTF-8' ?>" +
                "<OSD Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>" +
                "<DisplayName Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>" +
                "<Enable>" + $("#osdw").is(':checked') + "</Enable>" +
                "<PosX>" + nRet.getItem(0) + "</PosX>" +
                "<PosY>" + nRet.getItem(1) + "</PosY>" +
                "<Name>" + alltheText() + "</Name>" +
                "</DisplayName>" +
                "<DisplayTime Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>" +
                "<Enable>" + $("#osdt").is(':checked') + "</Enable>" +
                "<PosX>" + nRet.getItem(2) + "</PosX>" +
                "<PosY>" + nRet.getItem(3) + "</PosY>" +
                "</DisplayTime>" +
                "</OSD>";
            var cyURL = window.config.baseUrl + "/Pictures/" + ch + "/OSD";
            $.ajax({
                type: "PUT",
                url: cyURL,
                processData: false,
                async: true,
                data: cyXml,
                beforeSend: function(xhr) {
                    xhr.setRequestHeader("If-Modified-Since", "0");
                },
                complete: function(xhr, textStatus) {
                    SaveState(xhr);
                    CHOSDGetSel(ch)

                }
            });

        }

    } else {
        // OSD1的长度限制
        var arrInput = $("input[name='nName']");
        var realLengthArr=[]
        for (var i = 0; i < arrInput.length; i++) {
            var realLength = 0,
                charCode = -1
            for (var j = 0; j < $(arrInput[i]).val().length; j++) {
                charCode = $(arrInput[i]).val().charCodeAt(j)
                if (charCode > 255) realLength += 2;
                else realLength += 1
                realLengthArr.push(realLength);
            }
        }

        var osdMaxLine=deviceCap.osd2support == "true" ? deviceCap.osd1linechar : deviceCap.osdchar;

        var hasExceed = false;
        for (var s = 0; s < realLengthArr.length; s++) {
            if (realLengthArr[s] > osdMaxLine) {
                hasExceed = true;
                break;
            }
        }
        if (!hasExceed ) {
            if (deviceCap.osd2support == "true") {
                var vArr = [];
                vArr.push($("#osd2Text").val());
                if ($("#osd2Text1").val() != ""&&$("#osd2Text1").val()!= undefined){
                    vArr.push($("#osd2Text1").val());
                }
                for (var i = 0; i < vArr.length; i++) {
                    var add = 0
                    var charCode = -1
                    for (var k = 0; k < vArr[i].length; k++) {
                        charCode = vArr[i][k].charCodeAt()
                        if (charCode > 255) {
                            add += 2
                        } else {
                            add += 1
                        }
                    }
                }
                if (add < deviceCap.odTwol || add == deviceCap.odTwol) {
                    if ($("#osddt").is(':checked')) {
                        osdgetDianzline(gettheText(), $("#fontsize").val());
                    } else {
                        osdputV();
                    }
                } else {
                    return $.optionBootoast($.i18n.prop('i18n-addrule'), "danger");

                }
            } else {
                if ($("#osddt").is(':checked')) {
                    osdgetDianzline(gettheText(), $("#fontsize").val());
                } else {
                    osdputV();
                }
            }
        } else {
            return $.optionBootoast($.i18n.prop('i18n-addrule'), "danger");

        }

    }

}

function keepdit(data) {
    keepdoit.width = data.OSD.width;
    keepdoit.base64 = data.OSD.base64;
    keepdoit.height = data.OSD.height;
    keepdoit.length = data.OSD.length;
    if (deviceCap.osddoitWidth < keepdoit.width || deviceCap.osddoitHeight < keepdoit.height) {
        $.optionBootoast($.i18n.prop('i18n-dain-osd'), "danger");
        return
    } else {
        if (deviceCap.osd2support != 'true' || $("#osd2Enable").is(':checked') != true) {
            osdputV();
        } else {
            osdgetDianzline(alltheText2(), $("#fontsizce").val(), 1);
        }

    }
}

function keepditow(data) {
    keepdoit2.width = data.OSD.width;
    keepdoit2.base64 = data.OSD.base64;
    keepdoit2.height = data.OSD.height;
    keepdoit2.length = data.OSD.length;
    if (deviceCap.osddoitWidth2 < keepdoit2.width || deviceCap.osddoitHeight2 < keepdoit2.height) {
        $.optionBootoast($.i18n.prop('i18n-dain-osd2'), "danger");
        return
    } else {
        osdputV();
    }
}
// 缩放保持100%大小方法
function ScalePitch(data) {
    var bl = getRatio()
    var newH = 420 * 100 / bl
    var newW = 560 * 100 / bl
    var divd = document.getElementById(data)
    if (divd) {
        divd.style.height = newH + "px"
        divd.style.width = newW + "px"
    }

}
// 保存日期格式
function setDatetype() {
    var cyXml = get_timexml_doc;
    $(cyXml).find('Time').eq(0).find('DateTimeFormat').eq(0).text($("#Stackingtime").val());
    $(cyXml).find('Time').eq(0).find('TimeFormat').eq(0).text($("#Timeformat").val());
    $(cyXml).find('Time').eq(0).find('UpdateFlag').eq(0).text(2);
    var cyURL = window.config.baseUrl + "/System/Time";
    $.ajax({
        type: "PUT",
        url: cyURL,
        processData: false,
        data: cyXml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr, textStatus) {
            // SaveState(xhr);
            xhrState = xhr
        }
    });
}
// 获取日期格式
function getTimeselect() {
    $.ajax({
        url: window.config.baseUrl + "/System/Time",
        type: "GET",
        async: true,
        timeout: 15000,

        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc) {
            get_timexml_doc = xmlDoc;
            $("#Stackingtime").val($(xmlDoc).find('Time').eq(0).find('DateTimeFormat').eq(0).text()).trigger('change.select2')
            $("#Timeformat").val($(xmlDoc).find('Time').eq(0).find('TimeFormat').eq(0).text()).trigger('change.select2')
        }
    })
}
// 初始化时间
function getTimeinfo() {
    $.ajax({
        url: window.config.baseUrl + "/System/Time",
        type: "GET",
        async: true,
        timeout: 15000,

        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc) {
            w_year = 0;
            w_month = 0;
            w_day = 0;
            w_hour = 0;
            w_minute = 0;
            w_second = 0;

            var t = $(xmlDoc).find('Time').eq(0).find('SystemTime').eq(0).text();
            w_year = Number(t.substring(0, 4));
            w_month = Number(t.substring(4, 6));
            w_day = Number(t.substring(6, 8));
            w_hour = Number(t.substring(9, 11));
            w_minute = Number(t.substring(11, 13));
            w_second = Number(t.substring(13, 15));
            clearTimeout(config.inttime);
            Watch();
        },
        error: function(xhr, textStatus, errorThrown) {}
    });
}

function Watch() {
    webTimeUp();
    config.inttime = setTimeout("Watch()", 1000);
}
/*******************************************************
 Function:        webTimeUp
 Description:     更新web显示时间
 Input:             无
 Output:            无
 return:            无
 ********************************************************/

function webTimeUp() {
    w_second = w_second + 1;
    if (w_second == 60) {
        w_second = 0;
        w_minute = w_minute + 1;
    }
    if (w_minute == 60) {
        w_minute = 0;
        w_hour = w_hour + 1;
    }
    if (w_hour == 24) {
        w_hour = 0;
        switch (w_month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                w_day = w_day + 1;
                if (w_day == 32) {
                    w_day = 1;
                    w_month = w_month + 1;
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                w_day = w_day + 1;
                if (w_day == 31) {
                    w_day = 1;
                    w_month = w_month + 1;
                }
                break;
            case 2:
                w_day = w_day + 1;
                amari = w_year % 4;
                amari1 = w_year % 100;
                amari4 = w_year % 400;
                if (amari4 == 0) {
                    if (w_day == 30) {
                        w_day = 1;
                        w_month = w_month + 1;
                    }
                } else if (amari1 == 0) {
                    if (w_day == 29) {
                        w_day = 1;
                        w_month = w_month + 1;
                    }
                } else if (amari == 0) {
                    if (w_day == 30) {
                        w_day = 1;
                        w_month = w_month + 1;
                    }
                } else {
                    if (w_day == 29) {
                        w_day = 1;
                        w_month = w_month + 1;
                    }
                }
                break;
        }
    }
    if (w_month == 13) {
        w_month = 1;
        w_year = w_year + 1;
    }
    if (w_month < 10) D1month = "0" + w_month;
    else D1month = "" + w_month;
    if (w_day < 10) D1day = "0" + w_day;
    else D1day = "" + w_day;
    if (w_hour < 10) D1hour = "0" + w_hour;
    else D1hour = "" + w_hour;
    if (w_minute < 10) D1minute = "0" + w_minute;
    else D1minute = "" + w_minute;
    if (w_second < 10) D1second = "0" + w_second;
    else D1second = "" + w_second;

    $("#window1").text(w_year + '-' + D1month + '-' + D1day + ' ' + D1hour + ':' + D1minute + ':' + D1second);
}
//获取字符串总字符个数
function chartlengths(str) {
    var chartlength = 0,
        chieseCode = -1; //字符长度
    for (var j = 0; j < str.length; j++) {
        chieseCode = str.charCodeAt(j); //字符串.charCodeAt(index)>255 就是中文，否则是英文。
        if (chieseCode > 255) chartlength += 2;
        else chartlength += 1;
    };
    return chartlength;
};

function getOSDtricValue(data) {
    if ($("#osddt").is(':checked')) {
        var typeosd = 'PictureOSD';
    } else {
        var typeosd = 'TextOSD';
    }
    var cch = $("#NVR_ChnOSDChannelMask").val()
    var ny = parseInt(data.OSD.text_y[0]);
    var nx = parseInt(data.OSD.text_x[0]);
    var tx = parseInt(data.OSD.datetime_x);
    var ty = parseInt(data.OSD.datetime_y);
    if (deviceCap.osd2support == 'true') {
        osd2ty = ((data.OSD.text_y ? data.OSD.text_y[1] : 0));
        osd2tx = ((data.OSD.text_x ? data.OSD.text_x[1] : 0));
    } else {
        osd2ty = "";
        osd2tx = "";
    }
    if (deviceCap.osdTag == "true") {
        var cyXml = '<?xml version="1.0" encoding="UTF-8" ?>' +
            '<MultiLineOSD Version="1.0">' +
            '<OSDKind>' + typeosd + '</OSDKind>' +
            '<DisplayTime Version="1.0"><Enable>' + $("#osdt").is(':checked') + '</Enable><PosX>' + tx + '</PosX><PosY>' + ty + '</PosY></DisplayTime>' +
            '<OSD Version="1.0"><ID>1</ID><Enable>' + $("#osdw").is(':checked') + '</Enable><Text>' + alltheText() + '</Text><x>' + nx + '</x><y>' + ny + '</y></OSD>' +
            '<OSD Version="1.0"><ID>2</ID><Enable>' + $("#osd2Enable").is(':checked') + '</Enable><Text>' + alltheText2() + '</Text><x>' + osd2tx + '</x><y>' + osd2ty + '</y></OSD>' +
            '<OSD Version="1.0"><ID>3</ID><Enable>false</Enable><Text/><x>0</x><y>0</y></OSD>' +
            '<OSD Version="1.0"><ID>4</ID><Enable>false</Enable><Text/><x>0</x><y>0</y></OSD>' +
            '<OSDBmp><ID>1</ID><FontSize>' + $("#fontsize").val() + '</FontSize><BmpW>' + keepdoit.width + '</BmpW><BmpH>' + keepdoit.height + '</BmpH>' +
            '<OSDBmpBase64>' + keepdoit.base64 + '</OSDBmpBase64>' +
            '<OSDBmpBase64Len>' + keepdoit.length + '</OSDBmpBase64Len></OSDBmp>' +
            '<OSDBmp><ID>2</ID><FontSize>' + $("#fontsizce").val() + '</FontSize><BmpW>' + keepdoit2.width + '</BmpW><BmpH>' + keepdoit2.height + '</BmpH>' +
            '<OSDBmpBase64>' + keepdoit2.base64 + '</OSDBmpBase64>' +
            '<OSDBmpBase64Len>' + keepdoit2.length + '</OSDBmpBase64Len></OSDBmp></MultiLineOSD>'
        var cyURL = window.config.baseUrl + "/Pictures/" + cch + "/MultiOSDV2";
        $.ajax({
            type: "PUT",
            url: cyURL,
            processData: false,
            data: cyXml,
            beforeSend: function(xhr) {
                xhr.setRequestHeader("If-Modified-Since", "0");
            },
            complete: function(xhr) {
                SaveState(xhr);
                CHOSDGetSel(cch);
            }
        });
    } else {
        var cyXml = "<?xml version='1.0' encoding='UTF-8' ?>" +
            "<OSD Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>" +
            "<DisplayName Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>" +
            "<Enable>" + $("#osdw").is(':checked') + "</Enable>" +
            "<PosX>" + nx + "</PosX>" +
            "<PosY>" + ny + "</PosY>" +
            "<Name>" + alltheText() + "</Name>" +
            "</DisplayName>" +
            "<DisplayTime Version='1.0' xmlns='http://www.zwcloud.wang/ver10/XMLSchema'>" +
            "<Enable>" + $("#osdt").is(':checked') + "</Enable>" +
            "<PosX>" + tx + "</PosX>" +
            "<PosY>" + ty + "</PosY>" +
            "</DisplayTime>" +
            "</OSD>";
        var cyURL = window.config.baseUrl + "/Pictures/" + cch + "/OSD";
        $.ajax({
            type: "PUT",
            url: cyURL,
            processData: false,
            data: cyXml,
            beforeSend: function(xhr) {
                xhr.setRequestHeader("If-Modified-Since", "0");
            },
            complete: function(xhr) {
                SaveState(xhr);
                CHOSDGetSel(cch)
            }
        });

    }

}
