var networkXml, user = config.user
$(document).ready(function() {
    if (!$('.select2').hasClass('select2-offscreen')) {
        $(".select2").select2({
            minimumResultsForSearch: -1,
            width: 360
        });
    }
    $('#netTpye').change(function() {
        if ($(this).select2('val') === "TriggerBrokenline") {
            $(".isVideoEmail").hide();
        } else {
            $(".isVideoEmail").show();
        }
        getException($(this).select2('val'));
    });
    getException($('#netTpye').val());
    $("#ipSave").click(function() {
        if ("admin" == user || "super_admin" == user) {
            setNetworkInfo();
        } else {
            $.optionBootoast($.i18n.prop('i18n-propoe'), "danger")
        }
    });
})

function getException(type) {
    $.ajax({
        url: config.baseUrl + "/System/Exception",
        type: "GET",
        async: true,
        timeout: 15000,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        success: function(xmlDoc, textStatus, xhr) {
            var mail = $(xmlDoc).find('exception').eq(0).find(type).eq(0).find('Mail').find('Enable').text();
            if (mail == "true") {
                $("#VideoEmail").prop('checked', true);
            } else {
                $("#VideoEmail").prop('checked', false);
            }
            var alarm = $(xmlDoc).find('exception').eq(0).find(type).eq(0).find('AlarmOut').find('Enable').text();
            if (alarm == "true") {
                $("#VoiceAlarm").prop('checked', true);
            } else {
                $("#VoiceAlarm").prop('checked', false);
            }
        },
        error: function(xhr, textStatus, errorThrown) {
            $.optionBootoast($.i18n.prop('i18n-request-was-aborted'), "danger")
        }
    });

}

function setException(type) {
    var xml = "<?xml version='1.0' encoding='UTF-8' ?>" +
        "<exception Version='1.0'><" + type + " Version='1.0'>" +
        "<NotifyAMS Version='1.0'><Enable>false</Enable></NotifyAMS>" +
        "<BeepAlert Version='1.0'><Enable>false</Enable></BeepAlert>" +
        "<FullScreen Version='1.0'><Enable>false</Enable></FullScreen>" +
        "<Mail Version='1.0'><Enable>" + $("#VideoEmail").is(":checked") + "</Enable></Mail>" +
        "<Ftp Version='1.0'> <Enable></Enable></Ftp>";
    if ($("#VoiceAlarm").is(":checked")) {
        xml = xml + "<AlarmOut Version='1.0'><Enable>true</Enable><AlarmOutMask>1</AlarmOutMask></AlarmOut>";
    } else {
        xml = xml + "<AlarmOut Version='1.0'><Enable>false</Enable><AlarmOutMask>0</AlarmOutMask></AlarmOut>";
    }
    xml = xml + "<Snapshot Version='1.0'><Enable></Enable><SnapshotMask></Snapshot>" +
        "<Record Version='1.0'><Enable></Enable><RecordMask></RecordMask></Record>" +
        "<PTZ Version='1.0'><Enable></Enable><PTZActionList Version='1.0'></PTZActionList></PTZ>" +
        "</" + type + "></exception>";
    var cyURL = config.baseUrl + "/System/Exception";
    $.ajax({
        type: "PUT",
        url: cyURL,
        async: true,
        processData: false,
        data: xml,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("If-Modified-Since", "0");
        },
        complete: function(xhr) {
            SaveState(xhr);
        },
        error: function(xhr, textStatus, errorThrown) {
            $.optionBootoast($.i18n.prop('i18n-savefail'), "danger")
        }
    });
}